--- a/toys/pending/dhcp.c	2015-10-20 10:25:21.000000000 +0530
+++ b/toys/pending/dhcp.c	2015-10-20 11:51:17.000000000 +0530
@@ -4,17 +4,17 @@
  * Copyright 2013 Kyungwan Han <asura321@gmail.com>
  *
  * Not in SUSv4.
-USE_DHCP(NEWTOY(dhcp, "V:H:F:x*r:O*A#<0T#<0t#<0s:p:i:SBRCaovqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))
+USE_DHCP(NEWTOY(dhcp, "6V:H:F:x*r:O*A#<0>2147483647=20T#<0>2147483647=3t#<0>2147483647=3s:p:i:SBRCaovqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))
 
 config DHCP
   bool "dhcp"
-  default n
+  default y
   help
-   usage: dhcp [-fbnqvoCRB] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE]
+   usage: dhcp [-6fbnqvoCRB] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE]
                [-H HOSTNAME] [-V VENDOR] [-x OPT:VAL] [-O OPT]
 
-        Configure network dynamicaly using DHCP.
-
+       Configure network dynamicaly using DHCP.
+      -6 run dhcpV6 client.
       -i Interface to use (default eth0)
       -p Create pidfile
       -s Run PROG at DHCP events (default /usr/share/dhcp/default.script)
@@ -57,23 +57,14 @@
 #include <linux/if_ether.h>
 
 GLOBALS(
-    char *iface;
-    char *pidfile;
-    char *script;
-    long retries;
-    long timeout;
-    long tryagain;
-    struct arg_list *req_opt;
-    char *req_ip;
-    struct arg_list *pkt_opt;
-    char *fdn_name;
-    char *hostname;
-    char *vendor_cls;
+  char *iface, *pidfile, *script;
+  long retries, timeout, tryagain;
+  struct arg_list *req_opt;
+  char *req_ip;
+  struct arg_list *pkt_opt;
+  char *fdn_name, *hostname, *vendor_cls;
 )
 
-#define flag_get(f,v,d) ((toys.optflags & f) ? v : d)
-#define flag_chk(f)     ((toys.optflags & f) ? 1 : 0)
-
 #define STATE_INIT            0
 #define STATE_REQUESTING      1
 #define STATE_BOUND           2
@@ -136,34 +127,22 @@
 #define MODE_APP        2
 
 static void (*dbg)(char *format, ...);
-static void dummy(char *format, ...){
+static void dummy(char *format, ...)
+{
 	return;
 }
 
 typedef struct dhcpc_result_s {
-  struct in_addr serverid;
-  struct in_addr ipaddr;
-  struct in_addr netmask;
-  struct in_addr dnsaddr;
-  struct in_addr default_router;
+  struct in_addr serverid, ipaddr, netmask, dnsaddr, default_router;
   uint32_t lease_time;
 } dhcpc_result_t;
 
 typedef struct __attribute__((packed)) dhcp_msg_s {
-  uint8_t op;
-  uint8_t htype;
-  uint8_t hlen;
-  uint8_t hops;
+  uint8_t op, htype, hlen, hops;
   uint32_t xid;
-  uint16_t secs;
-  uint16_t flags;
-  uint32_t ciaddr;
-  uint32_t yiaddr;
-  uint32_t nsiaddr;
-  uint32_t ngiaddr;
-  uint8_t chaddr[16];
-  uint8_t sname[64];
-  uint8_t file[128];
+  uint16_t secs, flags;
+  uint32_t ciaddr, yiaddr, nsiaddr, ngiaddr;
+  uint8_t chaddr[16], sname[64], file[128];
   uint32_t cookie;
   uint8_t options[308];
 } dhcp_msg_t;
@@ -176,11 +155,8 @@
 
 typedef struct dhcpc_state_s {
   uint8_t macaddr[6];
-   char *iface;
-  int ifindex;
-  int sockfd;
-  int status;
-  int mode;
+  char *iface;
+  int ifindex, sockfd, status, mode;
   uint32_t mask;
   struct in_addr ipaddr;
   struct in_addr serverid;
@@ -199,7 +175,7 @@
 static dhcpc_state_t *state;
 static struct fd_pair sigfd;
 uint8_t bmacaddr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
- int set = 1;
+int set = 1;
 uint8_t infomode = LOG_CONSOLE;
 uint8_t raw_opt[29];
 int raw_optcount = 0;
@@ -208,57 +184,58 @@
 
 static option_val_t *msgopt_list = NULL;
 static option_val_t options_list[] = {
-    {"lease"          , DHCP_NUM32  | 0x33, NULL, 0},
-    {"subnet"         , DHCP_IP     | 0x01, NULL, 0},
-    {"broadcast"      , DHCP_IP     | 0x1c, NULL, 0},
-    {"router"         , DHCP_IP     | 0x03, NULL, 0},
-    {"ipttl"          , DHCP_NUM8   | 0x17, NULL, 0},
-    {"mtu"            , DHCP_NUM16  | 0x1a, NULL, 0},
-    {"hostname"       , DHCP_STRING | 0x0c, NULL, 0},
-    {"domain"         , DHCP_STRING | 0x0f, NULL, 0},
-    {"search"         , DHCP_STRLST | 0x77, NULL, 0},
-    {"nisdomain"      , DHCP_STRING | 0x28, NULL, 0},
-    {"timezone"       , DHCP_NUM32  | 0x02, NULL, 0},
-    {"tftp"           , DHCP_STRING | 0x42, NULL, 0},
-    {"bootfile"       , DHCP_STRING | 0x43, NULL, 0},
-    {"bootsize"       , DHCP_NUM16  | 0x0d, NULL, 0},
-    {"rootpath"       , DHCP_STRING | 0x11, NULL, 0},
-    {"wpad"           , DHCP_STRING | 0xfc, NULL, 0},
-    {"serverid"       , DHCP_IP     | 0x36, NULL, 0},
-    {"message"        , DHCP_STRING | 0x38, NULL, 0},
-    {"vlanid"         , DHCP_NUM32  | 0x84, NULL, 0},
-    {"vlanpriority"   , DHCP_NUM32  | 0x85, NULL, 0},
-    {"dns"            , DHCP_IPLIST | 0x06, NULL, 0},
-    {"wins"           , DHCP_IPLIST | 0x2c, NULL, 0},
-    {"nissrv"         , DHCP_IPLIST | 0x29, NULL, 0},
-    {"ntpsrv"         , DHCP_IPLIST | 0x2a, NULL, 0},
-    {"lprsrv"         , DHCP_IPLIST | 0x09, NULL, 0},
-    {"swapsrv"        , DHCP_IP     | 0x10, NULL, 0},
-    {"routes"         , DHCP_STCRTS | 0x21, NULL, 0},
-    {"staticroutes"   , DHCP_STCRTS | 0x79, NULL, 0},
-    {"msstaticroutes" , DHCP_STCRTS | 0xf9, NULL, 0},
+  {"lease"          , DHCP_NUM32  | 0x33, NULL, 0},
+  {"subnet"         , DHCP_IP     | 0x01, NULL, 0},
+  {"broadcast"      , DHCP_IP     | 0x1c, NULL, 0},
+  {"router"         , DHCP_IP     | 0x03, NULL, 0},
+  {"ipttl"          , DHCP_NUM8   | 0x17, NULL, 0},
+  {"mtu"            , DHCP_NUM16  | 0x1a, NULL, 0},
+  {"hostname"       , DHCP_STRING | 0x0c, NULL, 0},
+  {"domain"         , DHCP_STRING | 0x0f, NULL, 0},
+  {"search"         , DHCP_STRLST | 0x77, NULL, 0},
+  {"nisdomain"      , DHCP_STRING | 0x28, NULL, 0},
+  {"timezone"       , DHCP_NUM32  | 0x02, NULL, 0},
+  {"tftp"           , DHCP_STRING | 0x42, NULL, 0},
+  {"bootfile"       , DHCP_STRING | 0x43, NULL, 0},
+  {"bootsize"       , DHCP_NUM16  | 0x0d, NULL, 0},
+  {"rootpath"       , DHCP_STRING | 0x11, NULL, 0},
+  {"wpad"           , DHCP_STRING | 0xfc, NULL, 0},
+  {"serverid"       , DHCP_IP     | 0x36, NULL, 0},
+  {"message"        , DHCP_STRING | 0x38, NULL, 0},
+  {"vlanid"         , DHCP_NUM32  | 0x84, NULL, 0},
+  {"vlanpriority"   , DHCP_NUM32  | 0x85, NULL, 0},
+  {"dns"            , DHCP_IPLIST | 0x06, NULL, 0},
+  {"wins"           , DHCP_IPLIST | 0x2c, NULL, 0},
+  {"nissrv"         , DHCP_IPLIST | 0x29, NULL, 0},
+  {"ntpsrv"         , DHCP_IPLIST | 0x2a, NULL, 0},
+  {"lprsrv"         , DHCP_IPLIST | 0x09, NULL, 0},
+  {"swapsrv"        , DHCP_IP     | 0x10, NULL, 0},
+  {"routes"         , DHCP_STCRTS | 0x21, NULL, 0},
+  {"staticroutes"   , DHCP_STCRTS | 0x79, NULL, 0},
+  {"msstaticroutes" , DHCP_STCRTS | 0xf9, NULL, 0},
 };
 
 static  struct sock_filter filter_instr[] = {
-    BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
-    BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 6),
-    BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
-    BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K, 0x1fff, 4, 0),
-    BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), BPF_STMT(BPF_LD|BPF_H|BPF_IND, 2),
-    BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 68, 0, 1),
-    BPF_STMT(BPF_RET|BPF_K, 0xffffffff), BPF_STMT(BPF_RET|BPF_K, 0),
+  BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
+  BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 6),
+  BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
+  BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K, 0x1fff, 4, 0),
+  BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), BPF_STMT(BPF_LD|BPF_H|BPF_IND, 2),
+  BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 68, 0, 1),
+  BPF_STMT(BPF_RET|BPF_K, 0xffffffff), BPF_STMT(BPF_RET|BPF_K, 0),
 };
 
 static  struct sock_fprog filter_prog = {
-    .len = ARRAY_LEN(filter_instr), 
-    .filter = (struct sock_filter *) filter_instr,
+  .len = ARRAY_LEN(filter_instr), 
+  .filter = (struct sock_filter *) filter_instr,
 };
 
 // calculate options size.
 static int dhcp_opt_size(uint8_t *optionptr)
 {
   int i = 0;
-  for(;optionptr[i] != 0xff; i++) if(optionptr[i] != 0x00) i += optionptr[i + 1] + 2 -1;
+  for (;optionptr[i] != 0xff; i++)
+    if (optionptr[i] != 0x00) i += optionptr[i + 1] + 2 -1;
   return i;
 }
 
@@ -281,7 +258,8 @@
 }
 
 // gets information of INTERFACE and updates IFINDEX, MAC and IP
-static int get_interface( char *interface, int *ifindex, uint32_t *oip, uint8_t *mac)
+static int get_interface( char *interface, int *ifindex,
+        uint32_t *oip, uint8_t *mac)
 {
   struct ifreq req;
   struct sockaddr_in *ip;
@@ -308,7 +286,8 @@
   if (mac) {
     xioctl(fd, SIOCGIFHWADDR, &req);
     memcpy(mac, req.ifr_hwaddr.sa_data, 6);
-    dbg("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+    dbg("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", 
+            mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
   }
   close(fd);
   return 0;
@@ -354,7 +333,7 @@
     char pidbuf[12];
 
     sprintf(pidbuf, "%u", (unsigned)getpid());
-    write(pidfile, pidbuf, strlen(pidbuf));
+    xwrite(pidfile, pidbuf, strlen(pidbuf));
     close(pidfile);
   }
 }
@@ -460,9 +439,11 @@
     options_list[count].val = strdup(valstr);
     break;
   case DHCP_IPLIST:
-    while(valstr){
-      options_list[count].val = xrealloc(options_list[count].val, options_list[count].len + sizeof(uint32_t));
-      striptovar(valstr, ((uint8_t*)options_list[count].val)+options_list[count].len);
+    while(valstr) {
+      options_list[count].val = xrealloc(options_list[count].val, 
+              options_list[count].len + sizeof(uint32_t));
+      striptovar(valstr, ((uint8_t*)options_list[count].val)
+              + options_list[count].len);
       options_list[count].len += sizeof(uint32_t);
       valstr = strtok(NULL," \t");
     }
@@ -485,15 +466,21 @@
       if (!tp) error_exit("malformed static route option");
       *tp = '\0';
       mask = strtol(++tp, &tp, 10);
-      if (striptovar(grp, (uint8_t*)&nip) < 0) error_exit("malformed static route option");
-      while(*tp == ' ' || *tp == '\t' || *tp == '-') tp++;
-      if (striptovar(tp, (uint8_t*)&router) < 0) error_exit("malformed static route option");
-      options_list[count].val = xrealloc(options_list[count].val, options_list[count].len + 1 + mask/8 + 4);
-      memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &mask, 1);
+      if (striptovar(grp, (uint8_t*)&nip) < 0)
+        error_exit("malformed static route option");
+      while (*tp == ' ' || *tp == '\t' || *tp == '-') tp++;
+      if (striptovar(tp, (uint8_t*)&router) < 0)
+        error_exit("malformed static route option");
+      options_list[count].val = xrealloc(options_list[count].val,
+              options_list[count].len + 1 + mask/8 + 4);
+      memcpy(((uint8_t*)options_list[count].val)
+              + options_list[count].len, &mask, 1);
       options_list[count].len += 1;
