concat: Use plain read/write instead of buffered stdio

If we are just copying data from one file to another, we don't need to
fill a complete buffer, just read a chunk at a time, and write it to the
output.
This commit is contained in:
Michael Forney 2017-07-03 14:58:49 -07:00 committed by Anselm R Garbe
parent 9a3b12525b
commit 3276fbea1c
8 changed files with 202 additions and 171 deletions

39
cat.c
View File

@ -1,21 +1,10 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "text.h"
#include "util.h" #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 static void
usage(void) usage(void)
{ {
@ -25,37 +14,39 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp; int fd, ret = 0;
int ret = 0;
void (*cat)(FILE *, const char *, FILE *, const char *) = &concat;
ARGBEGIN { ARGBEGIN {
case 'u': case 'u':
cat = &uconcat;
break; break;
default: default:
usage(); usage();
} ARGEND } ARGEND
if (!argc) { if (!argc) {
cat(stdin, "<stdin>", stdout, "<stdout>"); if (concat(0, "<stdin>", 1, "<stdout>") < 0)
ret = 1;
} else { } else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if (!strcmp(*argv, "-")) { if (!strcmp(*argv, "-")) {
*argv = "<stdin>"; *argv = "<stdin>";
fp = stdin; fd = 0;
} else if (!(fp = fopen(*argv, "r"))) { } else if ((fd = open(*argv, O_RDONLY)) < 0) {
weprintf("fopen %s:", *argv); weprintf("open %s:", *argv);
ret = 1; ret = 1;
continue; continue;
} }
cat(fp, *argv, stdout, "<stdout>"); switch (concat(fd, *argv, 1, "<stdout>")) {
if (fp != stdin && fshut(fp, *argv)) case -1:
ret = 1; ret = 1;
break;
case -2:
return 1; /* exit on write error */
}
if (fd != 0)
close(fd);
} }
} }
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret; return ret;
} }

View File

@ -1,19 +1,23 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <unistd.h>
#include "../text.h"
#include "../util.h" #include "../util.h"
void int
concat(FILE *fp1, const char *s1, FILE *fp2, const char *s2) concat(int f1, const char *s1, int f2, const char *s2)
{ {
char buf[BUFSIZ]; char buf[BUFSIZ];
size_t n; ssize_t n;
while ((n = fread(buf, 1, sizeof(buf), fp1))) { while ((n = read(f1, buf, sizeof(buf))) > 0) {
fwrite(buf, 1, n, fp2); if (writeall(f2, buf, n) < 0) {
weprintf("write %s:", s2);
if (feof(fp1) || ferror(fp1) || ferror(fp2)) return -2;
break; }
} }
if (n < 0) {
weprintf("read %s:", s1);
return -1;
}
return 0;
} }

View File

