xref: /onnv-gate/usr/src/common/util/memstr.c (revision 12391:e5884cf20c37)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51651Spetede  * Common Development and Distribution License (the "License").
61651Spetede  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12391SSurya.Prakki@Sun.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #if defined(_BOOT)
260Sstevel@tonic-gate #include <sys/salib.h>
270Sstevel@tonic-gate #else
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #endif
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Implementations of functions described in memory(3C).
330Sstevel@tonic-gate  * These functions match the section 3C manpages.
340Sstevel@tonic-gate  */
35*12391SSurya.Prakki@Sun.COM /*
36*12391SSurya.Prakki@Sun.COM  * The SunStudio compiler may generate calls to _memmove, _memset,
37*12391SSurya.Prakki@Sun.COM  * and _memcpy; So we need to make sure that the correct symbols
38*12391SSurya.Prakki@Sun.COM  * exist for these calls.
39*12391SSurya.Prakki@Sun.COM  */
40*12391SSurya.Prakki@Sun.COM #pragma weak _memmove = memmove
410Sstevel@tonic-gate void *
memmove(void * s1,const void * s2,size_t n)420Sstevel@tonic-gate memmove(void *s1, const void *s2, size_t n)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate #if defined(_BOOT)
450Sstevel@tonic-gate 	bcopy(s2, s1, n);
460Sstevel@tonic-gate #else
470Sstevel@tonic-gate 	ovbcopy(s2, s1, n);
480Sstevel@tonic-gate #endif
490Sstevel@tonic-gate 	return (s1);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate 
52*12391SSurya.Prakki@Sun.COM #pragma weak _memset = memset
530Sstevel@tonic-gate void *
memset(void * s,int c,size_t n)540Sstevel@tonic-gate memset(void *s, int c, size_t n)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	unsigned char *t;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	if ((unsigned char)c == '\0')
590Sstevel@tonic-gate 		bzero(s, n);
600Sstevel@tonic-gate 	else {
610Sstevel@tonic-gate 		for (t = (unsigned char *)s; n > 0; n--)
620Sstevel@tonic-gate 			*t++ = (unsigned char)c;
630Sstevel@tonic-gate 	}
640Sstevel@tonic-gate 	return (s);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
670Sstevel@tonic-gate int
memcmp(const void * s1,const void * s2,size_t n)680Sstevel@tonic-gate memcmp(const void *s1, const void *s2, size_t n)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	const uchar_t *ps1 = s1;
710Sstevel@tonic-gate 	const uchar_t *ps2 = s2;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (s1 != s2 && n != 0) {
740Sstevel@tonic-gate 		do {
750Sstevel@tonic-gate 			if (*ps1++ != *ps2++)
760Sstevel@tonic-gate 				return (ps1[-1] - ps2[-1]);
770Sstevel@tonic-gate 		} while (--n != 0);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	return (0);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
831651Spetede #pragma weak _memcpy = memcpy
840Sstevel@tonic-gate void *
memcpy(void * s1,const void * s2,size_t n)850Sstevel@tonic-gate memcpy(void *s1, const void *s2, size_t n)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	bcopy(s2, s1, n);
880Sstevel@tonic-gate 	return (s1);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate void *
memchr(const void * sptr,int c1,size_t n)920Sstevel@tonic-gate memchr(const void *sptr, int c1, size_t n)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	if (n != 0) {
950Sstevel@tonic-gate 		unsigned char c = (unsigned char)c1;
960Sstevel@tonic-gate 		const unsigned char *sp = sptr;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 		do {
990Sstevel@tonic-gate 			if (*sp++ == c)
1000Sstevel@tonic-gate 				return ((void *)--sp);
1010Sstevel@tonic-gate 		} while (--n != 0);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	return (NULL);
1040Sstevel@tonic-gate }
105