add tail
This commit is contained in:
20
cat.c
20
cat.c
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user