@ -12,7 +12,6 @@
#include <utime.h> #include <utime.h>
#include "../fs.h" #include "../fs.h"
#include "../text.h"
#include "../util.h" #include "../util.h"
int cp_aflag = 0; int cp_aflag = 0;
@ -27,7 +26,7 @@ int
cp(const char *s1, const char *s2, int depth) cp(const char *s1, const char *s2, int depth)
{ {
DIR *dp; DIR *dp;
FILE *f1, *f2; int f1, f2;
struct dirent *d; struct dirent *d;
struct stat st; struct stat st;
struct timespec times[2]; struct timespec times[2];
@ -113,46 +112,38 @@ cp(const char *s1, const char *s2, int depth)
return 0; return 0;
} }
} else { } else {
if (!(f1 = fopen(s1, "r"))) { if ((f1 = open(s1, O_RDONLY)) < 0) {
weprintf("fopen %s:", s1); weprintf("open %s:", s1);
cp_status = 1; cp_status = 1;
return 0; return 0;
} }
if (!(f2 = fopen(s2, "w"))) { if ((f2 = creat(s2, st.st_mode)) < 0 && cp_fflag) {
if (cp_fflag) { if (unlink(s2) < 0 && errno != ENOENT) {
if (unlink(s2) < 0 && errno != ENOENT) { weprintf("unlink %s:", s2);
weprintf("unlink %s:", s2);
cp_status = 1;
fclose(f1);
return 0;
} else if (!(f2 = fopen(s2, "w"))) {
weprintf("fopen %s:", s2);
cp_status = 1;
fclose(f1);
return 0;
}
} else {
weprintf("fopen %s:", s2);
cp_status = 1; cp_status = 1;
fclose(f1); close(f1);
return 0; return 0;
} }
f2 = creat(s2, st.st_mode);
}
if (f2 < 0) {
weprintf("creat %s:", s2);
cp_status = 1;
close(f1);
return 0;
}
if (concat(f1, s1, f2, s2) < 0) {
cp_status = 1;
close(f1);
close(f2);
return 0;
} }
concat(f1, s1, f2, s2);
/* preserve permissions by default */ /* preserve permissions by default */
fchmod(fileno(f2), st.st_mode); fchmod(f2, st.st_mode);
if (fclose(f2) == EOF) { close(f1);
weprintf("fclose %s:", s2); close(f2);
cp_status = 1;
return 0;
}
if (fclose(f1) == EOF) {
weprintf("fclose %s:", s1);
cp_status = 1;
return 0;
}
} }
if (cp_aflag || cp_pflag) { if (cp_aflag || cp_pflag) {

View File

@ -1,7 +1,8 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "text.h"
#include "util.h" #include "util.h"
static void static void
@ -13,24 +14,26 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp, *tmpfp; char tmp[] = "/tmp/sponge-XXXXXX";
int ret = 0; int fd, tmpfd;
argv0 = argv[0], argc--, argv++; argv0 = argv[0], argc--, argv++;
if (argc != 1) if (argc != 1)
usage(); usage();
if (!(tmpfp = tmpfile())) if ((tmpfd = mkstemp(tmp)) < 0)
eprintf("tmpfile:"); eprintf("mkstemp:");
concat(stdin, "<stdin>", tmpfp, "<tmpfile>"); unlink(tmp);
rewind(tmpfp); if (concat(0, "<stdin>", tmpfd, "<tmpfile>") < 0)
return 1;
if (lseek(tmpfd, 0, SEEK_SET) < 0)
eprintf("lseek:");
if (!(fp = fopen(argv[0], "w"))) if ((fd = creat(argv[0], 0666)) < 0)
eprintf("fopen %s:", argv[0]); eprintf("creat %s:", argv[0]);
concat(tmpfp, "<tmpfile>", fp, argv[0]); if (concat(tmpfd, "<tmpfile>", fd, argv[0]) < 0)
return 1;
ret |= fshut(fp, argv[0]) | fshut(tmpfp, "<tmpfile>"); return 0;
return ret;
} }

197
tail.c
View File

