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

View File

@@ -1,7 +1,8 @@
/* 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"
static void
@@ -13,24 +14,26 @@ usage(void)
int
main(int argc, char *argv[])
{
FILE *fp, *tmpfp;
int ret = 0;
char tmp[] = "/tmp/sponge-XXXXXX";
int fd, tmpfd;
argv0 = argv[0], argc--, argv++;
if (argc != 1)
usage();
if (!(tmpfp = tmpfile()))
eprintf("tmpfile:");
concat(stdin, "<stdin>", tmpfp, "<tmpfile>");
rewind(tmpfp);
if ((tmpfd = mkstemp(tmp)) < 0)
eprintf("mkstemp:");
unlink(tmp);
if (concat(0, "<stdin>", tmpfd, "<tmpfile>") < 0)
return 1;
if (lseek(tmpfd, 0, SEEK_SET) < 0)
eprintf("lseek:");
if (!(fp = fopen(argv[0], "w")))
eprintf("fopen %s:", argv[0]);
concat(tmpfp, "<tmpfile>", fp, argv[0]);
if ((fd = creat(argv[0], 0666)) < 0)
eprintf("creat %s:", argv[0]);
if (concat(tmpfd, "<tmpfile>", fd, argv[0]) < 0)
return 1;
ret |= fshut(fp, argv[0]) | fshut(tmpfp, "<tmpfile>");
return ret;
return 0;
}