]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/path.c
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).
5 * Copyright (C) 2008 Cai Qian <qcai@redhat.com>
6 * Copyright (C) 2008-2012 Karel Zak <kzak@redhat.com>
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.
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.
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.
35 static size_t prefixlen
;
36 static char pathbuf
[PATH_MAX
];
39 path_vcreate(const char *path
, va_list ap
)
42 pathbuf
+ prefixlen
, sizeof(pathbuf
) - prefixlen
, path
, ap
);
46 if ((size_t)rc
>= sizeof(pathbuf
)) {
54 path_get(const char *path
, ...)
60 p
= path_vcreate(path
, ap
);
67 path_vfopen(const char *mode
, int exit_on_error
, const char *path
, va_list ap
)
70 const char *p
= path_vcreate(path
, ap
);
81 err(EXIT_FAILURE
, _("cannot open %s"), p
? p
: "path");
86 path_vopen(int flags
, const char *path
, va_list ap
)
89 const char *p
= path_vcreate(path
, ap
);
99 err(EXIT_FAILURE
, _("cannot open %s"), p
? p
: "path");
103 path_fopen(const char *mode
, int exit_on_error
, const char *path
, ...)
109 fd
= path_vfopen(mode
, exit_on_error
, path
, ap
);
116 path_read_str(char *result
, size_t len
, const char *path
, ...)
122 fd
= path_vfopen("r" UL_CLOEXECSTR
, 1, path
, ap
);
125 if (!fgets(result
, len
, fd
))
126 err(EXIT_FAILURE
, _("cannot read %s"), pathbuf
);
129 len
= strlen(result
);
130 if (result
[len
- 1] == '\n')
131 result
[len
- 1] = '\0';
135 path_read_s32(const char *path
, ...)
142 fd
= path_vfopen("r" UL_CLOEXECSTR
, 1, path
, ap
);
145 if (fscanf(fd
, "%d", &result
) != 1) {
147 err(EXIT_FAILURE
, _("cannot read %s"), pathbuf
);
149 errx(EXIT_FAILURE
, _("parse error: %s"), pathbuf
);
156 path_read_u64(const char *path
, ...)
163 fd
= path_vfopen("r", 1, path
, ap
);
166 if (fscanf(fd
, "%"SCNu64
, &result
) != 1) {
168 err(EXIT_FAILURE
, _("cannot read %s"), pathbuf
);
170 errx(EXIT_FAILURE
, _("parse error: %s"), pathbuf
);
177 path_write_str(const char *str
, const char *path
, ...)
183 fd
= path_vopen(O_WRONLY
|O_CLOEXEC
, path
, ap
);
185 result
= write_all(fd
, str
, strlen(str
));
191 path_exist(const char *path
, ...)
197 p
= path_vcreate(path
, ap
);
200 return p
&& access(p
, F_OK
) == 0;
203 #ifdef HAVE_CPU_SET_T
206 path_cpuparse(int maxcpus
, int islist
, const char *path
, va_list ap
)
210 size_t setsize
, len
= maxcpus
* 7;
213 fd
= path_vfopen("r" UL_CLOEXECSTR
, 1, path
, ap
);
215 if (!fgets(buf
, len
, fd
))
216 err(EXIT_FAILURE
, _("cannot read %s"), pathbuf
);
220 if (buf
[len
- 1] == '\n')
223 set
= cpuset_alloc(maxcpus
, &setsize
, NULL
);
225 err(EXIT_FAILURE
, _("failed to callocate cpu set"));
228 if (cpulist_parse(buf
, set
, setsize
, 0))
229 errx(EXIT_FAILURE
, _("failed to parse CPU list %s"), buf
);
231 if (cpumask_parse(buf
, set
, setsize
))
232 errx(EXIT_FAILURE
, _("failed to parse CPU mask %s"), buf
);
238 path_read_cpuset(int maxcpus
, const char *path
, ...)
244 set
= path_cpuparse(maxcpus
, 0, path
, ap
);
251 path_read_cpulist(int maxcpus
, const char *path
, ...)
257 set
= path_cpuparse(maxcpus
, 1, path
, ap
);
264 path_set_prefix(const char *prefix
)
266 size_t len
= strlen(prefix
);
268 if (len
>= sizeof(pathbuf
) - 1) {
269 errno
= ENAMETOOLONG
;
273 strcpy(pathbuf
, prefix
);
277 #endif /* HAVE_CPU_SET_T */