diff options
author | Lucas De Marchi <lucas.demarchi@intel.com> | 2014-10-09 01:14:16 -0300 |
---|---|---|
committer | Lucas De Marchi <lucas.demarchi@intel.com> | 2014-10-09 01:26:39 -0300 |
commit | f4e8c16291c58b71dfc622c11f3e00c818dcaebb (patch) | |
tree | c429fbbe7d95dd47da5a0633e3b9f450524b10c0 /shared | |
parent | 3753ae16f5026767afcf05f6c3bafda9a7eed0f0 (diff) | |
download | kmod-f4e8c16291c58b71dfc622c11f3e00c818dcaebb.tar.gz kmod-f4e8c16291c58b71dfc622c11f3e00c818dcaebb.tar.bz2 kmod-f4e8c16291c58b71dfc622c11f3e00c818dcaebb.zip |
Move remaining functions from libkmod-util to shared
Diffstat (limited to 'shared')
-rw-r--r-- | shared/util.c | 61 | ||||
-rw-r--r-- | shared/util.h | 5 |
2 files changed, 66 insertions, 0 deletions
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 */ /* ************************************************************************ */ |