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.
This commit is contained in:
parent
2513946536
commit
d457a43461
3 changed files with 3 additions and 49 deletions
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -4,5 +4,5 @@
|
|||
vlong
|
||||
atoll(char *s)
|
||||
{
|
||||
return strtoll(s, nil, 0);
|
||||
return strtoll(s, nil, 10);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue