Use ARC-Path to boot ReactOS
[reactos.git] / freeldr / freeldr / stdlib.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1999, 2000 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "freeldr.h"
21 #include "stdlib.h"
22
23 /*
24 * print() - prints unformatted text to stdout
25 */
26 void print(char *str)
27 {
28 int i;
29
30 for(i=0; i<strlen(str); i++)
31 putchar(str[i]);
32 }
33
34 /*
35 * convert_to_ascii() - converts a number to it's ascii equivalent
36 * from:
37 * GRUB -- GRand Unified Bootloader
38 * Copyright (C) 1996 Erich Boleyn <erich@uruk.org>
39 */
40 char *convert_to_ascii(char *buf, int c, ...)
41 {
42 unsigned long num = *((&c) + 1), mult = 10;
43 char *ptr = buf;
44
45 if (c == 'x')
46 mult = 16;
47
48 if ((num & 0x80000000uL) && c == 'd')
49 {
50 num = (~num)+1;
51 *(ptr++) = '-';
52 buf++;
53 }
54
55 do
56 {
57 int dig = num % mult;
58 *(ptr++) = ( (dig > 9) ? dig + 'a' - 10 : '0' + dig );
59 }
60 while (num /= mult);
61
62 /* reorder to correct direction!! */
63 {
64 char *ptr1 = ptr-1;
65 char *ptr2 = buf;
66 while (ptr1 > ptr2)
67 {
68 int c = *ptr1;
69 *ptr1 = *ptr2;
70 *ptr2 = c;
71 ptr1--;
72 ptr2++;
73 }
74 }
75
76 return ptr;
77 }
78
79 /*
80 * printf() - prints formatted text to stdout
81 * from:
82 * GRUB -- GRand Unified Bootloader
83 * Copyright (C) 1996 Erich Boleyn <erich@uruk.org>
84 */
85 void printf(char *format, ... )
86 {
87 int *dataptr = (int *) &format;
88 char c, *ptr, str[16];
89
90 dataptr++;
91
92 while ((c = *(format++)))
93 {
94 if (c != '%')
95 putchar(c);
96 else
97 switch (c = *(format++))
98 {
99 case 'd': case 'u': case 'x':
100 *convert_to_ascii(str, c, *((unsigned long *) dataptr++)) = 0;
101
102 ptr = str;
103
104 while (*ptr)
105 putchar(*(ptr++));
106 break;
107
108 case 'c': putchar((*(dataptr++))&0xff); break;
109
110 case 's':
111 ptr = (char *)(*(dataptr++));
112
113 while ((c = *(ptr++)))
114 putchar(c);
115 break;
116 }
117 }
118 }
119
120 void sprintf(char *buffer, char *format, ... )
121 {
122 int *dataptr = (int *) &format;
123 char c, *ptr, str[16];
124 char *p = buffer;
125
126 dataptr++;
127
128 while ((c = *(format++)))
129 {
130 if (c != '%')
131 {
132 *p = c;
133 p++;
134 }
135 else
136 switch (c = *(format++))
137 {
138 case 'd': case 'u': case 'x':
139 *convert_to_ascii(str, c, *((unsigned long *) dataptr++)) = 0;
140
141 ptr = str;
142
143 while (*ptr)
144 {
145 *p = *(ptr++);
146 p++;
147 }
148 break;
149
150 case 'c':
151 *p = (*(dataptr++))&0xff;
152 p++;
153 break;
154
155 case 's':
156 ptr = (char *)(*(dataptr++));
157
158 while ((c = *(ptr++)))
159 {
160 *p = c;
161 p++;
162 }
163 break;
164 }
165 }
166 *p=0;
167 }
168
169
170 int strlen(char *str)
171 {
172 int len;
173
174 for(len=0; str[len] != '\0'; len++);
175
176 return len;
177 }
178
179 char *itoa(int value, char *string, int radix)
180 {
181 if(radix == 16)
182 *convert_to_ascii(string, 'x', value) = 0;
183 else
184 *convert_to_ascii(string, 'd', value) = 0;
185
186 return string;
187 }
188
189 int toupper(int c)
190 {
191 if((c >= 'a') && (c <= 'z'))
192 c -= 32;
193
194 return c;
195 }
196
197 int tolower(int c)
198 {
199 if((c >= 'A') && (c <= 'Z'))
200 c += 32;
201
202 return c;
203 }
204
205 int memcmp(const void *buf1, const void *buf2, size_t count)
206 {
207 size_t i;
208 const char *buffer1 = buf1;
209 const char *buffer2 = buf2;
210
211 for(i=0; i<count; i++)
212 {
213 if(buffer1[i] == buffer2[i])
214 continue;
215 else
216 return (buffer1[i] - buffer2[i]);
217 }
218
219 return 0;
220 }
221
222 void *memcpy(void *dest, const void *src, size_t count)
223 {
224 size_t i;
225 char *buf1 = dest;
226 const char *buf2 = src;
227
228 for(i=0; i<count; i++)
229 buf1[i] = buf2[i];
230
231 return dest;
232 }
233
234 void *memset(void *dest, int c, size_t count)
235 {
236 size_t i;
237 char *buf1 = dest;
238
239 for(i=0; i<count; i++)
240 buf1[i] = c;
241
242 return dest;
243 }
244
245 char *strcpy(char *dest, char *src)
246 {
247 char *ret = dest;
248
249 while(*src)
250 *dest++ = *src++;
251 *dest = 0;
252
253 return ret;
254 }
255
256 char *strcat(char *dest, char *src)
257 {
258 char *ret = dest;
259
260 while(*dest)
261 dest++;
262
263 while(*src)
264 *dest++ = *src++;
265 *dest = 0;
266
267 return ret;
268 }
269
270 char *strchr(const char *s, int c)
271 {
272 char cc = c;
273 while (*s)
274 {
275 if (*s == cc)
276 return (char *)s;
277 s++;
278 }
279 if (cc == 0)
280 return (char *)s;
281 return 0;
282 }
283
284 int strcmp(const char *string1, const char *string2)
285 {
286 while(*string1 == *string2)
287 {
288 if(*string1 == 0)
289 return 0;
290
291 string1++;
292 string2++;
293 }
294
295 return *(unsigned const char *)string1 - *(unsigned const char *)(string2);
296 }
297
298 int stricmp(const char *string1, const char *string2)
299 {
300 while(tolower(*string1) == tolower(*string2))
301 {
302 if(*string1 == 0)
303 return 0;
304
305 string1++;
306 string2++;
307 }
308
309 return (int)tolower(*string1) - (int)tolower(*string2);
310 }
311
312 int _strnicmp(const char *string1, const char *string2, size_t length)
313 {
314 if (length == 0)
315 return 0;
316 do
317 {
318 if (toupper(*string1) != toupper(*string2++))
319 return toupper(*(unsigned const char *)string1) - toupper(*(unsigned const char *)--string2);
320 if (*string1++ == 0)
321 break;
322 }
323 while (--length != 0);
324 return 0;
325 }
326
327 char *fgets(char *string, int n, FILE *stream)
328 {
329 int i;
330
331 for(i=0; i<(n-1); i++)
332 {
333 if(feof(stream))
334 {
335 i++;
336 break;
337 }
338
339 ReadFile(stream, 1, string+i);
340
341 if(string[i] == '\n')
342 {
343 i++;
344 break;
345 }
346 }
347 string[i] = '\0';
348
349 return string;
350 }
351
352 int atoi(char *string)
353 {
354 int base;
355 int result = 0;
356 char *str;
357
358 if((string[0] == '0') && (string[1] == 'x'))
359 {
360 base = 16;
361 str = string + 2;
362 }
363 else
364 {
365 base = 10;
366 str = string;
367 }
368
369 while(1)
370 {
371 if((*str < '0') || (*str > '9'))
372 break;
373
374 result *= base;
375 result += (*str - '0');
376 str++;
377 }
378
379 return result;
380 }