]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Move libkmod-util.c to convenience util lib
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 28 Dec 2011 15:33:26 +0000 (13:33 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 28 Dec 2011 15:33:26 +0000 (13:33 -0200)
Share more code between tools and libkmod. underscores() in kmod-depmod
can not use the same function as in the lib, so rename it.

Makefile.am
libkmod/libkmod-util.c
libkmod/libkmod-util.h
libkmod/libkmod.c
tools/kmod-depmod.c

index a68145c860dac50537793b4844fc5bf2095c5c8e..7ea6e58af260d2297ccd686fbe0b9857af0503fc 100644 (file)
@@ -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 \
index 50d4d9151d73a48a8ee56b008ea04646094dbaef..95d5fba1669608f6c823ecdb95693398e24a819c 100644 (file)
@@ -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);
index f09c3273915a0e792a7bd8c41f3ec1ce2c302b66..a426715171f980477acd763c5b0d4ce44a220ecb 100644 (file)
@@ -1,10 +1,17 @@
 #ifndef _LIBKMOD_UTIL_H_
 #define _LIBKMOD_UTIL_H_
 
+#include "macro.h"
+
+#include <limits.h>
+#include <stdio.h>
+#include <sys/types.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)));
+#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)));
index 4f49ff5495b3b5113f6126ec4997cd4f4aa95841..10905078e21904c4630dabf121f0b2559719958e 100644 (file)
@@ -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,
index b87222992afd29c53c7fb227f14be10771232160..efc7b9d67a05870746558de2788740729e147921 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+#include "libkmod.h"
+#include "libkmod-array.h"
+#include "libkmod-hash.h"
+#include "libkmod-util.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
 #include <unistd.h>
 #include <ctype.h>
-#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;