mirror of
https://github.com/AuxXxilium/kmod.git
synced 2024-12-26 21:35:26 +07:00
Add helper alias_normalize()
This commit is contained in:
parent
4308b176e9
commit
b148b8606a
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user