xref: /onnv-gate/usr/src/uts/common/krtld/kobj_subr.c (revision 2267:c5d9a656170f)
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
5*2267Sdp  * Common Development and Distribution License (the "License").
6*2267Sdp  * 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*2267Sdp  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/param.h>
30*2267Sdp #include <sys/systm.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
33*2267Sdp  * Standalone copies of some basic routines.  Note that these routines
34*2267Sdp  * are transformed via Makefile -D flags into krtld_* routines.  So,
35*2267Sdp  * this version of strcmp() will become krtld_strcmp() when built.
36*2267Sdp  *
37*2267Sdp  * This dubious practice is so that krtld can have its own private
38*2267Sdp  * versions of these routines suitable for use during early boot,
39*2267Sdp  * when kernel-based routines might not work.  Make sure to use 'nm'
40*2267Sdp  * on your krtld to make sure it is calling the appropriate routines.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate int
440Sstevel@tonic-gate strcmp(const char *s1, const char *s2)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	if (s1 == s2)
470Sstevel@tonic-gate 		return (0);
480Sstevel@tonic-gate 	while (*s1 == *s2++)
490Sstevel@tonic-gate 		if (*s1++ == '\0')
500Sstevel@tonic-gate 			return (0);
510Sstevel@tonic-gate 	return (*s1 - s2[-1]);
520Sstevel@tonic-gate }
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * Compare strings (at most n bytes): return *s1-*s2 for the last
560Sstevel@tonic-gate  * characters in s1 and s2 which were compared.
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate int
590Sstevel@tonic-gate strncmp(const char *s1, const char *s2, size_t n)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	if (s1 == s2)
620Sstevel@tonic-gate 		return (0);
630Sstevel@tonic-gate 	n++;
640Sstevel@tonic-gate 	while (--n != 0 && *s1 == *s2++)
650Sstevel@tonic-gate 		if (*s1++ == '\0')
660Sstevel@tonic-gate 			return (0);
670Sstevel@tonic-gate 	return ((n == 0) ? 0 : *s1 - *--s2);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate size_t
710Sstevel@tonic-gate strlen(const char *s)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	const char *s0 = s + 1;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	while (*s++ != '\0')
760Sstevel@tonic-gate 		;
770Sstevel@tonic-gate 	return (s - s0);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate char *
810Sstevel@tonic-gate strcpy(char *s1, const char *s2)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	char *os1 = s1;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	while (*s1++ = *s2++)
860Sstevel@tonic-gate 		;
870Sstevel@tonic-gate 	return (os1);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate char *
910Sstevel@tonic-gate strncpy(char *s1, const char *s2, size_t n)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	char *os1 = s1;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	n++;
960Sstevel@tonic-gate 	while ((--n != 0) && ((*s1++ = *s2++) != '\0'))
970Sstevel@tonic-gate 		;
980Sstevel@tonic-gate 	if (n != 0)
990Sstevel@tonic-gate 		while (--n != 0)
1000Sstevel@tonic-gate 			*s1++ = '\0';
1010Sstevel@tonic-gate 	return (os1);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate char *
1050Sstevel@tonic-gate strcat(char *s1, const char *s2)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	char *os1 = s1;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	while (*s1++)
1100Sstevel@tonic-gate 		;
1110Sstevel@tonic-gate 	--s1;
1120Sstevel@tonic-gate 	while (*s1++ = *s2++)
1130Sstevel@tonic-gate 		;
1140Sstevel@tonic-gate 	return (os1);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
117*2267Sdp size_t
118*2267Sdp strlcat(char *dst, const char *src, size_t dstsize)
119*2267Sdp {
120*2267Sdp 	char *df = dst;
121*2267Sdp 	size_t left = dstsize;
122*2267Sdp 	size_t l1;
123*2267Sdp 	size_t l2 = strlen(src);
124*2267Sdp 	size_t copied;
125*2267Sdp 
126*2267Sdp 	while (left-- != 0 && *df != '\0')
127*2267Sdp 		df++;
128*2267Sdp 	l1 = df - dst;
129*2267Sdp 	if (dstsize == l1)
130*2267Sdp 		return (l1 + l2);
131*2267Sdp 
132*2267Sdp 	copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
133*2267Sdp 	bcopy(src, dst + l1, copied);
134*2267Sdp 	dst[l1+copied] = '\0';
135*2267Sdp 	return (l1 + l2);
136*2267Sdp }
137*2267Sdp 
1380Sstevel@tonic-gate char *
1390Sstevel@tonic-gate strchr(const char *sp, int c)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	do {
1430Sstevel@tonic-gate 		if (*sp == (char)c)
1440Sstevel@tonic-gate 			return ((char *)sp);
1450Sstevel@tonic-gate 	} while (*sp++);
1460Sstevel@tonic-gate 	return (NULL);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate void
1500Sstevel@tonic-gate bzero(void *p_arg, size_t count)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	char zero = 0;
1530Sstevel@tonic-gate 	caddr_t p = p_arg;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	while (count != 0)
1560Sstevel@tonic-gate 		*p++ = zero, count--;	/* Avoid clr for 68000, still... */
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate void
1600Sstevel@tonic-gate bcopy(const void *src_arg, void *dest_arg, size_t count)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	caddr_t src = (caddr_t)src_arg;
1630Sstevel@tonic-gate 	caddr_t dest = dest_arg;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (src < dest && (src + count) > dest) {
1660Sstevel@tonic-gate 		/* overlap copy */
1670Sstevel@tonic-gate 		while (--count != -1)
1680Sstevel@tonic-gate 			*(dest + count) = *(src + count);
1690Sstevel@tonic-gate 	} else {
1700Sstevel@tonic-gate 		while (--count != -1)
1710Sstevel@tonic-gate 			*dest++ = *src++;
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate }
174