mirror of
https://github.com/sheumann/hush.git
synced 2024-08-21 11:28:57 +00:00
Use enums for selected functionality, Reduce the size by nearly 100 Bytes
This commit is contained in:
parent
2480e3a30a
commit
02d090d3d2
@ -27,26 +27,33 @@
|
|||||||
#include "busybox.h"
|
#include "busybox.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;
|
||||||
|
static char print_type = 0;
|
||||||
|
enum print_e {
|
||||||
|
print_lines = 1,
|
||||||
|
print_words = 2,
|
||||||
|
print_chars = 4,
|
||||||
|
print_length = 8
|
||||||
|
};
|
||||||
|
|
||||||
static void print_counts(int lines, int words, int chars, int length,
|
static void print_counts(int lines, int words, int chars, int length,
|
||||||
const char *name)
|
const char *name)
|
||||||
{
|
{
|
||||||
char const *space = "";
|
char const *space = "";
|
||||||
|
|
||||||
if (print_lines) {
|
if (print_type & print_lines) {
|
||||||
printf("%7d", lines);
|
printf("%7d", lines);
|
||||||
space = " ";
|
space = " ";
|
||||||
}
|
}
|
||||||
if (print_words) {
|
if (print_type & print_words) {
|
||||||
printf("%s%7d", space, words);
|
printf("%s%7d", space, words);
|
||||||
space = " ";
|
space = " ";
|
||||||
}
|
}
|
||||||
if (print_chars) {
|
if (print_type & print_chars) {
|
||||||
printf("%s%7d", space, chars);
|
printf("%s%7d", space, chars);
|
||||||
space = " ";
|
space = " ";
|
||||||
}
|
}
|
||||||
if (print_length)
|
if (print_type & print_length)
|
||||||
printf("%s%7d", space, length);
|
printf("%s%7d", space, length);
|
||||||
if (*name)
|
if (*name)
|
||||||
printf(" %s", name);
|
printf(" %s", name);
|
||||||
@ -110,36 +117,37 @@ int wc_main(int argc, char **argv)
|
|||||||
int opt, status = EXIT_SUCCESS;
|
int opt, status = EXIT_SUCCESS;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "clLw")) > 0) {
|
while ((opt = getopt(argc, argv, "clLw")) > 0) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'c':
|
case 'c':
|
||||||
print_chars = 1;
|
print_type |= print_chars;
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
print_lines = 1;
|
print_type |= print_lines;
|
||||||
break;
|
break;
|
||||||
case 'L':
|
case 'L':
|
||||||
print_length = 1;
|
print_type |= print_length;
|
||||||
break;
|
break;
|
||||||
case 'w':
|
case 'w':
|
||||||
print_words = 1;
|
print_type |= print_words;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
show_usage();
|
show_usage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!print_lines && !print_words && !print_chars && !print_length)
|
if (print_type == 0) {
|
||||||
print_lines = print_words = print_chars = 1;
|
print_type = print_lines | print_words | print_chars;
|
||||||
|
}
|
||||||
|
|
||||||
if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
|
if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
|
||||||
wc_file(stdin, "");
|
wc_file(stdin, "");
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
while (optind < argc) {
|
while (optind < argc) {
|
||||||
if ((file = wfopen(argv[optind], "r")) != NULL)
|
file = wfopen(argv[optind], "r");
|
||||||
|
if (file != NULL)
|
||||||
wc_file(file, argv[optind]);
|
wc_file(file, argv[optind]);
|
||||||
else
|
else
|
||||||
status = EXIT_FAILURE;
|
status = EXIT_FAILURE;
|
||||||
|
Loading…
Reference in New Issue
Block a user