summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2013-12-31 23:03:47 +0000
committerIngo Schwarze <schwarze@openbsd.org>2013-12-31 23:03:47 +0000
commit00a83eb67618ef2d3d4efc692882efa3dc6252ca (patch)
tree59a7e5c44227c8d33e82baad01e4a6bcf77e7777
parentbe3cd034f5ca97ad2872d535487f43449fb84e8c (diff)
downloadmandoc-00a83eb67618ef2d3d4efc692882efa3dc6252ca.tar.gz
Provide strcasestr(3) and strsep(3) for Solaris 10 compat;
reported missing by Matthias Scheler <tron at NetBSD> via wiz@.
-rw-r--r--Makefile14
-rw-r--r--compat_strcasestr.c74
-rw-r--r--compat_strsep.c80
-rw-r--r--config.h.post6
-rw-r--r--test-strcasestr.c9
-rw-r--r--test-strsep.c10
6 files changed, 192 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 8e6a42df..5209ccf9 100644
--- a/Makefile
+++ b/Makefile
@@ -67,8 +67,10 @@ SRCS = Makefile \
chars.in \
compat_fgetln.c \
compat_getsubopt.c \
+ compat_strcasestr.c \
compat_strlcat.c \
compat_strlcpy.c \
+ compat_strsep.c \
config.h.post \
config.h.pre \
demandoc.1 \
@@ -190,8 +192,10 @@ LIBMANDOC_OBJS = $(LIBMAN_OBJS) \
COMPAT_OBJS = compat_fgetln.o \
compat_getsubopt.o \
compat_ohash.o \
+ compat_strcasestr.o \
compat_strlcat.o \
- compat_strlcpy.o
+ compat_strlcpy.o \
+ compat_strsep.o
arch.o: arch.in
att.o: att.in
@@ -386,6 +390,14 @@ config.h: config.h.pre config.h.post
echo '#define HAVE_STRLCPY'; \
rm test-strlcpy; \
fi; \
+ if $(CC) $(CFLAGS) -Werror -Wno-unused -o test-strcasestr test-strcasestr.c >> config.log 2>&1; then \
+ echo '#define HAVE_STRCASESTR'; \
+ rm test-strcasestr; \
+ fi; \
+ if $(CC) $(CFLAGS) -Werror -Wno-unused -o test-strsep test-strsep.c >> config.log 2>&1; then \
+ echo '#define HAVE_STRSEP'; \
+ rm test-strsep; \
+ fi; \
echo; \
cat config.h.post \
) > $@
diff --git a/compat_strcasestr.c b/compat_strcasestr.c
new file mode 100644
index 00000000..5216d021
--- /dev/null
+++ b/compat_strcasestr.c
@@ -0,0 +1,74 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_STRCASESTR
+
+int dummy;
+
+#else
+
+/* ($)NetBSD: strcasestr.c,v 1.2 2005/02/09 21:35:47 kleink Exp $ */
+
+/*-
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Chris Torek.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <string.h>
+
+#define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
+
+/*
+ * Find the first occurrence of find in s, ignore case.
+ */
+char *
+strcasestr(const char *s, const char *find)
+{
+ char c, sc;
+ size_t len;
+
+ if ((c = *find++) != 0) {
+ c = tolower((unsigned char)c);
+ len = strlen(find);
+ do {
+ do {
+ if ((sc = *s++) == 0)
+ return (NULL);
+ } while ((char)tolower((unsigned char)sc) != c);
+ } while (strncasecmp(s, find, len) != 0);
+ s--;
+ }
+ return __UNCONST(s);
+}
+
+#endif
diff --git a/compat_strsep.c b/compat_strsep.c
new file mode 100644
index 00000000..a5c58c62
--- /dev/null
+++ b/compat_strsep.c
@@ -0,0 +1,80 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_STRSEP
+
+int dummy;
+
+#else
+
+/* ($)OpenBSD: strsep.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */
+
+/*-
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Get next token from string *stringp, where tokens are possibly-empty
+ * strings separated by characters from delim.
+ *
+ * Writes NULs into the string at *stringp to end tokens.
+ * delim need not remain constant from call to call.
+ * On return, *stringp points past the last NUL written (if there might
+ * be further tokens), or is NULL (if there are definitely no more tokens).
+ *
+ * If *stringp is NULL, strsep returns NULL.
+ */
+char *
+strsep(char **stringp, const char *delim)
+{
+ char *s;
+ const char *spanp;
+ int c, sc;
+ char *tok;
+
+ if ((s = *stringp) == NULL)
+ return (NULL);
+ for (tok = s;;) {
+ c = *s++;
+ spanp = delim;
+ do {
+ if ((sc = *spanp++) == c) {
+ if (c == 0)
+ s = NULL;
+ else
+ s[-1] = 0;
+ *stringp = s;
+ return (tok);
+ }
+ } while (sc != 0);
+ }
+ /* NOTREACHED */
+}
+
+#endif
diff --git a/config.h.post b/config.h.post
index d04dac99..0a700426 100644
--- a/config.h.post
+++ b/config.h.post
@@ -15,12 +15,18 @@
# endif
#endif
+#ifndef HAVE_STRCASESTR
+extern char *strcasestr(const char *, const char *);
+#endif
#ifndef HAVE_STRLCAT
extern size_t strlcat(char *, const char *, size_t);
#endif
#ifndef HAVE_STRLCPY
extern size_t strlcpy(char *, const char *, size_t);
#endif
+#ifndef HAVE_STRSEP
+extern char *strsep(char **, const char *);
+#endif
#ifndef HAVE_GETSUBOPT
extern int getsubopt(char **, char * const *, char **);
extern char *suboptarg;
diff --git a/test-strcasestr.c b/test-strcasestr.c
new file mode 100644
index 00000000..2ffb420b
--- /dev/null
+++ b/test-strcasestr.c
@@ -0,0 +1,9 @@
+#include <string.h>
+
+int
+main(void)
+{
+ const char *big = "BigString";
+ char *cp = strcasestr(big, "Gst");
+ return(big + 2 != cp);
+}
diff --git a/test-strsep.c b/test-strsep.c
new file mode 100644
index 00000000..4c57c13e
--- /dev/null
+++ b/test-strsep.c
@@ -0,0 +1,10 @@
+#include <string.h>
+
+int
+main(void)
+{
+ char buf[6] = "aybxc";
+ char *workp = buf;
+ char *retp = strsep(&workp, "xy");
+ return( ! (retp == buf && '\0' == buf[1] && buf + 2 == workp));
+}