xref: /third_party/musl/src/network/inet_aton.c (revision 570af302)
1#include <ctype.h>
2#include <sys/socket.h>
3#include <netinet/in.h>
4#include <arpa/inet.h>
5#include <stdlib.h>
6
7int __inet_aton(const char *s0, struct in_addr *dest)
8{
9	const char *s = s0;
10	unsigned char *d = (void *)dest;
11	unsigned long a[4] = { 0 };
12	char *z;
13	int i;
14
15	for (i=0; i<4; i++) {
16		a[i] = strtoul(s, &z, 0);
17		if (z==s || (*z && *z != '.') || !isdigit(*s))
18			return 0;
19		if (!*z) break;
20		s=z+1;
21	}
22	if (i==4) return 0;
23	switch (i) {
24	case 0:
25		a[1] = a[0] & 0xffffff;
26		a[0] >>= 24;
27	case 1:
28		a[2] = a[1] & 0xffff;
29		a[1] >>= 16;
30	case 2:
31		a[3] = a[2] & 0xff;
32		a[2] >>= 8;
33	}
34	for (i=0; i<4; i++) {
35		if (a[i] > 255) return 0;
36		d[i] = a[i];
37	}
38	return 1;
39}
40
41weak_alias(__inet_aton, inet_aton);
42