summaryrefslogtreecommitdiff
path: root/tools-util.h
diff options
context:
space:
mode:
Diffstat (limited to 'tools-util.h')
-rw-r--r--tools-util.h54
1 files changed, 42 insertions, 12 deletions
diff --git a/tools-util.h b/tools-util.h
index 5c8ea13..09f00ef 100644
--- a/tools-util.h
+++ b/tools-util.h
@@ -1,10 +1,12 @@
#ifndef _TOOLS_UTIL_H
#define _TOOLS_UTIL_H
+#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
+#include <unistd.h>
#include <linux/byteorder.h>
#include <linux/kernel.h>
@@ -18,38 +20,66 @@ do { \
exit(EXIT_FAILURE); \
} while (0)
+static inline void *xcalloc(size_t count, size_t size)
+{
+ void *p = calloc(count, size);
+
+ if (!p)
+ die("insufficient memory");
+
+ return p;
+}
+
+static inline void *xmalloc(size_t size)
+{
+ void *p = malloc(size);
+
+ if (!p)
+ die("insufficient memory");
+
+ memset(p, 0, size);
+ return p;
+}
+
+static inline void xpread(int fd, void *buf, size_t count, off_t offset)
+{
+ ssize_t r = pread(fd, buf, count, offset);
+
+ if (r != count)
+ die("read error (ret %zi)", r);
+}
+
+static inline void xpwrite(int fd, const void *buf, size_t count, off_t offset)
+{
+ ssize_t r = pwrite(fd, buf, count, offset);
+
+ if (r != count)
+ die("write error (ret %zi err %s)", r, strerror(errno));
+}
+
enum units {
BYTES,
SECTORS,
HUMAN_READABLE,
};
-struct units_buf pr_units(u64, enum units);
+struct units_buf __pr_units(u64, enum units);
struct units_buf {
char b[20];
};
-long strtoul_or_die(const char *, size_t, const char *);
-
-u64 hatoi(const char *);
-unsigned hatoi_validate(const char *, const char *);
-unsigned nr_args(char * const *);
+#define pr_units(_v, _u) __pr_units(_v, _u).b
char *read_file_str(int, const char *);
u64 read_file_u64(int, const char *);
-ssize_t read_string_list(const char *, const char * const[]);
ssize_t read_string_list_or_die(const char *, const char * const[],
const char *);
-void print_string_list(const char * const[], size_t);
u64 get_size(const char *, int);
unsigned get_blocksize(const char *, int);
-#include "linux/bcache.h"
-#include "linux/bcache-ioctl.h"
-
int bcachectl_open(void);
struct bcache_handle {
@@ -59,6 +89,6 @@ struct bcache_handle {
struct bcache_handle bcache_fs_open(const char *);
-bool ask_proceed(void);
+bool ask_yn(void);
#endif /* _TOOLS_UTIL_H */