From: Lucas De Marchi Date: Tue, 27 Dec 2011 16:38:26 +0000 (-0200) Subject: Move util functions to libkmod-util.c X-Git-Tag: v3~46 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a4848e249f4bf2845e17cbb35a519b2b567f3710;p=thirdparty%2Fkmod.git Move util functions to libkmod-util.c These allow them to be later shared with tools. --- diff --git a/Makefile.am b/Makefile.am index 59c69993..1d67fc83 100644 --- a/Makefile.am +++ b/Makefile.am @@ -55,6 +55,7 @@ libkmod_libkmod_la_SOURCES =\ 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-module.c b/libkmod/libkmod-module.c index 1ec29e5b..64dda187 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -64,40 +64,6 @@ struct kmod_module { } init; }; -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); -} - static inline const char *path_join(const char *path, size_t prefixlen, char buf[PATH_MAX]) { diff --git a/libkmod/libkmod-private.h b/libkmod/libkmod-private.h index 5c9eeaf7..3c750f1c 100644 --- a/libkmod/libkmod-private.h +++ b/libkmod/libkmod-private.h @@ -123,7 +123,6 @@ const char * const *kmod_softdep_get_post(const struct kmod_list *l, unsigned in /* libkmod-module.c */ int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias, const char *name, struct kmod_module **mod); -char *modname_normalize(const char *modname, char buf[NAME_MAX], size_t *len) __attribute__((nonnull(1, 2))); int kmod_module_parse_depline(struct kmod_module *mod, char *line) __attribute__((nonnull(1, 2))); void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd) __attribute__((nonnull(1))); void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd) __attribute__((nonnull(1))); @@ -157,19 +156,6 @@ int kmod_elf_strip_section(struct kmod_elf *elf, const char *section) __must_che int kmod_elf_strip_vermagic(struct kmod_elf *elf) __must_check __attribute__((nonnull(1))); /* util functions */ -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))); -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))); -int read_str_long(int fd, long *value, int base) __must_check __attribute__((nonnull(2))); -int read_str_ulong(int fd, unsigned long *value, int base) __must_check __attribute__((nonnull(2))); -char *strchr_replace(char *s, int c, char r); -bool path_is_absolute(const char *p) __must_check __attribute__((nonnull(1))); -char *path_make_absolute_cwd(const char *p) __must_check __attribute__((nonnull(1))); -int alias_normalize(const char *alias, char buf[NAME_MAX], size_t *len) __must_check __attribute__((nonnull(1,2))); - +#include "libkmod-util.h" #endif diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c index 177b51a5..50d4d915 100644 --- a/libkmod/libkmod-util.c +++ b/libkmod/libkmod-util.c @@ -157,6 +157,40 @@ finish: return 0; } +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; +} + +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); +} + bool startswith(const char *s, const char *prefix) { size_t sl, pl; diff --git a/libkmod/libkmod-util.h b/libkmod/libkmod-util.h new file mode 100644 index 00000000..f09c3273 --- /dev/null +++ b/libkmod/libkmod-util.h @@ -0,0 +1,20 @@ +#ifndef _LIBKMOD_UTIL_H_ +#define _LIBKMOD_UTIL_H_ + +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))); +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))); +int read_str_long(int fd, long *value, int base) __must_check __attribute__((nonnull(2))); +int read_str_ulong(int fd, unsigned long *value, int base) __must_check __attribute__((nonnull(2))); +char *strchr_replace(char *s, int c, char r); +bool path_is_absolute(const char *p) __must_check __attribute__((nonnull(1))); +char *path_make_absolute_cwd(const char *p) __must_check __attribute__((nonnull(1))); +int alias_normalize(const char *alias, char buf[NAME_MAX], size_t *len) __must_check __attribute__((nonnull(1,2))); +char *modname_normalize(const char *modname, char buf[NAME_MAX], size_t *len) __attribute__((nonnull(1, 2))); +char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len) __attribute__((nonnull(2))); + +#endif