]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Add helper alias_normalize()
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 13 Dec 2011 12:24:22 +0000 (10:24 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 13 Dec 2011 12:41:18 +0000 (10:41 -0200)
libkmod/libkmod-private.h
libkmod/libkmod-util.c

index 3e219869ffe8aa40d48a94bf29518b8c7ef0cb0b..a139792c95e6e1e7dfd9bb91104f2ee4f689d3c5 100644 (file)
@@ -129,6 +129,7 @@ int read_str_ulong(int fd, unsigned long *value, int base) __must_check __attrib
 char *strchr_replace(char *s, int c, char r);
 bool path_is_absolute(const char *p) __must_check __attribute__((nonnull(1)));
 char *path_make_absolute_cwd(const char *p) __must_check __attribute__((nonnull(1)));
+int alias_normalize(const char *alias, char buf[NAME_MAX], size_t *len) __must_check __attribute__((nonnull(1,2)));
 
 
 #endif
index c5550b9c858f4af21923407ebee5fc64e585b8bf..177b51a5883044f94748a9515900756a3dfd637a 100644 (file)
@@ -118,6 +118,45 @@ char *underscores(struct kmod_ctx *ctx, char *s)
        return s;
 }
 
+inline int alias_normalize(const char *alias, char buf[NAME_MAX], size_t *len)
+{
+       size_t s;
+
+       for (s = 0; s < NAME_MAX - 1; s++) {
+               const char c = alias[s];
+               switch (c) {
+               case '-':
+                       buf[s] = '_';
+                       break;
+               case ']':
+                       return -EINVAL;
+               case '[':
+                       while (alias[s] != ']' &&
+                                       alias[s] != '.' && alias[s] != '\0')
+                               s++;
+
+                       if (alias[s] != ']')
+                               return -EINVAL;
+
+                       s++;
+                       break;
+               case '\0':
+               case '.':
+                       goto finish;
+               default:
+                       buf[s] = c;
+               }
+       }
+
+finish:
+       buf[s] = '\0';
+
+       if (len)
+               *len = s;
+
+       return 0;
+}
+
 bool startswith(const char *s, const char *prefix) {
         size_t sl, pl;