aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2016-11-24 21:06:24 +0000
committerTomasz Kramkowski <tk@the-tk.com>2016-11-24 21:06:24 +0000
commitc4c3178f2bc2b85b1d3af928f3e841bd6eb43e35 (patch)
treea5895b416896b3076796fa1a9ed66f0d897cdfc7
parent4b56f68e9876be3c42b93be1600cfe6d96ca1530 (diff)
downloadhktool-c4c3178f2bc2b85b1d3af928f3e841bd6eb43e35.tar.gz
hktool-c4c3178f2bc2b85b1d3af928f3e841bd6eb43e35.tar.xz
hktool-c4c3178f2bc2b85b1d3af928f3e841bd6eb43e35.zip
eprintf utilities
-rw-r--r--Makefile2
-rw-r--r--eprintf.c106
-rw-r--r--eprintf.h15
-rw-r--r--hktool.c2
4 files changed, 124 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index f1abf21..3c203df 100644
--- a/Makefile
+++ b/Makefile
@@ -15,7 +15,7 @@ CFLAGS = -std=c11 -O2 -flto $(WARNINGS) -MMD -MP $(shell $(PKG_CONFIG) --cflags
LDFLAGS = -Wl,--as-needed -O2 -flto
LDLIBS = $(shell $(PKG_CONFIG) --libs $(LIBS))
-OBJ := hktool.o halfkay.o log.o params.o util.o
+OBJ := hktool.o halfkay.o log.o params.o util.o eprintf.o
PREFIX ?= /usr/local
diff --git a/eprintf.c b/eprintf.c
new file mode 100644
index 0000000..7a885b3
--- /dev/null
+++ b/eprintf.c
@@ -0,0 +1,106 @@
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "eprintf.h"
+
+char *pname;
+
+/* eprintf: print error and exit, a trailing : in fmt prints strerror(errno) */
+void eprintf(const char *fmt, ...)
+{
+ int errnum = errno;
+ va_list va;
+
+ if (progname() != NULL)
+ fprintf(stderr, "%s: ", progname());
+
+ va_start(va, fmt);
+ vfprintf(stderr, fmt, va);
+ va_end(va);
+
+ if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
+ fprintf(stderr, " %s", strerror(errnum));
+ fputc('\n', stderr);
+
+ exit(EXIT_FAILURE);
+}
+
+/* weprintf: print warning, a trailing : in fmt prints strerror(errno) */
+void weprintf(const char *fmt, ...)
+{
+ int errnum = errno;
+ va_list va;
+
+ if (progname() != NULL)
+ fprintf(stderr, "%s: ", progname());
+
+ va_start(va, fmt);
+ vfprintf(stderr, fmt, va);
+ va_end(va);
+
+ if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
+ fprintf(stderr, " %s", strerror(errnum));
+ fputc('\n', stderr);
+}
+
+/* estrdup: attempt to strdup a string or exit on error */
+char *estrdup(const char *s)
+{
+ size_t len;
+ char *dup;
+
+ len = strlen(s);
+ if (len == SIZE_MAX)
+ eprintf("estrdup(\"%.20s\") failed: Too long", s);
+ dup = malloc(len + 1);
+ if (dup == NULL)
+ eprintf("estrdup(\"%.20s\") failed:", s);
+ strcpy(dup, s);
+
+ return dup;
+}
+
+/* emalloc: attempt to allocate memory or exit on error */
+void *emalloc(size_t n)
+{
+ void *p;
+
+ p = malloc(n);
+ if (p == NULL)
+ eprintf("emalloc(%zu) failed:", n);
+
+ return p;
+}
+
+/* erealloc: attempt to reallocate memory or exit on error */
+void *erealloc(void *p, size_t n)
+{
+ void *r;
+
+ r = realloc(p, n);
+ if (r == NULL)
+ eprintf("erealloc(%p, %zu) failed:", p, n);
+
+ return r;
+}
+
+/* progname: retrieve stored name of program */
+char *progname(void)
+{
+ return pname;
+}
+
+/* setprogname: set stored name of program */
+void setprogname(const char *s)
+{
+ if (pname != NULL) {
+ weprintf("setprogname(\"%.20s\") called more than once", s);
+ return;
+ }
+
+ pname = estrdup(s);
+}
diff --git a/eprintf.h b/eprintf.h
new file mode 100644
index 0000000..aecc3a6
--- /dev/null
+++ b/eprintf.h
@@ -0,0 +1,15 @@
+#ifndef EPRINTF_H
+#define EPRINTF_H
+#include <stdnoreturn.h>
+
+noreturn void eprintf(const char *fmt, ...)
+ __attribute__((format(printf, 1, 2)));
+void weprintf(const char *fmt, ...)
+ __attribute__((format(printf, 1, 2)));
+char *estrdup(const char *);
+void *emalloc(size_t);
+void *erealloc(void *, size_t);
+char *progname(void);
+void setprogname(const char *);
+
+#endif /* !EPRINTF_H */
diff --git a/hktool.c b/hktool.c
index f6c976b..a9bef02 100644
--- a/hktool.c
+++ b/hktool.c
@@ -25,6 +25,7 @@
#include "halfkay.h"
#include "log.h"
+#include "eprintf.h"
#include "params.h"
#ifndef VERSION
@@ -67,6 +68,7 @@ int main(int argc, char **argv)
struct flashparams fp;
argv0 = argv[0] != NULL ? argv[0] : "hktool";
+ setprogname(argv0);
while (opt = getopt(argc, argv, "f:h-lvr"), opt != -1) {
switch (opt) {