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 "libsysfs/sysfs/libsysfs.h"
39 char *get_action(void)
43 action = getenv("ACTION");
44 if (action != NULL && strlen(action) > ACTION_SIZE)
45 action[ACTION_SIZE-1] = '\0';
50 char *get_devpath(void)
54 devpath = getenv("DEVPATH");
55 if (devpath != NULL && strlen(devpath) > DEVPATH_SIZE)
56 devpath[DEVPATH_SIZE-1] = '\0';
61 char *get_devnode(void)
65 devnode = getenv("DEVNODE");
66 if (devnode != NULL && strlen(devnode) > NAME_SIZE)
67 devnode[NAME_SIZE-1] = '\0';
72 char *get_seqnum(void)
76 seqnum = getenv("SEQNUM");
81 char *get_subsystem(char *subsystem)
83 if (subsystem != NULL && strlen(subsystem) > SUBSYSTEM_SIZE)
84 subsystem[SUBSYSTEM_SIZE-1] = '\0';
89 #define BLOCK_PATH "/block/"
90 #define CLASS_PATH "/class/"
91 #define NET_PATH "/class/net/"
93 char get_device_type(const char *path, const char *subsystem)
95 if (strcmp(subsystem, "block") == 0)
98 if (strcmp(subsystem, "net") == 0)
101 if (strncmp(path, BLOCK_PATH, strlen(BLOCK_PATH)) == 0 &&
102 strlen(path) > strlen(BLOCK_PATH))
105 if (strncmp(path, NET_PATH, strlen(NET_PATH)) == 0 &&
106 strlen(path) > strlen(NET_PATH))
109 if (strncmp(path, CLASS_PATH, strlen(CLASS_PATH)) == 0 &&
110 strlen(path) > strlen(CLASS_PATH))
116 int file_map(const char *filename, char **buf, size_t *bufsize)
121 fd = open(filename, O_RDONLY);
126 if (fstat(fd, &stats) < 0) {
130 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
131 if (*buf == MAP_FAILED) {
134 *bufsize = stats.st_size;
141 void file_unmap(char *buf, size_t bufsize)
143 munmap(buf, bufsize);
146 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
150 for (count = cur; count < buflen && buf[count] != '\n'; count++);
156 struct list_head list;
157 char name[NAME_SIZE];
160 /* sort files in lexical order */
161 static int file_list_insert(char *filename, struct list_head *file_list)
163 struct files *loop_file;
164 struct files *new_file;
166 list_for_each_entry(loop_file, file_list, list) {
167 if (strcmp(loop_file->name, filename) > 0) {
172 new_file = malloc(sizeof(struct files));
173 if (new_file == NULL) {
178 strfieldcpy(new_file->name, filename);
179 list_add_tail(&new_file->list, &loop_file->list);
183 /* calls function for file or every file found in directory */
184 int call_foreach_file(int fnct(char *f) , char *dirname, char *suffix)
189 char file[NAME_SIZE];
190 struct files *loop_file;
191 struct files *tmp_file;
192 LIST_HEAD(file_list);
194 dbg("open directory '%s'", dirname);
195 dir = opendir(dirname);
197 dbg("unable to open '%s'", dirname);
203 if (ent == NULL || ent->d_name[0] == '\0')
206 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
209 /* look for file with specified suffix */
210 ext = strrchr(ent->d_name, '.');
214 if (strcmp(ext, suffix) != 0)
217 dbg("put file '%s/%s' in list", dirname, ent->d_name);
218 file_list_insert(ent->d_name, &file_list);
221 /* call function for every file in the list */
222 list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
223 strfieldcpy(file, dirname);
224 strfieldcat(file, "/");
225 strfieldcat(file, loop_file->name);
229 list_del(&loop_file->list);