kern
diff src/klibc/stdlib.c @ 91:f83f50c17c3b
continuing with the fs
added strtol and strstr to klibc
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 09 Dec 2011 15:29:54 +0200 |
parents | 7e40f14617ed |
children |
line diff
1.1 --- a/src/klibc/stdlib.c Fri Dec 09 13:44:15 2011 +0200 1.2 +++ b/src/klibc/stdlib.c Fri Dec 09 15:29:54 2011 +0200 1.3 @@ -3,16 +3,33 @@ 1.4 1.5 int atoi(const char *str) 1.6 { 1.7 - return atol(str); 1.8 + return strtol(str, 0, 10); 1.9 } 1.10 1.11 long atol(const char *str) 1.12 { 1.13 + return strtol(str, 0, 10); 1.14 +} 1.15 + 1.16 +long strtol(const char *str, char **endp, int base) 1.17 +{ 1.18 long acc = 0; 1.19 int sign = 1; 1.20 1.21 while(isspace(*str)) str++; 1.22 1.23 + if(base == 0) { 1.24 + if(str[0] == '0') { 1.25 + if(str[1] == 'x' || str[1] == 'X') { 1.26 + base = 16; 1.27 + } else { 1.28 + base = 8; 1.29 + } 1.30 + } else { 1.31 + base = 10; 1.32 + } 1.33 + } 1.34 + 1.35 if(*str == '+') { 1.36 str++; 1.37 } else if(*str == '-') { 1.38 @@ -20,11 +37,27 @@ 1.39 str++; 1.40 } 1.41 1.42 - while(*str && isdigit(*str)) { 1.43 - acc = acc * 10 + (*str - '0'); 1.44 + while(*str) { 1.45 + long val; 1.46 + char c = tolower(*str); 1.47 + 1.48 + if(isdigit(c)) { 1.49 + val = *str - '0'; 1.50 + } else if(c >= 'a' || c <= 'f') { 1.51 + val = 10 + c - 'a'; 1.52 + } 1.53 + if(val >= base) { 1.54 + break; 1.55 + } 1.56 + 1.57 + acc = acc * base + val; 1.58 str++; 1.59 } 1.60 1.61 + if(endp) { 1.62 + *endp = (char*)str; 1.63 + } 1.64 + 1.65 return sign > 0 ? acc : -acc; 1.66 } 1.67