aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2008-04-13 00:29:00 -0500
committerRob Landley <rob@landley.net>2008-04-13 00:29:00 -0500
commit3fc4e0fd7b4f286fa176b779b2cec243d61ade50 (patch)
tree13c05bc0680a2c7fa6c34cb4e3e5019d0f617b3f /lib
parentf639c65ac49b97fdf2445f6ba79da66eb986e925 (diff)
downloadtoybox-3fc4e0fd7b4f286fa176b779b2cec243d61ade50.tar.gz
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c11
-rw-r--r--lib/lib.h2
2 files changed, 7 insertions, 6 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 1f5d797f..6102e1b4 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -108,8 +108,9 @@ void *xrealloc(void *ptr, size_t size)
// 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 @@ void loopfiles(char **argv, void (*function)(int fd, char *name))
// 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 @@ char *get_rawline(int fd, long *plen)
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_rawline(int fd, long *plen)
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;
diff --git a/lib/lib.h b/lib/lib.h
index 39b21e4d..8ffd103f 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -85,7 +85,7 @@ long atolx(char *c);
off_t fdlength(int fd);
char *xreadlink(char *name);
void loopfiles(char **argv, void (*function)(int fd, char *name));
-char *get_rawline(int fd, long *plen);
+char *get_rawline(int fd, long *plen, char end);
char *get_line(int fd);
void xsendfile(int in, int out);
int copy_tempfile(int fdin, char *name, char **tempname);