megadrive_test1
view 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 |
line source
1 #include <stdlib.h>
2 #include <ctype.h>
4 int atoi(const char *str)
5 {
6 return strtol(str, 0, 10);
7 }
9 long atol(const char *str)
10 {
11 return strtol(str, 0, 10);
12 }
14 long strtol(const char *str, char **endp, int base)
15 {
16 long acc = 0;
17 int sign = 1;
19 while(isspace(*str)) str++;
21 if(base == 0) {
22 if(str[0] == '0') {
23 if(str[1] == 'x' || str[1] == 'X') {
24 base = 16;
25 } else {
26 base = 8;
27 }
28 } else {
29 base = 10;
30 }
31 }
33 if(*str == '+') {
34 str++;
35 } else if(*str == '-') {
36 sign = -1;
37 str++;
38 }
40 while(*str) {
41 long val;
42 char c = tolower(*str);
44 if(isdigit(c)) {
45 val = *str - '0';
46 } else if(c >= 'a' || c <= 'f') {
47 val = 10 + c - 'a';
48 }
49 if(val >= base) {
50 break;
51 }
53 acc = acc * base + val;
54 str++;
55 }
57 if(endp) {
58 *endp = (char*)str;
59 }
61 return sign > 0 ? acc : -acc;
62 }