2 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <sys/types.h>
32 #include <sys/utsname.h>
37 int log_priority(const char *priority)
42 prio = strtol(priority, &endptr, 10);
43 if (endptr[0] == '\0')
45 if (strncasecmp(priority, "err", 3) == 0)
47 if (strcasecmp(priority, "info") == 0)
49 if (strcasecmp(priority, "debug") == 0)
51 if (string_is_true(priority))
57 struct name_entry *name_list_add(struct list_head *name_list, const char *name, int sort)
59 struct name_entry *name_loop;
60 struct name_entry *name_new;
62 /* avoid duplicate entries */
63 list_for_each_entry(name_loop, name_list, node) {
64 if (strcmp(name_loop->name, name) == 0) {
65 dbg("'%s' is already in the list\n", name);
71 list_for_each_entry(name_loop, name_list, node) {
72 if (strcmp(name_loop->name, name) > 0)
76 name_new = malloc(sizeof(struct name_entry));
80 strlcpy(name_new->name, name, sizeof(name_new->name));
81 dbg("adding '%s'\n", name_new->name);
82 list_add_tail(&name_new->node, &name_loop->node);
87 struct name_entry *name_list_key_add(struct list_head *name_list, const char *key, const char *value)
89 struct name_entry *name_loop;
90 struct name_entry *name_new;
92 list_for_each_entry(name_loop, name_list, node) {
93 if (strncmp(name_loop->name, key, strlen(key)) == 0) {
94 dbg("key already present '%s', replace it\n", name_loop->name);
95 snprintf(name_loop->name, sizeof(name_loop->name), "%s=%s", key, value);
96 name_loop->name[sizeof(name_loop->name)-1] = '\0';
101 name_new = malloc(sizeof(struct name_entry));
102 if (name_new == NULL)
105 snprintf(name_new->name, sizeof(name_new->name), "%s=%s", key, value);
106 name_new->name[sizeof(name_new->name)-1] = '\0';
107 dbg("adding '%s'\n", name_new->name);
108 list_add_tail(&name_new->node, &name_loop->node);
113 int name_list_key_remove(struct list_head *name_list, const char *key)
115 struct name_entry *name_loop;
116 struct name_entry *name_tmp;
117 size_t keylen = strlen(key);
120 list_for_each_entry_safe(name_loop, name_tmp, name_list, node) {
121 if (strncmp(name_loop->name, key, keylen) != 0)
123 if (name_loop->name[keylen] != '=')
125 list_del(&name_loop->node);
133 void name_list_cleanup(struct list_head *name_list)
135 struct name_entry *name_loop;
136 struct name_entry *name_tmp;
138 list_for_each_entry_safe(name_loop, name_tmp, name_list, node) {
139 list_del(&name_loop->node);
144 /* calls function for every file found in specified directory */
145 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
149 char filename[PATH_SIZE];
151 dbg("open directory '%s'\n", dirname);
152 dir = opendir(dirname);
154 err("unable to open '%s': %s\n", dirname, strerror(errno));
160 if (ent == NULL || ent->d_name[0] == '\0')
163 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
166 /* look for file matching with specified suffix */
167 if (suffix != NULL) {
170 ext = strrchr(ent->d_name, '.');
173 if (strcmp(ext, suffix) != 0)
176 dbg("put file '%s/%s' into list\n", dirname, ent->d_name);
178 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
179 filename[sizeof(filename)-1] = '\0';
180 name_list_add(name_list, filename, 1);
187 uid_t lookup_user(const char *user)
195 if (errno == 0 || errno == ENOENT || errno == ESRCH)
196 err("specified user '%s' unknown\n", user);
198 err("error resolving user '%s': %s\n", user, strerror(errno));
205 extern gid_t lookup_group(const char *group)
211 gr = getgrnam(group);
213 if (errno == 0 || errno == ENOENT || errno == ESRCH)
214 err("specified group '%s' unknown\n", group);
216 err("error resolving group '%s': %s\n", group, strerror(errno));