2 * No copyright is claimed. This code is in the public domain; do with
5 * Written by Karel Zak <kzak@redhat.com> [2018]
8 * Simple functions to access files. Paths can be globally prefixed to read
9 * data from an alternative source (e.g. a /proc dump for regression tests).
11 * The paths is possible to format by printf-like way for functions with "f"
12 * postfix in the name (e.g. readf, openf, ... ul_path_readf_u64()).
14 * The ul_path_read_* API is possible to use without path_cxt handler. In this
15 * case is not possible to use global prefix and printf-like formatting.
25 #include "fileutils.h"
32 * Debug stuff (based on include/debug.h)
34 static UL_DEBUG_DEFINE_MASK(ulpath
);
35 UL_DEBUG_DEFINE_MASKNAMES(ulpath
) = UL_DEBUG_EMPTY_MASKNAMES
;
37 #define ULPATH_DEBUG_INIT (1 << 1)
38 #define ULPATH_DEBUG_CXT (1 << 2)
40 #define DBG(m, x) __UL_DBG(ulpath, ULPATH_DEBUG_, m, x)
41 #define ON_DBG(m, x) __UL_DBG_CALL(ulpath, ULPATH_DEBUG_, m, x)
43 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(ulpath)
46 void ul_path_init_debug(void)
48 if (ulpath_debug_mask
)
50 __UL_INIT_DEBUG_FROM_ENV(ulpath
, ULPATH_DEBUG_
, 0, ULPATH_DEBUG
);
53 struct path_cxt
*ul_new_path(const char *dir
, ...)
55 struct path_cxt
*pc
= calloc(1, sizeof(*pc
));
60 DBG(CXT
, ul_debugobj(pc
, "alloc"));
70 rc
= vasprintf(&pc
->dir_path
, dir
, ap
);
73 if (rc
< 0 || !pc
->dir_path
)
82 void ul_ref_path(struct path_cxt
*pc
)
88 void ul_unref_path(struct path_cxt
*pc
)
95 if (pc
->refcount
<= 0) {
96 DBG(CXT
, ul_debugobj(pc
, "dealloc"));
99 ul_path_close_dirfd(pc
);
106 int ul_path_set_prefix(struct path_cxt
*pc
, const char *prefix
)
110 assert(pc
->dir_fd
< 0);
120 DBG(CXT
, ul_debugobj(pc
, "new prefix: '%s'", p
));
124 const char *ul_path_get_prefix(struct path_cxt
*pc
)
126 return pc
? pc
->prefix
: NULL
;
129 int ul_path_set_dir(struct path_cxt
*pc
, const char *dir
)
139 if (pc
->dir_fd
>= 0) {
146 DBG(CXT
, ul_debugobj(pc
, "new dir: '%s'", p
));
150 const char *ul_path_get_dir(struct path_cxt
*pc
)
152 return pc
? pc
->dir_path
: NULL
;
155 int ul_path_set_dialect(struct path_cxt
*pc
, void *data
, void free_data(struct path_cxt
*))
158 pc
->free_dialect
= free_data
;
159 DBG(CXT
, ul_debugobj(pc
, "(re)set dialect"));
163 void *ul_path_get_dialect(struct path_cxt
*pc
)
165 return pc
? pc
->dialect
: NULL
;
168 int ul_path_set_enoent_redirect(struct path_cxt
*pc
, int (*func
)(struct path_cxt
*, const char *, int *))
170 pc
->redirect_on_enoent
= func
;
174 static const char *get_absdir(struct path_cxt
*pc
)
182 dirpath
= pc
->dir_path
;
188 rc
= snprintf(pc
->path_buffer
, sizeof(pc
->path_buffer
), "%s/%s", pc
->prefix
, dirpath
);
191 if ((size_t)rc
>= sizeof(pc
->path_buffer
)) {
192 errno
= ENAMETOOLONG
;
196 return pc
->path_buffer
;
199 int ul_path_is_accessible(struct path_cxt
*pc
)
207 path
= get_absdir(pc
);
210 return access(path
, F_OK
) == 0;
213 int ul_path_get_dirfd(struct path_cxt
*pc
)
216 assert(pc
->dir_path
);
218 if (pc
->dir_fd
< 0) {
219 const char *path
= get_absdir(pc
);
223 DBG(CXT
, ul_debugobj(pc
, "opening dir: '%s'", path
));
224 pc
->dir_fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
230 /* Note that next ul_path_get_dirfd() will reopen the directory */
231 void ul_path_close_dirfd(struct path_cxt
*pc
)
235 if (pc
->dir_fd
>= 0) {
236 DBG(CXT
, ul_debugobj(pc
, "closing dir"));
242 int ul_path_isopen_dirfd(struct path_cxt
*pc
)
244 return pc
&& pc
->dir_fd
>= 0;
247 static const char *ul_path_mkpath(struct path_cxt
*pc
, const char *path
, va_list ap
)
253 rc
= vsnprintf(pc
->path_buffer
, sizeof(pc
->path_buffer
), path
, ap
);
260 if ((size_t)rc
>= sizeof(pc
->path_buffer
)) {
261 errno
= ENAMETOOLONG
;
265 return pc
->path_buffer
;
268 char *ul_path_get_abspath(struct path_cxt
*pc
, char *buf
, size_t bufsz
, const char *path
, ...)
273 const char *tail
= NULL
, *dirpath
= pc
->dir_path
;
276 tail
= ul_path_mkpath(pc
, path
, ap
);
279 if (dirpath
&& *dirpath
== '/')
281 if (tail
&& *tail
== '/')
284 rc
= snprintf(buf
, bufsz
, "%s/%s/%s",
285 pc
->prefix
? pc
->prefix
: "",
286 dirpath
? dirpath
: "",
289 if ((size_t)rc
>= bufsz
) {
290 errno
= ENAMETOOLONG
;
294 const char *tmp
= get_absdir(pc
);
298 xstrncpy(buf
, tmp
, bufsz
);
305 int ul_path_access(struct path_cxt
*pc
, int mode
, const char *path
)
310 rc
= access(path
, mode
);
311 DBG(CXT
, ul_debug("access '%s' [no context, rc=%d]", path
, rc
));
313 int dir
= ul_path_get_dirfd(pc
);
319 rc
= faccessat(dir
, path
, mode
, 0);
321 if (rc
&& errno
== ENOENT
322 && pc
->redirect_on_enoent
323 && pc
->redirect_on_enoent(pc
, path
, &dir
) == 0)
324 rc
= faccessat(dir
, path
, mode
, 0);
326 DBG(CXT
, ul_debugobj(pc
, "access: '%s' [rc=%d]", path
, rc
));
331 int ul_path_accessf(struct path_cxt
*pc
, int mode
, const char *path
, ...)
337 p
= ul_path_mkpath(pc
, path
, ap
);
340 return !p
? -errno
: ul_path_access(pc
, mode
, p
);
343 int ul_path_stat(struct path_cxt
*pc
, struct stat
*sb
, int flags
, const char *path
)
348 rc
= path
? stat(path
, sb
) : -EINVAL
;
349 DBG(CXT
, ul_debug("stat '%s' [no context, rc=%d]", path
, rc
));
351 int dir
= ul_path_get_dirfd(pc
);
357 rc
= fstatat(dir
, path
, sb
, flags
);
360 rc
= fstat(dir
, sb
); /* dir itself */
362 if (rc
&& errno
== ENOENT
364 && pc
->redirect_on_enoent
365 && pc
->redirect_on_enoent(pc
, path
, &dir
) == 0)
366 rc
= fstatat(dir
, path
, sb
, 0);
368 DBG(CXT
, ul_debugobj(pc
, "stat '%s' [rc=%d]", path
, rc
));
373 int ul_path_vstatf(struct path_cxt
*pc
, struct stat
*sb
, int flags
, const char *path
, va_list ap
)
375 const char *p
= ul_path_mkpath(pc
, path
, ap
);
377 return !p
? -errno
: ul_path_stat(pc
, sb
, flags
, p
);
380 int ul_path_statf(struct path_cxt
*pc
, struct stat
*sb
, int flags
, const char *path
, ...)
386 rc
= ul_path_vstatf(pc
, sb
, flags
, path
, ap
);
392 int ul_path_open(struct path_cxt
*pc
, int flags
, const char *path
)
399 fd
= open(path
, flags
);
400 DBG(CXT
, ul_debug("opening '%s' [no context]", path
));
403 int dir
= ul_path_get_dirfd(pc
);
410 fdx
= fd
= openat(dir
, path
, flags
);
412 if (fd
< 0 && errno
== ENOENT
413 && pc
->redirect_on_enoent
414 && pc
->redirect_on_enoent(pc
, path
, &dir
) == 0)
415 fd
= openat(dir
, path
, flags
);
417 DBG(CXT
, ul_debugobj(pc
, "opening '%s'%s", path
, fdx
!= fd
? " [redirected]" : ""));
422 int ul_path_vopenf(struct path_cxt
*pc
, int flags
, const char *path
, va_list ap
)
424 const char *p
= ul_path_mkpath(pc
, path
, ap
);
426 return !p
? -errno
: ul_path_open(pc
, flags
, p
);
429 int ul_path_openf(struct path_cxt
*pc
, int flags
, const char *path
, ...)
435 rc
= ul_path_vopenf(pc
, flags
, path
, ap
);
442 * Maybe stupid, but good enough ;-)
444 static int mode2flags(const char *mode
)
449 for (p
= mode
; p
&& *p
; p
++) {
450 if (*p
== 'r' && *(p
+ 1) == '+')
455 else if (*p
== 'w' && *(p
+ 1) == '+')
456 flags
|= O_RDWR
| O_TRUNC
;
458 flags
|= O_WRONLY
| O_TRUNC
;
460 else if (*p
== 'a' && *(p
+ 1) == '+')
461 flags
|= O_RDWR
| O_APPEND
;
463 flags
|= O_WRONLY
| O_APPEND
;
465 else if (*p
== *UL_CLOEXECSTR
)
473 FILE *ul_path_fopen(struct path_cxt
*pc
, const char *mode
, const char *path
)
475 int flags
= mode2flags(mode
);
476 int fd
= ul_path_open(pc
, flags
, path
);
481 return fdopen(fd
, mode
);
485 FILE *ul_path_vfopenf(struct path_cxt
*pc
, const char *mode
, const char *path
, va_list ap
)
487 const char *p
= ul_path_mkpath(pc
, path
, ap
);
489 return !p
? NULL
: ul_path_fopen(pc
, mode
, p
);
492 FILE *ul_path_fopenf(struct path_cxt
*pc
, const char *mode
, const char *path
, ...)
498 f
= ul_path_vfopenf(pc
, mode
, path
, ap
);
505 * Open directory @path in read-only mode. If the path is NULL then duplicate FD
506 * to the directory addressed by @pc.
508 DIR *ul_path_opendir(struct path_cxt
*pc
, const char *path
)
514 fd
= ul_path_open(pc
, O_RDONLY
|O_CLOEXEC
, path
);
515 else if (pc
->dir_path
) {
518 DBG(CXT
, ul_debugobj(pc
, "duplicate dir path"));
519 dirfd
= ul_path_get_dirfd(pc
);
521 fd
= dup_fd_cloexec(dirfd
, STDERR_FILENO
+ 1);
539 * Open directory @path in read-only mode. If the path is NULL then duplicate FD
540 * to the directory addressed by @pc.
542 DIR *ul_path_vopendirf(struct path_cxt
*pc
, const char *path
, va_list ap
)
544 const char *p
= ul_path_mkpath(pc
, path
, ap
);
546 return !p
? NULL
: ul_path_opendir(pc
, p
);
550 * Open directory @path in read-only mode. If the path is NULL then duplicate FD
551 * to the directory addressed by @pc.
553 DIR *ul_path_opendirf(struct path_cxt
*pc
, const char *path
, ...)
559 dir
= ul_path_vopendirf(pc
, path
, ap
);
566 * If @path is NULL then readlink is called on @pc directory.
568 ssize_t
ul_path_readlink(struct path_cxt
*pc
, char *buf
, size_t bufsiz
, const char *path
)
574 const char *p
= get_absdir(pc
);
577 ssz
= readlink(p
, buf
, bufsiz
- 1);
579 dirfd
= ul_path_get_dirfd(pc
);
586 ssz
= readlinkat(dirfd
, path
, buf
, bufsiz
- 1);
595 * If @path is NULL then readlink is called on @pc directory.
597 ssize_t
ul_path_readlinkf(struct path_cxt
*pc
, char *buf
, size_t bufsiz
, const char *path
, ...)
603 p
= ul_path_mkpath(pc
, path
, ap
);
606 return !p
? -errno
: ul_path_readlink(pc
, buf
, bufsiz
, p
);
609 int ul_path_read(struct path_cxt
*pc
, char *buf
, size_t len
, const char *path
)
614 fd
= ul_path_open(pc
, O_RDONLY
|O_CLOEXEC
, path
);
618 DBG(CXT
, ul_debug(" reading '%s'", path
));
619 rc
= read_all(fd
, buf
, len
);
627 int ul_path_vreadf(struct path_cxt
*pc
, char *buf
, size_t len
, const char *path
, va_list ap
)
629 const char *p
= ul_path_mkpath(pc
, path
, ap
);
631 return !p
? -errno
: ul_path_read(pc
, buf
, len
, p
);
634 int ul_path_readf(struct path_cxt
*pc
, char *buf
, size_t len
, const char *path
, ...)
640 rc
= ul_path_vreadf(pc
, buf
, len
, path
, ap
);
648 * Returns newly allocated buffer with data from file. Maximal size is BUFSIZ
649 * (send patch if you need something bigger;-)
651 * Returns size of the string without \0, nothing is allocated if returns <= 0.
653 int ul_path_read_string(struct path_cxt
*pc
, char **str
, const char *path
)
663 rc
= ul_path_read_buffer(pc
, buf
, sizeof(buf
), path
);
674 int ul_path_readf_string(struct path_cxt
*pc
, char **str
, const char *path
, ...)
680 p
= ul_path_mkpath(pc
, path
, ap
);
683 return !p
? -errno
: ul_path_read_string(pc
, str
, p
);
686 int ul_path_read_buffer(struct path_cxt
*pc
, char *buf
, size_t bufsz
, const char *path
)
688 int rc
= ul_path_read(pc
, buf
, bufsz
- 1, path
);
694 /* Remove trailing newline (usual in sysfs) */
695 if (*(buf
+ rc
- 1) == '\n')
704 int ul_path_vreadf_buffer(struct path_cxt
*pc
, char *buf
, size_t bufsz
, const char *path
, va_list ap
)
708 p
= ul_path_mkpath(pc
, path
, ap
);
710 return !p
? -errno
: ul_path_read_buffer(pc
, buf
, bufsz
, p
);
713 int ul_path_readf_buffer(struct path_cxt
*pc
, char *buf
, size_t bufsz
, const char *path
, ...)
719 rc
= ul_path_vreadf_buffer(pc
, buf
, bufsz
, path
, ap
);
725 int ul_path_scanf(struct path_cxt
*pc
, const char *path
, const char *fmt
, ...)
731 f
= ul_path_fopen(pc
, "r" UL_CLOEXECSTR
, path
);
735 DBG(CXT
, ul_debug(" fscanf [%s] '%s'", fmt
, path
));
737 va_start(fmt_ap
, fmt
);
738 rc
= vfscanf(f
, fmt
, fmt_ap
);
745 int ul_path_scanff(struct path_cxt
*pc
, const char *path
, va_list ap
, const char *fmt
, ...)
751 f
= ul_path_vfopenf(pc
, "r" UL_CLOEXECSTR
, path
, ap
);
755 va_start(fmt_ap
, fmt
);
756 rc
= vfscanf(f
, fmt
, fmt_ap
);
764 int ul_path_read_s64(struct path_cxt
*pc
, int64_t *res
, const char *path
)
769 rc
= ul_path_scanf(pc
, path
, "%"SCNd64
, &x
);
777 int ul_path_readf_s64(struct path_cxt
*pc
, int64_t *res
, const char *path
, ...)
783 p
= ul_path_mkpath(pc
, path
, ap
);
786 return !p
? -errno
: ul_path_read_s64(pc
, res
, p
);
789 int ul_path_read_u64(struct path_cxt
*pc
, uint64_t *res
, const char *path
)
794 rc
= ul_path_scanf(pc
, path
, "%"SCNu64
, &x
);
802 int ul_path_readf_u64(struct path_cxt
*pc
, uint64_t *res
, const char *path
, ...)
808 p
= ul_path_mkpath(pc
, path
, ap
);
811 return !p
? -errno
: ul_path_read_u64(pc
, res
, p
);
814 int ul_path_read_s32(struct path_cxt
*pc
, int *res
, const char *path
)
818 rc
= ul_path_scanf(pc
, path
, "%d", &x
);
826 int ul_path_readf_s32(struct path_cxt
*pc
, int *res
, const char *path
, ...)
832 p
= ul_path_mkpath(pc
, path
, ap
);
835 return !p
? -errno
: ul_path_read_s32(pc
, res
, p
);
838 int ul_path_read_u32(struct path_cxt
*pc
, unsigned int *res
, const char *path
)
843 rc
= ul_path_scanf(pc
, path
, "%u", &x
);
851 int ul_path_readf_u32(struct path_cxt
*pc
, unsigned int *res
, const char *path
, ...)
857 p
= ul_path_mkpath(pc
, path
, ap
);
860 return !p
? -errno
: ul_path_read_u32(pc
, res
, p
);
863 int ul_path_read_majmin(struct path_cxt
*pc
, dev_t
*res
, const char *path
)
865 int rc
, maj
= 0, min
= 0;
867 rc
= ul_path_scanf(pc
, path
, "%d:%d", &maj
, &min
);
871 *res
= makedev(maj
, min
);
875 int ul_path_readf_majmin(struct path_cxt
*pc
, dev_t
*res
, const char *path
, ...)
881 p
= ul_path_mkpath(pc
, path
, ap
);
884 return !p
? -errno
: ul_path_read_majmin(pc
, res
, p
);
887 int ul_path_write_string(struct path_cxt
*pc
, const char *str
, const char *path
)
892 fd
= ul_path_open(pc
, O_WRONLY
|O_CLOEXEC
, path
);
896 rc
= write_all(fd
, str
, strlen(str
));
904 int ul_path_writef_string(struct path_cxt
*pc
, const char *str
, const char *path
, ...)
910 p
= ul_path_mkpath(pc
, path
, ap
);
913 return !p
? -errno
: ul_path_write_string(pc
, str
, p
);
916 int ul_path_write_s64(struct path_cxt
*pc
, int64_t num
, const char *path
)
918 char buf
[sizeof(stringify_value(LLONG_MAX
))];
922 fd
= ul_path_open(pc
, O_WRONLY
|O_CLOEXEC
, path
);
926 len
= snprintf(buf
, sizeof(buf
), "%" PRId64
, num
);
927 if (len
< 0 || (size_t) len
>= sizeof(buf
))
928 rc
= len
< 0 ? -errno
: -E2BIG
;
930 rc
= write_all(fd
, buf
, len
);
938 int ul_path_write_u64(struct path_cxt
*pc
, uint64_t num
, const char *path
)
940 char buf
[sizeof(stringify_value(ULLONG_MAX
))];
944 fd
= ul_path_open(pc
, O_WRONLY
|O_CLOEXEC
, path
);
948 len
= snprintf(buf
, sizeof(buf
), "%" PRIu64
, num
);
949 if (len
< 0 || (size_t) len
>= sizeof(buf
))
950 rc
= len
< 0 ? -errno
: -E2BIG
;
952 rc
= write_all(fd
, buf
, len
);
960 int ul_path_writef_u64(struct path_cxt
*pc
, uint64_t num
, const char *path
, ...)
966 p
= ul_path_mkpath(pc
, path
, ap
);
969 return !p
? -errno
: ul_path_write_u64(pc
, num
, p
);
973 int ul_path_count_dirents(struct path_cxt
*pc
, const char *path
)
978 dir
= ul_path_opendir(pc
, path
);
982 while (xreaddir(dir
)) r
++;
988 int ul_path_countf_dirents(struct path_cxt
*pc
, const char *path
, ...)
994 p
= ul_path_mkpath(pc
, path
, ap
);
997 return !p
? -errno
: ul_path_count_dirents(pc
, p
);
1000 /* first call (when @sub is NULL) opens the directory, last call closes the directory */
1001 int ul_path_next_dirent(struct path_cxt
*pc
, DIR **sub
, const char *dirname
, struct dirent
**d
)
1003 if (!pc
|| !sub
|| !d
)
1007 *sub
= ul_path_opendir(pc
, dirname
);
1012 *d
= xreaddir(*sub
);
1021 #ifdef HAVE_CPU_SET_T
1022 static int ul_path_cpuparse(struct path_cxt
*pc
, cpu_set_t
**set
, int maxcpus
, int islist
, const char *path
, va_list ap
)
1024 size_t setsize
, len
= maxcpus
* 7;
1034 rc
= ul_path_vreadf_buffer(pc
, buf
, len
, path
, ap
);
1038 *set
= cpuset_alloc(maxcpus
, &setsize
, NULL
);
1045 if (cpulist_parse(buf
, *set
, setsize
, 0)) {
1051 if (cpumask_parse(buf
, *set
, setsize
)) {
1068 int ul_path_readf_cpuset(struct path_cxt
*pc
, cpu_set_t
**set
, int maxcpus
, const char *path
, ...)
1074 rc
= ul_path_cpuparse(pc
, set
, maxcpus
, 0, path
, ap
);
1080 int ul_path_readf_cpulist(struct path_cxt
*pc
, cpu_set_t
**set
, int maxcpus
, const char *path
, ...)
1086 rc
= ul_path_cpuparse(pc
, set
, maxcpus
, 1, path
, ap
);
1092 #endif /* HAVE_CPU_SET_T */
1095 #ifdef TEST_PROGRAM_PATH
1098 static void __attribute__((__noreturn__
)) usage(void)
1100 fprintf(stdout
, " %s [options] <dir> <command>\n\n", program_invocation_short_name
);
1101 fputs(" -p, --prefix <dir> redirect hardcoded paths to <dir>\n", stdout
);
1103 fputs(" Commands:\n", stdout
);
1104 fputs(" read-u64 <file> read uint64_t from file\n", stdout
);
1105 fputs(" read-s64 <file> read int64_t from file\n", stdout
);
1106 fputs(" read-u32 <file> read uint32_t from file\n", stdout
);
1107 fputs(" read-s32 <file> read int32_t from file\n", stdout
);
1108 fputs(" read-string <file> read string from file\n", stdout
);
1109 fputs(" read-majmin <file> read devno from file\n", stdout
);
1110 fputs(" read-link <file> read symlink\n", stdout
);
1111 fputs(" write-string <file> <str> write string from file\n", stdout
);
1112 fputs(" write-u64 <file> <str> write uint64_t from file\n", stdout
);
1117 int main(int argc
, char *argv
[])
1120 const char *prefix
= NULL
, *dir
, *file
, *command
;
1121 struct path_cxt
*pc
= NULL
;
1123 static const struct option longopts
[] = {
1124 { "prefix", 1, NULL
, 'p' },
1125 { "help", 0, NULL
, 'h' },
1126 { NULL
, 0, NULL
, 0 },
1129 while((c
= getopt_long(argc
, argv
, "p:h", longopts
, NULL
)) != -1) {
1138 err(EXIT_FAILURE
, "try --help");
1143 errx(EXIT_FAILURE
, "<dir> not defined");
1144 dir
= argv
[optind
++];
1146 ul_path_init_debug();
1148 pc
= ul_new_path("%s", dir
);
1150 err(EXIT_FAILURE
, "failed to initialize path context");
1152 ul_path_set_prefix(pc
, prefix
);
1155 errx(EXIT_FAILURE
, "<command> not defined");
1156 command
= argv
[optind
++];
1158 if (strcmp(command
, "read-u32") == 0) {
1162 errx(EXIT_FAILURE
, "<file> not defined");
1163 file
= argv
[optind
++];
1165 if (ul_path_read_u32(pc
, &res
, file
) != 0)
1166 err(EXIT_FAILURE
, "read u64 failed");
1167 printf("read: %s: %u\n", file
, res
);
1169 if (ul_path_readf_u32(pc
, &res
, "%s", file
) != 0)
1170 err(EXIT_FAILURE
, "readf u64 failed");
1171 printf("readf: %s: %u\n", file
, res
);
1173 } else if (strcmp(command
, "read-s32") == 0) {
1177 errx(EXIT_FAILURE
, "<file> not defined");
1178 file
= argv
[optind
++];
1180 if (ul_path_read_s32(pc
, &res
, file
) != 0)
1181 err(EXIT_FAILURE
, "read u64 failed");
1182 printf("read: %s: %d\n", file
, res
);
1184 if (ul_path_readf_s32(pc
, &res
, "%s", file
) != 0)
1185 err(EXIT_FAILURE
, "readf u64 failed");
1186 printf("readf: %s: %d\n", file
, res
);
1188 } else if (strcmp(command
, "read-u64") == 0) {
1192 errx(EXIT_FAILURE
, "<file> not defined");
1193 file
= argv
[optind
++];
1195 if (ul_path_read_u64(pc
, &res
, file
) != 0)
1196 err(EXIT_FAILURE
, "read u64 failed");
1197 printf("read: %s: %" PRIu64
"\n", file
, res
);
1199 if (ul_path_readf_u64(pc
, &res
, "%s", file
) != 0)
1200 err(EXIT_FAILURE
, "readf u64 failed");
1201 printf("readf: %s: %" PRIu64
"\n", file
, res
);
1203 } else if (strcmp(command
, "read-s64") == 0) {
1207 errx(EXIT_FAILURE
, "<file> not defined");
1208 file
= argv
[optind
++];
1210 if (ul_path_read_s64(pc
, &res
, file
) != 0)
1211 err(EXIT_FAILURE
, "read u64 failed");
1212 printf("read: %s: %" PRIu64
"\n", file
, res
);
1214 if (ul_path_readf_s64(pc
, &res
, "%s", file
) != 0)
1215 err(EXIT_FAILURE
, "readf u64 failed");
1216 printf("readf: %s: %" PRIu64
"\n", file
, res
);
1218 } else if (strcmp(command
, "read-majmin") == 0) {
1222 errx(EXIT_FAILURE
, "<file> not defined");
1223 file
= argv
[optind
++];
1225 if (ul_path_read_majmin(pc
, &res
, file
) != 0)
1226 err(EXIT_FAILURE
, "read maj:min failed");
1227 printf("read: %s: %d\n", file
, (int) res
);
1229 if (ul_path_readf_majmin(pc
, &res
, "%s", file
) != 0)
1230 err(EXIT_FAILURE
, "readf maj:min failed");
1231 printf("readf: %s: %d\n", file
, (int) res
);
1233 } else if (strcmp(command
, "read-string") == 0) {
1237 errx(EXIT_FAILURE
, "<file> not defined");
1238 file
= argv
[optind
++];
1240 if (ul_path_read_string(pc
, &res
, file
) <= 0)
1241 err(EXIT_FAILURE
, "read string failed");
1242 printf("read: %s: %s\n", file
, res
);
1244 if (ul_path_readf_string(pc
, &res
, "%s", file
) <= 0)
1245 err(EXIT_FAILURE
, "readf string failed");
1246 printf("readf: %s: %s\n", file
, res
);
1248 } else if (strcmp(command
, "read-link") == 0) {
1252 errx(EXIT_FAILURE
, "<file> not defined");
1253 file
= argv
[optind
++];
1255 if (ul_path_readlink(pc
, res
, sizeof(res
), file
) < 0)
1256 err(EXIT_FAILURE
, "read symlink failed");
1257 printf("read: %s: %s\n", file
, res
);
1259 if (ul_path_readlinkf(pc
, res
, sizeof(res
), "%s", file
) < 0)
1260 err(EXIT_FAILURE
, "readf symlink failed");
1261 printf("readf: %s: %s\n", file
, res
);
1263 } else if (strcmp(command
, "write-string") == 0) {
1266 if (optind
+ 1 == argc
)
1267 errx(EXIT_FAILURE
, "<file> <string> not defined");
1268 file
= argv
[optind
++];
1269 str
= argv
[optind
++];
1271 if (ul_path_write_string(pc
, str
, file
) != 0)
1272 err(EXIT_FAILURE
, "write string failed");
1273 if (ul_path_writef_string(pc
, str
, "%s", file
) != 0)
1274 err(EXIT_FAILURE
, "writef string failed");
1276 } else if (strcmp(command
, "write-u64") == 0) {
1279 if (optind
+ 1 == argc
)
1280 errx(EXIT_FAILURE
, "<file> <num> not defined");
1281 file
= argv
[optind
++];
1282 num
= strtoumax(argv
[optind
++], NULL
, 0);
1284 if (ul_path_write_u64(pc
, num
, file
) != 0)
1285 err(EXIT_FAILURE
, "write u64 failed");
1286 if (ul_path_writef_u64(pc
, num
, "%s", file
) != 0)
1287 err(EXIT_FAILURE
, "writef u64 failed");
1291 return EXIT_SUCCESS
;
1293 #endif /* TEST_PROGRAM_PATH */