xref: /illumos-gate/usr/src/common/util/memstr.c (revision 489f6310fe8952e87fc1dce8af87990fcfd90f18)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #if defined(_BOOT)
26 #include <sys/salib.h>
27 #else
28 #include <sys/systm.h>
29 #endif
30 
31 /*
32  * Implementations of functions described in memory(3C).
33  * These functions match the section 3C manpages.
34  */
35 /*
36  * The SunStudio compiler may generate calls to _memmove, _memset,
37  * and _memcpy; So we need to make sure that the correct symbols
38  * exist for these calls. We also use _memset to work around some
39  * standards namespacing challenges.
40  */
41 #pragma weak _memmove = memmove
42 void *
43 memmove(void *s1, const void *s2, size_t n)
44 {
45 #if defined(_BOOT)
46 	bcopy(s2, s1, n);
47 #else
48 	ovbcopy(s2, s1, n);
49 #endif
50 	return (s1);
51 }
52 
53 #pragma weak _memset = memset
54 void *
55 memset(void *s, int c, size_t n)
56 {
57 	unsigned char *t;
58 
59 	if ((unsigned char)c == '\0')
60 		bzero(s, n);
61 	else {
62 		for (t = (unsigned char *)s; n > 0; n--)
63 			*t++ = (unsigned char)c;
64 	}
65 	return (s);
66 }
67 
68 int
69 memcmp(const void *s1, const void *s2, size_t n)
70 {
71 	const uchar_t *ps1 = s1;
72 	const uchar_t *ps2 = s2;
73 
74 	if (s1 != s2 && n != 0) {
75 		do {
76 			if (*ps1++ != *ps2++)
77 				return (ps1[-1] - ps2[-1]);
78 		} while (--n != 0);
79 	}
80 
81 	return (0);
82 }
83 
84 #pragma weak _memcpy = memcpy
85 void *
86 memcpy(void *s1, const void *s2, size_t n)
87 {
88 	bcopy(s2, s1, n);
89 	return (s1);
90 }
91 
92 void *
93 memchr(const void *sptr, int c1, size_t n)
94 {
95 	if (n != 0) {
96 		unsigned char c = (unsigned char)c1;
97 		const unsigned char *sp = sptr;
98 
99 		do {
100 			if (*sp++ == c)
101 				return ((void *)--sp);
102 		} while (--n != 0);
103 	}
104 	return (NULL);
105 }
106