Last active
October 1, 2017 14:04
-
-
Save fallenleavesguy/b52a760ca54adfbdacb359937c66545a to your computer and use it in GitHub Desktop.
cat program in c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char *argv[]) { | |
FILE *fp; | |
void filecopy(FILE *, FILE *); | |
char *prog = argv[0]; | |
if (argc == 1) | |
filecopy(stdin, stdout); | |
else | |
while(--argc > 0) | |
if ((fp = fopen(*++argv, "r")) == NULL) { | |
fprintf(stderr, "%s: can't open %s\n", prog, *argv); | |
return 1; | |
} else { | |
filecopy(fp, stdout); | |
fclose(fp); | |
} | |
if (ferror(stdout)) { | |
fprintf(stderr, "%s: error writing stdout\n", prog); | |
return 2; | |
} | |
return 0; | |
} | |
void filecopy(FILE *ifp, FILE *ofp) { | |
int c; | |
while ((c = getc(ifp)) != EOF) | |
putc(c, ofp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment