Files
first-repo/cat_v2.c

34 lines
693 B
C

#include <stdio.h>
#include <stdlib.h>
void filecopy(FILE *, FILE *);
int main(int argc, char *argv[]) {
FILE *file_p;
char *prog_name = argv[0];
if (argc == 1)
filecopy(stdin, stdout);
else
while (--argc > 0)
if ((file_p = fopen(*++argv, "r")) == NULL) {
fprintf(stderr, "%s: can't open: %s\n", prog_name, *argv);
exit(1);
} else {
filecopy(file_p, stdout);
fclose(file_p);
}
if (ferror(stdout)) {
fprintf(stderr, "%s: error writing stdout\n", prog_name);
exit(2);
}
exit(0);
}
void filecopy(FILE *in_file_p, FILE *out_file_p) {
int ch;
while ((ch = getc(in_file_p)) != EOF)
putc(ch, out_file_p);
}