]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/path.c
Merge branch '2012wk28'
[thirdparty/util-linux.git] / lib / path.c
1 /*
2 * Simple functions to access files.
3 *
4 * Taken from lscpu.c
5 *
6 * Copyright (C) 2008 Cai Qian <qcai@redhat.com>
7 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it would be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include <stdarg.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <errno.h>
29
30 #include "all-io.h"
31 #include "cpuset.h"
32 #include "path.h"
33 #include "nls.h"
34 #include "c.h"
35
36 static size_t prefixlen;
37 static char pathbuf[PATH_MAX];
38
39 static const char *
40 path_vcreate(const char *path, va_list ap)
41 {
42 if (prefixlen)
43 vsnprintf(pathbuf + prefixlen,
44 sizeof(pathbuf) - prefixlen, path, ap);
45 else
46 vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
47 return pathbuf;
48 }
49
50 static FILE *
51 path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
52 {
53 FILE *f;
54 const char *p = path_vcreate(path, ap);
55
56 f = fopen(p, mode);
57 if (!f && exit_on_error)
58 err(EXIT_FAILURE, _("cannot open %s"), p);
59 return f;
60 }
61
62 static int
63 path_vopen(int flags, const char *path, va_list ap)
64 {
65 int fd;
66 const char *p = path_vcreate(path, ap);
67
68 fd = open(p, flags);
69 if (fd == -1)
70 err(EXIT_FAILURE, _("cannot open %s"), p);
71 return fd;
72 }
73
74 FILE *
75 path_fopen(const char *mode, int exit_on_error, const char *path, ...)
76 {
77 FILE *fd;
78 va_list ap;
79
80 va_start(ap, path);
81 fd = path_vfopen(mode, exit_on_error, path, ap);
82 va_end(ap);
83
84 return fd;
85 }
86
87 void
88 path_getstr(char *result, size_t len, const char *path, ...)
89 {
90 FILE *fd;
91 va_list ap;
92
93 va_start(ap, path);
94 fd = path_vfopen("r", 1, path, ap);
95 va_end(ap);
96
97 if (!fgets(result, len, fd))
98 err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
99 fclose(fd);
100
101 len = strlen(result);
102 if (result[len - 1] == '\n')
103 result[len - 1] = '\0';
104 }
105
106 int
107 path_getnum(const char *path, ...)
108 {
109 FILE *fd;
110 va_list ap;
111 int result;
112
113 va_start(ap, path);
114 fd = path_vfopen("r", 1, path, ap);
115 va_end(ap);
116
117 if (fscanf(fd, "%d", &result) != 1) {
118 if (ferror(fd))
119 err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
120 else
121 errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
122 }
123 fclose(fd);
124 return result;
125 }
126
127 int
128 path_writestr(const char *str, const char *path, ...)
129 {
130 int fd, result;
131 va_list ap;
132
133 va_start(ap, path);
134 fd = path_vopen(O_WRONLY, path, ap);
135 va_end(ap);
136 result = write_all(fd, str, strlen(str));
137 close(fd);
138 return result;
139 }
140
141 int
142 path_exist(const char *path, ...)
143 {
144 va_list ap;
145 const char *p;
146
147 va_start(ap, path);
148 p = path_vcreate(path, ap);
149 va_end(ap);
150
151 return access(p, F_OK) == 0;
152 }
153
154 static cpu_set_t *
155 path_cpuparse(int maxcpus, int islist, const char *path, va_list ap)
156 {
157 FILE *fd;
158 cpu_set_t *set;
159 size_t setsize, len = maxcpus * 7;
160 char buf[len];
161
162 fd = path_vfopen("r", 1, path, ap);
163
164 if (!fgets(buf, len, fd))
165 err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
166 fclose(fd);
167
168 len = strlen(buf);
169 if (buf[len - 1] == '\n')
170 buf[len - 1] = '\0';
171
172 set = cpuset_alloc(maxcpus, &setsize, NULL);
173 if (!set)
174 err(EXIT_FAILURE, _("failed to callocate cpu set"));
175
176 if (islist) {
177 if (cpulist_parse(buf, set, setsize, 0))
178 errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf);
179 } else {
180 if (cpumask_parse(buf, set, setsize))
181 errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf);
182 }
183 return set;
184 }
185
186 cpu_set_t *
187 path_cpuset(int maxcpus, const char *path, ...)
188 {
189 va_list ap;
190 cpu_set_t *set;
191
192 va_start(ap, path);
193 set = path_cpuparse(maxcpus, 0, path, ap);
194 va_end(ap);
195
196 return set;
197 }
198
199 cpu_set_t *
200 path_cpulist(int maxcpus, const char *path, ...)
201 {
202 va_list ap;
203 cpu_set_t *set;
204
205 va_start(ap, path);
206 set = path_cpuparse(maxcpus, 1, path, ap);
207 va_end(ap);
208
209 return set;
210 }
211
212 void
213 path_setprefix(const char *prefix)
214 {
215 prefixlen = strlen(prefix);
216 strncpy(pathbuf, prefix, sizeof(pathbuf));
217 pathbuf[sizeof(pathbuf) - 1] = '\0';
218 }