@ -1,80 +1,125 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "text.h"
#include "utf.h" #include "utf.h"
#include "util.h" #include "util.h"
static char mode = 'n'; static char mode = 'n';
static void static int
dropinit(FILE *fp, const char *str, size_t n) dropinit(int fd, const char *fname, size_t count)
{ {
Rune r; Rune r;
char *buf = NULL; char buf[BUFSIZ], *p;
size_t size = 0, i = 1; ssize_t n;
ssize_t len; int nr;
if (mode == 'n') { if (count < 2)
while (i < n && (len = getline(&buf, &size, fp)) > 0) goto copy;
if (len > 0 && buf[len - 1] == '\n') count--; /* numbering starts at 1 */
i++; while (count && (n = read(fd, buf, sizeof(buf))) > 0) {
} else { if (mode == 'n') {
while (i < n && efgetrune(&r, fp, str)) for (p = buf; count && n > 0; p++, n--) {
i++; if (*p == '\n')
count--;
}
} else {
for (p = buf; count && n > 0; p += nr, n -= nr, count--) {
nr = charntorune(&r, p, n);
if (!nr) {
/* we don't have a full rune, move
* remaining data to beginning and read
* again */
memmove(buf, p, n);
break;
}
}
}
}
if (count) {
if (n < 0)
weprintf("read %s:", fname);
if (n <= 0)
return n;
}
/* write the rest of the buffer */
if (writeall(1, p, n) < 0)
eprintf("write:");
copy:
switch (concat(fd, fname, 1, "<stdout>")) {
case -1: /* read error */
return -1;
case -2: /* write error */
exit(1);
default:
return 0;
} }
free(buf);
concat(fp, str, stdout, "<stdout>");
} }
static void static int
taketail(FILE *fp, const char *str, size_t n) taketail(int fd, const char *fname, size_t count)
{ {
Rune *r = NULL; static char *buf = NULL;
struct line *ring = NULL; static size_t size = 0;
size_t i, j, *size = NULL; char *p;
ssize_t len; size_t len = 0, left;
int seenln = 0; ssize_t n;
if (!n) if (!count)
return; return 0;
for (;;) {
if (mode == 'n') { if (len + BUFSIZ > size) {
ring = ecalloc(n, sizeof(*ring)); /* make sure we have at least BUFSIZ to read */
size = ecalloc(n, sizeof(*size)); size += 2 * BUFSIZ;
buf = erealloc(buf, size);
for (i = j = 0; (len = getline(&ring[i].data, }
&size[i], fp)) > 0; seenln = 1) { n = read(fd, buf + len, size - len);
ring[i].len = len; if (n < 0) {
i = j = (i + 1) % n; weprintf("read %s:", fname);
return -1;
}
if (n == 0)
break;
len += n;
if (mode == 'n') {
/* ignore the last character; if it is a newline, it
* ends the last line */
for (p = buf + len - 2, left = count; p >= buf; p--) {
if (*p != '\n')
continue;
left--;
if (!left) {
p++;
break;
}
}
} else {
for (p = buf + len - 1, left = count; p >= buf; p--) {
/* skip utf-8 continuation bytes */
if ((*p & 0xc0) == 0x80)
continue;
left--;
if (!left)
break;
}
}
if (p > buf) {
len -= p - buf;
memmove(buf, p, len);
} }
} else {
r = ecalloc(n, sizeof(*r));
for (i = j = 0; efgetrune(&r[i], fp, str); )
i = j = (i + 1) % n;
} }
if (ferror(fp)) if (writeall(1, buf, len) < 0)
eprintf("%s: read error:", str); eprintf("write:");
return 0;
do {
if (seenln && ring && ring[j].data) {
fwrite(ring[j].data, 1, ring[j].len, stdout);
free(ring[j].data);
} else if (r) {
efputrune(&r[j], stdout, "<stdout>");
}
} while ((j = (j + 1) % n) != i);
free(ring);
free(size);
free(r);
} }
static void static void
@ -87,11 +132,11 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct stat st1, st2; struct stat st1, st2;
FILE *fp; int fd;
size_t tmpsize, n = 10; size_t n = 10;
int fflag = 0, ret = 0, newline = 0, many = 0; int fflag = 0, ret = 0, newline = 0, many = 0;
char *numstr, *tmp; char *numstr;
void (*tail)(FILE *, const char *, size_t) = taketail; int (*tail)(int, const char *, size_t) = taketail;
ARGBEGIN { ARGBEGIN {
case 'f': case 'f':
@ -113,17 +158,18 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND } ARGEND
if (!argc) if (!argc) {
tail(stdin, "<stdin>", n); if (tail(0, "<stdin>", n) < 0)
else { ret = 1;
} else {
if ((many = argc > 1) && fflag) if ((many = argc > 1) && fflag)
usage(); usage();
for (newline = 0; *argv; argc--, argv++) { for (newline = 0; *argv; argc--, argv++) {
if (!strcmp(*argv, "-")) { if (!strcmp(*argv, "-")) {
*argv = "<stdin>"; *argv = "<stdin>";
fp = stdin; fd = 0;
} else if (!(fp = fopen(*argv, "r"))) { } else if ((fd = open(*argv, O_RDONLY)) < 0) {
weprintf("fopen %s:", *argv); weprintf("open %s:", *argv);
ret = 1; ret = 1;
continue; continue;
} }
@ -134,27 +180,26 @@ main(int argc, char *argv[])
if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode))) if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
fflag = 0; fflag = 0;
newline = 1; newline = 1;
tail(fp, *argv, n); if (tail(fd, *argv, n) < 0) {
ret = 1;
fflag = 0;
}
if (!fflag) { if (!fflag) {
if (fp != stdin && fshut(fp, *argv)) if (fd != 0)
ret = 1; close(fd);
continue; continue;
} }
for (tmp = NULL, tmpsize = 0;;) { for (;;) {
while (getline(&tmp, &tmpsize, fp) > 0) { if (concat(fd, *argv, 1, "<stdout>") < 0)
fputs(tmp, stdout); exit(1);
fflush(stdout);
}
if (ferror(fp))
eprintf("readline %s:", *argv);
clearerr(fp);
/* ignore error in case file was removed, we continue /* ignore error in case file was removed, we continue
* tracking the existing open file descriptor */ * tracking the existing open file descriptor */
if (!stat(*argv, &st2)) { if (!stat(*argv, &st2)) {
if (st2.st_size < st1.st_size) { if (st2.st_size < st1.st_size) {
fprintf(stderr, "%s: file truncated\n", *argv); fprintf(stderr, "%s: file truncated\n", *argv);
rewind(fp); if (lseek(fd, SEEK_SET, 0) < 0)
eprintf("lseek:");
} }
st1 = st2; st1 = st2;
} }
@ -163,7 +208,5 @@ main(int argc, char *argv[])
} }
} }
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret; return ret;
} }

