Converted option parsing to using getopt(). Also managed to remove an
extraneous logic loop that existed only for the purpose of the special case of only one file. I replaced it with a variable to keep track of the number of files read.
This commit is contained in:
parent
99e370f0c6
commit
3950596e1e
@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
|
||||||
static int total_lines, total_words, total_chars, max_length;
|
static int total_lines, total_words, total_chars, max_length;
|
||||||
static int print_lines, print_words, print_chars, print_length;
|
static int print_lines, print_words, print_chars, print_length;
|
||||||
@ -103,13 +104,14 @@ static void wc_file(FILE * file, const char *name)
|
|||||||
int wc_main(int argc, char **argv)
|
int wc_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
FILE *file;
|
FILE *file;
|
||||||
|
unsigned int num_files_counted = 0;
|
||||||
|
int opt;
|
||||||
|
|
||||||
total_lines = total_words = total_chars = max_length = 0;
|
total_lines = total_words = total_chars = max_length = 0;
|
||||||
print_lines = print_words = print_chars = print_length = 0;
|
print_lines = print_words = print_chars = print_length = 0;
|
||||||
|
|
||||||
while (--argc && **(++argv) == '-') {
|
while ((opt = getopt(argc, argv, "clLw")) > 0) {
|
||||||
while (*++(*argv))
|
switch (opt) {
|
||||||
switch (**argv) {
|
|
||||||
case 'c':
|
case 'c':
|
||||||
print_chars = 1;
|
print_chars = 1;
|
||||||
break;
|
break;
|
||||||
@ -130,26 +132,24 @@ int wc_main(int argc, char **argv)
|
|||||||
if (!print_lines && !print_words && !print_chars && !print_length)
|
if (!print_lines && !print_words && !print_chars && !print_length)
|
||||||
print_lines = print_words = print_chars = 1;
|
print_lines = print_words = print_chars = 1;
|
||||||
|
|
||||||
if (argc == 0) {
|
if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
|
||||||
wc_file(stdin, "");
|
wc_file(stdin, "");
|
||||||
exit(TRUE);
|
exit(TRUE);
|
||||||
} else if (argc == 1) {
|
|
||||||
file = fopen(*argv, "r");
|
|
||||||
if (file == NULL) {
|
|
||||||
fatalError(*argv);
|
|
||||||
}
|
|
||||||
wc_file(file, *argv);
|
|
||||||
} else {
|
} else {
|
||||||
while (argc-- > 0) {
|
while (optind < argc) {
|
||||||
file = fopen(*argv, "r");
|
file = fopen(argv[optind], "r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
fatalError(*argv);
|
fatalError(argv[optind]);
|
||||||
}
|
}
|
||||||
wc_file(file, *argv);
|
wc_file(file, argv[optind]);
|
||||||
argv++;
|
num_files_counted++;
|
||||||
|
optind++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (num_files_counted > 1)
|
||||||
print_counts(total_lines, total_words, total_chars,
|
print_counts(total_lines, total_words, total_chars,
|
||||||
max_length, "total");
|
max_length, "total");
|
||||||
}
|
|
||||||
return(TRUE);
|
return 0 ;
|
||||||
}
|
}
|
||||||
|
34
wc.c
34
wc.c
@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
|
||||||
static int total_lines, total_words, total_chars, max_length;
|
static int total_lines, total_words, total_chars, max_length;
|
||||||
static int print_lines, print_words, print_chars, print_length;
|
static int print_lines, print_words, print_chars, print_length;
|
||||||
@ -103,13 +104,14 @@ static void wc_file(FILE * file, const char *name)
|
|||||||
int wc_main(int argc, char **argv)
|
int wc_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
FILE *file;
|
FILE *file;
|
||||||
|
unsigned int num_files_counted = 0;
|
||||||
|
int opt;
|
||||||
|
|
||||||
total_lines = total_words = total_chars = max_length = 0;
|
total_lines = total_words = total_chars = max_length = 0;
|
||||||
print_lines = print_words = print_chars = print_length = 0;
|
print_lines = print_words = print_chars = print_length = 0;
|
||||||
|
|
||||||
while (--argc && **(++argv) == '-') {
|
while ((opt = getopt(argc, argv, "clLw")) > 0) {
|
||||||
while (*++(*argv))
|
switch (opt) {
|
||||||
switch (**argv) {
|
|
||||||
case 'c':
|
case 'c':
|
||||||
print_chars = 1;
|
print_chars = 1;
|
||||||
break;
|
break;
|
||||||
@ -130,26 +132,24 @@ int wc_main(int argc, char **argv)
|
|||||||
if (!print_lines && !print_words && !print_chars && !print_length)
|
if (!print_lines && !print_words && !print_chars && !print_length)
|
||||||
print_lines = print_words = print_chars = 1;
|
print_lines = print_words = print_chars = 1;
|
||||||
|
|
||||||
if (argc == 0) {
|
if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
|
||||||
wc_file(stdin, "");
|
wc_file(stdin, "");
|
||||||
exit(TRUE);
|
exit(TRUE);
|
||||||
} else if (argc == 1) {
|
|
||||||
file = fopen(*argv, "r");
|
|
||||||
if (file == NULL) {
|
|
||||||
fatalError(*argv);
|
|
||||||
}
|
|
||||||
wc_file(file, *argv);
|
|
||||||
} else {
|
} else {
|
||||||
while (argc-- > 0) {
|
while (optind < argc) {
|
||||||
file = fopen(*argv, "r");
|
file = fopen(argv[optind], "r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
fatalError(*argv);
|
fatalError(argv[optind]);
|
||||||
}
|
}
|
||||||
wc_file(file, *argv);
|
wc_file(file, argv[optind]);
|
||||||
argv++;
|
num_files_counted++;
|
||||||
|
optind++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (num_files_counted > 1)
|
||||||
print_counts(total_lines, total_words, total_chars,
|
print_counts(total_lines, total_words, total_chars,
|
||||||
max_length, "total");
|
max_length, "total");
|
||||||
}
|
|
||||||
return(TRUE);
|
return 0 ;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user