Update copyrights
[dhcpcd-ui] / src / libdhcpcd / dhcpcd.c
index 4b9b61e02b7f13c9936f8095d27838deb1e58879..c8bdd14fbfd8cb48cd54b610fbc3dc06c77a55ab 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * libdhcpcd
- * Copyright 2009-2014 Roy Marples <roy@marples.name>
+ * Copyright 2009-2015 Roy Marples <roy@marples.name>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -37,7 +37,6 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <libintl.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -261,12 +260,12 @@ dhcpcd_encode_string_escape(char *dst, size_t len, const char *src, size_t slen)
 {
        const char *end;
        size_t bytes;
-       int c;
+       unsigned char c;
 
        end = src + slen;
        bytes = 0;
        while (src < end) {
-               c = *src++;
+               c = (unsigned char)*src++;
                if ((c == '\\' || !isascii(c) || !isprint(c))) {
                        if (c == '\\') {
                                if (dst) {
@@ -286,9 +285,9 @@ dhcpcd_encode_string_escape(char *dst, size_t len, const char *src, size_t slen)
                                        return -1;
                                }
                                *dst++ = '\\';
-                               *dst++ = (((unsigned char)c >> 6) & 03) + '0';
-                               *dst++ = (((unsigned char)c >> 3) & 07) + '0';
-                               *dst++ = ( (unsigned char)c       & 07) + '0';
+                               *dst++ = (char)(((c >> 6) & 03) + '0');
+                               *dst++ = (char)(((c >> 3) & 07) + '0');
+                               *dst++ = (char)(( c       & 07) + '0');
                                len -= 4;
                        }
                        bytes += 4;
@@ -511,6 +510,80 @@ dhcpcd_interfaces(DHCPCD_CONNECTION *con)
        return con->interfaces;
 }
 
+char **
+dhcpcd_interface_names(DHCPCD_CONNECTION *con, size_t *nnames)
+{
+       char **names;
+       size_t n;
+       DHCPCD_IF *i;
+
+       assert(con);
+       if (con->interfaces == NULL)
+               return NULL;
+
+       n = 0;
+       for (i = con->interfaces; i; i = i->next) {
+               if (strcmp(i->type, "link") == 0)
+                       n++;
+       }
+       names = malloc(sizeof(char *) * (n + 1));
+       if (names == NULL)
+               return NULL;
+       n = 0;
+       for (i = con->interfaces; i; i = i->next) {
+               if (strcmp(i->type, "link") == 0) {
+                       names[n] = strdup(i->ifname);
+                       if (names[n] == NULL) {
+                               dhcpcd_freev(names);
+                               return NULL;
+                       }
+                       n++;
+               }
+       }
+       names[n] = NULL;
+       if (nnames)
+               *nnames = n;
+
+       return names;
+}
+
+void
+dhcpcd_freev(char **argv)
+{
+       char **v;
+
+       if (argv) {
+               for (v = argv; *v; v++)
+                       free(*v);
+               free(argv);
+       }
+}
+
+static int
+dhcpcd_cmpstring(const void *p1, const void *p2)
+{
+       const char *s1, *s2;
+       int cmp;
+
+       s1 = *(char * const *)p1;
+       s2 = *(char * const *)p2;
+       if ((cmp = strcasecmp(s1, s2)) == 0)
+               cmp = strcmp(s1, s2);
+       return cmp;
+}
+
+char **
+dhcpcd_interface_names_sorted(DHCPCD_CONNECTION *con)
+{
+       char **names;
+       size_t nnames;
+
+       names = dhcpcd_interface_names(con, &nnames);
+       if (names)
+               qsort(names, nnames, sizeof(char *), dhcpcd_cmpstring);
+       return names;
+}
+
 DHCPCD_IF *
 dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
 {