megadrive_test1
annotate src/libc/stdlib.c @ 7:8253942b0a1a
in the middle of something
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 19 Feb 2017 14:00:26 +0200 |
parents | |
children |
rev | line source |
---|---|
nuclear@6 | 1 #include <stdlib.h> |
nuclear@6 | 2 #include <ctype.h> |
nuclear@6 | 3 |
nuclear@6 | 4 int atoi(const char *str) |
nuclear@6 | 5 { |
nuclear@6 | 6 return strtol(str, 0, 10); |
nuclear@6 | 7 } |
nuclear@6 | 8 |
nuclear@6 | 9 long atol(const char *str) |
nuclear@6 | 10 { |
nuclear@6 | 11 return strtol(str, 0, 10); |
nuclear@6 | 12 } |
nuclear@6 | 13 |
nuclear@6 | 14 long strtol(const char *str, char **endp, int base) |
nuclear@6 | 15 { |
nuclear@6 | 16 long acc = 0; |
nuclear@6 | 17 int sign = 1; |
nuclear@6 | 18 |
nuclear@6 | 19 while(isspace(*str)) str++; |
nuclear@6 | 20 |
nuclear@6 | 21 if(base == 0) { |
nuclear@6 | 22 if(str[0] == '0') { |
nuclear@6 | 23 if(str[1] == 'x' || str[1] == 'X') { |
nuclear@6 | 24 base = 16; |
nuclear@6 | 25 } else { |
nuclear@6 | 26 base = 8; |
nuclear@6 | 27 } |
nuclear@6 | 28 } else { |
nuclear@6 | 29 base = 10; |
nuclear@6 | 30 } |
nuclear@6 | 31 } |
nuclear@6 | 32 |
nuclear@6 | 33 if(*str == '+') { |
nuclear@6 | 34 str++; |
nuclear@6 | 35 } else if(*str == '-') { |
nuclear@6 | 36 sign = -1; |
nuclear@6 | 37 str++; |
nuclear@6 | 38 } |
nuclear@6 | 39 |
nuclear@6 | 40 while(*str) { |
nuclear@6 | 41 long val; |
nuclear@6 | 42 char c = tolower(*str); |
nuclear@6 | 43 |
nuclear@6 | 44 if(isdigit(c)) { |
nuclear@6 | 45 val = *str - '0'; |
nuclear@6 | 46 } else if(c >= 'a' || c <= 'f') { |
nuclear@6 | 47 val = 10 + c - 'a'; |
nuclear@6 | 48 } |
nuclear@6 | 49 if(val >= base) { |
nuclear@6 | 50 break; |
nuclear@6 | 51 } |
nuclear@6 | 52 |
nuclear@6 | 53 acc = acc * base + val; |
nuclear@6 | 54 str++; |
nuclear@6 | 55 } |
nuclear@6 | 56 |
nuclear@6 | 57 if(endp) { |
nuclear@6 | 58 *endp = (char*)str; |
nuclear@6 | 59 } |
nuclear@6 | 60 |
nuclear@6 | 61 return sign > 0 ? acc : -acc; |
nuclear@6 | 62 } |