2 * udev_utils.c - generic stuff used by udev
4 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@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.
33 #include <sys/types.h>
34 #include <sys/utsname.h>
39 int log_priority(const char *priority)
44 prio = strtol(priority, &endptr, 10);
45 if (endptr[0] == '\0')
47 if (strncasecmp(priority, "err", 3) == 0)
49 if (strcasecmp(priority, "info") == 0)
51 if (strcasecmp(priority, "debug") == 0)
53 if (string_is_true(priority))
59 char *name_list_add(struct list_head *name_list, const char *name, int sort)
61 struct name_entry *loop_name;
62 struct name_entry *new_name;
64 list_for_each_entry(loop_name, name_list, node) {
66 if (strcmp(loop_name->name, name) == 0) {
67 dbg("'%s' is already in the list", name);
68 return loop_name->name;
73 list_for_each_entry(loop_name, name_list, node) {
74 if (sort && strcmp(loop_name->name, name) > 0)
78 new_name = malloc(sizeof(struct name_entry));
82 strlcpy(new_name->name, name, sizeof(new_name->name));
83 dbg("adding '%s'", new_name->name);
84 list_add_tail(&new_name->node, &loop_name->node);
86 return new_name->name;
89 char *name_list_key_add(struct list_head *name_list, const char *key, const char *value)
91 struct name_entry *loop_name;
92 struct name_entry *new_name;
94 list_for_each_entry(loop_name, name_list, node) {
95 if (strncmp(loop_name->name, key, strlen(key)) == 0) {
96 dbg("key already present '%s', replace it", loop_name->name);
97 snprintf(loop_name->name, sizeof(loop_name->name), "%s=%s", key, value);
98 loop_name->name[sizeof(loop_name->name)-1] = '\0';
99 return loop_name->name;
103 new_name = malloc(sizeof(struct name_entry));
104 if (new_name == NULL)
107 snprintf(new_name->name, sizeof(new_name->name), "%s=%s", key, value);
108 new_name->name[sizeof(new_name->name)-1] = '\0';
109 dbg("adding '%s'", new_name->name);
110 list_add_tail(&new_name->node, &loop_name->node);
112 return new_name->name;
115 void name_list_cleanup(struct list_head *name_list)
117 struct name_entry *name_loop;
118 struct name_entry *temp_loop;
120 list_for_each_entry_safe(name_loop, temp_loop, name_list, node) {
121 list_del(&name_loop->node);
126 /* calls function for every file found in specified directory */
127 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
132 char filename[PATH_SIZE];
134 dbg("open directory '%s'", dirname);
135 dir = opendir(dirname);
137 err("unable to open '%s': %s", dirname, strerror(errno));
143 if (ent == NULL || ent->d_name[0] == '\0')
146 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
149 /* look for file matching with specified suffix */
150 ext = strrchr(ent->d_name, '.');
154 if (strcmp(ext, suffix) != 0)
157 dbg("put file '%s/%s' in list", dirname, ent->d_name);
159 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
160 filename[sizeof(filename)-1] = '\0';
161 name_list_add(name_list, filename, 1);
168 uid_t lookup_user(const char *user)
176 if (errno == 0 || errno == ENOENT || errno == ESRCH)
177 err("specified user unknown '%s'", user);
179 err("error resolving user '%s': %s", user, strerror(errno));
186 extern gid_t lookup_group(const char *group)
192 gr = getgrnam(group);
194 if (errno == 0 || errno == ENOENT || errno == ESRCH)
195 err("specified group unknown '%s'", group);
197 err("error resolving group '%s': %s", group, strerror(errno));