lib/path: add path_read_u64()

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2012-11-05 12:28:00 +01:00
parent ca01695b77
commit 37a5c7ee41
2 changed files with 27 additions and 1 deletions

View File

@ -2,6 +2,7 @@
#define UTIL_LINUX_PATH_H
#include <stdio.h>
#include <stdint.h>
extern FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...)
__attribute__ ((__format__ (__printf__, 3, 4)));
@ -10,7 +11,10 @@ extern void path_read_str(char *result, size_t len, const char *path, ...)
extern int path_write_str(const char *str, const char *path, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));
extern int path_read_s32(const char *path, ...)
__attribute__ ((__format__ (__printf__, 1, 2)));
__attribute__ ((__format__ (__printf__, 1, 2)));
extern uint64_t path_read_u64(const char *path, ...)
__attribute__ ((__format__ (__printf__, 1, 2)));
extern int path_exist(const char *path, ...)
__attribute__ ((__format__ (__printf__, 1, 2)));

View File

@ -27,6 +27,7 @@
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <inttypes.h>
#include <errno.h>
#include "all-io.h"
@ -125,6 +126,27 @@ path_read_s32(const char *path, ...)
return result;
}
uint64_t
path_read_u64(const char *path, ...)
{
FILE *fd;
va_list ap;
uint64_t result;
va_start(ap, path);
fd = path_vfopen("r", 1, path, ap);
va_end(ap);
if (fscanf(fd, "%"SCNu64, &result) != 1) {
if (ferror(fd))
err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
else
errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
}
fclose(fd);
return result;
}
int
path_write_str(const char *str, const char *path, ...)
{