#define _GNU_SOURCE // for program_invocation_name #include #include #include #include void cat(FILE* stream) { char buf[BUFSIZ]; do { fgets(buf, BUFSIZ, stream); if (ferror(stream)) { perror(program_invocation_name); exit(EXIT_FAILURE); } fputs(buf, stdout); } while (!feof(stream)); } int main(int argc, char** argv) { if (argc == 1) cat(stdin); else { for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-") == 0) cat(stdin); else if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--newline") == 0) putchar('\n'); else { FILE* stream = fopen(argv[i], "r"); if (!stream) { perror(program_invocation_name); return EXIT_FAILURE; } cat(stream); fclose(stream); } } } }