formatting changes, using getopt() now
This commit is contained in:
parent
74b0ebb9ca
commit
40b8855cda
1 changed files with 20 additions and 35 deletions
43
cat.c
43
cat.c
|
@ -5,26 +5,22 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
FILE * current_input_file;
|
FILE * cur_in_file;
|
||||||
int i; char c;
|
int i; char c;
|
||||||
|
|
||||||
int using_unbuffered_output;
|
int using_unbuffered_output;
|
||||||
|
|
||||||
int error_occurred;
|
int error_occurred;
|
||||||
|
|
||||||
for(i = 1; i < argc; i++) {
|
while((c = getopt(argc, argv, "u")) != -1) {
|
||||||
if(argv[i][0] == '-') {
|
if(c == 'u') {
|
||||||
if(argv[i][1] == 'u') {
|
|
||||||
using_unbuffered_output = 1;
|
using_unbuffered_output = 1;
|
||||||
i = argc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(argc == 1) {
|
if(argc == 1 || optind == argc) {
|
||||||
current_input_file = stdin;
|
cur_in_file = stdin;
|
||||||
for(c = fgetc(current_input_file); c != EOF;
|
for(c = fgetc(cur_in_file); c != EOF; c = fgetc(cur_in_file)) {
|
||||||
c = fgetc(current_input_file)) {
|
|
||||||
if(using_unbuffered_output) {
|
if(using_unbuffered_output) {
|
||||||
write(1, &c, 1);
|
write(1, &c, 1);
|
||||||
}
|
}
|
||||||
|
@ -32,32 +28,21 @@ int main(int argc, char *argv[]) {
|
||||||
fprintf(stdout, "%c", c);
|
fprintf(stdout, "%c", c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(current_input_file);
|
fclose(cur_in_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i = 1; i < argc; i++) {
|
for(i = optind; i < argc; i++) {
|
||||||
if(argv[i][0] == '-' && argv[i][1] == '\0') {
|
if(argv[i][0] == '-' && argv[i][1] == '\0') {
|
||||||
current_input_file = stdin;
|
cur_in_file = stdin;
|
||||||
}
|
|
||||||
else if(argv[i][0] == '-' && argv[i][1] == 'u' &&
|
|
||||||
argv[i][2] == '\0') {
|
|
||||||
if(argc > 2) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
current_input_file = stdin;
|
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]);
|
||||||
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;
|
error_occurred = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(c = fgetc(current_input_file); c != EOF;
|
for(c = fgetc(cur_in_file); c != EOF; c = fgetc(cur_in_file)) {
|
||||||
c = fgetc(current_input_file)) {
|
|
||||||
if(using_unbuffered_output) {
|
if(using_unbuffered_output) {
|
||||||
write(1, &c, 1);
|
write(1, &c, 1);
|
||||||
}
|
}
|
||||||
|
@ -65,7 +50,7 @@ int main(int argc, char *argv[]) {
|
||||||
fprintf(stdout, "%c", c);
|
fprintf(stdout, "%c", c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(current_input_file);
|
fclose(cur_in_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(error_occurred) {
|
if(error_occurred) {
|
||||||
|
|
Loading…
Reference in a new issue