Create find_applet_by_name function. Save 32 bytes.

This commit is contained in:
Matt Kraai 2001-02-01 19:21:20 +00:00
parent 05e782ddd3
commit f2cc2762bb
8 changed files with 37 additions and 41 deletions

View File

@ -87,8 +87,8 @@ static void install_links(const char *busybox, int use_symbolic_links)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct BB_applet search_applet, *applet; struct BB_applet *applet;
const char *s; const char *s;
for (s = applet_name = argv[0]; *s != '\0';) { for (s = applet_name = argv[0]; *s != '\0';) {
if (*s++ == '/') if (*s++ == '/')
@ -104,10 +104,7 @@ int main(int argc, char **argv)
#endif #endif
/* Do a binary search to find the applet entry given the name. */ /* Do a binary search to find the applet entry given the name. */
search_applet.name = applet_name; if ((applet = find_applet_by_name(applet_name)) != NULL) {
applet = bsearch(&search_applet, applets, NUM_APPLETS,
sizeof(struct BB_applet), applet_name_compare);
if (applet != NULL) {
if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0) if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0)
usage(applet->usage); usage(applet->usage);
exit((*(applet->main)) (argc, argv)); exit((*(applet->main)) (argc, argv));

View File

@ -87,8 +87,8 @@ static void install_links(const char *busybox, int use_symbolic_links)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct BB_applet search_applet, *applet; struct BB_applet *applet;
const char *s; const char *s;
for (s = applet_name = argv[0]; *s != '\0';) { for (s = applet_name = argv[0]; *s != '\0';) {
if (*s++ == '/') if (*s++ == '/')
@ -104,10 +104,7 @@ int main(int argc, char **argv)
#endif #endif
/* Do a binary search to find the applet entry given the name. */ /* Do a binary search to find the applet entry given the name. */
search_applet.name = applet_name; if ((applet = find_applet_by_name(applet_name)) != NULL) {
applet = bsearch(&search_applet, applets, NUM_APPLETS,
sizeof(struct BB_applet), applet_name_compare);
if (applet != NULL) {
if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0) if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0)
usage(applet->usage); usage(applet->usage);
exit((*(applet->main)) (argc, argv)); exit((*(applet->main)) (argc, argv));

View File

@ -88,7 +88,6 @@ extern const struct BB_applet applets[];
#undef PROTOTYPES #undef PROTOTYPES
extern const char *applet_name; extern const char *applet_name;
extern int applet_name_compare(const void *x, const void *y);
extern void usage(const char *usage) __attribute__ ((noreturn)); extern void usage(const char *usage) __attribute__ ((noreturn));
extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
@ -153,6 +152,7 @@ extern char *get_last_path_component(char *path);
extern FILE *wfopen(const char *path, const char *mode); extern FILE *wfopen(const char *path, const char *mode);
extern FILE *xfopen(const char *path, const char *mode); extern FILE *xfopen(const char *path, const char *mode);
extern void chomp(char *s); extern void chomp(char *s);
extern struct BB_applet *find_applet_by_name(const char *name);
#ifndef DMALLOC #ifndef DMALLOC
extern void *xmalloc (size_t size); extern void *xmalloc (size_t size);

View File

@ -88,7 +88,6 @@ extern const struct BB_applet applets[];
#undef PROTOTYPES #undef PROTOTYPES
extern const char *applet_name; extern const char *applet_name;
extern int applet_name_compare(const void *x, const void *y);
extern void usage(const char *usage) __attribute__ ((noreturn)); extern void usage(const char *usage) __attribute__ ((noreturn));
extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
@ -153,6 +152,7 @@ extern char *get_last_path_component(char *path);
extern FILE *wfopen(const char *path, const char *mode); extern FILE *wfopen(const char *path, const char *mode);
extern FILE *xfopen(const char *path, const char *mode); extern FILE *xfopen(const char *path, const char *mode);
extern void chomp(char *s); extern void chomp(char *s);
extern struct BB_applet *find_applet_by_name(const char *name);
#ifndef DMALLOC #ifndef DMALLOC
extern void *xmalloc (size_t size); extern void *xmalloc (size_t size);

12
lash.c
View File

@ -67,7 +67,6 @@
static const int MAX_LINE = 256; /* size of input buffer for cwd data */ static const int MAX_LINE = 256; /* size of input buffer for cwd data */
static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
extern size_t NUM_APPLETS;
enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE, enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
@ -1371,7 +1370,8 @@ static int pseudo_exec(struct child_prog *child)
{ {
struct built_in_command *x; struct built_in_command *x;
#ifdef BB_FEATURE_SH_STANDALONE_SHELL #ifdef BB_FEATURE_SH_STANDALONE_SHELL
struct BB_applet search_applet, *applet; struct BB_applet *applet;
const char *name;
#endif #endif
/* Check if the command matches any of the non-forking builtins. /* Check if the command matches any of the non-forking builtins.
@ -1404,7 +1404,7 @@ static int pseudo_exec(struct child_prog *child)
* /bin/foo invocation will fork and exec /bin/foo, even if * /bin/foo invocation will fork and exec /bin/foo, even if
* /bin/foo is a symlink to busybox. * /bin/foo is a symlink to busybox.
*/ */
search_applet.name = child->argv[0]; name = child->argv[0];
#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN #ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
/* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
@ -1412,13 +1412,11 @@ static int pseudo_exec(struct child_prog *child)
* /bin/cat exists on the filesystem and is _not_ busybox. * /bin/cat exists on the filesystem and is _not_ busybox.
* Some systems want this, others do not. Choose wisely. :-) * Some systems want this, others do not. Choose wisely. :-)
*/ */
search_applet.name = get_last_path_component(search_applet.name); name = get_last_path_component(name);
#endif #endif
/* Do a binary search to find the applet entry given the name. */ /* Do a binary search to find the applet entry given the name. */
applet = bsearch(&search_applet, applets, NUM_APPLETS, if ((applet = find_applet_by_name(name)) != NULL) {
sizeof(struct BB_applet), applet_name_compare);
if (applet != NULL) {
int argc_l; int argc_l;
char** argv=child->argv; char** argv=child->argv;
for(argc_l=0;*argv!=NULL; argv++, argc_l++); for(argc_l=0;*argv!=NULL; argv++, argc_l++);

12
sh.c
View File

@ -67,7 +67,6 @@
static const int MAX_LINE = 256; /* size of input buffer for cwd data */ static const int MAX_LINE = 256; /* size of input buffer for cwd data */
static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
extern size_t NUM_APPLETS;
enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE, enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
@ -1371,7 +1370,8 @@ static int pseudo_exec(struct child_prog *child)
{ {
struct built_in_command *x; struct built_in_command *x;
#ifdef BB_FEATURE_SH_STANDALONE_SHELL #ifdef BB_FEATURE_SH_STANDALONE_SHELL
struct BB_applet search_applet, *applet; struct BB_applet *applet;
const char *name;
#endif #endif
/* Check if the command matches any of the non-forking builtins. /* Check if the command matches any of the non-forking builtins.
@ -1404,7 +1404,7 @@ static int pseudo_exec(struct child_prog *child)
* /bin/foo invocation will fork and exec /bin/foo, even if * /bin/foo invocation will fork and exec /bin/foo, even if
* /bin/foo is a symlink to busybox. * /bin/foo is a symlink to busybox.
*/ */
search_applet.name = child->argv[0]; name = child->argv[0];
#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN #ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
/* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
@ -1412,13 +1412,11 @@ static int pseudo_exec(struct child_prog *child)
* /bin/cat exists on the filesystem and is _not_ busybox. * /bin/cat exists on the filesystem and is _not_ busybox.
* Some systems want this, others do not. Choose wisely. :-) * Some systems want this, others do not. Choose wisely. :-)
*/ */
search_applet.name = get_last_path_component(search_applet.name); name = get_last_path_component(name);
#endif #endif
/* Do a binary search to find the applet entry given the name. */ /* Do a binary search to find the applet entry given the name. */
applet = bsearch(&search_applet, applets, NUM_APPLETS, if ((applet = find_applet_by_name(name)) != NULL) {
sizeof(struct BB_applet), applet_name_compare);
if (applet != NULL) {
int argc_l; int argc_l;
char** argv=child->argv; char** argv=child->argv;
for(argc_l=0;*argv!=NULL; argv++, argc_l++); for(argc_l=0;*argv!=NULL; argv++, argc_l++);

View File

@ -67,7 +67,6 @@
static const int MAX_LINE = 256; /* size of input buffer for cwd data */ static const int MAX_LINE = 256; /* size of input buffer for cwd data */
static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
extern size_t NUM_APPLETS;
enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE, enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
@ -1371,7 +1370,8 @@ static int pseudo_exec(struct child_prog *child)
{ {
struct built_in_command *x; struct built_in_command *x;
#ifdef BB_FEATURE_SH_STANDALONE_SHELL #ifdef BB_FEATURE_SH_STANDALONE_SHELL
struct BB_applet search_applet, *applet; struct BB_applet *applet;
const char *name;
#endif #endif
/* Check if the command matches any of the non-forking builtins. /* Check if the command matches any of the non-forking builtins.
@ -1404,7 +1404,7 @@ static int pseudo_exec(struct child_prog *child)
* /bin/foo invocation will fork and exec /bin/foo, even if * /bin/foo invocation will fork and exec /bin/foo, even if
* /bin/foo is a symlink to busybox. * /bin/foo is a symlink to busybox.
*/ */
search_applet.name = child->argv[0]; name = child->argv[0];
#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN #ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
/* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
@ -1412,13 +1412,11 @@ static int pseudo_exec(struct child_prog *child)
* /bin/cat exists on the filesystem and is _not_ busybox. * /bin/cat exists on the filesystem and is _not_ busybox.
* Some systems want this, others do not. Choose wisely. :-) * Some systems want this, others do not. Choose wisely. :-)
*/ */
search_applet.name = get_last_path_component(search_applet.name); name = get_last_path_component(name);
#endif #endif
/* Do a binary search to find the applet entry given the name. */ /* Do a binary search to find the applet entry given the name. */
applet = bsearch(&search_applet, applets, NUM_APPLETS, if ((applet = find_applet_by_name(name)) != NULL) {
sizeof(struct BB_applet), applet_name_compare);
if (applet != NULL) {
int argc_l; int argc_l;
char** argv=child->argv; char** argv=child->argv;
for(argc_l=0;*argv!=NULL; argv++, argc_l++); for(argc_l=0;*argv!=NULL; argv++, argc_l++);

View File

@ -1691,12 +1691,20 @@ FILE *xfopen(const char *path, const char *mode)
} }
#endif #endif
int applet_name_compare(const void *x, const void *y) static int applet_name_compare(const void *x, const void *y)
{ {
const struct BB_applet *applet1 = x; const char *name = x;
const struct BB_applet *applet2 = y; const struct BB_applet *applet = y;
return strcmp(applet1->name, applet2->name); return strcmp(name, applet->name);
}
extern size_t NUM_APPLETS;
struct BB_applet *find_applet_by_name(const char *name)
{
return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
applet_name_compare);
} }
#if defined BB_DD || defined BB_TAIL #if defined BB_DD || defined BB_TAIL