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
This commit is contained in:
Denis Vlasenko
2008-04-01 14:47:57 +00:00
parent 165f5b394f
commit 468aea2d88
14 changed files with 84 additions and 91 deletions

View File

@@ -101,6 +101,7 @@ lib-y += wfopen_input.o
lib-y += xatonum.o
lib-y += xconnect.o
lib-y += xfuncs.o
lib-y += xfunc_die.o
lib-y += xgetcwd.o
lib-y += xgethostbyname.o
lib-y += xreadlink.o

View File

@@ -36,6 +36,13 @@ static const char usage_messages[] ALIGN1 = ""
/* Include generated applet names, pointers to <applet>_main, etc */
#include "applet_tables.h"
/* ...and if applet_tables generator says we have only one applet... */
#ifdef SINGLE_APPLET_MAIN
#undef ENABLE_FEATURE_INDIVIDUAL
#define ENABLE_FEATURE_INDIVIDUAL 1
#undef USE_FEATURE_INDIVIDUAL
#define USE_FEATURE_INDIVIDUAL(...) __VA_ARGS__
#endif
#if ENABLE_FEATURE_COMPRESS_USAGE
@@ -77,6 +84,23 @@ static const char *unpack_usage_messages(void)
void bb_show_usage(void)
{
if (ENABLE_SHOW_USAGE) {
#ifdef SINGLE_APPLET_STR
/* Imagine that this applet is "true". Dont suck in printf! */
const char *p;
const char *usage_string = p = unpack_usage_messages();
if (*p == '\b') {
write(2, "\nNo help available.\n\n",
sizeof("\nNo help available.\n\n") - 1);
} else {
write(2, "\nUsage: "SINGLE_APPLET_STR" ",
sizeof("\nUsage: "SINGLE_APPLET_STR" ") - 1);
write(2, p, strlen(p));
write(2, "\n\n", 2);
}
dealloc_usage_messages((char*)usage_string);
#else
// TODO: in this case, stdio is sucked in by busybox_main() anyway...
const char *format_string;
const char *p;
const char *usage_string = p = unpack_usage_messages();
@@ -84,18 +108,17 @@ void bb_show_usage(void)
if (ap < 0) /* never happens, paranoia */
xfunc_die();
while (ap) {
while (*p++) continue;
ap--;
}
fprintf(stderr, "%s multi-call binary\n", bb_banner);
format_string = "\nUsage: %s %s\n\n";
if (*p == '\b')
format_string = "\nNo help available.\n\n";
fprintf(stderr, format_string, applet_name, p);
dealloc_usage_messages((char*)usage_string);
#endif
}
xfunc_die();
}
@@ -123,6 +146,9 @@ int find_applet_by_name(const char *name)
int *const bb_errno __attribute__ ((section (".data")));
#endif
void lbb_prepare(const char *applet
USE_FEATURE_INDIVIDUAL(, char **argv))
MAIN_EXTERNALLY_VISIBLE;
void lbb_prepare(const char *applet
USE_FEATURE_INDIVIDUAL(, char **argv))
{
@@ -158,6 +184,9 @@ const char *applet_name;
bool re_execed;
#endif
#if !ENABLE_FEATURE_INDIVIDUAL
USE_FEATURE_SUID(static uid_t ruid;) /* real uid */
#if ENABLE_FEATURE_SUID_CONFIG
@@ -660,6 +689,9 @@ void run_applet_and_exit(const char *name, char **argv)
exit(busybox_main(argv));
}
#endif /* !ENABLE_FEATURE_INDIVIDUAL */
#if ENABLE_BUILD_LIBBUSYBOX
int lbb_main(char **argv)
@@ -667,6 +699,11 @@ int lbb_main(char **argv)
int main(int argc ATTRIBUTE_UNUSED, char **argv)
#endif
{
#if ENABLE_FEATURE_INDIVIDUAL
/* Only one applet is selected by the user! */
lbb_prepare(SINGLE_APPLET_STR USE_FEATURE_INDIVIDUAL(, argv));
return SINGLE_APPLET_MAIN(argc, argv);
#else
lbb_prepare("busybox" USE_FEATURE_INDIVIDUAL(, argv));
#if !BB_MMU
@@ -685,4 +722,5 @@ int main(int argc ATTRIBUTE_UNUSED, char **argv)
run_applet_and_exit(applet_name, argv);
bb_error_msg_and_die("applet not found");
#endif
}

View File

@@ -9,33 +9,6 @@
#include "libbb.h"
int die_sleep;
#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_HUSH
jmp_buf die_jmp;
#endif
void xfunc_die(void)
{
if (die_sleep) {
if ((ENABLE_FEATURE_PREFER_APPLETS || ENABLE_HUSH)
&& die_sleep < 0
) {
/* Special case. We arrive here if NOFORK applet
* calls xfunc, which then decides to die.
* We don't die, but jump instead back to caller.
* NOFORK applets still cannot carelessly call xfuncs:
* p = xmalloc(10);
* q = xmalloc(10); // BUG! if this dies, we leak p!
*/
/* -2222 means "zero" (longjmp can't pass 0)
* run_nofork_applet() catches -2222. */
longjmp(die_jmp, xfunc_error_retval ? xfunc_error_retval : -2222);
}
sleep(die_sleep);
}
exit(xfunc_error_retval);
}
void bb_error_msg_and_die(const char *s, ...)
{
va_list p;

View File

@@ -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