Skip to content

Instantly share code, notes, and snippets.

@clauswitt
Created January 2, 2015 09:47
Show Gist options
  • Save clauswitt/283e3ebe20569da846f5 to your computer and use it in GitHub Desktop.
Save clauswitt/283e3ebe20569da846f5 to your computer and use it in GitHub Desktop.
A simple (dumbed down) cat clone
#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