From f4e8c16291c58b71dfc622c11f3e00c818dcaebb Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Thu, 9 Oct 2014 01:14:16 -0300 Subject: Move remaining functions from libkmod-util to shared --- shared/util.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ shared/util.h | 5 +++++ 2 files changed, 66 insertions(+) (limited to 'shared') diff --git a/shared/util.c b/shared/util.c index f6ce61d..3902823 100644 --- a/shared/util.c +++ b/shared/util.c @@ -35,6 +35,20 @@ #define USEC_PER_SEC 1000000ULL #define NSEC_PER_USEC 1000ULL +static const struct kmod_ext { + const char *ext; + size_t len; +} kmod_exts[] = { + {KMOD_EXTENSION_UNCOMPRESSED, sizeof(KMOD_EXTENSION_UNCOMPRESSED) - 1}, +#ifdef ENABLE_ZLIB + {".ko.gz", sizeof(".ko.gz") - 1}, +#endif +#ifdef ENABLE_XZ + {".ko.xz", sizeof(".ko.xz") - 1}, +#endif + { } +}; + /* string handling functions and memory allocations */ /* ************************************************************************ */ @@ -100,6 +114,53 @@ finish: return 0; } +char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *len) +{ + size_t s; + + for (s = 0; s < PATH_MAX - 1; s++) { + const char c = modname[s]; + if (c == '-') + buf[s] = '_'; + else if (c == '\0' || c == '.') + break; + else + buf[s] = c; + } + + buf[s] = '\0'; + + if (len) + *len = s; + + return buf; +} + +char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len) +{ + char *modname; + + modname = basename(path); + if (modname == NULL || modname[0] == '\0') + return NULL; + + return modname_normalize(modname, buf, len); +} + +bool path_ends_with_kmod_ext(const char *path, size_t len) +{ + const struct kmod_ext *eitr; + + for (eitr = kmod_exts; eitr->ext != NULL; eitr++) { + if (len <= eitr->len) + continue; + if (streq(path + len - eitr->len, eitr->ext)) + return true; + } + + return false; +} + /* read-like and fread-like functions */ /* ************************************************************************ */ ssize_t read_str_safe(int fd, char *buf, size_t buflen) diff --git a/shared/util.h b/shared/util.h index 53a2d29..ef3881a 100644 --- a/shared/util.h +++ b/shared/util.h @@ -18,7 +18,12 @@ void *memdup(const void *p, size_t n) __attribute__((nonnull(1))); /* module-related functions */ /* ************************************************************************ */ +#define KMOD_EXTENSION_UNCOMPRESSED ".ko" + int alias_normalize(const char *alias, char buf[static PATH_MAX], size_t *len) _must_check_ __attribute__((nonnull(1,2))); +char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(1, 2))); +char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(2))); +bool path_ends_with_kmod_ext(const char *path, size_t len) __attribute__((nonnull(1))); /* read-like and fread-like functions */ /* ************************************************************************ */ -- cgit v1.2.3