commit a1fa37b0737ca364df053cf246891c4fe723670a
parent e710d47a98ac2003a535a8663584b7fc659b9051
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Tue, 31 Aug 2021 11:25:26 +0200
put compatibility functions into separate files
Diffstat:
M | Makefile | | | 21 | ++++++++++++++------- |
M | rangetest.c | | | 27 | ++------------------------- |
A | strlcpy.c | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | strnlen.c | | | 32 | ++++++++++++++++++++++++++++++++ |
A | util.h | | | 17 | +++++++++++++++++ |
5 files changed, 115 insertions(+), 32 deletions(-)
diff --git a/Makefile b/Makefile
@@ -21,14 +21,22 @@ BIN =\
SRC = ${BIN:=.c}
+COMPATSRC =\
+ strnlen.c\
+ strlcpy.c\
+
+COMPATOBJ = ${COMPATSRC:.c=.o}
+
HDR =\
arg.h\
+ util.h\
+LIB = ${COMPATOBJ}
LIBS = -lm
_CFLAGS = ${CFLAGS} ${INCS} -DVERSION=\"${VERSION}\"
_LDFLAGS = ${LDFLAGS} ${LIBS}
-_CPPFLAGS = ${CPPFLAGS}
+_CPPFLAGS = ${CPPFLAGS} -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=700 -D_BSD_SOURCE
MAN1 = ${BIN:=.1} ${SCRIPTS:=.1}
DOC =\
@@ -37,19 +45,18 @@ DOC =\
all: ${BIN}
-${BIN}: ${@:=.o}
+${BIN}: ${LIB} ${@:=.o}
-OBJ = ${SRC:.c=.o}
+OBJ = ${SRC:.c=.o} ${COMPATOBJ}
${OBJ}: ${HDR}
.o:
- ${CC} -o $@ $< ${_LDFLAGS}
+ ${CC} -o $@ $< ${_LDFLAGS} ${LIB}
.c.o:
${CC} ${_CFLAGS} ${_CPPFLAGS} -o $@ -c $<
-
install:
# installing executable files and scripts.
mkdir -p "${DESTDIR}${PREFIX}/bin"
@@ -76,13 +83,13 @@ uninstall:
dist:
rm -rf "${NAME}-${VERSION}"
mkdir -p "${NAME}-${VERSION}"
- cp -rf ${MAN1} ${DOC} ${BIN} ${SCRIPTS} "${NAME}-${VERSION}"
+ cp -rf ${MAN1} ${DOC} ${SRC} ${COMPATSRC} ${SCRIPTS} "${NAME}-${VERSION}"
# make tarball
tar cf - "${NAME}-${VERSION}" | \
gzip -c > "${NAME}-${VERSION}.tar.gz"
rm -rf "${NAME}-${VERSION}"
clean:
- rm -f ${BIN} ${OBJ}
+ rm -f ${BIN} ${OBJ} ${LIB}
.PHONY: install uninstall dist clean
diff --git a/rangetest.c b/rangetest.c
@@ -5,35 +5,12 @@
#include <string.h>
#include "arg.h"
+#include "util.h"
#define VALUESTR "@VAL@"
char *argv0;
-#ifdef NEED_STRLCPY /* OpenBSD implementation */
-size_t
-strlcpy(char *dst, const char *src, size_t dsize) {
- const char *osrc = src;
- size_t nleft = dsize;
-
- if (nleft != 0) {
- while (--nleft != 0) {
- if ((*dst++= *src++) == '\0')
- break;
- }
- }
-
- if (nleft == 0) {
- if (dsize != 0)
- *dst = '\0';
- while (*src++)
- ;
- }
-
- return(src - osrc - 1);
-}
-#endif /* NEED_STRLCPY */
-
static void
usage(void)
{
@@ -66,7 +43,7 @@ static void
binary_search(char *cmd, char *cmd0, double minv, double maxv, int maxiter)
{
int minfail, maxfail;
- int iter;
+ int iter = 0;
double val;
minfail = launch(cmd, cmd0, minv);
diff --git a/strlcpy.c b/strlcpy.c
@@ -0,0 +1,50 @@
+/* $OpenBSD: strlcpy.c,v 1.16 2019/01/25 00:19:25 millert Exp $ */
+
+/*
+ * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
+ *
+ * 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 <sys/types.h>
+#include <string.h>
+
+/*
+ * 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/strnlen.c b/strnlen.c
@@ -0,0 +1,32 @@
+/* $OpenBSD: strnlen.c,v 1.9 2019/01/25 00:19:25 millert Exp $ */
+
+/*
+ * Copyright (c) 2010 Todd C. Miller <millert@openbsd.org>
+ *
+ * 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 <sys/types.h>
+
+#include <string.h>
+
+size_t
+strnlen(const char *str, size_t maxlen)
+{
+ const char *cp;
+
+ for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
+ ;
+
+ return (size_t)(cp - str);
+}
diff --git a/util.h b/util.h
@@ -0,0 +1,17 @@
+#ifndef UTIL_H_
+#define UTIL_H_
+
+#ifdef __OpenBSD__
+#include <unistd.h>
+#else
+#define pledge(p1, p2) 0
+#define unveil(p1, p2) 0
+#endif
+
+#undef strlcpy
+size_t strlcpy(char *dst, const char *src, size_t dsize);
+
+#undef strnlen
+size_t strnlen(const char *str, size_t maxlen);
+
+#endif