1
text.h
View File

@ -13,5 +13,4 @@ struct linebuf {
#define EMPTY_LINEBUF {NULL, 0, 0,} #define EMPTY_LINEBUF {NULL, 0, 0,}
void getlines(FILE *, struct linebuf *); void getlines(FILE *, struct linebuf *);
void concat(FILE *, const char *, FILE *, const char *);
int linecmp(struct line *, struct line *); int linecmp(struct line *, struct line *);

1
util.h
View File

@ -64,6 +64,7 @@ int eregcomp(regex_t *, const char *, int);
/* io */ /* io */
ssize_t writeall(int, const void *, size_t); ssize_t writeall(int, const void *, size_t);
int concat(int, const char *, int, const char *);
/* misc */ /* misc */
void enmasse(int, char **, int (*)(const char *, const char *, int)); void enmasse(int, char **, int (*)(const char *, const char *, int));

View File

@ -2,6 +2,7 @@
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -10,7 +11,6 @@
#include <sys/wait.h> #include <sys/wait.h>
#include "util.h" #include "util.h"
#include "text.h"
static int Dflag = 0; static int Dflag = 0;
static gid_t group; static gid_t group;
@ -44,7 +44,7 @@ static int
install(const char *s1, const char *s2, int depth) install(const char *s1, const char *s2, int depth)
{ {
DIR *dp; DIR *dp;
FILE *f1, *f2; int f1, f2;
struct dirent *d; struct dirent *d;
struct stat st; struct stat st;
ssize_t r; ssize_t r;
@ -92,23 +92,22 @@ install(const char *s1, const char *s2, int depth)
else if (mknod(s2, (st.st_mode & ~07777) | mode, st.st_rdev) < 0) else if (mknod(s2, (st.st_mode & ~07777) | mode, st.st_rdev) < 0)
eprintf("mknod %s:", s2); eprintf("mknod %s:", s2);
} else { } else {
if (!(f1 = fopen(s1, "r"))) if ((f1 = open(s1, O_RDONLY)) < 0)
eprintf("fopen %s:", s1); eprintf("open %s:", s1);
if (!(f2 = fopen(s2, "w"))) { if ((f2 = creat(s2, 0600)) < 0) {
if (unlink(s2) < 0 && errno != ENOENT) if (unlink(s2) < 0 && errno != ENOENT)
eprintf("unlink %s:", s2); eprintf("unlink %s:", s2);
else if (!(f2 = fopen(s2, "w"))) if ((f2 = creat(s2, 0600)) < 0)
eprintf("fopen %s:", s2); eprintf("creat %s:", s2);
} }
concat(f1, s1, f2, s2); if (concat(f1, s1, f2, s2) < 0)
exit(1);
if (fchmod(fileno(f2), mode) < 0) if (fchmod(f2, mode) < 0)
eprintf("fchmod %s:", s2); eprintf("fchmod %s:", s2);
if (fclose(f2) == EOF) close(f1);
eprintf("fclose %s:", s2); close(f2);
if (fclose(f1) == EOF)
eprintf("fclose %s:", s1);
} }
if (lchown(s2, owner, group) < 0) if (lchown(s2, owner, group) < 0)