compat: add strlcpy, strlcat, for glibc
6 files changed, 119 insertions(+), 4 deletions(-) | |||
---|---|---|---|
M | Makefile | +7 | -4 |
M | config.mk | +5 | -0 |
A | strlcat.c | +55 | -0 |
A | strlcpy.c | +50 | -0 |
M | urmoms-index.c | +1 | -0 |
M | urmoms.c | +1 | -0 |
1@@ -5,6 +5,9 @@ VERSION = 0.1
2 SRC = \
3 urmoms.c\
4 urmoms-index.c
5+COMPATSRC = \
6+ strlcat.c\
7+ strlcpy.c
8 BIN = \
9 urmoms\
10 urmoms-index
11@@ -15,9 +18,9 @@ DOC = \
12 LICENSE\
13 README\
14 TODO
15-HDR =
16+HDR = compat.h
17
18-OBJ = ${SRC:.c=.o}
19+OBJ = ${SRC:.c=.o} ${EXTRAOBJ}
20
21 all: $(BIN)
22
23@@ -42,10 +45,10 @@ config.h:
24 @echo creating $@ from config.def.h
25 @cp config.def.h $@
26
27-urmoms: urmoms.o
28+urmoms: urmoms.o ${EXTRAOBJ}
29 ${CC} -o $@ urmoms.o ${LDFLAGS}
30
31-urmoms-index: urmoms-index.o
32+urmoms-index: urmoms-index.o ${EXTRAOBJ}
33 ${CC} -o $@ urmoms-index.o ${LDFLAGS}
34
35 clean:
M · config.mk
+5, -0 1@@ -26,5 +26,10 @@ LDFLAGS = ${LIBS}
2 # -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -D_BSD_SOURCE ${INCS}
3 #LDFLAGS = -static -s ${LIBS}
4
5+# uncomment for compat
6+CFLAGS += -DCOMPAT
7+# uncomment if your libc doesn't support strlcat, strlcpy.
8+EXTRAOBJ = strlcat.o strlcpy.o
9+
10 # compiler and linker
11 #CC = cc
A · strlcat.c
+55, -0 1@@ -0,0 +1,55 @@
2+/* $OpenBSD: strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */
3+
4+/*
5+ * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
6+ *
7+ * Permission to use, copy, modify, and distribute this software for any
8+ * purpose with or without fee is hereby granted, provided that the above
9+ * copyright notice and this permission notice appear in all copies.
10+ *
11+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18+ */
19+
20+#include <sys/types.h>
21+#include <string.h>
22+
23+/*
24+ * Appends src to string dst of size dsize (unlike strncat, dsize is the
25+ * full size of dst, not space left). At most dsize-1 characters
26+ * will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
27+ * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
28+ * If retval >= dsize, truncation occurred.
29+ */
30+size_t
31+strlcat(char *dst, const char *src, size_t dsize)
32+{
33+ const char *odst = dst;
34+ const char *osrc = src;
35+ size_t n = dsize;
36+ size_t dlen;
37+
38+ /* Find the end of dst and adjust bytes left but don't go past end. */
39+ while (n-- != 0 && *dst != '\0')
40+ dst++;
41+ dlen = dst - odst;
42+ n = dsize - dlen;
43+
44+ if (n-- == 0)
45+ return(dlen + strlen(src));
46+ while (*src != '\0') {
47+ if (n != 0) {
48+ *dst++ = *src;
49+ n--;
50+ }
51+ src++;
52+ }
53+ *dst = '\0';
54+
55+ return(dlen + (src - osrc)); /* count does not include NUL */
56+}
A · strlcpy.c
+50, -0 1@@ -0,0 +1,50 @@
2+/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */
3+
4+/*
5+ * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
6+ *
7+ * Permission to use, copy, modify, and distribute this software for any
8+ * purpose with or without fee is hereby granted, provided that the above
9+ * copyright notice and this permission notice appear in all copies.
10+ *
11+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18+ */
19+
20+#include <sys/types.h>
21+#include <string.h>
22+
23+/*
24+ * Copy string src to buffer dst of size dsize. At most dsize-1
25+ * chars will be copied. Always NUL terminates (unless dsize == 0).
26+ * Returns strlen(src); if retval >= dsize, truncation occurred.
27+ */
28+size_t
29+strlcpy(char *dst, const char *src, size_t dsize)
30+{
31+ const char *osrc = src;
32+ size_t nleft = dsize;
33+
34+ /* Copy as many bytes as will fit. */
35+ if (nleft != 0) {
36+ while (--nleft != 0) {
37+ if ((*dst++ = *src++) == '\0')
38+ break;
39+ }
40+ }
41+
42+ /* Not enough room in dst, add NUL and traverse rest of src. */
43+ if (nleft == 0) {
44+ if (dsize != 0)
45+ *dst = '\0'; /* NUL-terminate dst */
46+ while (*src++)
47+ ;
48+ }
49+
50+ return(src - osrc - 1); /* count does not include NUL */
51+}
M · urmoms-index.c
+1, -01@@ -12,6 +12,7 @@
2
3 #include <git2.h>
4
5+#include "compat.h"
6 #include "config.h"
7
8 static git_repository *repo;
M · urmoms.c
+1, -01@@ -12,6 +12,7 @@
2
3 #include <git2.h>
4
5+#include "compat.h"
6 #include "config.h"
7
8 struct commitinfo {