summaryrefslogtreecommitdiff
path: root/sys/src/libc/port/atol.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libc/port/atol.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libc/port/atol.c')
-rwxr-xr-xsys/src/libc/port/atol.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/sys/src/libc/port/atol.c b/sys/src/libc/port/atol.c
new file mode 100755
index 000000000..6928da7bd
--- /dev/null
+++ b/sys/src/libc/port/atol.c
@@ -0,0 +1,53 @@
+#include <u.h>
+#include <libc.h>
+
+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;
+}
+
+int
+atoi(char *s)
+{
+
+ return atol(s);
+}