]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/path.c
Merge branch 'opts' of https://github.com/jwpi/util-linux
[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 if (prefixlen)
42 vsnprintf(pathbuf + prefixlen,
43 sizeof(pathbuf) - prefixlen, path, ap);
44 else
45 vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
46 return pathbuf;
47 }
48
49 char *
50 path_strdup(const char *path, ...)
51 {
52 const char *p;
53 va_list ap;
54
55 va_start(ap, path);
56 p = path_vcreate(path, ap);
57 va_end(ap);
58
59 return p ? strdup(p) : NULL;
60 }
61
62 static FILE *
63 path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
64 {
65 FILE *f;
66 const char *p = path_vcreate(path, ap);
67
68 f = fopen(p, mode);
69 if (!f && exit_on_error)
70 err(EXIT_FAILURE, _("cannot open %s"), p);
71 return f;
72 }
73
74 static int
75 path_vopen(int flags, const char *path, va_list ap)
76 {
77 int fd;
78 const char *p = path_vcreate(path, ap);
79
80 fd = open(p, flags);
81 if (fd == -1)
82 err(EXIT_FAILURE, _("cannot open %s"), p);
83 return fd;
84 }
85
86 FILE *
87 path_fopen(const char *mode, int exit_on_error, const char *path, ...)
88 {
89 FILE *fd;
90 va_list ap;
91
92 va_start(ap, path);
93 fd = path_vfopen(mode, exit_on_error, path, ap);
94 va_end(ap);
95
96 return fd;
97 }
98
99 void
100 path_read_str(char *result, size_t len, const char *path, ...)
101 {
102 FILE *fd;
103 va_list ap;
104
105 va_start(ap, path);
106 fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
107 va_end(ap);
108
109 if (!fgets(result, len, fd))
110 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
111 fclose(fd);
112
113 len = strlen(result);
114 if (result[len - 1] == '\n')
115 result[len - 1] = '\0';
116 }
117
118 int
119 path_read_s32(const char *path, ...)
120 {
121 FILE *fd;
122 va_list ap;
123 int result;
124
125 va_start(ap, path);
126 fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
127 va_end(ap);
128
129 if (fscanf(fd, "%d", &result) != 1) {
130 if (ferror(fd))
131 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
132 else
133 errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
134 }
135 fclose(fd);
136 return result;
137 }
138
139 uint64_t
140 path_read_u64(const char *path, ...)
141 {
142 FILE *fd;
143 va_list ap;
144 uint64_t result;
145
146 va_start(ap, path);
147 fd = path_vfopen("r", 1, path, ap);
148 va_end(ap);
149
150 if (fscanf(fd, "%"SCNu64, &result) != 1) {
151 if (ferror(fd))
152 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
153 else
154 errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
155 }
156 fclose(fd);
157 return result;
158 }
159
160 int
161 path_write_str(const char *str, const char *path, ...)
162 {
163 int fd, result;
164 va_list ap;
165
166 va_start(ap, path);
167 fd = path_vopen(O_WRONLY|O_CLOEXEC, path, ap);
168 va_end(ap);
169 result = write_all(fd, str, strlen(str));
170 close(fd);
171 return result;
172 }
173
174 int
175 path_exist(const char *path, ...)
176 {
177 va_list ap;
178 const char *p;
179
180 va_start(ap, path);
181 p = path_vcreate(path, ap);
182 va_end(ap);
183
184 return access(p, F_OK) == 0;
185 }
186
187 #ifdef HAVE_CPU_SET_T
188
189 static cpu_set_t *
190 path_cpuparse(int maxcpus, int islist, const char *path, va_list ap)
191 {
192 FILE *fd;
193 cpu_set_t *set;
194 size_t setsize, len = maxcpus * 7;
195 char buf[len];
196
197 fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
198
199 if (!fgets(buf, len, fd))
200 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
201 fclose(fd);
202
203 len = strlen(buf);
204 if (buf[len - 1] == '\n')
205 buf[len - 1] = '\0';
206
207 set = cpuset_alloc(maxcpus, &setsize, NULL);
208 if (!set)
209 err(EXIT_FAILURE, _("failed to callocate cpu set"));
210
211 if (islist) {
212 if (cpulist_parse(buf, set, setsize, 0))
213 errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf);
214 } else {
215 if (cpumask_parse(buf, set, setsize))
216 errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf);
217 }
218 return set;
219 }
220
221 cpu_set_t *
222 path_read_cpuset(int maxcpus, const char *path, ...)
223 {
224 va_list ap;
225 cpu_set_t *set;
226
227 va_start(ap, path);
228 set = path_cpuparse(maxcpus, 0, path, ap);
229 va_end(ap);
230
231 return set;
232 }
233
234 cpu_set_t *
235 path_read_cpulist(int maxcpus, const char *path, ...)
236 {
237 va_list ap;
238 cpu_set_t *set;
239
240 va_start(ap, path);
241 set = path_cpuparse(maxcpus, 1, path, ap);
242 va_end(ap);
243
244 return set;
245 }
246
247 #endif /* HAVE_CPU_SET_T */
248
249 void
250 path_set_prefix(const char *prefix)
251 {
252 prefixlen = strlen(prefix);
253 strncpy(pathbuf, prefix, sizeof(pathbuf));
254 pathbuf[sizeof(pathbuf) - 1] = '\0';
255 }