xref: /illumos-gate/usr/src/common/util/memstr.c (revision 82333da9)
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.
39  */
40 #pragma weak _memmove = memmove
41 void *
memmove(void * s1,const void * s2,size_t n)42 memmove(void *s1, const void *s2, size_t n)
43 {
44 #if defined(_BOOT)
45 	bcopy(s2, s1, n);
46 #else
47 	ovbcopy(s2, s1, n);
48 #endif
49 	return (s1);
50 }
51 
52 #pragma weak _memset = memset
53 void *
memset(void * s,int c,size_t n)54 memset(void *s, int c, size_t n)
55 {
56 	unsigned char *t;
57 
58 	if ((unsigned char)c == '\0')
59 		bzero(s, n);
60 	else {
61 		for (t = (unsigned char *)s; n > 0; n--)
62 			*t++ = (unsigned char)c;
63 	}
64 	return (s);
65 }
66 
67 int
memcmp(const void * s1,const void * s2,size_t n)68 memcmp(const void *s1, const void *s2, size_t n)
69 {
70 	const uchar_t *ps1 = s1;
71 	const uchar_t *ps2 = s2;
72 
73 	if (s1 != s2 && n != 0) {
74 		do {
75 			if (*ps1++ != *ps2++)
76 				return (ps1[-1] - ps2[-1]);
77 		} while (--n != 0);
78 	}
79 
80 	return (0);
81 }
82 
83 #pragma weak _memcpy = memcpy
84 void *
memcpy(void * s1,const void * s2,size_t n)85 memcpy(void *s1, const void *s2, size_t n)
86 {
87 	bcopy(s2, s1, n);
88 	return (s1);
89 }
90 
91 void *
memchr(const void * sptr,int c1,size_t n)92 memchr(const void *sptr, int c1, size_t n)
93 {
94 	if (n != 0) {
95 		unsigned char c = (unsigned char)c1;
96 		const unsigned char *sp = sptr;
97 
98 		do {
99 			if (*sp++ == c)
100 				return ((void *)--sp);
101 		} while (--n != 0);
102 	}
103 	return (NULL);
104 }
105