be80b2d983d1e5d1ec1a503cc469ca5f400a9cb6
[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 *strncpy(char *dest, char *src, size_t count)
257 {
258 char *ret = dest;
259
260 while((*src) && (count--))
261 *dest++ = *src++;
262 *dest = 0;
263
264 return ret;
265 }
266
267 char *strcat(char *dest, char *src)
268 {
269 char *ret = dest;
270
271 while(*dest)
272 dest++;
273
274 while(*src)
275 *dest++ = *src++;
276 *dest = 0;
277
278 return ret;
279 }
280
281 char *strchr(const char *s, int c)
282 {
283 char cc = c;
284 while (*s)
285 {
286 if (*s == cc)
287 return (char *)s;
288 s++;
289 }
290 if (cc == 0)
291 return (char *)s;
292 return 0;
293 }
294
295 int strcmp(const char *string1, const char *string2)
296 {
297 while(*string1 == *string2)
298 {
299 if(*string1 == 0)
300 return 0;
301
302 string1++;
303 string2++;
304 }
305
306 return *(unsigned const char *)string1 - *(unsigned const char *)(string2);
307 }
308
309 int stricmp(const char *string1, const char *string2)
310 {
311 while(tolower(*string1) == tolower(*string2))
312 {
313 if(*string1 == 0)
314 return 0;
315
316 string1++;
317 string2++;
318 }
319
320 return (int)tolower(*string1) - (int)tolower(*string2);
321 }
322
323 int _strnicmp(const char *string1, const char *string2, size_t length)
324 {
325 if (length == 0)
326 return 0;
327 do
328 {
329 if (toupper(*string1) != toupper(*string2++))
330 return toupper(*(unsigned const char *)string1) - toupper(*(unsigned const char *)--string2);
331 if (*string1++ == 0)
332 break;
333 }
334 while (--length != 0);
335 return 0;
336 }
337
338 char *fgets(char *string, int n, PFILE stream)
339 {
340 int i;
341
342 for(i=0; i<(n-1); i++)
343 {
344 if(IsEndOfFile(stream))
345 {
346 i++;
347 break;
348 }
349
350 ReadFile(stream, 1, NULL, string+i);
351
352 if(string[i] == '\n')
353 {
354 i++;
355 break;
356 }
357 }
358 string[i] = '\0';
359
360 return string;
361 }
362
363 int atoi(char *string)
364 {
365 int base;
366 int result = 0;
367 char *str;
368
369 if((string[0] == '0') && (string[1] == 'x'))
370 {
371 base = 16;
372 str = string + 2;
373 }
374 else
375 {
376 base = 10;
377 str = string;
378 }
379
380 while(1)
381 {
382 if((*str < '0') || (*str > '9'))
383 break;
384
385 result *= base;
386 result += (*str - '0');
387 str++;
388 }
389
390 return result;
391 }