2 * udev_lib - generic stuff used by udev
4 * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <sys/utsname.h>
36 #include "udev_utils.h"
40 void udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem)
42 memset(udev, 0x00, sizeof(struct udevice));
45 strfieldcpy(udev->devpath, devpath);
47 strfieldcpy(udev->subsystem, subsystem);
49 if (strcmp(udev->subsystem, "block") == 0)
51 else if (strcmp(udev->subsystem, "net") == 0)
53 else if (strncmp(udev->devpath, "/block/", 7) == 0)
55 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
57 else if (strncmp(udev->devpath, "/class/", 7) == 0)
61 strcpy(udev->owner, "root");
62 strcpy(udev->group, "root");
65 int kernel_release_satisfactory(int version, int patchlevel, int sublevel)
67 static int kversion = 0;
68 static int kpatchlevel;
76 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
82 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
88 int create_path(const char *path)
95 pos = strrchr(p, '/');
96 if (pos == p || pos == NULL)
99 while (pos[-1] == '/')
104 dbg("stat '%s'\n", p);
105 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
108 if (create_path (p) != 0)
111 dbg("mkdir '%s'\n", p);
112 return mkdir(p, 0755);
115 int parse_get_pair(char **orig_string, char **left, char **right)
118 char *string = *orig_string;
123 /* eat any whitespace */
124 while (isspace(*string) || *string == ',')
127 /* split based on '=' */
128 temp = strsep(&string, "=");
133 /* take the right side and strip off the '"' */
134 while (isspace(*string))
141 temp = strsep(&string, "\"");
142 if (!string || *temp == '\0')
145 *orig_string = string;
150 int file_map(const char *filename, char **buf, size_t *bufsize)
155 fd = open(filename, O_RDONLY);
160 if (fstat(fd, &stats) < 0) {
165 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
166 if (*buf == MAP_FAILED) {
170 *bufsize = stats.st_size;
177 void file_unmap(char *buf, size_t bufsize)
179 munmap(buf, bufsize);
182 /* return number of chars until the next newline, skip escaped newline */
183 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
188 for (count = cur; count < buflen; count++) {
189 if (!escape && buf[count] == '\n')
192 if (buf[count] == '\\')
201 void no_trailing_slash(char *path)
206 if (len > 0 && path[len-1] == '/')
211 struct list_head list;
212 char name[NAME_SIZE];
215 /* sort files in lexical order */
216 static int file_list_insert(char *filename, struct list_head *file_list)
218 struct files *loop_file;
219 struct files *new_file;
221 list_for_each_entry(loop_file, file_list, list) {
222 if (strcmp(loop_file->name, filename) > 0) {
227 new_file = malloc(sizeof(struct files));
228 if (new_file == NULL) {
233 strfieldcpy(new_file->name, filename);
234 list_add_tail(&new_file->list, &loop_file->list);
238 /* calls function for every file found in specified directory */
239 int call_foreach_file(file_fnct_t fnct, const char *dirname,
240 const char *suffix, void *data)
245 struct files *loop_file;
246 struct files *tmp_file;
247 LIST_HEAD(file_list);
249 dbg("open directory '%s'", dirname);
250 dir = opendir(dirname);
252 dbg("unable to open '%s'", dirname);
258 if (ent == NULL || ent->d_name[0] == '\0')
261 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
264 /* look for file with specified suffix */
265 ext = strrchr(ent->d_name, '.');
269 if (strcmp(ext, suffix) != 0)
272 dbg("put file '%s/%s' in list", dirname, ent->d_name);
273 file_list_insert(ent->d_name, &file_list);
276 /* call function for every file in the list */
277 list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
278 char filename[NAME_SIZE];
280 snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
281 filename[NAME_SIZE-1] = '\0';
283 fnct(filename, data);
285 list_del(&loop_file->list);