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