diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2015-08-03 18:10:53 +0200 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2015-08-03 18:10:53 +0200 |
commit | d457a43461852636db313c4854590b2c62a21e23 (patch) | |
tree | 14df482e9e64f59872558aa6e7ce7bd076112fae | |
parent | 25139465363b2230b69789013e3e0b5f82d76564 (diff) |
libc: make atoi() not parse c-style octal and hex numbers
interpreting octal breaks parsing of decimal numbers with
leading zeros. the manpage listed this in the BUGS section,
so we'r going to fix it as this just causes confusion as
most callers of atoi() do not expect it.
-rw-r--r-- | sys/man/2/atof | 7 | ||||
-rw-r--r-- | sys/src/libc/port/atol.c | 43 | ||||
-rw-r--r-- | sys/src/libc/port/atoll.c | 2 |
3 files changed, 3 insertions, 49 deletions
diff --git a/sys/man/2/atof b/sys/man/2/atof index cf7a88fde..885aade28 100644 --- a/sys/man/2/atof +++ b/sys/man/2/atof @@ -135,10 +135,3 @@ Zero is returned if the beginning of the input string is not interpretable as a number; even in this case, .I rptr will be updated. -.SH BUGS -.I Atoi, -.I atol, -and -.I atoll -accept octal and hexadecimal numbers in the style of C, -contrary to the ANSI specification. diff --git a/sys/src/libc/port/atol.c b/sys/src/libc/port/atol.c index 6928da7bd..cf7270c6f 100644 --- a/sys/src/libc/port/atol.c +++ b/sys/src/libc/port/atol.c @@ -4,50 +4,11 @@ long atol(char *s) { - long n; - int f, c; - - n = 0; - f = 0; - while(*s == ' ' || *s == '\t') - s++; - if(*s == '-' || *s == '+') { - if(*s++ == '-') - f = 1; - while(*s == ' ' || *s == '\t') - s++; - } - if(s[0]=='0' && s[1]) { - if(s[1]=='x' || s[1]=='X'){ - s += 2; - for(;;) { - c = *s; - if(c >= '0' && c <= '9') - n = n*16 + c - '0'; - else - if(c >= 'a' && c <= 'f') - n = n*16 + c - 'a' + 10; - else - if(c >= 'A' && c <= 'F') - n = n*16 + c - 'A' + 10; - else - break; - s++; - } - } else - while(*s >= '0' && *s <= '7') - n = n*8 + *s++ - '0'; - } else - while(*s >= '0' && *s <= '9') - n = n*10 + *s++ - '0'; - if(f) - n = -n; - return n; + return strtol(s, nil, 10); } int atoi(char *s) { - - return atol(s); + return strtol(s, nil, 10); } diff --git a/sys/src/libc/port/atoll.c b/sys/src/libc/port/atoll.c index b4497de59..d4d794248 100644 --- a/sys/src/libc/port/atoll.c +++ b/sys/src/libc/port/atoll.c @@ -4,5 +4,5 @@ vlong atoll(char *s) { - return strtoll(s, nil, 0); + return strtoll(s, nil, 10); } |