xref: /plan9/sys/src/libc/port/atol.c (revision 59cc4ca53493a3c6d2349fe2b7f7c40f7dce7294)
1 #include <u.h>
2 #include <libc.h>
3 
4 long
atol(char * s)5 atol(char *s)
6 {
7 	long n;
8 	int f, c;
9 
10 	n = 0;
11 	f = 0;
12 	while(*s == ' ' || *s == '\t')
13 		s++;
14 	if(*s == '-' || *s == '+') {
15 		if(*s++ == '-')
16 			f = 1;
17 		while(*s == ' ' || *s == '\t')
18 			s++;
19 	}
20 	if(s[0]=='0' && s[1]) {
21 		if(s[1]=='x' || s[1]=='X'){
22 			s += 2;
23 			for(;;) {
24 				c = *s;
25 				if(c >= '0' && c <= '9')
26 					n = n*16 + c - '0';
27 				else
28 				if(c >= 'a' && c <= 'f')
29 					n = n*16 + c - 'a' + 10;
30 				else
31 				if(c >= 'A' && c <= 'F')
32 					n = n*16 + c - 'A' + 10;
33 				else
34 					break;
35 				s++;
36 			}
37 		} else
38 			while(*s >= '0' && *s <= '7')
39 				n = n*8 + *s++ - '0';
40 	} else
41 		while(*s >= '0' && *s <= '9')
42 			n = n*10 + *s++ - '0';
43 	if(f)
44 		n = -n;
45 	return n;
46 }
47 
48 int
atoi(char * s)49 atoi(char *s)
50 {
51 
52 	return atol(s);
53 }
54