2020-12-12 06:33:20 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
2020-02-12 05:30:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-12-02 02:32:26 +00:00
|
|
|
#include <unistd.h>
|
2020-02-12 05:30:50 +00:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2020-12-02 02:32:26 +00:00
|
|
|
FILE * current_input_file;
|
|
|
|
int i; char c;
|
|
|
|
|
|
|
|
int using_unbuffered_output;
|
|
|
|
|
|
|
|
int error_occurred;
|
|
|
|
|
|
|
|
for(i = 1; i < argc; i++) {
|
|
|
|
if(argv[i][0] == '-') {
|
|
|
|
if(argv[i][1] == 'u') {
|
|
|
|
using_unbuffered_output = 1;
|
|
|
|
i = argc;
|
|
|
|
}
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:32:26 +00:00
|
|
|
if(argc == 1) {
|
|
|
|
current_input_file = stdin;
|
|
|
|
for(c = fgetc(current_input_file); c != EOF;
|
|
|
|
c = fgetc(current_input_file)) {
|
|
|
|
if(using_unbuffered_output) {
|
|
|
|
write(1, &c, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stdout, "%c", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(current_input_file);
|
|
|
|
}
|
|
|
|
|
2020-02-12 05:30:50 +00:00
|
|
|
for(i = 1; i < argc; i++) {
|
2020-12-02 02:32:26 +00:00
|
|
|
if(argv[i][0] == '-' && argv[i][1] == '\0') {
|
|
|
|
current_input_file = stdin;
|
|
|
|
}
|
|
|
|
else if(argv[i][0] == '-' && argv[i][1] == 'u' &&
|
|
|
|
argv[i][2] == '\0') {
|
|
|
|
if(argc > 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
current_input_file = stdin;
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 05:30:50 +00:00
|
|
|
else {
|
2020-12-02 02:32:26 +00:00
|
|
|
current_input_file = fopen(argv[i], "r");
|
|
|
|
if(current_input_file == NULL) {
|
|
|
|
fprintf(stderr, "%s: %s: No such file or directory\n",
|
|
|
|
argv[0], argv[i]);
|
|
|
|
error_occurred = 1;
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-02 02:32:26 +00:00
|
|
|
for(c = fgetc(current_input_file); c != EOF;
|
|
|
|
c = fgetc(current_input_file)) {
|
|
|
|
if(using_unbuffered_output) {
|
|
|
|
write(1, &c, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stdout, "%c", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(current_input_file);
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 02:32:26 +00:00
|
|
|
if(error_occurred) {
|
2020-02-12 05:30:50 +00:00
|
|
|
return 1;
|
2020-12-02 02:32:26 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-02-12 05:30:50 +00:00
|
|
|
return 0;
|
2020-12-02 02:32:26 +00:00
|
|
|
}
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|