62 lines
1.2 KiB
C
Executable file
62 lines
1.2 KiB
C
Executable file
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
int main(int argc, char *argv[]) {
|
|
FILE * cur_in_file;
|
|
int i; char c;
|
|
|
|
int using_unbuffered_output;
|
|
|
|
int error_occurred;
|
|
|
|
while((c = getopt(argc, argv, "u")) != -1) {
|
|
if(c == 'u') {
|
|
using_unbuffered_output = 1;
|
|
}
|
|
}
|
|
|
|
if(argc == 1 || optind == argc) {
|
|
cur_in_file = stdin;
|
|
for(c = fgetc(cur_in_file); c != EOF; c = fgetc(cur_in_file)) {
|
|
if(using_unbuffered_output) {
|
|
write(1, &c, 1);
|
|
}
|
|
else {
|
|
fprintf(stdout, "%c", c);
|
|
}
|
|
}
|
|
fclose(cur_in_file);
|
|
}
|
|
|
|
for(i = optind; i < argc; i++) {
|
|
if(argv[i][0] == '-' && argv[i][1] == '\0') {
|
|
cur_in_file = stdin;
|
|
}
|
|
else {
|
|
cur_in_file = fopen(argv[i], "r");
|
|
if(cur_in_file == NULL) {
|
|
fprintf(stderr, "%s: %s: No such file or directory\n", argv[0], argv[i]);
|
|
error_occurred = 1;
|
|
}
|
|
}
|
|
for(c = fgetc(cur_in_file); c != EOF; c = fgetc(cur_in_file)) {
|
|
if(using_unbuffered_output) {
|
|
write(1, &c, 1);
|
|
}
|
|
else {
|
|
fprintf(stdout, "%c", c);
|
|
}
|
|
}
|
|
fclose(cur_in_file);
|
|
}
|
|
|
|
if(error_occurred) {
|
|
return 1;
|
|
}
|
|
else {
|
|
return 0;
|
|
}
|
|
}
|