diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/ape/lib/bsd/inet_addr.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/ape/lib/bsd/inet_addr.c')
-rwxr-xr-x | sys/src/ape/lib/bsd/inet_addr.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/sys/src/ape/lib/bsd/inet_addr.c b/sys/src/ape/lib/bsd/inet_addr.c new file mode 100755 index 000000000..63cd69cc4 --- /dev/null +++ b/sys/src/ape/lib/bsd/inet_addr.c @@ -0,0 +1,52 @@ +/* posix */ +#include <sys/types.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> + +/* bsd extensions */ +#include <sys/uio.h> +#include <sys/socket.h> +#include <netinet/in.h> + +#define CLASS(x) (x[0]>>6) + +unsigned long +inet_addr(char *from) +{ + int i; + char *p; + unsigned char to[4]; + unsigned long x; + + p = from; + memset(to, 0, 4); + for(i = 0; i < 4 && *p; i++){ + to[i] = strtoul(p, &p, 0); + if(*p == '.') + p++; + } + + switch(CLASS(to)){ + case 0: /* class A - 1 byte net */ + case 1: + if(i == 3){ + to[3] = to[2]; + to[2] = to[1]; + to[1] = 0; + } else if (i == 2){ + to[3] = to[1]; + to[1] = 0; + } + break; + case 2: /* class B - 2 byte net */ + if(i == 3){ + to[3] = to[2]; + to[2] = 0; + } + break; + } + x = nptohl(to); + x = htonl(x); + return x; +} |