This commit is contained in:
Connor Lane Smith
2011-05-26 16:18:42 -01:00
parent 0236550c77
commit bf9626d408
9 changed files with 130 additions and 20 deletions

20
cat.c
View File

@@ -2,10 +2,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "text.h"
#include "util.h"
static void cat(FILE *, const char *);
int
main(int argc, char *argv[])
{
@@ -14,25 +13,12 @@ main(int argc, char *argv[])
if(getopt(argc, argv, "") != -1)
exit(EXIT_FAILURE);
if(optind == argc)
cat(stdin, "<stdin>");
concat(stdin, "<stdin>", stdout, "<stdout>");
else for(; optind < argc; optind++) {
if(!(fp = fopen(argv[optind], "r")))
eprintf("fopen %s:", argv[optind]);
cat(fp, argv[optind]);
concat(fp, argv[optind], stdout, "<stdout>");
fclose(fp);
}
return EXIT_SUCCESS;
}
void
cat(FILE *fp, const char *str)
{
char buf[BUFSIZ];
size_t n;
while((n = fread(buf, 1, sizeof buf, fp)) > 0)
if(fwrite(buf, 1, n, stdout) != n)
eprintf("<stdout>: write error:");
if(ferror(fp))
eprintf("%s: read error:", str);
}