]> git.saurik.com Git - apple/xnu.git/blob - osfmk/device/subrs.c
a9c6e0beb47635d023f10da013b550852d6ad9f2
[apple/xnu.git] / osfmk / device / subrs.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 *(C)UNIX System Laboratories, Inc. all or some portions of this file are
35 *derived from material licensed to the University of California by
36 *American Telephone and Telegraph Co. or UNIX System Laboratories,
37 *Inc. and are reproduced herein with the permission of UNIX System
38 *Laboratories, Inc.
39 */
40
41 /*
42 * Mach Operating System
43 * Copyright (c) 1993,1991,1990,1989,1988 Carnegie Mellon University
44 * All Rights Reserved.
45 *
46 * Permission to use, copy, modify and distribute this software and its
47 * documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
51 *
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
54 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 *
56 * Carnegie Mellon requests users of this software to return to
57 *
58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
62 *
63 * any improvements or extensions that they make and grant Carnegie Mellon
64 * the rights to redistribute these changes.
65 */
66 /*
67 */
68 /*
69 * Copyright (c) 1988 Regents of the University of California.
70 * All rights reserved.
71 *
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
74 * are met:
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. All advertising materials mentioning features or use of this software
81 * must display the following acknowledgement:
82 * This product includes software developed by the University of
83 * California, Berkeley and its contributors.
84 * 4. Neither the name of the University nor the names of its contributors
85 * may be used to endorse or promote products derived from this software
86 * without specific prior written permission.
87 *
88 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98 * SUCH DAMAGE.
99 */
100 /*
101 * Random device subroutines and stubs.
102 */
103
104 #include <vm/vm_kern.h>
105 #include <kern/misc_protos.h>
106
107 /* String routines, from CMU */
108 #ifdef strcpy
109 #undef strcmp
110 #undef strncmp
111 #undef strcpy
112 #undef strncpy
113 #undef strlen
114 #endif
115
116 /*
117 * Abstract:
118 * strcmp (s1, s2) compares the strings "s1" and "s2".
119 * It returns 0 if the strings are identical. It returns
120 * > 0 if the first character that differs in the two strings
121 * is larger in s1 than in s2 or if s1 is longer than s2 and
122 * the contents are identical up to the length of s2.
123 * It returns < 0 if the first differing character is smaller
124 * in s1 than in s2 or if s1 is shorter than s2 and the
125 * contents are identical upto the length of s1.
126 */
127
128 int
129 strcmp(
130 register const char *s1,
131 register const char *s2)
132 {
133 register unsigned int a, b;
134
135 do {
136 a = *s1++;
137 b = *s2++;
138 if (a != b)
139 return a-b; /* includes case when
140 'a' is zero and 'b' is not zero
141 or vice versa */
142 } while (a != '\0');
143
144 return 0; /* both are zero */
145 }
146
147 /*
148 * Abstract:
149 * strncmp (s1, s2, n) compares the strings "s1" and "s2"
150 * in exactly the same way as strcmp does. Except the
151 * comparison runs for at most "n" characters.
152 */
153
154 int
155 strncmp(
156 register const char *s1,
157 register const char *s2,
158 size_t n)
159 {
160 register unsigned int a, b;
161
162 while (n != 0) {
163 a = *s1++;
164 b = *s2++;
165 if (a != b)
166 return a-b; /* includes case when
167 'a' is zero and 'b' is not zero
168 or vice versa */
169 if (a == '\0')
170 return 0; /* both are zero */
171 n--;
172 }
173
174 return 0;
175 }
176
177
178 //
179 // Lame implementation just for use by strcasecmp/strncasecmp
180 //
181 static int
182 tolower(unsigned char ch)
183 {
184 if (ch >= 'A' && ch <= 'Z')
185 ch = 'a' + (ch - 'A');
186
187 return ch;
188 }
189
190 int
191 strcasecmp(const char *s1, const char *s2)
192 {
193 const unsigned char *us1 = (const u_char *)s1,
194 *us2 = (const u_char *)s2;
195
196 while (tolower(*us1) == tolower(*us2++))
197 if (*us1++ == '\0')
198 return (0);
199 return (tolower(*us1) - tolower(*--us2));
200 }
201
202 int
203 strncasecmp(const char *s1, const char *s2, size_t n)
204 {
205 if (n != 0) {
206 const unsigned char *us1 = (const u_char *)s1,
207 *us2 = (const u_char *)s2;
208
209 do {
210 if (tolower(*us1) != tolower(*us2++))
211 return (tolower(*us1) - tolower(*--us2));
212 if (*us1++ == '\0')
213 break;
214 } while (--n != 0);
215 }
216 return (0);
217 }
218
219
220 /*
221 * Abstract:
222 * strcpy copies the contents of the string "from" including
223 * the null terminator to the string "to". A pointer to "to"
224 * is returned.
225 */
226
227 char *
228 strcpy(
229 register char *to,
230 register const char *from)
231 {
232 register char *ret = to;
233
234 while ((*to++ = *from++) != '\0')
235 continue;
236
237 return ret;
238 }
239
240
241 /*
242 * Abstract:
243 * strncpy copies "count" characters from the "from" string to
244 * the "to" string. If "from" contains less than "count" characters
245 * "to" will be padded with null characters until exactly "count"
246 * characters have been written. The return value is a pointer
247 * to the "to" string.
248 */
249
250 char *
251 strncpy(
252 char *s1,
253 const char *s2,
254 size_t n)
255 {
256 char *os1 = s1;
257 unsigned long i;
258
259 for (i = 0; i < n;)
260 if ((*s1++ = *s2++) == '\0')
261 for (i++; i < n; i++)
262 *s1++ = '\0';
263 else
264 i++;
265 return (os1);
266 }
267
268 /*
269 * atoi:
270 *
271 * This function converts an ascii string into an integer.
272 *
273 * input : string
274 * output : a number
275 */
276
277 int
278 atoi(
279 u_char *cp)
280 {
281 int number;
282
283 for (number = 0; ('0' <= *cp) && (*cp <= '9'); cp++)
284 number = (number * 10) + (*cp - '0');
285
286 return( number );
287 }
288
289 /*
290 * convert an ASCII string (decimal radix) to an integer
291 * inputs:
292 * p string pointer.
293 * t char **, return a pointer to the cahr which terminates the
294 * numeric string.
295 * returns:
296 * integer value of the numeric string.
297 * side effect:
298 * pointer to terminating char.
299 */
300
301 int
302 atoi_term(
303 char *p, /* IN */
304 char **t) /* OUT */
305 {
306 register int n;
307 register int f;
308
309 n = 0;
310 f = 0;
311 for(;;p++) {
312 switch(*p) {
313 case ' ':
314 case '\t':
315 continue;
316 case '-':
317 f++;
318 case '+':
319 p++;
320 }
321 break;
322 }
323 while(*p >= '0' && *p <= '9')
324 n = n*10 + *p++ - '0';
325
326 /* return pointer to terminating character */
327 if ( t )
328 *t = p;
329
330 return(f? -n: n);
331 }
332
333 /*
334 * convert an integer to an ASCII string.
335 * inputs:
336 * num integer to be converted
337 * str string pointer.
338 *
339 * outputs:
340 * pointer to string start.
341 */
342
343 char *
344 itoa(
345 int num,
346 char *str)
347 {
348 char digits[11];
349 register char *dp;
350 register char *cp = str;
351
352 if (num == 0) {
353 *cp++ = '0';
354 }
355 else {
356 dp = digits;
357 while (num) {
358 *dp++ = '0' + num % 10;
359 num /= 10;
360 }
361 while (dp != digits) {
362 *cp++ = *--dp;
363 }
364 }
365 *cp++ = '\0';
366
367 return str;
368 }
369
370 char *
371 strcat(
372 register char *dest,
373 register const char *src)
374 {
375 char *old = dest;
376
377 while (*dest)
378 ++dest;
379 while (*dest++ = *src++)
380 ;
381 return (old);
382 }
383