]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev_utils.c
087 release
[thirdparty/systemd.git] / udev_utils.c
CommitLineData
c81b35c0 1/*
e8d569b4 2 * udev_utils.c - generic stuff used by udev
c81b35c0 3 *
e8d569b4 4 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
c81b35c0
KS
5 *
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.
9 *
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.
14 *
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.
18 *
19 */
20
21
22#include <stdlib.h>
23#include <stdio.h>
4a539daf 24#include <stddef.h>
c81b35c0
KS
25#include <unistd.h>
26#include <fcntl.h>
4a539daf 27#include <errno.h>
9f8dfa19 28#include <ctype.h>
4a539daf 29#include <dirent.h>
6a522681 30#include <syslog.h>
d402af7d 31#include <sys/utsname.h>
c81b35c0 32
c81b35c0 33#include "udev.h"
c81b35c0 34
6a522681
KS
35
36int log_priority(const char *priority)
37{
38 char *endptr;
39 int prio;
40
41 prio = strtol(priority, &endptr, 10);
42 if (endptr[0] == '\0')
43 return prio;
44 if (strncasecmp(priority, "err", 3) == 0)
45 return LOG_ERR;
46 if (strcasecmp(priority, "info") == 0)
47 return LOG_INFO;
48 if (strcasecmp(priority, "debug") == 0)
49 return LOG_DEBUG;
50 if (string_is_true(priority))
51 return LOG_ERR;
52
53 return 0;
54}
55
f0308095 56int name_list_add(struct list_head *name_list, const char *name, int sort)
4a539daf 57{
9ed47a9f
KS
58 struct name_entry *loop_name;
59 struct name_entry *new_name;
60
61 list_for_each_entry(loop_name, name_list, node) {
62 /* avoid doubles */
63 if (strcmp(loop_name->name, name) == 0) {
64 dbg("'%s' is already in the list", name);
65 return 0;
4a539daf
KS
66 }
67 }
68
b8476286
KS
69 if (sort)
70 list_for_each_entry(loop_name, name_list, node) {
71 if (sort && strcmp(loop_name->name, name) > 0)
72 break;
73 }
74
9ed47a9f
KS
75 new_name = malloc(sizeof(struct name_entry));
76 if (new_name == NULL) {
4a539daf
KS
77 dbg("error malloc");
78 return -ENOMEM;
79 }
80
63f61c5c 81 strlcpy(new_name->name, name, sizeof(new_name->name));
b8476286
KS
82 dbg("adding '%s'", new_name->name);
83 list_add_tail(&new_name->node, &loop_name->node);
84
85 return 0;
86}
87
88int name_list_key_add(struct list_head *name_list, const char *key, const char *value)
89{
90 struct name_entry *loop_name;
91 struct name_entry *new_name;
92
93 list_for_each_entry(loop_name, name_list, node) {
94 if (strncmp(loop_name->name, key, strlen(key)) == 0) {
95 dbg("key already present '%s', replace it", loop_name->name);
96 snprintf(loop_name->name, sizeof(loop_name->name), "%s=%s", key, value);
97 loop_name->name[sizeof(loop_name->name)-1] = '\0';
98 return 0;
99 }
100 }
101
102 new_name = malloc(sizeof(struct name_entry));
103 if (new_name == NULL) {
104 dbg("error malloc");
105 return -ENOMEM;
106 }
107
108 snprintf(new_name->name, sizeof(new_name->name), "%s=%s", key, value);
109 new_name->name[sizeof(new_name->name)-1] = '\0';
110 dbg("adding '%s'", new_name->name);
9ed47a9f 111 list_add_tail(&new_name->node, &loop_name->node);
f0308095 112
4a539daf
KS
113 return 0;
114}
115
fb179207
KS
116void name_list_cleanup(struct list_head *name_list)
117{
118 struct name_entry *name_loop;
119 struct name_entry *temp_loop;
120
121 list_for_each_entry_safe(name_loop, temp_loop, name_list, node) {
122 list_del(&name_loop->node);
123 free(name_loop);
124 }
125}
126
aef6bb13 127/* calls function for every file found in specified directory */
67747e1d 128int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
4a539daf
KS
129{
130 struct dirent *ent;
131 DIR *dir;
132 char *ext;
67747e1d 133 char filename[PATH_SIZE];
4a539daf
KS
134
135 dbg("open directory '%s'", dirname);
136 dir = opendir(dirname);
137 if (dir == NULL) {
df4e89bf 138 err("unable to open '%s': %s", dirname, strerror(errno));
4a539daf
KS
139 return -1;
140 }
141
142 while (1) {
143 ent = readdir(dir);
144 if (ent == NULL || ent->d_name[0] == '\0')
145 break;
146
147 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
148 continue;
149
67747e1d 150 /* look for file matching with specified suffix */
4a539daf
KS
151 ext = strrchr(ent->d_name, '.');
152 if (ext == NULL)
153 continue;
154
155 if (strcmp(ext, suffix) != 0)
156 continue;
157
158 dbg("put file '%s/%s' in list", dirname, ent->d_name);
4a539daf 159
67747e1d 160 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
63f61c5c 161 filename[sizeof(filename)-1] = '\0';
67747e1d 162 name_list_add(name_list, filename, 1);
4a539daf
KS
163 }
164
165 closedir(dir);
166 return 0;
167}