about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--README12
-rw-r--r--example.c6
-rw-r--r--libste.324
-rw-r--r--ste.h1
-rw-r--r--stecpe.c2
-rw-r--r--stecpy.c11
-rw-r--r--tests.c25
8 files changed, 67 insertions, 18 deletions
diff --git a/Makefile b/Makefile
index 0be968b..71f87bd 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,8 @@
+CFLAGS=-g -O2 -Wall -Wno-switch -Wextra -Wwrite-strings
+
 all: libste.a example
 
-libste.a: stechr.o stecpe.o stecpy.o steprn.o
+libste.a: stechr.o stecpe.o stecpy.o steccpy.o steprn.o
 	$(AR) $(ARFLAGS) $@ $^
 
 example: example.o libste.a
diff --git a/README b/README
index 79a04b8..5776228 100644
--- a/README
+++ b/README
@@ -10,6 +10,9 @@ SYNOPSIS
      stecpy(char *dst, char *end, const char *src);
 
      char *
+     steccpy(char *dst, char *end, const char *src, int c);
+
+     char *
      stecpe(char *dst, char *end, const char *src, const char *srcend);
 
      char *
@@ -19,13 +22,18 @@ SYNOPSIS
      steprn(char *dst, char *end, const char *fmt, ...);
 
 DESCRIPTION
-     libste provides four useful functions for dealing with strings.
+     libste provides five useful functions for dealing with strings.
 
      stecpy copies the NUL-terminated string src to dst, but writes no
      characters beyond end.  If any characters are copied, dst will be NUL-
      terminated and the return value is a pointer to the NUL byte.  On
      truncation, end is returned.
 
+     steccpy copies the NUL-terminated string src to dst, stopping when the
+     character c is found.  It writes no characters beyond end.  If any
+     characters are copied, dst will be NUL-terminated and the return value is
+     a pointer to the NUL byte.  On truncation, end is returned.
+
      stecpe copies the string between src and srcend to dst, but writes no
      characters beyond end.  If any characters are copied, dst will be NUL-
      terminated and the return value is a pointer to the NUL byte.  On
@@ -33,7 +41,7 @@ DESCRIPTION
 
      stechr returns a pointer to the first occurence of c (converted to a
      char) in the NUL-terminated string pointed to by src, but reads no
-     characters beyond end.  If c is not found, stecpy returns a pointer to
+     characters beyond end.  If c is not found, stechr returns a pointer to
      the first NUL byte in src, or end if none was found.
 
      steprn uses vsnprintf(3) to write formatted output to dst, but writes no
