xref: /titanic_52/usr/src/common/util/memstr.c (revision c40d696f8f0e05103b3795dd37198e00ae7ef955)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #if defined(_BOOT)
29 #include <sys/salib.h>
30 #else
31 #include <sys/systm.h>
32 #endif
33 
34 /*
35  * Implementations of functions described in memory(3C).
36  * These functions match the section 3C manpages.
37  */
38 
39 void *
40 memmove(void *s1, const void *s2, size_t n)
41 {
42 #if defined(_BOOT)
43 	bcopy(s2, s1, n);
44 #else
45 	ovbcopy(s2, s1, n);
46 #endif
47 	return (s1);
48 }
49 
50 void *
51 memset(void *s, int c, size_t n)
52 {
53 	unsigned char *t;
54 
55 	if ((unsigned char)c == '\0')
56 		bzero(s, n);
57 	else {
58 		for (t = (unsigned char *)s; n > 0; n--)
59 			*t++ = (unsigned char)c;
60 	}
61 	return (s);
62 }
63 
64 int
65 memcmp(const void *s1, const void *s2, size_t n)
66 {
67 	const uchar_t *ps1 = s1;
68 	const uchar_t *ps2 = s2;
69 
70 	if (s1 != s2 && n != 0) {
71 		do {
72 			if (*ps1++ != *ps2++)
73 				return (ps1[-1] - ps2[-1]);
74 		} while (--n != 0);
75 	}
76 
77 	return (0);
78 }
79 
80 /*
81  * The SunStudio compiler may generate calls to _memcpy and so we
82  * need to make sure that the correct symbol exists for these calls.
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