// Simple cat emulation for a COMP2041/9044 lecture example // andrewt@unsw.edu.au #include #include // write bytes of stream to stdout void process_stream(FILE *stream) { int byte; while ((byte = fgetc(stream)) != EOF) { if (fputc(byte, stdout) == EOF) { perror("cat:"); exit(1); } } } // process files given as arguments // if no arguments process stdin int main(int argc, char *argv[]) { if (argc == 1) { process_stream(stdin); } else { for (int i = 1; i < argc; i++) { FILE *in = fopen(argv[i], "r"); if (in == NULL) { fprintf(stderr, "%s: %s: ", argv[0], argv[i]); perror(""); return 1; } process_stream(in); fclose(in); } } return 0; }