From: Lucas De Marchi Date: Wed, 28 Dec 2011 15:33:26 +0000 (-0200) Subject: Move libkmod-util.c to convenience util lib X-Git-Tag: v3~41 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0c010fae102286cfe12a5a57830f78509bd69c7e;p=thirdparty%2Fkmod.git Move libkmod-util.c to convenience util lib Share more code between tools and libkmod. underscores() in kmod-depmod can not use the same function as in the lib, so rename it. --- diff --git a/Makefile.am b/Makefile.am index a68145c8..7ea6e58a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -44,7 +44,9 @@ noinst_LTLIBRARIES = libkmod/libkmod-util.la libkmod_libkmod_util_la_SOURCES = libkmod/libkmod-hash.c \ libkmod/libkmod-hash.h \ libkmod/libkmod-array.c \ - libkmod/libkmod-array.h + libkmod/libkmod-array.h \ + libkmod/libkmod-util.c \ + libkmod/libkmod-util.h include_HEADERS = libkmod/libkmod.h lib_LTLIBRARIES = libkmod/libkmod.la @@ -56,8 +58,6 @@ libkmod_libkmod_la_SOURCES =\ libkmod/libkmod.c \ libkmod/libkmod-list.c \ libkmod/libkmod-config.c \ - libkmod/libkmod-util.c \ - libkmod/libkmod-util.h \ libkmod/libkmod-index.c \ libkmod/libkmod-index.h \ libkmod/libkmod-module.c \ diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c index 50d4d915..95d5fba1 100644 --- a/libkmod/libkmod-util.c +++ b/libkmod/libkmod-util.c @@ -191,24 +191,6 @@ char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len) return modname_normalize(modname, buf, len); } -bool startswith(const char *s, const char *prefix) { - size_t sl, pl; - - assert(s); - assert(prefix); - - sl = strlen(s); - pl = strlen(prefix); - - if (pl == 0) - return true; - - if (sl < pl) - return false; - - return memcmp(s, prefix, pl) == 0; -} - inline void *memdup(const void *p, size_t n) { void *r = malloc(n); diff --git a/libkmod/libkmod-util.h b/libkmod/libkmod-util.h index f09c3273..a4267151 100644 --- a/libkmod/libkmod-util.h +++ b/libkmod/libkmod-util.h @@ -1,10 +1,17 @@ #ifndef _LIBKMOD_UTIL_H_ #define _LIBKMOD_UTIL_H_ +#include "macro.h" + +#include +#include +#include + + char *getline_wrapped(FILE *fp, unsigned int *linenum) __attribute__((nonnull(1))); char *underscores(struct kmod_ctx *ctx, char *s) __attribute__((nonnull(1, 2))); #define streq(a, b) (strcmp((a), (b)) == 0) -bool startswith(const char *s, const char *prefix) __attribute__((nonnull(1, 2))); +#define strstartswith(a, b) (strncmp(a, b, strlen(b)) == 0) void *memdup(const void *p, size_t n) __attribute__((nonnull(1))); ssize_t read_str_safe(int fd, char *buf, size_t buflen) __must_check __attribute__((nonnull(2))); diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 4f49ff54..10905078 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -424,7 +424,7 @@ fail: int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name, struct kmod_list **list) { - if (!startswith(name, "symbol:")) + if (!strstartswith(name, "symbol:")) return 0; return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_SYMBOL, name, diff --git a/tools/kmod-depmod.c b/tools/kmod-depmod.c index b8722299..efc7b9d6 100644 --- a/tools/kmod-depmod.c +++ b/tools/kmod-depmod.c @@ -16,6 +16,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include "libkmod.h" +#include "libkmod-array.h" +#include "libkmod-hash.h" +#include "libkmod-util.h" #include #include @@ -31,12 +35,6 @@ #include #include #include -#include "libkmod.h" -#include "libkmod-hash.h" -#include "libkmod-array.h" - -#define streq(a, b) (strcmp(a, b) == 0) -#define strstartswith(a, b) (strncmp(a, b, strlen(b)) == 0) #define DEFAULT_VERBOSE LOG_WARNING static int verbose = DEFAULT_VERBOSE; @@ -585,8 +583,8 @@ static void index_write(const struct index_node *node, FILE *out) /* END: code from module-init-tools/index.c just modified to compile here. */ -/* utils (similar to libkmod-utils.c) *********************************/ -static const char *underscores(const char *input, char *output, size_t outputlen) +/* utils (variants of libkmod-utils.c) *********************************/ +static const char *underscores2(const char *input, char *output, size_t outputlen) { size_t i; @@ -620,40 +618,6 @@ static const char *underscores(const char *input, char *output, size_t outputlen return output; } -static inline char *modname_normalize(const char *modname, char buf[NAME_MAX], - size_t *len) -{ - size_t s; - - for (s = 0; s < NAME_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; -} - -static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len) -{ - char *modname; - - modname = basename(path); - if (modname == NULL || modname[0] == '\0') - return NULL; - - return modname_normalize(modname, buf, len); -} - /* configuration parsing **********************************************/ struct cfg_override { struct cfg_override *next; @@ -774,53 +738,6 @@ static int cfg_kernel_matches(const struct cfg *cfg, const char *pattern) return status == 0; } -/* same as libkmod-util.c */ -static char *getline_wrapped(FILE *fp, unsigned int *linenum) -{ - int size = 256; - int i = 0; - char *buf = malloc(size); - - for(;;) { - int ch = getc_unlocked(fp); - - switch(ch) { - case EOF: - if (i == 0) { - free(buf); - return NULL; - } - /* else fall through */ - - case '\n': - if (linenum) - (*linenum)++; - if (i == size) - buf = realloc(buf, size + 1); - buf[i] = '\0'; - return buf; - - case '\\': - ch = getc_unlocked(fp); - - if (ch == '\n') { - if (linenum) - (*linenum)++; - continue; - } - /* else fall through */ - - default: - buf[i++] = ch; - - if (i == size) { - size *= 2; - buf = realloc(buf, size); - } - } - } -} - static int cfg_file_parse(struct cfg *cfg, const char *filename) { char *line; @@ -2077,7 +1994,7 @@ static int output_aliases_bin(struct depmod *depmod, FILE *out) if (!streq(key, "alias")) continue; - alias = underscores(value, buf, sizeof(buf)); + alias = underscores2(value, buf, sizeof(buf)); if (alias == NULL) continue;