summaryrefslogtreecommitdiff
path: root/sys/src/libc/port/atol.c
blob: 6928da7bd751835733883ceb7ac9bd46ef1f0591 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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);
}