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