Add helper alias_normalize()

This commit is contained in:
Lucas De Marchi 2011-12-13 10:24:22 -02:00
parent 4308b176e9
commit b148b8606a
2 changed files with 40 additions and 0 deletions

View 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); char *strchr_replace(char *s, int c, char r);
bool path_is_absolute(const char *p) __must_check __attribute__((nonnull(1))); 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))); 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 #endif

View File

@ -118,6 +118,45 @@ char *underscores(struct kmod_ctx *ctx, char *s)
return 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) { bool startswith(const char *s, const char *prefix) {
size_t sl, pl; size_t sl, pl;