Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 1 commit
  • 4 files changed
  • 0 comments
  • 1 contributor
4  runawk/Makefile
@@ -28,6 +28,10 @@ SRCS =		runawk.c dynarray.c file_hier.c
28 28
 
29 29
 MAN =		runawk.1
30 30
 
  31
+MKC_COMMON_HEADERS  = string.h
  32
+MKC_SOURCE_FUNCLIBS = strlcat strlcpy
  33
+MKC_CHECK_FUNCS3    = strlcat strlcpy
  34
+
31 35
 CFLAGS +=	-DAWK_PROG='"${AWK_PROG}"'
32 36
 CFLAGS +=	-DSTDIN_FILENAME='"${STDIN_FILENAME}"'
33 37
 CFLAGS +=	-DMODULESDIR='"${MODULESDIR}:${MODULESDIR}/gawk"'
8  runawk/file_hier.c
@@ -30,6 +30,14 @@
30 30
 
31 31
 #include "file_hier.h"
32 32
 
  33
+#if HAVE_FUNC3_STRLCPY
  34
+size_t strlcpy(char *dst, const char *src, size_t size);
  35
+#endif
  36
+
  37
+#if HAVE_FUNC3_STRLCAT
  38
+size_t strlcat(char *dst, const char *src, size_t size);
  39
+#endif
  40
+
33 41
 void file_hier (
34 42
 	const char *dir,
35 43
 	void (*proc_file) (const char *fn),
57  runawk/strlcat.c
... ...
@@ -0,0 +1,57 @@
  1
+/*
  2
+ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  3
+ *
  4
+ * Permission to use, copy, modify, and distribute this software for any
  5
+ * purpose with or without fee is hereby granted, provided that the above
  6
+ * copyright notice and this permission notice appear in all copies.
  7
+ *
  8
+ * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
  9
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  10
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
  11
+ * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15
+ */
  16
+
  17
+#include <assert.h>
  18
+#include <string.h>
  19
+
  20
+/*
  21
+ * Appends src to string dst of size siz (unlike strncat, siz is the
  22
+ * full size of dst, not space left).  At most siz-1 characters
  23
+ * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
  24
+ * Returns strlen(src) + MIN(siz, strlen(initial dst)).
  25
+ * If retval >= siz, truncation occurred.
  26
+ */
  27
+size_t
  28
+strlcat (char *dst, const char *src, size_t siz)
  29
+{
  30
+	char *d = dst;
  31
+	const char *s = src;
  32
+	size_t n = siz;
  33
+	size_t dlen;
  34
+
  35
+	assert (dst != NULL);
  36
+	assert (src != NULL);
  37
+
  38
+	/* Find the end of dst and adjust bytes left but don't go past end */
  39
+	while (n-- != 0 && *d != '\0')
  40
+		d++;
  41
+	dlen = d - dst;
  42
+	n = siz - dlen;
  43
+
  44
+	if (n == 0)
  45
+		return (dlen + strlen(s));
  46
+
  47
+	while (*s != '\0'){
  48
+		if (n != 1){
  49
+			*d++ = *s;
  50
+			n--;
  51
+		}
  52
+		s++;
  53
+	}
  54
+	*d = '\0';
  55
+
  56
+	return (dlen + (s - src));	/* count does not include NUL */
  57
+}
52  runawk/strlcpy.c
... ...
@@ -0,0 +1,52 @@
  1
+/*
  2
+ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  3
+ *
  4
+ * Permission to use, copy, modify, and distribute this software for any
  5
+ * purpose with or without fee is hereby granted, provided that the above
  6
+ * copyright notice and this permission notice appear in all copies.
  7
+ *
  8
+ * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
  9
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  10
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
  11
+ * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15
+ */
  16
+
  17
+#include <assert.h>
  18
+#include <string.h>
  19
+
  20
+/*
  21
+ * Copy src to string dst of size siz.  At most siz-1 characters
  22
+ * will be copied.  Always NUL terminates (unless siz == 0).
  23
+ * Returns strlen(src); if retval >= siz, truncation occurred.
  24
+ */
  25
+size_t
  26
+strlcpy(char *dst, const char *src, size_t siz)
  27
+{
  28
+	char *d = dst;
  29
+	const char *s = src;
  30
+	size_t n = siz;
  31
+
  32
+	assert (dst != NULL);
  33
+	assert (src != NULL);
  34
+
  35
+	/* Copy as many bytes as will fit */
  36
+	if (n != 0 && --n != 0){
  37
+		do{
  38
+			if ((*d++ = *s++) == 0)
  39
+				break;
  40
+		}while (--n != 0);
  41
+	}
  42
+
  43
+	/* Not enough room in dst, add NUL and traverse rest of src */
  44
+	if (n == 0){
  45
+		if (siz != 0)
  46
+			*d = '\0';		/* NUL-terminate dst */
  47
+		while (*s++)
  48
+			;
  49
+	}
  50
+
  51
+	return (s - src - 1);	/* count does not include NUL */
  52
+}

No commit comments for this range

Something went wrong with that request. Please try again.