]> git.saurik.com Git - apple/xnu.git/blob - osfmk/device/subrs.c
1282fd398f7ae514c2b38d9752742c26524d0824
[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 * atoi:
219 *
220 * This function converts an ascii string into an integer.
221 *
222 * input : string
223 * output : a number
224 */
225
226 int
227 atoi(
228 u_char *cp)
229 {
230 int number;
231
232 for (number = 0; ('0' <= *cp) && (*cp <= '9'); cp++)
233 number = (number * 10) + (*cp - '0');
234
235 return( number );
236 }
237
238 /*
239 * convert an ASCII string (decimal radix) to an integer
240 * inputs:
241 * p string pointer.
242 * t char **, return a pointer to the cahr which terminates the
243 * numeric string.
244 * returns:
245 * integer value of the numeric string.
246 * side effect:
247 * pointer to terminating char.
248 */
249
250 int
251 atoi_term(
252 char *p, /* IN */
253 char **t) /* OUT */
254 {
255 register int n;
256 register int f;
257
258 n = 0;
259 f = 0;
260 for(;;p++) {
261 switch(*p) {
262 case ' ':
263 case '\t':
264 continue;
265 case '-':
266 f++;
267 case '+':
268 p++;
269 }
270 break;
271 }
272 while(*p >= '0' && *p <= '9')
273 n = n*10 + *p++ - '0';
274
275 /* return pointer to terminating character */
276 if ( t )
277 *t = p;
278
279 return(f? -n: n);
280 }
281
282 /*
283 * convert an integer to an ASCII string.
284 * inputs:
285 * num integer to be converted
286 * str string pointer.
287 *
288 * outputs:
289 * pointer to string start.
290 */
291
292 char *
293 itoa(
294 int num,
295 char *str)
296 {
297 char digits[11];
298 register char *dp;
299 register char *cp = str;
300
301 if (num == 0) {
302 *cp++ = '0';
303 }
304 else {
305 dp = digits;
306 while (num) {
307 *dp++ = '0' + num % 10;
308 num /= 10;
309 }
310 while (dp != digits) {
311 *cp++ = *--dp;
312 }
313 }
314 *cp++ = '\0';
315
316 return str;
317 }
318
319 char *
320 strcat(
321 register char *dest,
322 register const char *src)
323 {
324 char *old = dest;
325
326 while (*dest)
327 ++dest;
328 while (*dest++ = *src++)
329 ;
330 return (old);
331 }
332