Created
January 2, 2015 09:47
-
-
Save clauswitt/283e3ebe20569da846f5 to your computer and use it in GitHub Desktop.
A simple (dumbed down) cat clone
This file contains 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 <unistd.h> | |
#include <stdio.h> | |
int main(int argc, char **argv) | |
{ | |
FILE *read_this; | |
if (isatty(fileno(stdin))) { | |
if(argc > 1) { | |
printf("filename: %s\n", argv[1]); | |
read_this = fopen(argv[1], "r"); | |
} else { | |
puts("give me a file! Either stdin, a pipe or a filename after programname"); | |
return 1; | |
} | |
} | |
else { | |
read_this = stdin; | |
} | |
if (read_this == NULL) { | |
fprintf(stderr, "Can't open input file\n"); | |
return 1; | |
} | |
char buf[BUFSIZ]; | |
while(NULL != fgets(buf, sizeof buf, read_this)) { | |
printf("%s", buf); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment