aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2001-02-01 19:21:20 +0000
committerMatt Kraai <kraai@debian.org>2001-02-01 19:21:20 +0000
commitf2cc2762bb273fbabe8c2eadeb3b8669550582f0 (patch)
treee1bc52067709069328f058b80b9a1273c7c240d2
parent05e782ddd3dc58245c889529bb8aeeaddf24bf71 (diff)
downloadbusybox-w32-f2cc2762bb273fbabe8c2eadeb3b8669550582f0.tar.gz
busybox-w32-f2cc2762bb273fbabe8c2eadeb3b8669550582f0.tar.bz2
busybox-w32-f2cc2762bb273fbabe8c2eadeb3b8669550582f0.zip
Create find_applet_by_name function. Save 32 bytes.
-rw-r--r--applets/busybox.c11
-rw-r--r--busybox.c11
-rw-r--r--busybox.h2
-rw-r--r--include/busybox.h2
-rw-r--r--lash.c12
-rw-r--r--sh.c12
-rw-r--r--shell/lash.c12
-rw-r--r--utility.c16
8 files changed, 37 insertions, 41 deletions
diff --git a/applets/busybox.c b/applets/busybox.c
index f10467a3b..1409efa54 100644
--- a/applets/busybox.c
+++ b/applets/busybox.c
@@ -87,8 +87,8 @@ static void install_links(const char *busybox, int use_symbolic_links)
87 87
88int main(int argc, char **argv) 88int main(int argc, char **argv)
89{ 89{
90 struct BB_applet search_applet, *applet; 90 struct BB_applet *applet;
91 const char *s; 91 const char *s;
92 92
93 for (s = applet_name = argv[0]; *s != '\0';) { 93 for (s = applet_name = argv[0]; *s != '\0';) {
94 if (*s++ == '/') 94 if (*s++ == '/')
@@ -104,12 +104,9 @@ int main(int argc, char **argv)
104#endif 104#endif
105 105
106 /* Do a binary search to find the applet entry given the name. */ 106 /* Do a binary search to find the applet entry given the name. */
107 search_applet.name = applet_name; 107 if ((applet = find_applet_by_name(applet_name)) != NULL) {
108 applet = bsearch(&search_applet, applets, NUM_APPLETS,
109 sizeof(struct BB_applet), applet_name_compare);
110 if (applet != NULL) {
111 if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0) 108 if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0)
112 usage(applet->usage); 109 usage(applet->usage);
113 exit((*(applet->main)) (argc, argv)); 110 exit((*(applet->main)) (argc, argv));
114 } 111 }
115 112
diff --git a/busybox.c b/busybox.c
index f10467a3b..1409efa54 100644
--- a/busybox.c
+++ b/busybox.c
@@ -87,8 +87,8 @@ static void install_links(const char *busybox, int use_symbolic_links)
87 87
88int main(int argc, char **argv) 88int main(int argc, char **argv)
89{ 89{
90 struct BB_applet search_applet, *applet; 90 struct BB_applet *applet;
91 const char *s; 91 const char *s;
92 92
93 for (s = applet_name = argv[0]; *s != '\0';) { 93 for (s = applet_name = argv[0]; *s != '\0';) {
94 if (*s++ == '/') 94 if (*s++ == '/')
@@ -104,12 +104,9 @@ int main(int argc, char **argv)
104#endif 104#endif
105 105
106 /* Do a binary search to find the applet entry given the name. */ 106 /* Do a binary search to find the applet entry given the name. */
107 search_applet.name = applet_name; 107 if ((applet = find_applet_by_name(applet_name)) != NULL) {
108 applet = bsearch(&search_applet, applets, NUM_APPLETS,
109 sizeof(struct BB_applet), applet_name_compare);
110 if (applet != NULL) {
111 if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0) 108 if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0)
112 usage(applet->usage); 109 usage(applet->usage);
113 exit((*(applet->main)) (argc, argv)); 110 exit((*(applet->main)) (argc, argv));
114 } 111 }
115 112
diff --git a/busybox.h b/busybox.h
index e332ed412..fc5e8d874 100644
--- a/busybox.h
+++ b/busybox.h
@@ -88,7 +88,6 @@ extern const struct BB_applet applets[];
88#undef PROTOTYPES 88#undef PROTOTYPES
89 89
90extern const char *applet_name; 90extern const char *applet_name;
91extern int applet_name_compare(const void *x, const void *y);
92 91
93extern void usage(const char *usage) __attribute__ ((noreturn)); 92extern void usage(const char *usage) __attribute__ ((noreturn));
94extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); 93extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
@@ -153,6 +152,7 @@ extern char *get_last_path_component(char *path);
153extern FILE *wfopen(const char *path, const char *mode); 152extern FILE *wfopen(const char *path, const char *mode);
154extern FILE *xfopen(const char *path, const char *mode); 153extern FILE *xfopen(const char *path, const char *mode);
155extern void chomp(char *s); 154extern void chomp(char *s);
155extern struct BB_applet *find_applet_by_name(const char *name);
156 156
157#ifndef DMALLOC 157#ifndef DMALLOC
158extern void *xmalloc (size_t size); 158extern void *xmalloc (size_t size);
diff --git a/include/busybox.h b/include/busybox.h
index e332ed412..fc5e8d874 100644
--- a/include/busybox.h
+++ b/include/busybox.h
@@ -88,7 +88,6 @@ extern const struct BB_applet applets[];
88#undef PROTOTYPES 88#undef PROTOTYPES
89 89
90extern const char *applet_name; 90extern const char *applet_name;
91extern int applet_name_compare(const void *x, const void *y);
92 91
93extern void usage(const char *usage) __attribute__ ((noreturn)); 92extern void usage(const char *usage) __attribute__ ((noreturn));
94extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); 93extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
@@ -153,6 +152,7 @@ extern char *get_last_path_component(char *path);
153extern FILE *wfopen(const char *path, const char *mode); 152extern FILE *wfopen(const char *path, const char *mode);
154extern FILE *xfopen(const char *path, const char *mode); 153extern FILE *xfopen(const char *path, const char *mode);
155extern void chomp(char *s); 154extern void chomp(char *s);
155extern struct BB_applet *find_applet_by_name(const char *name);
156 156
157#ifndef DMALLOC 157#ifndef DMALLOC
158extern void *xmalloc (size_t size); 158extern void *xmalloc (size_t size);
diff --git a/lash.c b/lash.c
index cc0d78687..87c37caa9 100644
--- a/lash.c
+++ b/lash.c
@@ -67,7 +67,6 @@
67static const int MAX_LINE = 256; /* size of input buffer for cwd data */ 67static const int MAX_LINE = 256; /* size of input buffer for cwd data */
68static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ 68static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
69#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" 69#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
70extern size_t NUM_APPLETS;
71 70
72 71
73enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE, 72enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
@@ -1371,7 +1370,8 @@ static int pseudo_exec(struct child_prog *child)
1371{ 1370{
1372 struct built_in_command *x; 1371 struct built_in_command *x;
1373#ifdef BB_FEATURE_SH_STANDALONE_SHELL 1372#ifdef BB_FEATURE_SH_STANDALONE_SHELL
1374 struct BB_applet search_applet, *applet; 1373 struct BB_applet *applet;
1374 const char *name;
1375#endif 1375#endif
1376 1376
1377 /* Check if the command matches any of the non-forking builtins. 1377 /* Check if the command matches any of the non-forking builtins.
@@ -1404,7 +1404,7 @@ static int pseudo_exec(struct child_prog *child)
1404 * /bin/foo invocation will fork and exec /bin/foo, even if 1404 * /bin/foo invocation will fork and exec /bin/foo, even if
1405 * /bin/foo is a symlink to busybox. 1405 * /bin/foo is a symlink to busybox.
1406 */ 1406 */
1407 search_applet.name = child->argv[0]; 1407 name = child->argv[0];
1408 1408
1409#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN 1409#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
1410 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then 1410 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
@@ -1412,13 +1412,11 @@ static int pseudo_exec(struct child_prog *child)
1412 * /bin/cat exists on the filesystem and is _not_ busybox. 1412 * /bin/cat exists on the filesystem and is _not_ busybox.
1413 * Some systems want this, others do not. Choose wisely. :-) 1413 * Some systems want this, others do not. Choose wisely. :-)
1414 */ 1414 */
1415 search_applet.name = get_last_path_component(search_applet.name); 1415 name = get_last_path_component(name);
1416#endif 1416#endif
1417 1417
1418 /* Do a binary search to find the applet entry given the name. */ 1418 /* Do a binary search to find the applet entry given the name. */
1419 applet = bsearch(&search_applet, applets, NUM_APPLETS, 1419 if ((applet = find_applet_by_name(name)) != NULL) {
1420 sizeof(struct BB_applet), applet_name_compare);
1421 if (applet != NULL) {
1422 int argc_l; 1420 int argc_l;
1423 char** argv=child->argv; 1421 char** argv=child->argv;
1424 for(argc_l=0;*argv!=NULL; argv++, argc_l++); 1422 for(argc_l=0;*argv!=NULL; argv++, argc_l++);
diff --git a/sh.c b/sh.c
index cc0d78687..87c37caa9 100644
--- a/sh.c
+++ b/sh.c
@@ -67,7 +67,6 @@
67static const int MAX_LINE = 256; /* size of input buffer for cwd data */ 67static const int MAX_LINE = 256; /* size of input buffer for cwd data */
68static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ 68static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
69#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" 69#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
70extern size_t NUM_APPLETS;
71 70
72 71
73enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE, 72enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
@@ -1371,7 +1370,8 @@ static int pseudo_exec(struct child_prog *child)
1371{ 1370{
1372 struct built_in_command *x; 1371 struct built_in_command *x;
1373#ifdef BB_FEATURE_SH_STANDALONE_SHELL 1372#ifdef BB_FEATURE_SH_STANDALONE_SHELL
1374 struct BB_applet search_applet, *applet; 1373 struct BB_applet *applet;
1374 const char *name;
1375#endif 1375#endif
1376 1376
1377 /* Check if the command matches any of the non-forking builtins. 1377 /* Check if the command matches any of the non-forking builtins.
@@ -1404,7 +1404,7 @@ static int pseudo_exec(struct child_prog *child)
1404 * /bin/foo invocation will fork and exec /bin/foo, even if 1404 * /bin/foo invocation will fork and exec /bin/foo, even if
1405 * /bin/foo is a symlink to busybox. 1405 * /bin/foo is a symlink to busybox.
1406 */ 1406 */
1407 search_applet.name = child->argv[0]; 1407 name = child->argv[0];
1408 1408
1409#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN 1409#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
1410 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then 1410 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
@@ -1412,13 +1412,11 @@ static int pseudo_exec(struct child_prog *child)
1412 * /bin/cat exists on the filesystem and is _not_ busybox. 1412 * /bin/cat exists on the filesystem and is _not_ busybox.
1413 * Some systems want this, others do not. Choose wisely. :-) 1413 * Some systems want this, others do not. Choose wisely. :-)
1414 */ 1414 */
1415 search_applet.name = get_last_path_component(search_applet.name); 1415 name = get_last_path_component(name);
1416#endif 1416#endif
1417 1417
1418 /* Do a binary search to find the applet entry given the name. */ 1418 /* Do a binary search to find the applet entry given the name. */
1419 applet = bsearch(&search_applet, applets, NUM_APPLETS, 1419 if ((applet = find_applet_by_name(name)) != NULL) {
1420 sizeof(struct BB_applet), applet_name_compare);
1421 if (applet != NULL) {
1422 int argc_l; 1420 int argc_l;
1423 char** argv=child->argv; 1421 char** argv=child->argv;
1424 for(argc_l=0;*argv!=NULL; argv++, argc_l++); 1422 for(argc_l=0;*argv!=NULL; argv++, argc_l++);
diff --git a/shell/lash.c b/shell/lash.c
index cc0d78687..87c37caa9 100644
--- a/shell/lash.c
+++ b/shell/lash.c
@@ -67,7 +67,6 @@
67static const int MAX_LINE = 256; /* size of input buffer for cwd data */ 67static const int MAX_LINE = 256; /* size of input buffer for cwd data */
68static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ 68static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
69#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" 69#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
70extern size_t NUM_APPLETS;
71 70
72 71
73enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE, 72enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
@@ -1371,7 +1370,8 @@ static int pseudo_exec(struct child_prog *child)
1371{ 1370{
1372 struct built_in_command *x; 1371 struct built_in_command *x;
1373#ifdef BB_FEATURE_SH_STANDALONE_SHELL 1372#ifdef BB_FEATURE_SH_STANDALONE_SHELL
1374 struct BB_applet search_applet, *applet; 1373 struct BB_applet *applet;
1374 const char *name;
1375#endif 1375#endif
1376 1376
1377 /* Check if the command matches any of the non-forking builtins. 1377 /* Check if the command matches any of the non-forking builtins.
@@ -1404,7 +1404,7 @@ static int pseudo_exec(struct child_prog *child)
1404 * /bin/foo invocation will fork and exec /bin/foo, even if 1404 * /bin/foo invocation will fork and exec /bin/foo, even if
1405 * /bin/foo is a symlink to busybox. 1405 * /bin/foo is a symlink to busybox.
1406 */ 1406 */
1407 search_applet.name = child->argv[0]; 1407 name = child->argv[0];
1408 1408
1409#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN 1409#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
1410 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then 1410 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
@@ -1412,13 +1412,11 @@ static int pseudo_exec(struct child_prog *child)
1412 * /bin/cat exists on the filesystem and is _not_ busybox. 1412 * /bin/cat exists on the filesystem and is _not_ busybox.
1413 * Some systems want this, others do not. Choose wisely. :-) 1413 * Some systems want this, others do not. Choose wisely. :-)
1414 */ 1414 */
1415 search_applet.name = get_last_path_component(search_applet.name); 1415 name = get_last_path_component(name);
1416#endif 1416#endif
1417 1417
1418 /* Do a binary search to find the applet entry given the name. */ 1418 /* Do a binary search to find the applet entry given the name. */
1419 applet = bsearch(&search_applet, applets, NUM_APPLETS, 1419 if ((applet = find_applet_by_name(name)) != NULL) {
1420 sizeof(struct BB_applet), applet_name_compare);
1421 if (applet != NULL) {
1422 int argc_l; 1420 int argc_l;
1423 char** argv=child->argv; 1421 char** argv=child->argv;
1424 for(argc_l=0;*argv!=NULL; argv++, argc_l++); 1422 for(argc_l=0;*argv!=NULL; argv++, argc_l++);
diff --git a/utility.c b/utility.c
index 0a0e652af..69403c8c2 100644
--- a/utility.c
+++ b/utility.c
@@ -1691,12 +1691,20 @@ FILE *xfopen(const char *path, const char *mode)
1691} 1691}
1692#endif 1692#endif
1693 1693
1694int applet_name_compare(const void *x, const void *y) 1694static int applet_name_compare(const void *x, const void *y)
1695{ 1695{
1696 const struct BB_applet *applet1 = x; 1696 const char *name = x;
1697 const struct BB_applet *applet2 = y; 1697 const struct BB_applet *applet = y;
1698 1698
1699 return strcmp(applet1->name, applet2->name); 1699 return strcmp(name, applet->name);
1700}
1701
1702extern size_t NUM_APPLETS;
1703
1704struct BB_applet *find_applet_by_name(const char *name)
1705{
1706 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
1707 applet_name_compare);
1700} 1708}
1701 1709
1702#if defined BB_DD || defined BB_TAIL 1710#if defined BB_DD || defined BB_TAIL