sbase/cat.c

39 lines
753 B
C
Raw Normal View History

2011-05-23 01:36:34 +00:00
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
2011-05-24 10:05:36 +00:00
#include <unistd.h>
2011-05-23 01:36:34 +00:00
#include "util.h"
static void cat(FILE *, const char *);
int
main(int argc, char *argv[])
{
FILE *fp;
2011-05-24 10:05:36 +00:00
if(getopt(argc, argv, "") != -1)
exit(EXIT_FAILURE);
if(optind == argc)
2011-05-23 01:36:34 +00:00
cat(stdin, "<stdin>");
2011-05-24 10:05:36 +00:00
else for(; optind < argc; optind++) {
if(!(fp = fopen(argv[optind], "r")))
eprintf("fopen %s:", argv[optind]);
cat(fp, argv[optind]);
2011-05-23 01:36:34 +00:00
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);
}