summaryrefslogtreecommitdiff
path: root/user/lib/atox.c
blob: c4bef59223dcc7b31f3ade7535268ce695308605 (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
#include <stdlib.h>
#include <ctype.h>

#define ATOX(name, type)             \
	type name(const char *s)         \
	{                                \
		for (; isspace(*s); s++)     \
			;                        \
		int neg = 0;                 \
		switch (*s) {                \
		case '-':                    \
			neg = 1;                 \
			/* fallthrough */        \
		case '+':                    \
			s++;                     \
			break;                   \
		}                            \
		type num = 0;                \
		for (; *s == '0'; s++)       \
			;                        \
		for (; isdigit(*s); s++) {   \
			num *= 10;               \
			num += *s - '0';         \
		}                            \
		return num * (neg ? -1 : 1); \
	}

ATOX(atoi, int)
ATOX(atol, long int)
ATOX(atoll, long long int)