xref: /illumos-gate/usr/src/common/util/memstr.c (revision 7c478bd9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #if defined(_BOOT)
30 #include <sys/salib.h>
31 #else
32 #include <sys/systm.h>
33 #endif
34 
35 /*
36  * Implementations of functions described in memory(3C).
37  * These functions match the section 3C manpages.
38  */
39 
40 void *
41 memmove(void *s1, const void *s2, size_t n)
42 {
43 #if defined(_BOOT)
44 	bcopy(s2, s1, n);
45 #else
46 	ovbcopy(s2, s1, n);
47 #endif
48 	return (s1);
49 }
50 
51 void *
52 memset(void *s, int c, size_t n)
53 {
54 	unsigned char *t;
55 
56 	if ((unsigned char)c == '\0')
57 		bzero(s, n);
58 	else {
59 		for (t = (unsigned char *)s; n > 0; n--)
60 			*t++ = (unsigned char)c;
61 	}
62 	return (s);
63 }
64 
65 int
66 memcmp(const void *s1, const void *s2, size_t n)
67 {
68 	const uchar_t *ps1 = s1;
69 	const uchar_t *ps2 = s2;
70 
71 	if (s1 != s2 && n != 0) {
72 		do {
73 			if (*ps1++ != *ps2++)
74 				return (ps1[-1] - ps2[-1]);
75 		} while (--n != 0);
76 	}
77 
78 	return (0);
79 }
80 
81 void *
82 memcpy(void *s1, const void *s2, size_t n)
83 {
84 	bcopy(s2, s1, n);
85 	return (s1);
86 }
87 
88 void *
89 memchr(const void *sptr, int c1, size_t n)
90 {
91 	if (n != 0) {
92 		unsigned char c = (unsigned char)c1;
93 		const unsigned char *sp = sptr;
94 
95 		do {
96 			if (*sp++ == c)
97 				return ((void *)--sp);
98 		} while (--n != 0);
99 	}
100 	return (NULL);
101 }
102