]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/path: add path_read_u64()
authorKarel Zak <kzak@redhat.com>
Mon, 5 Nov 2012 11:28:00 +0000 (12:28 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 23 Nov 2012 13:58:21 +0000 (14:58 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/path.h
lib/path.c

index cefaaa0a469787fc26d0997dc456c3dd67e36790..615d28491aad7d5f966d828426438785a38eb5ba 100644 (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)));
 
index f4118ccd3c3ab693752012c9454eeb54b5f9455c..4f955d91cda8eea92ce1bbcfd24f94386714f26d 100644 (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, ...)
 {