]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/bsd/inet_aton.c
audiohda: fix syntax error
[plan9front.git] / sys / src / ape / lib / bsd / inet_aton.c
1 /* posix */
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 /* bsd extensions */
8 #include <sys/uio.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11
12 #define CLASS(x)        (x[0]>>6)
13
14 int
15 inet_aton(char *from, struct in_addr *in)
16 {
17         unsigned char *to;
18         unsigned long x;
19         char *p;
20         int i;
21
22         in->s_addr = 0;
23         to = (unsigned char*)&in->s_addr;
24         if(*from == 0)
25                 return 0;
26         for(i = 0; i < 4 && *from; i++, from = p){
27                 x = strtoul(from, &p, 0);
28                 if(x != (unsigned char)x || p == from)
29                         return 0;       /* parse error */
30                 to[i] = x;
31                 if(*p == '.')
32                         p++;
33                 else if(*p != 0)
34                         return 0;       /* parse error */
35         }
36
37         switch(CLASS(to)){
38         case 0: /* class A - 1 byte net */
39         case 1:
40                 if(i == 3){
41                         to[3] = to[2];
42                         to[2] = to[1];
43                         to[1] = 0;
44                 } else if (i == 2){
45                         to[3] = to[1];
46                         to[1] = 0;
47                 }
48                 break;
49         case 2: /* class B - 2 byte net */
50                 if(i == 3){
51                         to[3] = to[2];
52                         to[2] = 0;
53                 }
54                 break;
55         }
56         return 1;
57 }