diff --git a/libkmod/libkmod-private.h b/libkmod/libkmod-private.h index 3e21986..a139792 100644 --- a/libkmod/libkmod-private.h +++ b/libkmod/libkmod-private.h @@ -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 diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c index c5550b9..177b51a 100644 --- a/libkmod/libkmod-util.c +++ b/libkmod/libkmod-util.c @@ -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;