]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/path.c
lib/signames: fix redefinition of 'sys_signame' on OSX
[thirdparty/util-linux.git] / lib / path.c
1 /*
2 * Simple functions to access files. Paths can be globally prefixed to read
3 * data from an alternative source (e.g. a /proc dump for regression tests).
4 *
5 * Copyright (C) 2008 Cai Qian <qcai@redhat.com>
6 * Copyright (C) 2008-2012 Karel Zak <kzak@redhat.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it would be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include <stdarg.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <inttypes.h>
28 #include <errno.h>
29
30 #include "all-io.h"
31 #include "path.h"
32 #include "nls.h"
33 #include "c.h"
34
35 static size_t prefixlen;
36 static char pathbuf[PATH_MAX];
37
38 static const char *
39 path_vcreate(const char *path, va_list ap)
40 {
41 int rc = vsnprintf(
42 pathbuf + prefixlen, sizeof(pathbuf) - prefixlen, path, ap);
43
44 if (rc < 0)
45 return NULL;
46 if ((size_t)rc >= sizeof(pathbuf)) {
47 errno = ENAMETOOLONG;
48 return NULL;
49 }
50 return pathbuf;
51 }
52
53 const char *
54 path_get(const char *path, ...)
55 {
56 const char *p;
57 va_list ap;
58
59 va_start(ap, path);
60 p = path_vcreate(path, ap);
61 va_end(ap);
62
63 return p;
64 }
65
66 static FILE *
67 path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
68 {
69 FILE *f;
70 const char *p = path_vcreate(path, ap);
71 if (!p)
72 goto err;
73
74 f = fopen(p, mode);
75 if (!f)
76 goto err;
77
78 return f;
79 err:
80 if (exit_on_error)
81 err(EXIT_FAILURE, _("cannot open %s"), p ? p : "path");
82 return NULL;
83 }
84
85 static int
86 path_vopen(int flags, const char *path, va_list ap)
87 {
88 int fd;
89 const char *p = path_vcreate(path, ap);
90 if (!p)
91 goto err;
92
93 fd = open(p, flags);
94 if (fd == -1)
95 goto err;
96
97 return fd;
98 err:
99 err(EXIT_FAILURE, _("cannot open %s"), p ? p : "path");
100 }
101
102 FILE *
103 path_fopen(const char *mode, int exit_on_error, const char *path, ...)
104 {
105 FILE *fd;
106 va_list ap;
107
108 va_start(ap, path);
109 fd = path_vfopen(mode, exit_on_error, path, ap);
110 va_end(ap);
111
112 return fd;
113 }
114
115 void
116 path_read_str(char *result, size_t len, const char *path, ...)
117 {
118 FILE *fd;
119 va_list ap;
120
121 va_start(ap, path);
122 fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
123 va_end(ap);
124
125 if (!fgets(result, len, fd))
126 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
127 fclose(fd);
128
129 len = strlen(result);
130 if (result[len - 1] == '\n')
131 result[len - 1] = '\0';
132 }
133
134 int
135 path_read_s32(const char *path, ...)
136 {
137 FILE *fd;
138 va_list ap;
139 int result;
140
141 va_start(ap, path);
142 fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
143 va_end(ap);
144
145 if (fscanf(fd, "%d", &result) != 1) {
146 if (ferror(fd))
147 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
148 else
149 errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
150 }
151 fclose(fd);
152 return result;
153 }
154
155 uint64_t
156 path_read_u64(const char *path, ...)
157 {
158 FILE *fd;
159 va_list ap;
160 uint64_t result;
161
162 va_start(ap, path);
163 fd = path_vfopen("r", 1, path, ap);
164 va_end(ap);
165
166 if (fscanf(fd, "%"SCNu64, &result) != 1) {
167 if (ferror(fd))
168 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
169 else
170 errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
171 }
172 fclose(fd);
173 return result;
174 }
175
176 int
177 path_write_str(const char *str, const char *path, ...)
178 {
179 int fd, result;
180 va_list ap;
181
182 va_start(ap, path);
183 fd = path_vopen(O_WRONLY|O_CLOEXEC, path, ap);
184 va_end(ap);
185 result = write_all(fd, str, strlen(str));
186 close(fd);
187 return result;
188 }
189
190 int
191 path_exist(const char *path, ...)
192 {
193 va_list ap;
194 const char *p;
195
196 va_start(ap, path);
197 p = path_vcreate(path, ap);
198 va_end(ap);
199
200 return p && access(p, F_OK) == 0;
201 }
202
203 #ifdef HAVE_CPU_SET_T
204
205 static cpu_set_t *
206 path_cpuparse(int maxcpus, int islist, const char *path, va_list ap)
207 {
208 FILE *fd;
209 cpu_set_t *set;
210 size_t setsize, len = maxcpus * 7;
211 char buf[len];
212
213 fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
214
215 if (!fgets(buf, len, fd))
216 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
217 fclose(fd);
218
219 len = strlen(buf);
220 if (buf[len - 1] == '\n')
221 buf[len - 1] = '\0';
222
223 set = cpuset_alloc(maxcpus, &setsize, NULL);
224 if (!set)
225 err(EXIT_FAILURE, _("failed to callocate cpu set"));
226
227 if (islist) {
228 if (cpulist_parse(buf, set, setsize, 0))
229 errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf);
230 } else {
231 if (cpumask_parse(buf, set, setsize))
232 errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf);
233 }
234 return set;
235 }
236
237 cpu_set_t *
238 path_read_cpuset(int maxcpus, const char *path, ...)
239 {
240 va_list ap;
241 cpu_set_t *set;
242
243 va_start(ap, path);
244 set = path_cpuparse(maxcpus, 0, path, ap);
245 va_end(ap);
246
247 return set;
248 }
249
250 cpu_set_t *
251 path_read_cpulist(int maxcpus, const char *path, ...)
252 {
253 va_list ap;
254 cpu_set_t *set;
255
256 va_start(ap, path);
257 set = path_cpuparse(maxcpus, 1, path, ap);
258 va_end(ap);
259
260 return set;
261 }
262
263 int
264 path_set_prefix(const char *prefix)
265 {
266 size_t len = strlen(prefix);
267
268 if (len >= sizeof(pathbuf) - 1) {
269 errno = ENAMETOOLONG;
270 return -1;
271 }
272 prefixlen = len;
273 strcpy(pathbuf, prefix);
274 return 0;
275 }
276
277 #endif /* HAVE_CPU_SET_T */