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.
31 #include <sys/utsname.h>
35 #include "udev_utils.h"
39 void udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem)
41 memset(udev, 0x00, sizeof(struct udevice));
44 strfieldcpy(udev->devpath, devpath);
46 strfieldcpy(udev->subsystem, subsystem);
48 if (strcmp(udev->subsystem, "block") == 0)
50 else if (strcmp(udev->subsystem, "net") == 0)
52 else if (strncmp(udev->devpath, "/block/", 7) == 0)
54 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
56 else if (strncmp(udev->devpath, "/class/", 7) == 0)
60 int kernel_release_satisfactory(int version, int patchlevel, int sublevel)
62 static int kversion = 0;
63 static int kpatchlevel;
71 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
77 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
83 int create_path(const char *path)
90 pos = strrchr(p, '/');
91 if (pos == p || pos == NULL)
94 while (pos[-1] == '/')
99 dbg("stat '%s'\n", p);
100 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
103 if (create_path (p) != 0)
106 dbg("mkdir '%s'\n", p);
107 return mkdir(p, 0755);
110 int file_map(const char *filename, char **buf, size_t *bufsize)
115 fd = open(filename, O_RDONLY);
120 if (fstat(fd, &stats) < 0) {
125 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
126 if (*buf == MAP_FAILED) {
130 *bufsize = stats.st_size;
137 void file_unmap(char *buf, size_t bufsize)
139 munmap(buf, bufsize);
142 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
146 for (count = cur; count < buflen && buf[count] != '\n'; count++);
151 void no_trailing_slash(char *path)
156 if (len > 0 && path[len-1] == '/')
161 struct list_head list;
162 char name[NAME_SIZE];
165 /* sort files in lexical order */
166 static int file_list_insert(char *filename, struct list_head *file_list)
168 struct files *loop_file;
169 struct files *new_file;
171 list_for_each_entry(loop_file, file_list, list) {
172 if (strcmp(loop_file->name, filename) > 0) {
177 new_file = malloc(sizeof(struct files));
178 if (new_file == NULL) {
183 strfieldcpy(new_file->name, filename);
184 list_add_tail(&new_file->list, &loop_file->list);
188 /* calls function for every file found in specified directory */
189 int call_foreach_file(file_fnct_t fnct, const char *dirname,
190 const char *suffix, void *data)
195 struct files *loop_file;
196 struct files *tmp_file;
197 LIST_HEAD(file_list);
199 dbg("open directory '%s'", dirname);
200 dir = opendir(dirname);
202 dbg("unable to open '%s'", dirname);
208 if (ent == NULL || ent->d_name[0] == '\0')
211 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
214 /* look for file with specified suffix */
215 ext = strrchr(ent->d_name, '.');
219 if (strcmp(ext, suffix) != 0)
222 dbg("put file '%s/%s' in list", dirname, ent->d_name);
223 file_list_insert(ent->d_name, &file_list);
226 /* call function for every file in the list */
227 list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
228 char filename[NAME_SIZE];
230 snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
231 filename[NAME_SIZE-1] = '\0';
233 fnct(filename, data);
235 list_del(&loop_file->list);