tadd compatibility for non-bsd systems - stopwatch - simple timer for console or x root window
git clone git://src.adamsgaard.dk/stopwatch
Log
Files
Refs
README
LICENSE
---
commit ee1e1c68dbfe3d6db034e8c6e06c8d260e169350
parent a29ecda577a623fa15841d77c519fe08465e0637
Author: Anders Damsgaard 
Date:   Thu, 28 Jan 2021 14:10:26 +0100

add compatibility for non-bsd systems

Diffstat:
  M Makefile                            |      11 ++++++++---
  A strlcpy.c                           |      49 +++++++++++++++++++++++++++++++
  A strtonum.c                          |      66 +++++++++++++++++++++++++++++++
  M timeutil.c                          |       4 ++--
  M timeutil.h                          |      11 +++++++++++

5 files changed, 136 insertions(+), 5 deletions(-)
---
diff --git a/Makefile b/Makefile
t@@ -20,7 +20,12 @@ LIBTIMEUTIL = libtimeutil.a
 LIBTIMEUTILSRC = timeutil.c
 LIBTIMEUTILOBJ = ${LIBTIMEUTILSRC:.c=.o}
 
-LIB = ${LIBTIMEUTIL}
+COMPATSRC =\
+        strlcpy.c\
+        strtonum.c
+COMPATOBJ = ${COMPATSRC:.c=.o}
+
+LIB = ${LIBTIMEUTIL} ${COMPATOBJ}
 
 X11INC = /usr/X11R6/include
 X11LIB = /usr/X11R6/lib
t@@ -40,7 +45,7 @@ all: ${BIN}
 
 ${BIN}: ${LIB} ${@:=.o}
 
-OBJ = ${SRC:.c=.o} ${LIBTIMEUTILOBJ}
+OBJ = ${SRC:.c=.o} ${LIBTIMEUTILOBJ} ${COMPATOBJ}
 
 ${OBJ}: ${HDR}
 
t@@ -57,7 +62,7 @@ ${LIBTIMEUTIL}: ${LIBTIMEUTILOBJ}
 dist:
         rm -rf "${NAME}-${VERSION}"
         mkdir -p "${NAME}-${VERSION}"
-        cp -f ${MAN1} ${DOC} ${SRC} ${LIBTIMEUTILSRC} Makefile "${NAME}-${VERSION}"
+        cp -f ${MAN1} ${DOC} ${SRC} ${LIBTIMEUTILSRC} ${COMPATSRC} Makefile "${NAME}-${VERSION}"
         tar cf - "${NAME}-${VERSION}" | gzip -c > "${NAME}-${VERSION}.tar.gz"
         rm -rf "${NAME}-${VERSION}"
 
diff --git a/strlcpy.c b/strlcpy.c
t@@ -0,0 +1,49 @@
+/*        $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $        */
+
+/*
+ * Copyright (c) 1998, 2015 Todd C. Miller 
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include 
+
+/*
+ * Copy string src to buffer dst of size dsize.  At most dsize-1
+ * chars will be copied.  Always NUL terminates (unless dsize == 0).
+ * Returns strlen(src); if retval >= dsize, truncation occurred.
+ */
+size_t
+strlcpy(char *dst, const char *src, size_t dsize)
+{
+        const char *osrc = src;
+        size_t nleft = dsize;
+
+        /* Copy as many bytes as will fit. */
+        if (nleft != 0) {
+                while (--nleft != 0) {
+                        if ((*dst++ = *src++) == '\0')
+                                break;
+                }
+        }
+
+        /* Not enough room in dst, add NUL and traverse rest of src. */
+        if (nleft == 0) {
+                if (dsize != 0)
+                        *dst = '\0';                /* NUL-terminate dst */
+                while (*src++)
+                        ;
+        }
+
+        return(src - osrc - 1);        /* count does not include NUL */
+}
diff --git a/strtonum.c b/strtonum.c
t@@ -0,0 +1,66 @@
+/*        $OpenBSD: strtonum.c,v 1.8 2015/09/13 08:31:48 guenther Exp $        */
+
+/*
+ * Copyright (c) 2004 Ted Unangst and Todd Miller
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include 
+#include 
+#include 
+
+#define        INVALID                1
+#define        TOOSMALL        2
+#define        TOOLARGE        3
+
+long long
+strtonum(const char *numstr, long long minval, long long maxval,
+    const char **errstrp)
+{
+        long long ll = 0;
+        int error = 0;
+        char *ep;
+        struct errval {
+                const char *errstr;
+                int err;
+        } ev[4] = {
+                { NULL,                0 },
+                { "invalid",        EINVAL },
+                { "too small",        ERANGE },
+                { "too large",        ERANGE },
+        };
+
+        ev[0].err = errno;
+        errno = 0;
+        if (minval > maxval) {
+                error = INVALID;
+        } else {
+                ll = strtoll(numstr, &ep, 10);
+                if (numstr == ep || *ep != '\0')
+                        error = INVALID;
+                else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
+                        error = TOOSMALL;
+                else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
+                        error = TOOLARGE;
+        }
+        if (errstrp != NULL)
+                *errstrp = ev[error].errstr;
+        errno = ev[error].err;
+        if (error)
+                ll = 0;
+
+        return (ll);
+}
+DEF_WEAK(strtonum);
diff --git a/timeutil.c b/timeutil.c
t@@ -2,9 +2,9 @@
 #include 
 
 void
-format_time(char *out, size_t outsiz, time_t t_elapsed, char *prefix, char *postfix)
+format_time(char *out, size_t outsiz, size_t t_elapsed, char *prefix, char *postfix)
 {
-        time_t h = 0, m = 0, s = 0;
+        size_t h = 0, m = 0, s = 0;
 
         h = t_elapsed / 3600;
         m = (t_elapsed % 3600) / 60;
diff --git a/timeutil.h b/timeutil.h
t@@ -1 +1,12 @@
+#ifndef __OpenBSD__
+#include 
+#define LINE_MAX 2048
+#endif
+
 void format_time(char *out, size_t outsiz, time_t t_elapsed, char *prefix, char *postfix);
+
+#undef strlcpy
+size_t strlcpy(char *, const char *, size_t);
+
+#undef strtonum
+long long strtonum(const char *, long long, long long, const char **);