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