xref: /titanic_51/usr/src/uts/common/krtld/kobj_subr.c (revision 381a2a9a387f449fab7d0c7e97c4184c26963abf)
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 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 
32 /*
33  * Standalone copies of some basic routines.  Note that these routines
34  * are transformed via Makefile -D flags into krtld_* routines.  So,
35  * this version of strcmp() will become krtld_strcmp() when built.
36  *
37  * This dubious practice is so that krtld can have its own private
38  * versions of these routines suitable for use during early boot,
39  * when kernel-based routines might not work.  Make sure to use 'nm'
40  * on your krtld to make sure it is calling the appropriate routines.
41  */
42 
43 int
44 strcmp(const char *s1, const char *s2)
45 {
46 	if (s1 == s2)
47 		return (0);
48 	while (*s1 == *s2++)
49 		if (*s1++ == '\0')
50 			return (0);
51 	return (*s1 - s2[-1]);
52 }
53 
54 /*
55  * Compare strings (at most n bytes): return *s1-*s2 for the last
56  * characters in s1 and s2 which were compared.
57  */
58 int
59 strncmp(const char *s1, const char *s2, size_t n)
60 {
61 	if (s1 == s2)
62 		return (0);
63 	n++;
64 	while (--n != 0 && *s1 == *s2++)
65 		if (*s1++ == '\0')
66 			return (0);
67 	return ((n == 0) ? 0 : *s1 - *--s2);
68 }
69 
70 size_t
71 strlen(const char *s)
72 {
73 	const char *s0 = s + 1;
74 
75 	while (*s++ != '\0')
76 		;
77 	return (s - s0);
78 }
79 
80 char *
81 strcpy(char *s1, const char *s2)
82 {
83 	char *os1 = s1;
84 
85 	while (*s1++ = *s2++)
86 		;
87 	return (os1);
88 }
89 
90 char *
91 strncpy(char *s1, const char *s2, size_t n)
92 {
93 	char *os1 = s1;
94 
95 	n++;
96 	while ((--n != 0) && ((*s1++ = *s2++) != '\0'))
97 		;
98 	if (n != 0)
99 		while (--n != 0)
100 			*s1++ = '\0';
101 	return (os1);
102 }
103 
104 char *
105 strcat(char *s1, const char *s2)
106 {
107 	char *os1 = s1;
108 
109 	while (*s1++)
110 		;
111 	--s1;
112 	while (*s1++ = *s2++)
113 		;
114 	return (os1);
115 }
116 
117 size_t
118 strlcat(char *dst, const char *src, size_t dstsize)
119 {
120 	char *df = dst;
121 	size_t left = dstsize;
122 	size_t l1;
123 	size_t l2 = strlen(src);
124 	size_t copied;
125 
126 	while (left-- != 0 && *df != '\0')
127 		df++;
128 	l1 = df - dst;
129 	if (dstsize == l1)
130 		return (l1 + l2);
131 
132 	copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
133 	bcopy(src, dst + l1, copied);
134 	dst[l1+copied] = '\0';
135 	return (l1 + l2);
136 }
137 
138 char *
139 strchr(const char *sp, int c)
140 {
141 
142 	do {
143 		if (*sp == (char)c)
144 			return ((char *)sp);
145 	} while (*sp++);
146 	return (NULL);
147 }
148 
149 void
150 bzero(void *p_arg, size_t count)
151 {
152 	char zero = 0;
153 	caddr_t p = p_arg;
154 
155 	while (count != 0)
156 		*p++ = zero, count--;	/* Avoid clr for 68000, still... */
157 }
158 
159 void
160 bcopy(const void *src_arg, void *dest_arg, size_t count)
161 {
162 	caddr_t src = (caddr_t)src_arg;
163 	caddr_t dest = dest_arg;
164 
165 	if (src < dest && (src + count) > dest) {
166 		/* overlap copy */
167 		while (--count != -1)
168 			*(dest + count) = *(src + count);
169 	} else {
170 		while (--count != -1)
171 			*dest++ = *src++;
172 	}
173 }
174