Added -u option
This commit is contained in:
parent
9a42532595
commit
75632e656a
1 changed files with 62 additions and 22 deletions
84
cat.c
84
cat.c
|
@ -1,35 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
FILE * in_file;
|
||||
int i, error = 0;
|
||||
char c;
|
||||
|
||||
if(argc == 1) {
|
||||
in_file = stdin;
|
||||
for(c = fgetc(in_file); c != EOF; c = fgetc(in_file)) {
|
||||
fprintf(stdout, "%c", c);
|
||||
}
|
||||
}
|
||||
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] == '-' && argv[i][1] == '\0')
|
||||
in_file = stdin;
|
||||
else {
|
||||
in_file = fopen(argv[i], "r");
|
||||
if(in_file == NULL) {
|
||||
fprintf(stderr, "Cannot find file %s\n", argv[i]);
|
||||
error = 1;
|
||||
continue;
|
||||
if(argv[i][0] == '-') {
|
||||
if(argv[i][1] == 'u') {
|
||||
using_unbuffered_output = 1;
|
||||
i = argc;
|
||||
}
|
||||
}
|
||||
for(c = fgetc(in_file); c != EOF; c = fgetc(in_file))
|
||||
fprintf(stdout, "%c", c);
|
||||
}
|
||||
|
||||
if(error)
|
||||
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);
|
||||
}
|
||||
|
||||
for(i = 1; i < argc; i++) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
else {
|
||||
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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
if(error_occurred) {
|
||||
return 1;
|
||||
else
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue