]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Use strlcpy if available, if not define our own.
authorRoy Marples <roy@marples.name>
Thu, 5 Apr 2007 17:31:05 +0000 (17:31 +0000)
committerRoy Marples <roy@marples.name>
Thu, 5 Apr 2007 17:31:05 +0000 (17:31 +0000)
common.c
common.h
configure.c
dhcp.c
dhcpcd.c
interface.c
socket.c

index 8246b8e118e5b764ad7ef53ef2d39feca315dde3..ae6267899e644b6bdb0660897dd0e49eddb8f90b 100644 (file)
--- a/common.c
+++ b/common.c
 #include "common.h"
 #include "logger.h"
 
-/* A way of safely handling strncpy */
-char *safe_strncpy (char *dst, const char *src, size_t size)
+/* strlcpy is nice, shame glibc does not define it */
+#ifdef __GLIBC__
+size_t strlcpy (char *dst, const char *src, size_t size)
 {
-  if (! size)
-    return dst;
+  const char *s = src;
+  size_t n = size;
 
-  dst[--size] = '\0';
-  return strncpy (dst, src, size);
+  if (n && --n)
+    do
+      {
+       if (! (*dst++ = *src++))
+         break;
+      }
+    while (--n);
+
+  if (! n)
+    {
+      if (size)
+       *dst = '\0';
+      while (*src++)
+       ;
+    }
+
+  return (src - s - 1);
 }
+#endif
 
 /* This requires us to link to rt on glibc, so we use sysinfo instead */
 #ifdef __linux__
index e788e0bb6fc3c18198b4435da6d135a8a6d764fd..9b1eefef376489f8f3f0d363f29d614df944c57f 100644 (file)
--- a/common.h
+++ b/common.h
 #ifndef COMMON_H
 #define COMMON_H
 
-char *safe_strncpy (char *dst, const char *src, size_t size);
+#ifdef __GLIBC__
+size_t strlcpy (char *dst, const char *src, size_t size);
+#endif
+
 long uptime (void);
 void *xmalloc (size_t size);
 
index a50b6880ee1beeb1c879dde4d927c1e799f7af4f..2d013a30069eab542f43bf9243950a48edaa0089 100644 (file)
@@ -689,7 +689,7 @@ int configure (const options_t *options, interface_t *iface,
       || strcmp (curhostname, "localhost") == 0)
     {
       if (dhcp->hostname)
-       safe_strncpy (newhostname, dhcp->hostname, sizeof (newhostname)); 
+       strlcpy (newhostname, dhcp->hostname, sizeof (newhostname)); 
 
       if (*newhostname)
        {
diff --git a/dhcp.c b/dhcp.c
index 31e385f2b67caa097922d1fb47bcf7210b449efb..2fa3f52617bc10d732b7e66d5017287657fec4cd 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -513,7 +513,7 @@ int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message)
   memset (first_route, 0, sizeof (route_t));
 
   dhcp->address.s_addr = message->yiaddr;
-  safe_strncpy (dhcp->servername, message->servername,
+  strlcpy (dhcp->servername, message->servername,
                sizeof (dhcp->servername));
 
 #define LEN_ERR \
index ff74da0338b86cec2917562199aa4fd01f30a2aa..31e9dbade9edb8b5d3ab2496a03de85d8567a3a5 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -206,7 +206,7 @@ int main(int argc, char **argv)
            exit (EXIT_FAILURE);
          }
        else
-         safe_strncpy (options.hostname, optarg, sizeof (options.hostname));
+         strlcpy (options.hostname, optarg, sizeof (options.hostname));
        break;
       case 'i':
        if (strlen (optarg) > CLASS_ID_MAX_LEN)
@@ -216,7 +216,7 @@ int main(int argc, char **argv)
            exit (EXIT_FAILURE);
          }
        else
-         sprintf (options.classid, "%s", optarg);
+         strlcpy (options.classid, optarg, sizeof (options.classid));
        break;
       case 'k':
        options.signal = SIGHUP;
@@ -298,7 +298,7 @@ int main(int argc, char **argv)
            exit (EXIT_FAILURE);
          }
        else
-         sprintf(options.clientid, "%s", optarg);
+         strlcpy (options.clientid, optarg, sizeof (options.clientid));
        break;
       case 'M':
        options.domtu = false;
@@ -334,7 +334,7 @@ int main(int argc, char **argv)
                  argv[optind], IF_NAMESIZE);
          exit (EXIT_FAILURE);
        }
-      safe_strncpy (options.interface, argv[optind],
+      strlcpy (options.interface, argv[optind],
                    sizeof (options.interface));
     }
   else
index 3f4e248137647547334b26676a43589c52e72954..172acd66494567527260848b7649e7d84a29e401 100644 (file)
@@ -190,7 +190,7 @@ interface_t *read_interface (const char *ifname, int metric)
 #endif
 
   memset (&ifr, 0, sizeof (struct ifreq));
-  safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+  strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
   if ((s = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
     {
       logger (LOG_ERR, "socket: %s", strerror (errno));
@@ -200,7 +200,7 @@ interface_t *read_interface (const char *ifname, int metric)
 #ifdef __linux__
   /* Do something with the metric parameter to satisfy the compiler warning */
   metric = 0;
-  safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+  strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
   if (ioctl (s, SIOCGIFHWADDR, &ifr) <0)
     {
       logger (LOG_ERR, "ioctl SIOCGIFHWADDR: %s", strerror (errno));
@@ -227,7 +227,7 @@ interface_t *read_interface (const char *ifname, int metric)
   family = ifr.ifr_hwaddr.sa_family;
 #else
   ifr.ifr_metric = metric;
-  safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+  strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
   if (ioctl (s, SIOCSIFMETRIC, &ifr) < 0)
     {
       logger (LOG_ERR, "ioctl SIOCSIFMETRIC: %s", strerror (errno));
@@ -236,7 +236,7 @@ interface_t *read_interface (const char *ifname, int metric)
     }
 #endif
 
-  safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+  strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
   if (ioctl(s, SIOCGIFMTU, &ifr) < 0)
     {
       logger (LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror (errno));
@@ -247,7 +247,7 @@ interface_t *read_interface (const char *ifname, int metric)
     {
       logger (LOG_DEBUG, "MTU of %d is too low, setting to %d", ifr.ifr_mtu, MTU_MIN);
       ifr.ifr_mtu = MTU_MIN;
-      safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+      strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
       if (ioctl(s, SIOCSIFMTU, &ifr) < 0)
        {
          logger (LOG_ERR, "ioctl SIOCSIFMTU,: %s", strerror (errno));
@@ -257,7 +257,7 @@ interface_t *read_interface (const char *ifname, int metric)
     }
   mtu = ifr.ifr_mtu;
 
-  safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+  strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
   if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
     {
       logger (LOG_ERR, "ioctl SIOCGIFFLAGS: %s", strerror (errno));
@@ -266,7 +266,7 @@ interface_t *read_interface (const char *ifname, int metric)
     }
 
   ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
-  safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+  strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
   if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
     {
       logger (LOG_ERR, "ioctl SIOCSIFFLAGS: %s", strerror (errno));
@@ -278,7 +278,7 @@ interface_t *read_interface (const char *ifname, int metric)
 
   iface = xmalloc (sizeof (interface_t));
   memset (iface, 0, sizeof (interface_t));
-  safe_strncpy (iface->name, ifname, IF_NAMESIZE);
+  strlcpy (iface->name, ifname, IF_NAMESIZE);
   snprintf (iface->infofile, PATH_MAX, INFOFILE, ifname);
   memcpy (&iface->hwaddr, hwaddr, hwlen);
   iface->hwlen = hwlen;
@@ -309,7 +309,7 @@ int get_mtu (const char *ifname)
     }
 
   memset (&ifr, 0, sizeof (struct ifreq));
-  safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+  strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
   r = ioctl (s, SIOCGIFMTU, &ifr);
   close (s);
 
@@ -336,7 +336,7 @@ int set_mtu (const char *ifname, short int mtu)
 
   memset (&ifr, 0, sizeof (struct ifreq));
   logger (LOG_DEBUG, "setting MTU to %d", mtu);
-  safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
+  strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
   ifr.ifr_mtu = mtu;
   r = ioctl (s, SIOCSIFMTU, &ifr);
   close (s);
@@ -365,7 +365,7 @@ static int do_address (const char *ifname, struct in_addr address,
     }
 
   memset (&ifa, 0, sizeof (ifa));
-  safe_strncpy (ifa.ifra_name, ifname, sizeof (ifa.ifra_name));
+  strlcpy (ifa.ifra_name, ifname, sizeof (ifa.ifra_name));
 
 #define ADDADDR(_var, _addr) \
     { \
index f2d8a25c486144714310f96d70976e45aedb8844..cfd0041749a79f66ee06b6be8290eee33f3abf38 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -262,7 +262,7 @@ int open_socket (interface_t *iface, bool arp)
     }
 
   memset (&ifr, 0, sizeof (struct ifreq));
-  safe_strncpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name));
+  strlcpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name));
   if (ioctl (fd, BIOCSETIF, &ifr) < 0)
     {
       logger (LOG_ERR, "cannot attach interface `%s' to bpf device `%s': %s",