[netcf-devel] [PATCH 3/7] Add strlower, update dutil_linux.c:aug_match_mac

Jonas Eriksson jonas.j.eriksson at ericsson.com
Thu Jul 23 12:37:04 UTC 2009


dutil.{c,h}:
  - Added strllower

dutil_linux.{c,h}:
  - Made use of strllower and aug_fmt_match in aug_match_mac
  - Fixed a typo in the comment above the function head and prototype

drv_initscruots.c:
  - Updated the use of aug_match_mac
  - Broke the long function head of drv_list_interfaces
---
 src/drv_initscripts.c |    5 +++--
 src/dutil.c           |    7 +++++++
 src/dutil.h           |    3 +++
 src/dutil_linux.c     |   26 +++++++++++---------------
 src/dutil_linux.h     |    2 +-
 5 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
index 0c69071..550df65 100644
--- a/src/drv_initscripts.c
+++ b/src/drv_initscripts.c
@@ -344,7 +344,8 @@ static int list_interface_ids(struct netcf *ncf,
     return -1;
 }
 
-int drv_list_interfaces(struct netcf *ncf, int maxnames, char **names, unsigned int flags) {
+int drv_list_interfaces(struct netcf *ncf, int maxnames, char **names,
+        unsigned int flags) {
     return list_interface_ids(ncf, maxnames, names, flags, "DEVICE");
 }
 
@@ -677,7 +678,7 @@ int drv_lookup_by_mac_string(struct netcf *ncf, const char *mac,
     ERR_BAIL(ncf);
 
     nmatches = aug_match_mac(ncf, mac, &matches);
-    ERR_THROW(nmatches < 0, ncf, EOTHER, "looking up %s failed", mac);
+    ERR_BAIL(ncf);
     if (nmatches == 0) {
         result = 0;
         goto done;
diff --git a/src/dutil.c b/src/dutil.c
index 28832b3..2066a45 100644
--- a/src/dutil.c
+++ b/src/dutil.c
@@ -62,6 +62,13 @@ int xasprintf(char **strp, const char *format, ...) {
   return result;
 }
 
+/* Convert string of max length buflen to uppercase in place */
+void strllower(char *s, unsigned int buflen) {
+    if (s != NULL)
+        for (int i = 0; s[i] != '\0' && i < buflen; i++)
+            s[i] = tolower(s[i]);
+}
+
 /* Get the Augeas instance; if we already initialized it, just return
  * it. Otherwise, create a new one and return that.
  */
diff --git a/src/dutil.h b/src/dutil.h
index 6dc05e2..eee5e4e 100644
--- a/src/dutil.h
+++ b/src/dutil.h
@@ -45,6 +45,9 @@ struct augeas_pv {
 /* Like asprintf, but set *STRP to NULL on error */
 int xasprintf(char **strp, const char *format, ...);
 
+/* Convert string of max length buflen to uppercase in place */
+void strllower(char *s, unsigned int buflen); 
+
 /* Get or create the augeas instance from NCF */
 struct augeas *get_augeas(struct netcf *ncf);
 
diff --git a/src/dutil_linux.c b/src/dutil_linux.c
index 4e99ef6..9727367 100644
--- a/src/dutil_linux.c
+++ b/src/dutil_linux.c
@@ -39,23 +39,20 @@
 #include "dutil.h"
 #include "dutil_linux.h"
 
-/* Returns a list of all interfaces with MAC address INTF */
+/* Returns a list of all interfaces with MAC address MAC */
 int aug_match_mac(struct netcf *ncf, const char *mac, char ***matches) {
-    int r, nmatches;
-    char *path;
-    struct augeas *aug = get_augeas(ncf);
+    int nmatches;
+    char *lcmac = NULL;
 
-    r = xasprintf(&path,
-            "/files/sys/class/net/*[address/content = '%s']", mac);
-    ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+    /* Make sure that the mac address is in lower case */
+    lcmac = strndup(mac, 17);
+    ERR_COND_BAIL(lcmac == NULL, ncf, ENOMEM);
+    strllower(lcmac, 18);
 
-    r = aug_match(aug, path, matches);
-    /* Messages for a aug_match-fail are handled outside this function */
-    if (r < 0)
-        goto error;
+    nmatches = aug_fmt_match(ncf, matches,
+            "/files/sys/class/net/*[address/content = '%s']", lcmac);
+    ERR_BAIL(ncf);
 
-    nmatches = r;
-    r = -1;
     for (int i = 0; i < nmatches; i++) {
         char *n = strrchr((*matches)[i], '/');
         ERR_THROW(n == NULL, ncf, EINTERNAL, "missing / in sysfs path");
@@ -72,8 +69,7 @@ int aug_match_mac(struct netcf *ncf, const char *mac, char ***matches) {
     return nmatches;
 
  error:
-    FREE(path);
-    return r;
+    return -1;
 }
 
 /* Get the MAC address of the interface INTF */
diff --git a/src/dutil_linux.h b/src/dutil_linux.h
index 0351b8b..085c2f9 100644
--- a/src/dutil_linux.h
+++ b/src/dutil_linux.h
@@ -23,7 +23,7 @@
 #ifndef DUTIL_LINUX_H_
 #define DUTIL_LINUX_H_
 
-/* Returns a list of all interfaces with MAC address INTF */
+/* Returns a list of all interfaces with MAC address MAC */
 int aug_match_mac(struct netcf *ncf, const char *mac, char ***matches);
 
 /* Get the MAC address of the interface INTF */
-- 
1.6.2



More information about the netcf-devel mailing list