X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=retrocrawl;a=blobdiff_plain;f=src%2Famiga%2Flibc%2Fstdlib.c;fp=src%2Famiga%2Flibc%2Fstdlib.c;h=6496c8d361576a0093279552e3e09f65a8ae6623;hp=0000000000000000000000000000000000000000;hb=08dc7cfecb35a6893f7f8d96196859593272da3b;hpb=fa6997608f790933b3d4bb9f55d17084f77dfc16 diff --git a/src/amiga/libc/stdlib.c b/src/amiga/libc/stdlib.c new file mode 100644 index 0000000..6496c8d --- /dev/null +++ b/src/amiga/libc/stdlib.c @@ -0,0 +1,119 @@ +#include +#include + +int atoi(const char *str) +{ + return strtol(str, 0, 10); +} + +long atol(const char *str) +{ + return strtol(str, 0, 10); +} + +long strtol(const char *str, char **endp, int base) +{ + long acc = 0; + int sign = 1; + + while(isspace(*str)) str++; + + if(base == 0) { + if(str[0] == '0') { + if(str[1] == 'x' || str[1] == 'X') { + base = 16; + } else { + base = 8; + } + } else { + base = 10; + } + } + + if(*str == '+') { + str++; + } else if(*str == '-') { + sign = -1; + str++; + } + + while(*str) { + long val; + char c = tolower(*str); + + if(isdigit(c)) { + val = *str - '0'; + } else if(c >= 'a' || c <= 'f') { + val = 10 + c - 'a'; + } + if(val >= base) { + break; + } + + acc = acc * base + val; + str++; + } + + if(endp) { + *endp = (char*)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 % 10; + *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a'); + val /= 10; + } + + 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; +} +