]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
kmod-depmod: add utility functions to be used by binary dumps.
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>
Sat, 24 Dec 2011 01:16:20 +0000 (23:16 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 27 Dec 2011 14:09:16 +0000 (12:09 -0200)
Binary dumps will use functions to convert alias to underscores and
paths to module names.

tools/kmod-depmod.c

index 934dc60dbfcef493e998bbe72caa70cb7119acea..5b6f947335a6fb2713936a1759c4bfcf98a18437 100644 (file)
@@ -902,6 +902,75 @@ static void array_sort(struct array *array, int (*cmp)(const void *a, const void
        qsort(array->array, array->count, sizeof(void *), cmp);
 }
 
+/* utils (similar to libkmod-utils.c) *********************************/
+static const char *underscores(const char *input, char *output, size_t outputlen)
+{
+       size_t i;
+
+       for (i = 0; input[i] != '\0' && i < outputlen - 1; i++) {
+               switch (input[i]) {
+               case '-':
+                       output[i] = '_';
+                       break;
+
+               case ']':
+                       WRN("Unmatched bracket in %s\n", input);
+                       return NULL;
+
+               case '[': {
+                       size_t off = strcspn(input + i, "]");
+                       if (input[i + off] == '\0') {
+                               WRN("Unmatched bracket in %s\n", input);
+                               return NULL;
+                       }
+                       memcpy(output + i, input + i, off + 1);
+                       i += off;
+                       break;
+               }
+
+               default:
+                       output[i] = input[i];
+               }
+       }
+       output[i] = '\0';
+
+       return output;
+}
+
+static inline char *modname_normalize(const char *modname, char buf[NAME_MAX],
+                                               size_t *len)
+{
+       size_t s;
+
+       for (s = 0; s < NAME_MAX - 1; s++) {
+               const char c = modname[s];
+               if (c == '-')
+                       buf[s] = '_';
+               else if (c == '\0' || c == '.')
+                       break;
+               else
+                       buf[s] = c;
+       }
+
+       buf[s] = '\0';
+
+       if (len)
+               *len = s;
+
+       return buf;
+}
+
+static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len)
+{
+       char *modname;
+
+       modname = basename(path);
+       if (modname == NULL || modname[0] == '\0')
+               return NULL;
+
+       return modname_normalize(modname, buf, len);
+}
+
 /* configuration parsing **********************************************/
 struct cfg_override {
        struct cfg_override *next;