diff lib/lib.c @ 284:603275a05524

Teach get_rawline() to continue until a configurable char, and xstrndup() shouldn't die when it's told to chop out a subsection of a string.
author Rob Landley <rob@landley.net>
date Sun, 13 Apr 2008 00:29:00 -0500
parents 8d751063a563
children b4077be6c746
line wrap: on
line diff
--- a/lib/lib.c	Wed Apr 09 22:24:36 2008 -0500
+++ b/lib/lib.c	Sun Apr 13 00:29:00 2008 -0500
@@ -108,8 +108,9 @@
 // Die unless we can allocate a copy of this many bytes of string.
 void *xstrndup(char *s, size_t n)
 {
-	void *ret = xmalloc(++n);
-	xstrcpy(ret, s, n);
+	char *ret = xmalloc(++n);
+	strncpy(ret, s, n);
+	ret[--n]=0;
 
 	return ret;
 }
@@ -612,7 +613,7 @@
 
 // Slow, but small.
 
-char *get_rawline(int fd, long *plen)
+char *get_rawline(int fd, long *plen, char end)
 {
 	char c, *buf = NULL;
 	long len = 0;
@@ -620,7 +621,7 @@
 	for (;;) {
 		if (1>read(fd, &c, 1)) break;
 		if (!(len & 63)) buf=xrealloc(buf, len+64);
-		if ((buf[len++]=c) == '\n') break;
+		if ((buf[len++]=c) == end) break;
 	}
 	if (buf) buf[len]=0;
 	if (plen) *plen = len;
@@ -631,7 +632,7 @@
 char *get_line(int fd)
 {
 	long len;
-	char *buf = get_rawline(fd, &len);
+	char *buf = get_rawline(fd, &len, '\n');
 
 	if (buf && buf[--len]=='\n') buf[len]=0;