getopt update

This commit is contained in:
Gitea 2020-12-13 14:28:17 -06:00
parent c21c4b2739
commit 5366f9d38a

41
wc.c
View file

@ -2,54 +2,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int count_words(FILE * file);
int count_newls(FILE * file);
int count_bytes(FILE * file);
int main(int argc, char ** argv) {
int flags = 0;
int flags = 0, do_multibyte = 0;
int c_byte = 0, c_nl = 0, c_word = 0;
int t_byte = 0, t_nl = 0, t_word = 0;
int i, fstart = 1;
int i, error = 0; char c;
FILE * in_file;
for(i = 1; i < argc; i++) {
if(argv[i][0] == '-') {
switch(argv[i][1]) {
case 'c': flags |= 04;fstart++; break;
case 'l': flags |= 01; fstart++; break;
case 'w': flags |= 02; fstart++; break;
default: break;
}
if(argv[i][2] != '\0') {
switch(argv[i][2]) {
case 'c': flags |= 04; break;
while((c = getopt(argc, argv, "clwm")) != -1) {
switch(c) {
case 'c': flags |= 04; do_multibyte = 0; break;
case 'l': flags |= 01; break;
case 'w': flags |= 02; break;
default: break;
case 'm': flags |= 04; do_multibyte = 1; break;
}
if(argv[i][3] != '\0') {
switch(argv[i][3]) {
case 'c': flags |= 04; break;
case 'l': flags |= 01; break;
case 'w': flags |= 02; break;
default: break;
}
}
}
}
else break;
}
if((flags & 03) == 0) {
flags |= 04;
}
for(i = fstart; i < argc; i++) {
for(i = optind; i < argc; i++) {
in_file = fopen(argv[i], "r");
if(in_file == NULL) {
fprintf(stderr, "Cannot find file %s\n", argv[i]);
fprintf(stderr, "%s: couldn't open file %s\n", argv[0], argv[i]);
error = 1;
continue;
}
if(flags & 04) {
@ -80,7 +63,7 @@ int main(int argc, char ** argv) {
fclose(in_file);
}
if((argc - fstart) > 1) {
if((argc - optind) > 1) {
if(flags & 01) {
printf("%d ", t_nl);
}
@ -93,6 +76,8 @@ int main(int argc, char ** argv) {
printf("total\n");
}
return error;
}
int count_words(FILE * file) {