-      memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &nip, mask/8);
+      memcpy(((uint8_t*)options_list[count].val)
+              + options_list[count].len, &nip, mask/8);
       options_list[count].len += mask/8;
-      memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &router, 4);
+      memcpy(((uint8_t*)options_list[count].val)
+              + options_list[count].len, &router, 4);
       options_list[count].len += 4;
       tp = NULL;
       grp = strtok(NULL, ",");
@@ -518,7 +505,7 @@
   }
   if (msgopt_list) {
     for (count = 0; count < size; count++) {
-        if ((msgopt_list[count].len == 0) || (msgopt_list[count].val == NULL)) continue;
+        if ((!msgopt_list[count].len) || (!msgopt_list[count].val)) continue;
         ret = setenv(msgopt_list[count].key, (char*)msgopt_list[count].val, 1);
         if (ret) return ret;
       }
@@ -531,10 +518,10 @@
 {
   volatile int error = 0;
   pid_t pid;
-  char *argv[3];
+  char *argv[3], *script = "/usr/share/dhcp/default.script";
   struct stat sts;
-  char *script = flag_get(FLAG_s, TT.script, "/usr/share/dhcp/default.script");
-
+    
+  if (TT.script) script = TT.script;
   if (stat(script, &sts) == -1 && errno == ENOENT) return;
   if (fill_envp(res)) {
     dbg("Failed to create environment variables.");
@@ -600,7 +587,8 @@
     return -1;
   }
   state->mode = MODE_RAW;
-  if (setsockopt(state->sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog)) < 0)
+  if (setsockopt(state->sockfd, SOL_SOCKET, SO_ATTACH_FILTER, 
+      &filter_prog, sizeof(filter_prog)) < 0)
     dbg("MODE RAW : filter attach fail.\n");
 
   dbg("MODE RAW : success\n");
@@ -623,7 +611,8 @@
     return -1;
   }
   setsockopt(state->sockfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set));