diff --git a/example.c b/example.c
index e9f57d5..0429663 100644
--- a/example.c
+++ b/example.c
@@ -11,13 +11,13 @@
 int
 main(int argc, char *argv[])
 {
-	char *path = getenv("PATH");
+	const char *path = getenv("PATH");
 	if (!path)
 	    path = "";
 
-	char *program = argc > 1 ? argv[1] : "xyzzy";
+	const char *program = argc > 1 ? argv[1] : "xyzzy";
 
-	char *pathend = path + strlen(path);
+	const char *pathend = path + strlen(path);
 
 	char buf[PATH_MAX];
 	char *bufend = buf + sizeof buf;
diff --git a/libste.3 b/libste.3
index 0aadb70..994d12a 100644
--- a/libste.3
+++ b/libste.3
@@ -12,6 +12,8 @@
 .Ft "char *"
 .Fn stecpy "char *dst" "char *end" "const char *src"
 .Ft "char *"
+.Fn steccpy "char *dst" "char *end" "const char *src" "int c"
+.Ft "char *"
 .Fn stecpe "char *dst" "char *end" "const char *src" "const char *srcend"
 .Ft "char *"
 .Fn stechr "const char *src" "const char *end" "int c"
@@ -19,7 +21,7 @@
 .Fn steprn "char *dst" "char *end" "const char *fmt" "..."
 .Sh DESCRIPTION
 .Nm libste
-provides four useful functions for dealing with strings.
+provides five useful functions for dealing with strings.
 .Pp
 .Nm stecpy
 copies the NUL-terminated string
@@ -36,6 +38,24 @@ On truncation,
 .Fa end
 is returned.
 .Pp
+.Nm steccpy
+copies the NUL-terminated string
+.Fa src
+to
+.Fa dst ,
+stopping when the character
+.Fa c
+is found.
+It writes no characters beyond
+.Fa end .
+If any characters are copied,
+.Fa dst
+will be NUL-terminated
+and the return value is a pointer to the NUL byte.
+On truncation,
+.Fa end
+is returned.
+.Pp
 .Nm stecpe
 copies the string between
 .Fa src
@@ -64,7 +84,7 @@ but reads no characters beyond
 If
 .Fa c
 is not found,
-.Nm
+.Nm stechr
 returns a pointer to the first NUL byte in
 .Fa src ,
 or
diff --git a/ste.h b/ste.h
index b2ff64e..ed8fc53 100644
--- a/ste.h
+++ b/ste.h
@@ -1,4 +1,5 @@
 char *stecpy(char *dst, char *end, const char *src);
+char *steccpy(char *dst, char *end, const char *src, int c);
 char *stecpe(char *dst, char *end, const char *src, const char *srcend);
 char *stechr(const char *src, const char *end, int c);
 char *steprn(char *dst, char *end, const char *fmt, ...);
diff --git a/stecpe.c b/stecpe.c
index 0cfbaad..b943843 100644
--- a/stecpe.c
+++ b/stecpe.c
@@ -7,7 +7,7 @@ stecpe(char *dst, const char *end, const char *src, const char *srcend)
         if (dst >= end)
                 return dst;
 
-	size_t l = end - dst - 1;
+	ptrdiff_t l = end - dst - 1;
 	size_t t = 1;
 	if (srcend - src < l) {
 		l = srcend - src;
diff --git a/stecpy.c b/stecpy.c
index ac521ab..2fec72d 100644
--- a/stecpy.c
+++ b/stecpy.c
@@ -1,14 +1,11 @@
-#include <stddef.h>
-
 char *
 stecpy(char *dst, char *end, const char *src)
 {
-        if (dst >= end)
-                return dst;
+	if (dst >= end)
+		return dst;
 
-	size_t n = end - dst;
-	while (n && (*dst = *src))
-		n--, src++, dst++;
+	while (dst < end && (*dst = *src))
+		src++, dst++;
 
 	if (dst == end)
 		dst[-1] = 0;
diff --git a/tests.c b/tests.c
index 306a693..14829a8 100644
--- a/tests.c
+++ b/tests.c
@@ -6,7 +6,7 @@
 static int status;
 
 void
-is(char *desc, int ok)
+is(const char *desc, int ok)
 {
 	if (!ok)
 		status = 1;
@@ -16,7 +16,7 @@ is(char *desc, int ok)
 int
 main()
 {
-	printf("1..40\n");
+	printf("1..49\n");
 
 	printf("# stecpy\n");
 
@@ -125,5 +125,26 @@ main()
 	is("y not found in first 6 chars", y == buf2 + 6);
 
 
+	printf("# steccpy\n");
+	pos = buf;
+	pos = steccpy(pos, end, "abc,def", ',');
+	is("1x3 = 3", strlen(buf) == 3);
+	pos = steccpy(pos, end, "def:ghijkl", ':');
+	is("2x3 = 6", strlen(buf) == 6);
+	pos = steccpy(pos, end, ":ghi", ':');
+	pos = steccpy(pos, end, "ghi", ':');
+	is("3x3 = 9", strlen(buf) == 9);
+	pos = steccpy(pos, end, "jkl", '\0');
+	is("4x3 = 12", strlen(buf) == 12);
+	pos = steccpy(pos, end, "mnopqst", '!');
+	is("5x3 = 15", strlen(buf) == 15);
+	pos = steccpy(pos, end, "full", '!');
+	is("buffer is full", strlen(buf) == 15);
+	is("return value is end", pos == end);
+	pos = steccpy(pos, end, "fuller", 'r');
+	is("buffer doesn't get fuller", strlen(buf) == 15);
+	is("return value is end", pos == end);
+
+
 	return status;
 }