]>
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 vsnprintf(pathbuf
+ prefixlen
,
43 sizeof(pathbuf
) - prefixlen
, path
, ap
);
45 vsnprintf(pathbuf
, sizeof(pathbuf
), path
, ap
);
50 path_strdup(const char *path
, ...)
56 p
= path_vcreate(path
, ap
);
59 return p
? strdup(p
) : NULL
;
63 path_vfopen(const char *mode
, int exit_on_error
, const char *path
, va_list ap
)
66 const char *p
= path_vcreate(path
, ap
);
69 if (!f
&& exit_on_error
)
70 err(EXIT_FAILURE
, _("cannot open %s"), p
);
75 path_vopen(int flags
, const char *path
, va_list ap
)
78 const char *p
= path_vcreate(path
, ap
);
82 err(EXIT_FAILURE
, _("cannot open %s"), p
);
87 path_fopen(const char *mode
, int exit_on_error
, const char *path
, ...)
93 fd
= path_vfopen(mode
, exit_on_error
, path
, ap
);
100 path_read_str(char *result
, size_t len
, const char *path
, ...)
106 fd
= path_vfopen("r" UL_CLOEXECSTR
, 1, path
, ap
);
109 if (!fgets(result
, len
, fd
))
110 err(EXIT_FAILURE
, _("cannot read %s"), pathbuf
);
113 len
= strlen(result
);
114 if (result
[len
- 1] == '\n')
115 result
[len
- 1] = '\0';
119 path_read_s32(const char *path
, ...)
126 fd
= path_vfopen("r" UL_CLOEXECSTR
, 1, path
, ap
);
129 if (fscanf(fd
, "%d", &result
) != 1) {
131 err(EXIT_FAILURE
, _("cannot read %s"), pathbuf
);
133 errx(EXIT_FAILURE
, _("parse error: %s"), pathbuf
);
140 path_read_u64(const char *path
, ...)
147 fd
= path_vfopen("r", 1, path
, ap
);
150 if (fscanf(fd
, "%"SCNu64
, &result
) != 1) {
152 err(EXIT_FAILURE
, _("cannot read %s"), pathbuf
);
154 errx(EXIT_FAILURE
, _("parse error: %s"), pathbuf
);
161 path_write_str(const char *str
, const char *path
, ...)
167 fd
= path_vopen(O_WRONLY
|O_CLOEXEC
, path
, ap
);
169 result
= write_all(fd
, str
, strlen(str
));
175 path_exist(const char *path
, ...)
181 p
= path_vcreate(path
, ap
);
184 return access(p
, F_OK
) == 0;
187 #ifdef HAVE_CPU_SET_T
190 path_cpuparse(int maxcpus
, int islist
, const char *path
, va_list ap
)
194 size_t setsize
, len
= maxcpus
* 7;
197 fd
= path_vfopen("r" UL_CLOEXECSTR
, 1, path
, ap
);
199 if (!fgets(buf
, len
, fd
))
200 err(EXIT_FAILURE
, _("cannot read %s"), pathbuf
);
204 if (buf
[len
- 1] == '\n')
207 set
= cpuset_alloc(maxcpus
, &setsize
, NULL
);
209 err(EXIT_FAILURE
, _("failed to callocate cpu set"));
212 if (cpulist_parse(buf
, set
, setsize
, 0))
213 errx(EXIT_FAILURE
, _("failed to parse CPU list %s"), buf
);
215 if (cpumask_parse(buf
, set
, setsize
))
216 errx(EXIT_FAILURE
, _("failed to parse CPU mask %s"), buf
);
222 path_read_cpuset(int maxcpus
, const char *path
, ...)
228 set
= path_cpuparse(maxcpus
, 0, path
, ap
);
235 path_read_cpulist(int maxcpus
, const char *path
, ...)
241 set
= path_cpuparse(maxcpus
, 1, path
, ap
);
247 #endif /* HAVE_CPU_SET_T */
250 path_set_prefix(const char *prefix
)
252 prefixlen
= strlen(prefix
);
253 strncpy(pathbuf
, prefix
, sizeof(pathbuf
));
254 pathbuf
[sizeof(pathbuf
) - 1] = '\0';