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.
32 #include <sys/types.h>
33 #include <sys/utsname.h>
38 int log_priority(const char *priority)
43 prio = strtol(priority, &endptr, 10);
44 if (endptr[0] == '\0')
46 if (strncasecmp(priority, "err", 3) == 0)
48 if (strcasecmp(priority, "info") == 0)
50 if (strcasecmp(priority, "debug") == 0)
52 if (string_is_true(priority))
58 struct name_entry *name_list_add(struct list_head *name_list, const char *name, int sort)
60 struct name_entry *name_loop;
61 struct name_entry *name_new;
63 /* avoid duplicate entries */
64 list_for_each_entry(name_loop, name_list, node) {
65 if (strcmp(name_loop->name, name) == 0) {
66 dbg("'%s' is already in the list\n", name);
72 list_for_each_entry(name_loop, name_list, node) {
73 if (strcmp(name_loop->name, name) > 0)
77 name_new = malloc(sizeof(struct name_entry));
80 memset(name_new, 0x00, sizeof(struct name_entry));
81 strlcpy(name_new->name, name, sizeof(name_new->name));
82 dbg("adding '%s'\n", name_new->name);
83 list_add_tail(&name_new->node, &name_loop->node);
88 struct name_entry *name_list_key_add(struct list_head *name_list, const char *key, const char *value)
90 struct name_entry *name_loop;
91 struct name_entry *name_new;
93 list_for_each_entry(name_loop, name_list, node) {
94 if (strncmp(name_loop->name, key, strlen(key)) == 0) {
95 dbg("key already present '%s', replace it\n", name_loop->name);
96 snprintf(name_loop->name, sizeof(name_loop->name), "%s=%s", key, value);
97 name_loop->name[sizeof(name_loop->name)-1] = '\0';
102 name_new = malloc(sizeof(struct name_entry));
103 if (name_new == NULL)
105 memset(name_new, 0x00, sizeof(struct name_entry));
106 snprintf(name_new->name, sizeof(name_new->name), "%s=%s", key, value);
107 name_new->name[sizeof(name_new->name)-1] = '\0';
108 dbg("adding '%s'\n", name_new->name);
109 list_add_tail(&name_new->node, &name_loop->node);
114 int name_list_key_remove(struct list_head *name_list, const char *key)
116 struct name_entry *name_loop;
117 struct name_entry *name_tmp;
118 size_t keylen = strlen(key);
121 list_for_each_entry_safe(name_loop, name_tmp, name_list, node) {
122 if (strncmp(name_loop->name, key, keylen) != 0)
124 if (name_loop->name[keylen] != '=')
126 list_del(&name_loop->node);
134 void name_list_cleanup(struct list_head *name_list)
136 struct name_entry *name_loop;
137 struct name_entry *name_tmp;
139 list_for_each_entry_safe(name_loop, name_tmp, name_list, node) {
140 list_del(&name_loop->node);
145 /* calls function for every file found in specified directory */
146 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
150 char filename[PATH_SIZE];
152 dbg("open directory '%s'\n", dirname);
153 dir = opendir(dirname);
155 err("unable to open '%s': %s\n", dirname, strerror(errno));
161 if (ent == NULL || ent->d_name[0] == '\0')
164 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
167 /* look for file matching with specified suffix */
168 if (suffix != NULL) {
171 ext = strrchr(ent->d_name, '.');
174 if (strcmp(ext, suffix) != 0)
177 dbg("put file '%s/%s' into list\n", dirname, ent->d_name);
179 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
180 filename[sizeof(filename)-1] = '\0';
181 name_list_add(name_list, filename, 1);
188 uid_t lookup_user(const char *user)
196 if (errno == 0 || errno == ENOENT || errno == ESRCH)
197 err("specified user '%s' unknown\n", user);
199 err("error resolving user '%s': %s\n", user, strerror(errno));
206 extern gid_t lookup_group(const char *group)
212 gr = getgrnam(group);
214 if (errno == 0 || errno == ENOENT || errno == ESRCH)
215 err("specified group '%s' unknown\n", group);
217 err("error resolving group '%s': %s\n", group, strerror(errno));