-  if (setsockopt(state->sockfd, SOL_SOCKET, SO_BROADCAST, &set, sizeof(set)) == -1) {
+  if (setsockopt(state->sockfd, SOL_SOCKET, SO_BROADCAST, 
+      &set, sizeof(set)) == -1) {
     dbg("MODE APP : brodcast failed.\n");
     close(state->sockfd);
     return -1;
@@ -691,7 +680,8 @@
     dbg("\tPacket with bad UDP checksum received, ignoring\n");
     return -2;
   }
-  memcpy(&state->pdhcp, &packet.dhcp, bytes - (sizeof(packet.iph) + sizeof(packet.udph)));
+  memcpy(&state->pdhcp, &packet.dhcp, 
+    bytes - (sizeof(packet.iph) + sizeof(packet.udph)));
   if (state->pdhcp.cookie != htonl(DHCP_MAGIC)) {
     dbg("\tPacket with bad magic, ignoring\n");
     return -2;
@@ -857,7 +847,8 @@
   return optptr + 2;
 }
 
-static uint8_t *dhcpc_addstropt(uint8_t *optptr, uint8_t opcode, char* str, int len)
+static uint8_t *dhcpc_addstropt(uint8_t *optptr, uint8_t opcode,
+        char* str, int len)
 {
   *optptr++ = opcode;
   *optptr++ = len;
@@ -907,14 +898,14 @@
   *len = 0;
   optptr++;
 
-  if (!flag_chk(FLAG_o)) {
+  if (!(toys.optflags & FLAG_o)) {
     *len = 4;
     *optptr++ = DHCP_OPTION_SUBNET_MASK;
     *optptr++ = DHCP_OPTION_ROUTER;
     *optptr++ = DHCP_OPTION_DNS_SERVER;
     *optptr++ = DHCP_OPTION_BROADCAST;
   }
-  if (flag_chk(FLAG_O)) {
+  if (toys.optflags & FLAG_O) {
     memcpy(optptr++, raw_opt, raw_optcount);
     *len += raw_optcount;
   }
@@ -933,7 +924,7 @@
   int count;
   int size = ARRAY_LEN(options_list);
   for (count = 0; count < size; count++) {
-    if ((options_list[count].len == 0) || (options_list[count].val == NULL)) continue;
+    if ((!options_list[count].len) || (!options_list[count].val)) continue;
     *optptr++ = (uint8_t) (options_list[count].code & 0x00FF);
     *optptr++ = (uint8_t) options_list[count].len;
     memcpy(optptr, options_list[count].val, options_list[count].len);
@@ -980,13 +971,12 @@
   return var;
 }
 
-
 // sends dhcp msg of MSGTYPE
 static int dhcpc_sendmsg(int msgtype)
 {
   uint8_t *pend;
   struct in_addr rqsd;
-  char *vendor;
+  char *vendor = "toybox";
 
   // Create the common message header settings
   memset(&state->pdhcp, 0, sizeof(dhcp_msg_t));
@@ -1002,36 +992,43 @@
   pend = state->pdhcp.options;
   pend = dhcpc_addmsgtype(pend, msgtype);
 
-  if (!flag_chk(FLAG_C)) pend = dhcpc_addclientid(pend);
+  if (!(toys.optflags & FLAG_C)) pend = dhcpc_addclientid(pend);
   // Handle the message specific settings
   switch (msgtype) {
   case DHCPDISCOVER: // Broadcast DISCOVER message to all servers
     state->pdhcp.flags = htons(BOOTP_BROADCAST); //  Broadcast bit.
-    if (flag_chk(FLAG_r)) {
+    if (toys.optflags & FLAG_r) {
       inet_aton(TT.req_ip, &rqsd);
       pend = dhcpc_addreqipaddr(&rqsd, pend);
     }
     pend = dhcpc_addmaxsize(pend, htons(sizeof(dhcp_raw_t)));
-    vendor = flag_get(FLAG_V, TT.vendor_cls, "toybox\0");
+    if (TT.vendor_cls) vendor = TT.vendor_cls;
     pend = dhcpc_addstropt(pend, DHCP_OPTION_VENDOR, vendor, strlen(vendor));
-    if (flag_chk(FLAG_H)) pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, TT.hostname, strlen(TT.hostname));
-    if (flag_chk(FLAG_F)) pend = dhcpc_addfdnname(pend, TT.fdn_name);
-    if ((!flag_chk(FLAG_o)) || flag_chk(FLAG_O)) pend = dhcpc_addreqoptions(pend);
-    if (flag_chk(FLAG_x)) pend = set_xopt(pend);
+    if (toys.optflags & FLAG_H)
+      pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME,
+              TT.hostname, strlen(TT.hostname));
+    if (toys.optflags & FLAG_F) pend = dhcpc_addfdnname(pend, TT.fdn_name);
+    if (!(toys.optflags & FLAG_o) || (toys.optflags & FLAG_O))
+      pend = dhcpc_addreqoptions(pend);
+    if (toys.optflags & FLAG_x) pend = set_xopt(pend);
     break;
   case DHCPREQUEST: // Send REQUEST message to the server that sent the *first* OFFER
     state->pdhcp.flags = htons(BOOTP_BROADCAST); //  Broadcast bit.
-    if (state->status == STATE_RENEWING) memcpy(&state->pdhcp.ciaddr, &state->ipaddr.s_addr, 4);
+    if (state->status == STATE_RENEWING)
+      memcpy(&state->pdhcp.ciaddr, &state->ipaddr.s_addr, 4);
     pend = dhcpc_addmaxsize(pend, htons(sizeof(dhcp_raw_t)));
     rqsd.s_addr = htonl(server);
     pend = dhcpc_addserverid(&rqsd, pend);
     pend = dhcpc_addreqipaddr(&state->ipaddr, pend);
-    vendor = flag_get(FLAG_V, TT.vendor_cls, "toybox\0");
+    if (TT.vendor_cls) vendor = TT.vendor_cls;
     pend = dhcpc_addstropt(pend, DHCP_OPTION_VENDOR, vendor, strlen(vendor));
-    if (flag_chk(FLAG_H)) pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, TT.hostname, strlen(TT.hostname));
-    if (flag_chk(FLAG_F)) pend = dhcpc_addfdnname(pend, TT.fdn_name);
-    if ((!flag_chk(FLAG_o)) || flag_chk(FLAG_O)) pend = dhcpc_addreqoptions(pend);
-    if (flag_chk(FLAG_x)) pend = set_xopt(pend);
+    if (toys.optflags & FLAG_H)
+      pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, 
+              TT.hostname, strlen(TT.hostname));
+    if (toys.optflags & FLAG_F) pend = dhcpc_addfdnname(pend, TT.fdn_name);
+    if (!(toys.optflags & FLAG_o) || (toys.optflags & FLAG_O))
+      pend = dhcpc_addreqoptions(pend);
+    if (toys.optflags & FLAG_x) pend = set_xopt(pend);
     break;
   case DHCPRELEASE: // Send RELEASE message to the server.
     memcpy(&state->pdhcp.ciaddr, &state->ipaddr.s_addr, 4);
@@ -1060,7 +1057,7 @@
   struct in_addr addr;
   int count, optlen, size = ARRAY_LEN(options_list);
 
-  if (flag_chk(FLAG_x)) {
+  if (toys.optflags & FLAG_x) {
     if(msgopt_list){
       for (count = 0; count < size; count++){
         if(msgopt_list[count].val) free(msgopt_list[count].val);
@@ -1174,7 +1171,8 @@
             ((uint8_t*) &nip)[1], ((uint8_t*) &nip)[2], ((uint8_t*) &nip)[3]);
         pfx = " ";
         dest += sprintf(dest, "/%u ", mask);
-        dest += sprintf(dest, "%u.%u.%u.%u", options[0], options[1], options[2], options[3]);
+        dest += sprintf(dest, "%u.%u.%u.%u",
+                options[0], options[1], options[2], options[3]);
         options += 4;
         optlen -= 4;
       }
@@ -1185,8 +1183,10 @@
     }
     optptr += optptr[1] + 2;
   }
-  if ((overloaded == 1) || (overloaded == 3)) dhcpc_parseoptions(presult, optptr);
-  if ((overloaded == 2) || (overloaded == 3)) dhcpc_parseoptions(presult, optptr);
+  if ((overloaded == 1) || (overloaded == 3))
+    dhcpc_parseoptions(presult, optptr);
+  if ((overloaded == 2) || (overloaded == 3))
+    dhcpc_parseoptions(presult, optptr);
   return type;
 }
 
@@ -1235,11 +1235,13 @@
 
   mode_app();
   // send release packet
-  if (state->status == STATE_BOUND || state->status == STATE_RENEWING || state->status == STATE_REBINDING) {
+  if (state->status == STATE_BOUND || state->status == STATE_RENEWING
+          || state->status == STATE_REBINDING) {
     temp_addr.s_addr = htonl(server);
     xstrncpy(buffer, inet_ntoa(temp_addr), sizeof(buffer));
     temp_addr.s_addr = state->ipaddr.s_addr;
-    infomsg( infomode, "Unicasting a release of %s to %s", inet_ntoa(temp_addr), buffer);
+    infomsg( infomode, "Unicasting a release of %s to %s", 
+            inet_ntoa(temp_addr), buffer);
     dhcpc_sendmsg(DHCPRELEASE);
     run_script(NULL, "deconfig");
   }
@@ -1255,7 +1257,7 @@
   int count, size = ARRAY_LEN(options_list);
   for (count = 0; count < size; count++)
     if (options_list[count].val) free(options_list[count].val);
-  if(flag_chk(FLAG_x)){
+  if (toys.optflags & FLAG_x) {
     for (count = 0; count < size; count++)
         if (msgopt_list[count].val) free(msgopt_list[count].val);
     free(msgopt_list);
@@ -1270,26 +1272,48 @@
   uint8_t packets = 0, retries = 0;
   uint32_t timeout = 0, waited = 0;
   fd_set rfds;
+  
+  if (toys.optflags & FLAG_6) {
+    int cnt = 0;
+    char **argv6 = xzalloc((17) * sizeof(char*)); 
+    argv6[cnt++] = "dhcp6";
+    if (toys.optflags & FLAG_i) argv6[cnt++] = xmprintf("-i%s",TT.iface);
+    if (toys.optflags & FLAG_p) argv6[cnt++] = xmprintf("-p%s",TT.pidfile);
+    if (toys.optflags & FLAG_s) argv6[cnt++] = xmprintf("-s%s",TT.script);
+    if (toys.optflags & FLAG_t) argv6[cnt++] = xmprintf("-t%d",TT.retries);
+    if (toys.optflags & FLAG_T) argv6[cnt++] = xmprintf("-T%d",TT.timeout);
+    if (toys.optflags & FLAG_A) argv6[cnt++] = xmprintf("-A%d",TT.tryagain);
+    if (toys.optflags & FLAG_r) argv6[cnt++] = xmprintf("-r%s",TT.req_ip);
+    if (toys.optflags & FLAG_f) argv6[cnt++] = "-f";
+    if (toys.optflags & FLAG_b) argv6[cnt++] = "-b";
+    if (toys.optflags & FLAG_n) argv6[cnt++] = "-n";
+    if (toys.optflags & FLAG_q) argv6[cnt++] = "-q";
+    if (toys.optflags & FLAG_R) argv6[cnt++] = "-R";
+    if (toys.optflags & FLAG_S) argv6[cnt++] = "-S";
+    if (toys.optflags & FLAG_v) argv6[cnt++] = "-v";
+    xexec(argv6);
+  }
 
   xid = 0;
   setlinebuf(stdout);
   dbg = dummy;
-  if (flag_chk(FLAG_v)) dbg = xprintf;
-  if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
-  retries = flag_get(FLAG_t, TT.retries, 3);
-  if (flag_chk(FLAG_S)) {
-      openlog("UDHCPC :", LOG_PID, LOG_DAEMON);
-      infomode |= LOG_SYSTEM;
+  if (toys.optflags & FLAG_v) dbg = xprintf;
+  if (toys.optflags & FLAG_p) write_pid(TT.pidfile);
+  
+  retries = TT.retries;
+  if (toys.optflags & FLAG_S) {
+    openlog("UDHCPC :", LOG_PID, LOG_DAEMON);
+    infomode |= LOG_SYSTEM;
   }
   infomsg(infomode, "dhcp started");
-  if (flag_chk(FLAG_O)) {
+  if (toys.optflags & FLAG_O) {
     while (TT.req_opt) {
       raw_opt[raw_optcount] = (uint8_t) strtoopt(TT.req_opt->arg, 1);
       raw_optcount++;
       TT.req_opt = TT.req_opt->next;
     }
   }
-  if (flag_chk(FLAG_x)) {
+  if (toys.optflags & FLAG_x) {
     while (TT.pkt_opt) {
       (void) strtoopt(TT.pkt_opt->arg, 0);
       TT.pkt_opt = TT.pkt_opt->next;
@@ -1298,7 +1322,7 @@
   memset(&result, 0, sizeof(dhcpc_result_t));
   state = (dhcpc_state_t*) xmalloc(sizeof(dhcpc_state_t));
   memset(state, 0, sizeof(dhcpc_state_t));
-  state->iface = flag_get(FLAG_i, TT.iface, "eth0");
+  state->iface = TT.iface ? TT.iface : "eth0";
 
   if (get_interface(state->iface, &state->ifindex, NULL, state->macaddr))
     perror_exit("Failed to get interface %s", state->iface);
@@ -1339,25 +1363,25 @@
           infomsg(infomode, "Sending discover...");
           dhcpc_sendmsg(DHCPDISCOVER);
           server = 0;
-          timeout = flag_get(FLAG_T, TT.timeout, 3);
+          timeout = TT.timeout;
           waited = 0;
           packets++;
           continue;
         }
 lease_fail:
         run_script(NULL,"leasefail");
-        if (flag_chk(FLAG_n)) {
+        if (toys.optflags & FLAG_n) {
           infomsg(infomode, "Lease failed. Exiting");
           goto ret_with_sockfd;
         }
-        if (flag_chk(FLAG_b)) {
+        if (toys.optflags & FLAG_b) {
           infomsg(infomode, "Lease failed. Going Daemon mode");
-          daemon(0, 0);
-          if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
+          if (daemon(0,0)) perror_exit("daemonize");
+          if (toys.optflags & FLAG_p) write_pid(TT.pidfile);
           toys.optflags &= ~FLAG_b;
           toys.optflags |= FLAG_f;
         }
-        timeout = flag_get(FLAG_A, TT.tryagain, 20);
+        timeout = TT.tryagain;
         waited = 0;
         packets = 0;
         continue;
@@ -1366,8 +1390,11 @@
           memcpy(&state->ipaddr.s_addr,&state->pdhcp.yiaddr, 4);
           dhcpc_sendmsg(DHCPREQUEST);
           infomsg(infomode, "Sending select for %d.%d.%d.%d...",
-              (result.ipaddr.s_addr >> 24) & 0xff, (result.ipaddr.s_addr >> 16) & 0xff, (result.ipaddr.s_addr >> 8) & 0xff, (result.ipaddr.s_addr) & 0xff);
-          timeout = flag_get(FLAG_T, TT.timeout, 3);
+              (result.ipaddr.s_addr >> 24) & 0xff,
+              (result.ipaddr.s_addr >> 16) & 0xff,
+              (result.ipaddr.s_addr >> 8) & 0xff,
+              (result.ipaddr.s_addr) & 0xff);
+          timeout = TT.timeout;
           waited = 0;
           packets++;
           continue;
@@ -1436,7 +1463,7 @@
         continue;
       case SIGTERM:
         infomsg(infomode, "Received SIGTERM");
-        if (flag_chk(FLAG_R)) release();
+        if (toys.optflags & FLAG_R) release();
         goto ret_with_sockfd;
       default: break;
       }
@@ -1454,9 +1481,10 @@
       waited += time(NULL) - timestmp;
       memset(&result, 0, sizeof(dhcpc_result_t));
       msgType = dhcpc_parsemsg(&result);
-      if (msgType != DHCPNAK && result.ipaddr.s_addr == 0 ) continue;       // no ip for me ignore
-      if (!msgType || !get_option_serverid(state->pdhcp.options, &result)) continue; //no server id ignore
-      if (msgType == DHCPOFFER && server == 0) server = result.serverid.s_addr; // select the server
+      if (msgType != DHCPNAK && !result.ipaddr.s_addr) continue; // no ip for me ignore
+      if (!msgType || !get_option_serverid(state->pdhcp.options, &result))
+        continue; //no server id ignore
+      if (msgType == DHCPOFFER && !server) server = result.serverid.s_addr; // select the server
       if (result.serverid.s_addr != server) continue; // not from the server we requested ignore
       dhcpc_parseoptions(&result, state->pdhcp.options);
       get_option_lease(state->pdhcp.options, &result);
@@ -1477,21 +1505,28 @@
       case STATE_REBINDING:
         if (msgType == DHCPACK) {
           timeout = result.lease_time / 2;
-          run_script(&result, state->status == STATE_REQUESTING ? "bound" : "renew");
+          run_script(&result, state->status == STATE_REQUESTING
+                  ? "bound" : "renew");
           state->status = STATE_BOUND;
-          infomsg(infomode, "Lease of %d.%d.%d.%d obtained, lease time %d from server %d.%d.%d.%d",
-              (result.ipaddr.s_addr >> 24) & 0xff, (result.ipaddr.s_addr >> 16) & 0xff, (result.ipaddr.s_addr >> 8) & 0xff, (result.ipaddr.s_addr) & 0xff,
-              result.lease_time,
-              (result.serverid.s_addr >> 24) & 0xff, (result.serverid.s_addr >> 16) & 0xff, (result.serverid.s_addr >> 8) & 0xff, (result.serverid.s_addr) & 0xff);
-          if (flag_chk(FLAG_q)) {
-            if (flag_chk(FLAG_R)) release();
+          infomsg(infomode, "Lease of %d.%d.%d.%d obtained, "
+                  "lease time %d from server %d.%d.%d.%d",
+                  (result.ipaddr.s_addr >> 24) & 0xff,
+                  (result.ipaddr.s_addr >> 16) & 0xff,
+                  (result.ipaddr.s_addr >> 8) & 0xff,
+                  (result.ipaddr.s_addr) & 0xff, result.lease_time,
+                  (result.serverid.s_addr >> 24) & 0xff,
+                  (result.serverid.s_addr >> 16) & 0xff,
+                  (result.serverid.s_addr >> 8) & 0xff,
+                  (result.serverid.s_addr) & 0xff);
+          if (toys.optflags & FLAG_q) {
+            if (toys.optflags & FLAG_R) release();
             goto ret_with_sockfd;
           }
           toys.optflags &= ~FLAG_n;
-          if (!flag_chk(FLAG_f)) {
-            daemon(0, 0);
+          if (!(toys.optflags & FLAG_f)) {
+            if (daemon(0,0)) perror_exit("daemonize");
             toys.optflags |= FLAG_f;
-            if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
+            if (toys.optflags & FLAG_p) write_pid(TT.pidfile);
           }
           waited = 0;
           continue;
