]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/path.c
2 * Simple functions to access files.
6 * Copyright (C) 2008 Cai Qian <qcai@redhat.com>
7 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
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.
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.
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.
36 static size_t prefixlen
;
37 static char pathbuf
[PATH_MAX
];
40 path_vcreate(const char *path
, va_list ap
)
43 vsnprintf(pathbuf
+ prefixlen
,
44 sizeof(pathbuf
) - prefixlen
, path
, ap
);
46 vsnprintf(pathbuf
, sizeof(pathbuf
), path
, ap
);
51 path_vfopen(const char *mode
, int exit_on_error
, const char *path
, va_list ap
)
54 const char *p
= path_vcreate(path
, ap
);
57 if (!f
&& exit_on_error
)
58 err(EXIT_FAILURE
, _("cannot open %s"), p
);
63 path_vopen(int flags
, const char *path
, va_list ap
)
66 const char *p
= path_vcreate(path
, ap
);
70 err(EXIT_FAILURE
, _("cannot open %s"), p
);
75 path_fopen(const char *mode
, int exit_on_error
, const char *path
, ...)
81 fd
= path_vfopen(mode
, exit_on_error
, path
, ap
);
88 path_getstr(char *result
, size_t len
, const char *path
, ...)
94 fd
= path_vfopen("r", 1, path
, ap
);
97 if (!fgets(result
, len
, fd
))
98 err(EXIT_FAILURE
, _("failed to read: %s"), pathbuf
);
101 len
= strlen(result
);
102 if (result
[len
- 1] == '\n')
103 result
[len
- 1] = '\0';
107 path_getnum(const char *path
, ...)
114 fd
= path_vfopen("r", 1, path
, ap
);
117 if (fscanf(fd
, "%d", &result
) != 1) {
119 err(EXIT_FAILURE
, _("failed to read: %s"), pathbuf
);
121 errx(EXIT_FAILURE
, _("parse error: %s"), pathbuf
);
128 path_writestr(const char *str
, const char *path
, ...)
134 fd
= path_vopen(O_WRONLY
, path
, ap
);
136 result
= write_all(fd
, str
, strlen(str
));
142 path_exist(const char *path
, ...)
148 p
= path_vcreate(path
, ap
);
151 return access(p
, F_OK
) == 0;
155 path_cpuparse(int maxcpus
, int islist
, const char *path
, va_list ap
)
159 size_t setsize
, len
= maxcpus
* 7;
162 fd
= path_vfopen("r", 1, path
, ap
);
164 if (!fgets(buf
, len
, fd
))
165 err(EXIT_FAILURE
, _("failed to read: %s"), pathbuf
);
169 if (buf
[len
- 1] == '\n')
172 set
= cpuset_alloc(maxcpus
, &setsize
, NULL
);
174 err(EXIT_FAILURE
, _("failed to callocate cpu set"));
177 if (cpulist_parse(buf
, set
, setsize
, 0))
178 errx(EXIT_FAILURE
, _("failed to parse CPU list %s"), buf
);
180 if (cpumask_parse(buf
, set
, setsize
))
181 errx(EXIT_FAILURE
, _("failed to parse CPU mask %s"), buf
);
187 path_cpuset(int maxcpus
, const char *path
, ...)
193 set
= path_cpuparse(maxcpus
, 0, path
, ap
);
200 path_cpulist(int maxcpus
, const char *path
, ...)
206 set
= path_cpuparse(maxcpus
, 1, path
, ap
);
213 path_setprefix(const char *prefix
)
215 prefixlen
= strlen(prefix
);
216 strncpy(pathbuf
, prefix
, sizeof(pathbuf
));
217 pathbuf
[sizeof(pathbuf
) - 1] = '\0';