kern
diff src/klibc/stdlib.c @ 6:7e40f14617ed
forgot to add stdarg.h and stdlib.c
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 13 Jan 2011 20:24:10 +0200 |
parents | |
children | f83f50c17c3b |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/klibc/stdlib.c Thu Jan 13 20:24:10 2011 +0200 1.3 @@ -0,0 +1,86 @@ 1.4 +#include <stdlib.h> 1.5 +#include <ctype.h> 1.6 + 1.7 +int atoi(const char *str) 1.8 +{ 1.9 + return atol(str); 1.10 +} 1.11 + 1.12 +long atol(const char *str) 1.13 +{ 1.14 + long acc = 0; 1.15 + int sign = 1; 1.16 + 1.17 + while(isspace(*str)) str++; 1.18 + 1.19 + if(*str == '+') { 1.20 + str++; 1.21 + } else if(*str == '-') { 1.22 + sign = -1; 1.23 + str++; 1.24 + } 1.25 + 1.26 + while(*str && isdigit(*str)) { 1.27 + acc = acc * 10 + (*str - '0'); 1.28 + str++; 1.29 + } 1.30 + 1.31 + return sign > 0 ? acc : -acc; 1.32 +} 1.33 + 1.34 +void itoa(int val, char *buf, int base) 1.35 +{ 1.36 + static char rbuf[16]; 1.37 + char *ptr = rbuf; 1.38 + int neg = 0; 1.39 + 1.40 + if(val < 0) { 1.41 + neg = 1; 1.42 + val = -val; 1.43 + } 1.44 + 1.45 + if(val == 0) { 1.46 + *ptr++ = '0'; 1.47 + } 1.48 + 1.49 + while(val) { 1.50 + int digit = val % base; 1.51 + *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a'); 1.52 + val /= base; 1.53 + } 1.54 + 1.55 + if(neg) { 1.56 + *ptr++ = '-'; 1.57 + } 1.58 + 1.59 + ptr--; 1.60 + 1.61 + while(ptr >= rbuf) { 1.62 + *buf++ = *ptr--; 1.63 + } 1.64 + *buf = 0; 1.65 +} 1.66 + 1.67 +void utoa(unsigned int val, char *buf, int base) 1.68 +{ 1.69 + static char rbuf[16]; 1.70 + char *ptr = rbuf; 1.71 + 1.72 + if(val == 0) { 1.73 + *ptr++ = '0'; 1.74 + } 1.75 + 1.76 + while(val) { 1.77 + unsigned int digit = val % base; 1.78 + *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a'); 1.79 + val /= base; 1.80 + } 1.81 + 1.82 + ptr--; 1.83 + 1.84 + while(ptr >= rbuf) { 1.85 + *buf++ = *ptr--; 1.86 + } 1.87 + *buf = 0; 1.88 +} 1.89 +