# HG changeset patch # User John Tsiombikas # Date 1294943050 -7200 # Node ID 7e40f14617ed8c0ff7821942bb7ed800b0871650 # Parent 633e35c64772740186895372bbf6d1509a9e9698 forgot to add stdarg.h and stdlib.c diff -r 633e35c64772 -r 7e40f14617ed src/klibc/stdarg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/klibc/stdarg.h Thu Jan 13 20:24:10 2011 +0200 @@ -0,0 +1,12 @@ +#ifndef STDARG_H_ +#define STDARG_H_ + +/* Assumes that arguments are passed on the stack 4-byte aligned */ + +typedef int* va_list; + +#define va_start(ap, last) ((ap) = (int*)&(last) + 1) +#define va_arg(ap, type) (*(type*)(ap)++) +#define va_end(ap) + +#endif /* STDARG_H_ */ diff -r 633e35c64772 -r 7e40f14617ed src/klibc/stdlib.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/klibc/stdlib.c Thu Jan 13 20:24:10 2011 +0200 @@ -0,0 +1,86 @@ +#include +#include + +int atoi(const char *str) +{ + return atol(str); +} + +long atol(const char *str) +{ + long acc = 0; + int sign = 1; + + while(isspace(*str)) str++; + + if(*str == '+') { + str++; + } else if(*str == '-') { + sign = -1; + str++; + } + + while(*str && isdigit(*str)) { + acc = acc * 10 + (*str - '0'); + str++; + } + + return sign > 0 ? acc : -acc; +} + +void itoa(int val, char *buf, int base) +{ + static char rbuf[16]; + char *ptr = rbuf; + int neg = 0; + + if(val < 0) { + neg = 1; + val = -val; + } + + if(val == 0) { + *ptr++ = '0'; + } + + while(val) { + int digit = val % base; + *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a'); + val /= base; + } + + if(neg) { + *ptr++ = '-'; + } + + ptr--; + + while(ptr >= rbuf) { + *buf++ = *ptr--; + } + *buf = 0; +} + +void utoa(unsigned int val, char *buf, int base) +{ + static char rbuf[16]; + char *ptr = rbuf; + + if(val == 0) { + *ptr++ = '0'; + } + + while(val) { + unsigned int digit = val % base; + *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a'); + val /= base; + } + + ptr--; + + while(ptr >= rbuf) { + *buf++ = *ptr--; + } + *buf = 0; +} +