]> git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-util.c
Reorder and reorganize header files
[thirdparty/kmod.git] / libkmod / libkmod-util.c
1 /*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
5 * Copyright (C) 2012 Lucas De Marchi <lucas.de.marchi@gmail.com>
6 * Copyright (C) 2013 Intel Corporation. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <shared/util.h>
32
33 #include "libkmod.h"
34 #include "libkmod-internal.h"
35
36 const struct kmod_ext kmod_exts[] = {
37 {".ko", sizeof(".ko") - 1},
38 #ifdef ENABLE_ZLIB
39 {".ko.gz", sizeof(".ko.gz") - 1},
40 #endif
41 #ifdef ENABLE_XZ
42 {".ko.xz", sizeof(".ko.xz") - 1},
43 #endif
44 { }
45 };
46
47 inline int alias_normalize(const char *alias, char buf[PATH_MAX], size_t *len)
48 {
49 size_t s;
50
51 for (s = 0; s < PATH_MAX - 1; s++) {
52 const char c = alias[s];
53 switch (c) {
54 case '-':
55 buf[s] = '_';
56 break;
57 case ']':
58 return -EINVAL;
59 case '[':
60 while (alias[s] != ']' && alias[s] != '\0') {
61 buf[s] = alias[s];
62 s++;
63 }
64
65 if (alias[s] != ']')
66 return -EINVAL;
67
68 buf[s] = alias[s];
69 break;
70 case '\0':
71 goto finish;
72 default:
73 buf[s] = c;
74 }
75 }
76
77 finish:
78 buf[s] = '\0';
79
80 if (len)
81 *len = s;
82
83 return 0;
84 }
85
86 inline char *modname_normalize(const char *modname, char buf[PATH_MAX],
87 size_t *len)
88 {
89 size_t s;
90
91 for (s = 0; s < PATH_MAX - 1; s++) {
92 const char c = modname[s];
93 if (c == '-')
94 buf[s] = '_';
95 else if (c == '\0' || c == '.')
96 break;
97 else
98 buf[s] = c;
99 }
100
101 buf[s] = '\0';
102
103 if (len)
104 *len = s;
105
106 return buf;
107 }
108
109 char *path_to_modname(const char *path, char buf[PATH_MAX], size_t *len)
110 {
111 char *modname;
112
113 modname = basename(path);
114 if (modname == NULL || modname[0] == '\0')
115 return NULL;
116
117 return modname_normalize(modname, buf, len);
118 }
119
120 bool path_ends_with_kmod_ext(const char *path, size_t len)
121 {
122 const struct kmod_ext *eitr;
123
124 for (eitr = kmod_exts; eitr->ext != NULL; eitr++) {
125 if (len <= eitr->len)
126 continue;
127 if (streq(path + len - eitr->len, eitr->ext))
128 return true;
129 }
130
131 return false;
132 }