]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Add underscores() helper to replace - with _
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 30 Nov 2011 04:14:33 +0000 (02:14 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 30 Nov 2011 04:14:33 +0000 (02:14 -0200)
libkmod/libkmod-private.h
libkmod/libkmod-util.c

index ae9a95a3ad7af88ab7865ab7848047c46d6754e8..256f6ec6bd502f3c29a633d6553a3d25dde78741 100644 (file)
@@ -63,5 +63,6 @@ int kmod_parse_config(struct kmod_ctx *ctx, struct kmod_config *config);
 void kmod_free_config(struct kmod_ctx *ctx, struct kmod_config *config);
 
 char *getline_wrapped(FILE *fp, unsigned int *linenum);
+char *underscores(struct kmod_ctx *ctx, char *s);
 
 #endif
index a1f5fa1088d7a6e146a7bce753e829f3ed606a2a..1e58b9951a1314fe0b71687ba171bf0b9506be55 100644 (file)
@@ -83,3 +83,35 @@ char *getline_wrapped(FILE *fp, unsigned int *linenum)
                }
        }
 }
+
+/*
+ * Replace dashes with underscores.
+ * Dashes inside character range patterns (e.g. [0-9]) are left unchanged.
+ */
+char *underscores(struct kmod_ctx *ctx, char *s)
+{
+       unsigned int i;
+
+       if (!s)
+               return NULL;
+
+       for (i = 0; s[i]; i++) {
+               switch (s[i]) {
+               case '-':
+                       s[i] = '_';
+                       break;
+
+               case ']':
+                       INFO(ctx, "Unmatched bracket in %s\n", s);
+                       break;
+
+               case '[':
+                       i += strcspn(&s[i], "]");
+                       if (!s[i])
+                               INFO(ctx, "Unmatched bracket in %s\n", s);
+                       break;
+               }
+       }
+       return s;
+}
+