summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/bsd/inet_aton.c
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@gmx.de>2013-04-01 19:16:18 +0200
committercinap_lenrek <cinap_lenrek@gmx.de>2013-04-01 19:16:18 +0200
commite8a02760901e0b700ba845c9f57601f662e0e0aa (patch)
tree016c9e31ff1b3182e7b3492b4abf2de6c8d469d1 /sys/src/ape/lib/bsd/inet_aton.c
parentbbe95085d4e4cc1efc26d236c29edcc9714a1524 (diff)
ape: add inet_aton()
Diffstat (limited to 'sys/src/ape/lib/bsd/inet_aton.c')
-rw-r--r--sys/src/ape/lib/bsd/inet_aton.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/sys/src/ape/lib/bsd/inet_aton.c b/sys/src/ape/lib/bsd/inet_aton.c
new file mode 100644
index 000000000..8af377862
--- /dev/null
+++ b/sys/src/ape/lib/bsd/inet_aton.c
@@ -0,0 +1,57 @@
+/* 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)
+
+int
+inet_aton(char *from, struct in_addr *in)
+{
+ unsigned char *to;
+ unsigned long x;
+ char *p;
+ int i;
+
+ in->s_addr = 0;
+ to = (unsigned char*)&in->s_addr;
+ if(*from == 0)
+ return 0;
+ for(i = 0; i < 4 && *from; i++, from = p){
+ x = strtoul(from, &p, 0);
+ if(x != (unsigned char)x || p == from)
+ return 0; /* parse error */
+ to[i] = x;
+ if(*p == '.')
+ p++;
+ else if(*p != 0)
+ return 0; /* parse error */
+ }
+
+ 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;
+ }
+ return 1;
+}