Move alias_normalize() to shared

This commit is contained in:
Lucas De Marchi 2014-10-09 00:43:01 -03:00
parent 66bf1a7ff9
commit 2b0104fe3c
5 changed files with 44 additions and 43 deletions

View File

@ -47,45 +47,6 @@ static const struct kmod_ext {
{ }
};
inline int alias_normalize(const char *alias, char buf[PATH_MAX], size_t *len)
{
size_t s;
for (s = 0; s < PATH_MAX - 1; s++) {
const char c = alias[s];
switch (c) {
case '-':
buf[s] = '_';
break;
case ']':
return -EINVAL;
case '[':
while (alias[s] != ']' && alias[s] != '\0') {
buf[s] = alias[s];
s++;
}
if (alias[s] != ']')
return -EINVAL;
buf[s] = alias[s];
break;
case '\0':
goto finish;
default:
buf[s] = c;
}
}
finish:
buf[s] = '\0';
if (len)
*len = s;
return 0;
}
inline char *modname_normalize(const char *modname, char buf[PATH_MAX],
size_t *len)
{

View File

@ -8,7 +8,6 @@
#define KMOD_EXTENSION_UNCOMPRESSED ".ko"
int alias_normalize(const char *alias, char buf[PATH_MAX], size_t *len) _must_check_ __attribute__((nonnull(1,2)));
char *modname_normalize(const char *modname, char buf[PATH_MAX], size_t *len) __attribute__((nonnull(1, 2)));
char *path_to_modname(const char *path, char buf[PATH_MAX], size_t *len) __attribute__((nonnull(2)));
bool path_ends_with_kmod_ext(const char *path, size_t len) __attribute__((nonnull(1)));

View File

@ -60,6 +60,46 @@ char *strchr_replace(char *s, int c, char r)
return s;
}
/* module-related functions */
/* ************************************************************************ */
int alias_normalize(const char *alias, char buf[static PATH_MAX], size_t *len)
{
size_t i;
for (i = 0; i < PATH_MAX - 1; i++) {
const char c = alias[i];
switch (c) {
case '-':
buf[i] = '_';
break;
case ']':
return -EINVAL;
case '[':
while (alias[i] != ']' && alias[i] != '\0') {
buf[i] = alias[i];
i++;
}
if (alias[i] != ']')
return -EINVAL;
buf[i] = alias[i];
break;
case '\0':
goto finish;
default:
buf[i] = c;
}
}
finish:
buf[i] = '\0';
if (len)
*len = i;
return 0;
}
/* read-like and fread-like functions */
/* ************************************************************************ */
ssize_t read_str_safe(int fd, char *buf, size_t buflen)

View File

@ -16,6 +16,10 @@
char *strchr_replace(char *s, int c, char r);
void *memdup(const void *p, size_t n) __attribute__((nonnull(1)));
/* module-related functions */
/* ************************************************************************ */
int alias_normalize(const char *alias, char buf[static PATH_MAX], size_t *len) _must_check_ __attribute__((nonnull(1,2)));
/* read-like and fread-like functions */
/* ************************************************************************ */
ssize_t read_str_safe(int fd, char *buf, size_t buflen) _must_check_ __attribute__((nonnull(2)));

View File

@ -23,9 +23,6 @@
#include <shared/util.h>
#include <libkmod.h>
#include <libkmod-util.h>
#include "testsuite.h"
static int alias_1(const struct test *t)