Skip to content

Instantly share code, notes, and snippets.

@fallenleavesguy
Last active October 1, 2017 14:04
Show Gist options
  • Save fallenleavesguy/b52a760ca54adfbdacb359937c66545a to your computer and use it in GitHub Desktop.
Save fallenleavesguy/b52a760ca54adfbdacb359937c66545a to your computer and use it in GitHub Desktop.
cat program in c
#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