From f4e8c16291c58b71dfc622c11f3e00c818dcaebb Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Thu, 9 Oct 2014 01:14:16 -0300 Subject: [PATCH] Move remaining functions from libkmod-util to shared --- Makefile.am | 4 +- libkmod/docs/Makefile.am | 1 - libkmod/libkmod-internal.h | 2 - libkmod/libkmod-util.c | 96 ---------------------------------------------- libkmod/libkmod-util.h | 13 ------- shared/util.c | 61 +++++++++++++++++++++++++++++ shared/util.h | 5 +++ testsuite/testsuite.c | 2 - tools/depmod.c | 1 - tools/modinfo.c | 3 +- 10 files changed, 69 insertions(+), 119 deletions(-) delete mode 100644 libkmod/libkmod-util.c delete mode 100644 libkmod/libkmod-util.h diff --git a/Makefile.am b/Makefile.am index 87702ee..46972b3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -51,9 +51,7 @@ libkmod_libkmod_util_la_SOURCES = \ shared/hash.c \ shared/hash.h \ shared/util.c \ - shared/util.h \ - libkmod/libkmod-util.c \ - libkmod/libkmod-util.h + shared/util.h include_HEADERS = libkmod/libkmod.h lib_LTLIBRARIES = libkmod/libkmod.la diff --git a/libkmod/docs/Makefile.am b/libkmod/docs/Makefile.am index cd3bb16..06f65c8 100644 --- a/libkmod/docs/Makefile.am +++ b/libkmod/docs/Makefile.am @@ -21,7 +21,6 @@ HFILE_GLOB = $(top_srcdir)/libkmod/libkmod.h CFILE_GLOB = $(top_srcdir)/libkmod/libkmod.c $(top_srcdir)/libkmod/libkmod-module.c $(top_srcdir)/libkmod/libkmod-list.c IGNORE_HFILES = libkmod-internal.h \ - libkmod-util.h \ libkmod-index.h content_files = version.xml diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h index 83a5bc2..417f232 100644 --- a/libkmod/libkmod-internal.h +++ b/libkmod/libkmod-internal.h @@ -189,5 +189,3 @@ struct kmod_signature_info { const char *algo, *hash_algo, *id_type; }; bool kmod_module_signature_info(const struct kmod_file *file, struct kmod_signature_info *sig_info) _must_check_ __attribute__((nonnull(1, 2))); -/* util functions */ -#include "libkmod-util.h" diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c deleted file mode 100644 index ec6ee40..0000000 --- a/libkmod/libkmod-util.c +++ /dev/null @@ -1,96 +0,0 @@ -/* - * libkmod - interface to kernel module operations - * - * Copyright (C) 2011-2013 ProFUSION embedded systems - * Copyright (C) 2012 Lucas De Marchi - * Copyright (C) 2013 Intel Corporation. All rights reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "libkmod.h" -#include "libkmod-internal.h" - -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 - { } -}; - -inline char *modname_normalize(const char *modname, char buf[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[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; -} diff --git a/libkmod/libkmod-util.h b/libkmod/libkmod-util.h deleted file mode 100644 index 46db70d..0000000 --- a/libkmod/libkmod-util.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include -#include -#include - -#include - -#define KMOD_EXTENSION_UNCOMPRESSED ".ko" - -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))); 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 */ /* ************************************************************************ */ diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index d8acf68..c784374 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -34,8 +34,6 @@ #include -#include - #include "testsuite.h" static const char *ANSI_HIGHLIGHT_GREEN_ON = "\x1B[1;32m"; diff --git a/tools/depmod.c b/tools/depmod.c index e812ae6..fec458c 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -37,7 +37,6 @@ #include #include -#include #include "kmod.h" diff --git a/tools/modinfo.c b/tools/modinfo.c index 72ac03c..74ec183 100644 --- a/tools/modinfo.c +++ b/tools/modinfo.c @@ -27,8 +27,9 @@ #include #include +#include + #include -#include #include "kmod.h" -- 2.7.4