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 char *name_list_add(struct list_head *name_list, const char *name, int sort)
59 struct name_entry *loop_name;
60 struct name_entry *new_name;
62 list_for_each_entry(loop_name, name_list, node) {
64 if (strcmp(loop_name->name, name) == 0) {
65 dbg("'%s' is already in the list", name);
66 return loop_name->name;
71 list_for_each_entry(loop_name, name_list, node) {
72 if (sort && strcmp(loop_name->name, name) > 0)
76 new_name = malloc(sizeof(struct name_entry));
80 strlcpy(new_name->name, name, sizeof(new_name->name));
81 dbg("adding '%s'", new_name->name);
82 list_add_tail(&new_name->node, &loop_name->node);
84 return new_name->name;
87 char *name_list_key_add(struct list_head *name_list, const char *key, const char *value)
89 struct name_entry *loop_name;
90 struct name_entry *new_name;
92 list_for_each_entry(loop_name, name_list, node) {
93 if (strncmp(loop_name->name, key, strlen(key)) == 0) {
94 dbg("key already present '%s', replace it", loop_name->name);
95 snprintf(loop_name->name, sizeof(loop_name->name), "%s=%s", key, value);
96 loop_name->name[sizeof(loop_name->name)-1] = '\0';
97 return loop_name->name;
101 new_name = malloc(sizeof(struct name_entry));
102 if (new_name == NULL)
105 snprintf(new_name->name, sizeof(new_name->name), "%s=%s", key, value);
106 new_name->name[sizeof(new_name->name)-1] = '\0';
107 dbg("adding '%s'", new_name->name);
108 list_add_tail(&new_name->node, &loop_name->node);
110 return new_name->name;
113 void name_list_cleanup(struct list_head *name_list)
115 struct name_entry *name_loop;
116 struct name_entry *temp_loop;
118 list_for_each_entry_safe(name_loop, temp_loop, name_list, node) {
119 list_del(&name_loop->node);
124 /* calls function for every file found in specified directory */
125 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
130 char filename[PATH_SIZE];
132 dbg("open directory '%s'", dirname);
133 dir = opendir(dirname);
135 err("unable to open '%s': %s", dirname, strerror(errno));
141 if (ent == NULL || ent->d_name[0] == '\0')
144 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
147 /* look for file matching with specified suffix */
148 ext = strrchr(ent->d_name, '.');
152 if (strcmp(ext, suffix) != 0)
155 dbg("put file '%s/%s' in list", dirname, ent->d_name);
157 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
158 filename[sizeof(filename)-1] = '\0';
159 name_list_add(name_list, filename, 1);
166 uid_t lookup_user(const char *user)
174 if (errno == 0 || errno == ENOENT || errno == ESRCH)
175 err("specified user '%s' unknown", user);
177 err("error resolving user '%s': %s", user, strerror(errno));
184 extern gid_t lookup_group(const char *group)
190 gr = getgrnam(group);
192 if (errno == 0 || errno == ENOENT || errno == ESRCH)
193 err("specified group '%s' unknown", group);
195 err("error resolving group '%s': %s", group, strerror(errno));