[PATCH 03/65] add util functions from sbase

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Wed, 16 Apr 2014 17:02:28 +0200

Signed-off-by: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
---
 ii.c           |  1 +
 util.h         | 13 ++++++++++++
 util/eprintf.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 util/estrtol.c | 27 +++++++++++++++++++++++
 util/strlcpy.c | 32 ++++++++++++++++++++++++++++
 5 files changed, 140 insertions(+)
 create mode 100644 util.h
 create mode 100644 util/eprintf.c
 create mode 100644 util/estrtol.c
 create mode 100644 util/strlcpy.c
diff --git a/ii.c b/ii.c
index b503e1d..b710145 100644
--- a/ii.c
+++ b/ii.c
_AT_@ -17,6 +17,7 @@
 #include <time.h>
 #include <unistd.h>
 
+#include "util.h"
 #ifndef PIPE_BUF /* FreeBSD don't know PIPE_BUF */
 #define PIPE_BUF 4096
 #endif
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..df6a3d8
--- /dev/null
+++ b/util.h
_AT_@ -0,0 +1,13 @@
+/* See LICENSE file for copyright and license details. */
+#include <stddef.h>
+#include <sys/stat.h>
+#include "arg.h"
+
+extern char *argv0;
+
+void eprintf(const char *, ...);
+void enprintf(int, const char *, ...);
+long estrtol(const char *, int);
+#undef strlcpy
+size_t strlcpy(char *, const char *, size_t);
+void weprintf(const char *, ...);
diff --git a/util/eprintf.c b/util/eprintf.c
new file mode 100644
index 0000000..91b19b7
--- /dev/null
+++ b/util/eprintf.c
_AT_@ -0,0 +1,67 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../util.h"
+
+char *argv0;
+
+static void venprintf(int, const char *, va_list);
+
+void
+eprintf(const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	venprintf(EXIT_FAILURE, fmt, ap);
+	va_end(ap);
+}
+
+void
+enprintf(int status, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	venprintf(status, fmt, ap);
+	va_end(ap);
+}
+
+void
+venprintf(int status, const char *fmt, va_list ap)
+{
+#ifdef DEBUG
+	fprintf(stderr, "%s: ", argv0);
+#endif
+
+	vfprintf(stderr, fmt, ap);
+
+	if(fmt[0] && fmt[strlen(fmt)-1] == ':') {
+		fputc(' ', stderr);
+		perror(NULL);
+	}
+
+	exit(status);
+}
+
+void
+weprintf(const char *fmt, ...)
+{
+	va_list ap;
+
+#ifdef DEBUG
+	fprintf(stderr, "%s: ", argv0);
+#endif
+
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+
+	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
+		fputc(' ', stderr);
+		perror(NULL);
+	}
+}
diff --git a/util/estrtol.c b/util/estrtol.c
new file mode 100644
index 0000000..ac37b8c
--- /dev/null
+++ b/util/estrtol.c
_AT_@ -0,0 +1,27 @@
+/* See LICENSE file for copyright and license details. */
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "../util.h"
+
+long
+estrtol(const char *s, int base)
+{
+	char *end;
+	long n;
+
+	errno = 0;
+	n = strtol(s, &end, base);
+	if(*end != '\0') {
+		if(base == 0)
+			eprintf("%s: not an integer\n", s);
+		else
+			eprintf("%s: not a base %d integer\n", s, base);
+	}
+	if(errno != 0)
+		eprintf("%s:", s);
+
+	return n;
+}
+
diff --git a/util/strlcpy.c b/util/strlcpy.c
new file mode 100644
index 0000000..62fb7f6
--- /dev/null
+++ b/util/strlcpy.c
_AT_@ -0,0 +1,32 @@
+/* Taken from OpenBSD */
+#include <sys/types.h>
+#include <string.h>
+#include "../util.h"
+
+/*
+ * Copy src to string dst of size siz.  At most siz-1 characters
+ * will be copied.  Always NUL terminates (unless siz == 0).
+ * Returns strlen(src); if retval >= siz, truncation occurred.
+ */
+size_t
+strlcpy(char *dst, const char *src, size_t siz)
+{
+        char *d = dst;
+        const char *s = src;
+        size_t n = siz;
+        /* Copy as many bytes as will fit */
+        if (n != 0) {
+                while (--n != 0) {
+                        if ((*d++ = *s++) == '\0')
+                                break;
+                }
+        }
+        /* Not enough room in dst, add NUL and traverse rest of src */
+        if (n == 0) {
+                if (siz != 0)
+                        *d = '\0';              /* NUL-terminate dst */
+                while (*s++)
+                        ;
+        }
+        return(s - src - 1);    /* count does not include NUL */
+}
-- 
2.4.10
--Multipart=_Mon__9_May_2016_17_21_10_+0200_I.6cpFVydhq75aaE
Content-Type: text/x-diff;
 name="0004-add-arg.h.patch"
Content-Disposition: attachment;
 filename="0004-add-arg.h.patch"
Content-Transfer-Encoding: 7bit
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon May 09 2016 - 17:24:22 CEST