aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2007-12-15 21:47:25 -0600
committerRob Landley <rob@landley.net>2007-12-15 21:47:25 -0600
commitbc07865a504c291b9c88e41b3481ee6b44334b4d (patch)
tree591494c428601e5324d91814cbcb690e4e530ba6 /lib/lib.c
parent4e68de1ef854fadd74fcb63c3a5ad15dce457a4c (diff)
downloadtoybox-bc07865a504c291b9c88e41b3481ee6b44334b4d.tar.gz
Start of "patch" support. Writes to stdout at the moment.
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 389adcdc..547fe400 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -192,6 +192,11 @@ int xopen(char *path, int flags)
return xcreate(path, flags, 0);
}
+void xclose(int fd)
+{
+ if (close(fd)) perror_exit("xclose");
+}
+
// Die unless we can open/create a file, returning FILE *.
FILE *xfopen(char *path, char *mode)
{
@@ -567,3 +572,46 @@ void loopfiles(char **argv, void (*function)(int fd, char *name))
close(fd);
} while (*++argv);
}
+
+// Slow, but small.
+
+char *get_rawline(int fd, long *plen)
+{
+ char c, *buf = NULL;
+ long len = 0;
+
+ for (;;) {
+ if (1>read(fd, &c, 1)) break;
+ if (!(len & 63)) buf=xrealloc(buf, len+64);
+ if ((buf[len++]=c) == '\n') break;
+ }
+ if (buf) buf[len]=0;
+ if (plen) *plen = len;
+
+ return buf;
+}
+
+char *get_line(int fd)
+{
+ long len;
+ char *buf = get_rawline(fd, &len);
+
+ if (buf && buf[--len]=='\n') buf[len]=0;
+
+ return buf;
+}
+
+// Copy the rest of in to out and close both files.
+
+void xsendfile(int in, int out)
+{
+ long len;
+
+ if (in<0) return;
+ for (;;) {
+ len = xread(in, toybuf, sizeof(toybuf));
+ if (len<1) break;
+ xwrite(out, toybuf, len);
+ }
+ xclose(in);
+}