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