From 468aea2d8800cc0496383616d82d7c957ae8bc50 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Tue, 1 Apr 2008 14:47:57 +0000 Subject: shells: do not frocibly enable test, echo and kill _applets_, just build relevant source and use xxx_main functions. build system: add a special case when we have exactly one applet enabled (makes "true", "false", "basename" REALLY tiny). getopt32: do not use stdio. function old new delta getopt32 1385 1412 +27 make_device 1187 1200 +13 basename_main 120 127 +7 tcpudpsvd_main 1922 1926 +4 testcmd 5 - -5 echocmd 5 - -5 fuser_main 1243 1231 -12 ------------------------------------------------------------------------------ (add/remove: 0/2 grow/shrink: 4/1 up/down: 51/-22) Total: 29 bytes --- libbb/getopt32.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'libbb/getopt32.c') diff --git a/libbb/getopt32.c b/libbb/getopt32.c index 51e030653..c0d885603 100644 --- a/libbb/getopt32.c +++ b/libbb/getopt32.c @@ -285,6 +285,10 @@ const char *const bb_argv_dash[] = { "-", NULL }; const char *opt_complementary; +/* Many small applets don't want to suck in stdio.h only because + * they need to parse options by calling us */ +#define DONT_USE_PRINTF 1 + enum { PARAM_STRING, PARAM_LIST, @@ -335,7 +339,8 @@ getopt32(char **argv, const char *applet_opts, ...) #define SHOW_USAGE_IF_ERROR 1 #define ALL_ARGV_IS_OPTS 2 #define FIRST_ARGV_IS_OPT 4 -#define FREE_FIRST_ARGV_IS_OPT 8 +#define FREE_FIRST_ARGV_IS_OPT (8 * !DONT_USE_PRINTF) + int spec_flgs = 0; argc = 0; @@ -489,10 +494,16 @@ getopt32(char **argv, const char *applet_opts, ...) va_end(p); if (spec_flgs & FIRST_ARGV_IS_OPT) { - if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0') { + if (argv[1] && argv[1][0] != '-' && argv[1][1] != '\0') { +#if DONT_USE_PRINTF + char *pp = alloca(strlen(argv[1]) + 2); + *pp++ = '-'; + argv[1] = strcpy(pp, argv[1]); +#else argv[1] = xasprintf("-%s", argv[1]); if (ENABLE_FEATURE_CLEAN_UP) spec_flgs |= FREE_FIRST_ARGV_IS_OPT; +#endif } } @@ -573,7 +584,7 @@ getopt32(char **argv, const char *applet_opts, ...) } } -#if (ENABLE_AR || ENABLE_TAR) && ENABLE_FEATURE_CLEAN_UP +#if ENABLE_FEATURE_CLEAN_UP if (spec_flgs & FREE_FIRST_ARGV_IS_OPT) free(argv[1]); #endif -- cgit v1.2.3