shithub: riscv

Download patch

ref: d457a43461852636db313c4854590b2c62a21e23
parent: 25139465363b2230b69789013e3e0b5f82d76564
author: cinap_lenrek <[email protected]>
date: Mon Aug 3 14:10:53 EDT 2015

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.

--- a/sys/man/2/atof
+++ b/sys/man/2/atof
@@ -135,10 +135,3 @@
 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.
--- 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);
 }
--- 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);
 }