sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit 7d6d11493afd8a6c8a30e777f4a8d2a72e10f1c3
parent ce261fee912041647ff48e40e76cbc4508ca6746
Author: Quentin Rameau <quinq@fifth.space>
Date:   Tue Mar  1 11:04:11 +0100

cat: fix u flag

Our libutil concat() always uses a buffer for input wich is obviously
not compatible with unbuffered io. Here is a local uconcat() which
naively copies input to stdout char by char.

Diffstat:
cat.c | 17++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/cat.c b/cat.c @@ -7,6 +7,16 @@ #include "util.h" static void +uconcat(FILE *fp1, const char *s1, FILE *fp2, const char *s2) +{ + int c; + + setbuf(fp2, NULL); + while ((c = getc(fp1)) != EOF) + putc(c, fp2); +} + +static void usage(void) { eprintf("usage: %s [-u] [file ...]\n", argv0); @@ -17,17 +27,18 @@ main(int argc, char *argv[]) { FILE *fp; int ret = 0; + void (*cat)(FILE *, const char *, FILE *, const char *) = &concat; ARGBEGIN { case 'u': - setbuf(stdout, NULL); + cat = &uconcat; break; default: usage(); } ARGEND if (!argc) { - concat(stdin, "<stdin>", stdout, "<stdout>"); + cat(stdin, "<stdin>", stdout, "<stdout>"); } else { for (; *argv; argc--, argv++) { if (!strcmp(*argv, "-")) { @@ -38,7 +49,7 @@ main(int argc, char *argv[]) ret = 1; continue; } - concat(fp, *argv, stdout, "<stdout>"); + cat(fp, *argv, stdout, "<stdout>"); if (fp != stdin && fshut(fp, *argv)) ret = 1; }