rework options to get rid of extra variables

This commit is contained in:
Mike Frysinger 2005-04-24 03:53:12 +00:00
parent 058e740d1e
commit f06c494636

View File

@ -36,8 +36,9 @@
#include "busybox.h" #include "busybox.h"
/* vars to control behavior */ /* vars to control behavior */
static int follow_links = 0; #define OPT_TERSE 2
static int terse = 0; #define OPT_DEREFERNCE 4
static long flags;
static char const *file_type(struct stat const *st) static char const *file_type(struct stat const *st)
{ {
@ -425,7 +426,7 @@ static int do_statfs(char const *filename, char const *format)
#ifdef CONFIG_FEATURE_STAT_FORMAT #ifdef CONFIG_FEATURE_STAT_FORMAT
if (format == NULL) if (format == NULL)
format = (terse format = (flags & OPT_TERSE
? "%n %i %l %t %s %S %b %f %a %c %d\n" ? "%n %i %l %t %s %S %b %f %a %c %d\n"
: " File: \"%n\"\n" : " File: \"%n\"\n"
" ID: %-8i Namelen: %-7l Type: %T\n" " ID: %-8i Namelen: %-7l Type: %T\n"
@ -435,7 +436,7 @@ static int do_statfs(char const *filename, char const *format)
print_it(format, filename, print_statfs, &statfsbuf); print_it(format, filename, print_statfs, &statfsbuf);
#else #else
format = (terse format = (flags & OPT_TERSE
? "%s %Lx %lu " ? "%s %Lx %lu "
: " File: \"%s\"\n" : " File: \"%s\"\n"
" ID: %-8Lx Namelen: %-7lu "); " ID: %-8Lx Namelen: %-7lu ");
@ -444,12 +445,12 @@ static int do_statfs(char const *filename, char const *format)
statfsbuf.f_fsid, statfsbuf.f_fsid,
statfsbuf.f_namelen); statfsbuf.f_namelen);
if (terse) if (flags & OPT_TERSE)
printf("%lx ", (unsigned long int) (statfsbuf.f_type)); printf("%lx ", (unsigned long int) (statfsbuf.f_type));
else else
printf("Type: %s\n", human_fstype(statfsbuf.f_type)); printf("Type: %s\n", human_fstype(statfsbuf.f_type));
format = (terse format = (flags & OPT_TERSE
? "%lu %lu %ld %ld %ld %ld %ld\n" ? "%lu %lu %ld %ld %ld %ld %ld\n"
: "Block size: %-10lu Fundamental block size: %lu\n" : "Block size: %-10lu Fundamental block size: %lu\n"
"Blocks: Total: %-10ld Free: %-10ld Available: %ld\n" "Blocks: Total: %-10ld Free: %-10ld Available: %ld\n"
@ -472,14 +473,14 @@ static int do_stat(char const *filename, char const *format)
{ {
struct stat statbuf; struct stat statbuf;
if ((follow_links ? stat : lstat) (filename, &statbuf) != 0) { if ((flags & OPT_DEREFERNCE ? stat : lstat) (filename, &statbuf) != 0) {
bb_perror_msg("cannot stat '%s'", filename); bb_perror_msg("cannot stat '%s'", filename);
return 0; return 0;
} }
#ifdef CONFIG_FEATURE_STAT_FORMAT #ifdef CONFIG_FEATURE_STAT_FORMAT
if (format == NULL) { if (format == NULL) {
if (terse) { if (flags & OPT_TERSE) {
format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"; format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n";
} else { } else {
if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) { if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
@ -502,7 +503,7 @@ static int do_stat(char const *filename, char const *format)
} }
print_it(format, filename, print_stat, &statbuf); print_it(format, filename, print_stat, &statbuf);
#else #else
if (terse) { if (flags & OPT_TERSE) {
printf("%s %lu %lu %lx %lu %lu %lx %lu %lu %lx %lx %lu %lu %lu %lu\n", printf("%s %lu %lu %lx %lu %lu %lx %lu %lu %lx %lx %lu %lu %lu %lu\n",
filename, filename,
(uintmax_t) (statbuf.st_size), (uintmax_t) (statbuf.st_size),
@ -574,10 +575,9 @@ int stat_main(int argc, char **argv)
int i; int i;
char *format = NULL; char *format = NULL;
int ok = 1; int ok = 1;
long flags;
int (*statfunc)(char const *, char const *) = do_stat; int (*statfunc)(char const *, char const *) = do_stat;
flags = bb_getopt_ulflags(argc, argv, "fLlt" flags = bb_getopt_ulflags(argc, argv, "ftL"
#ifdef CONFIG_FEATURE_STAT_FORMAT #ifdef CONFIG_FEATURE_STAT_FORMAT
"c:", &format "c:", &format
#endif #endif
@ -585,10 +585,6 @@ int stat_main(int argc, char **argv)
if (flags & 1) /* -f */ if (flags & 1) /* -f */
statfunc = do_statfs; statfunc = do_statfs;
if (flags & 2 || flags & 4) /* -L, -l */
follow_links = 1;
if (flags & 8) /* -t */
terse = 1;
if (argc == optind) /* files */ if (argc == optind) /* files */
bb_show_usage(); bb_show_usage();