aboutsummaryrefslogtreecommitdiffstats
path: root/usr/klibc/inet/inet_aton.c
blob: 954e5d8d90be6d50554df52d910c031d74b026e6 (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
/*
 * inet/inet_aton.c
 */

#include <arpa/inet.h>
#include <ctype.h>
#include <stdio.h>

int inet_aton(const char *str, struct in_addr *addr)
{
	int i = 0, digits = 0, val = 0;
	union {
		uint8_t	 b[4];
		uint32_t l;
	} a;
	char ch;

	for (;;) {
		ch = *str++;
		if (ch == (i == 3 ? 0 : '.')) {
			if (digits == 0)
				return 0;
			a.b[i] = val;
			if (++i == 4)
				break;
			digits = 0;
			val = 0;
		} else if (isdigit((unsigned char)ch)) {
			digits++;
			val = val * 10 + (ch - '0');
			if (val > 0xff)
				return 0;
		} else {
			return 0;
		}
	}

	addr->s_addr = a.l;
	return 1;
}