summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-12-24 15:23:28 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-12-24 15:23:28 +0000
commit0ee3999d13b662d233dfc7decdd4d691cd66fdda (patch)
tree283bf4908f787f50a90cb84c014a580485307a9b
parent7cea262273659e9894d446a3f9ea6b83fb18892a (diff)
downloadbusybox-w32-0ee3999d13b662d233dfc7decdd4d691cd66fdda.tar.gz
busybox-w32-0ee3999d13b662d233dfc7decdd4d691cd66fdda.tar.bz2
busybox-w32-0ee3999d13b662d233dfc7decdd4d691cd66fdda.zip
random tiny size savings
-rw-r--r--applets/applets.c18
-rw-r--r--applets/busybox.c66
-rw-r--r--include/busybox.h5
-rw-r--r--shell/ash.c16
-rw-r--r--shell/msh.c8
5 files changed, 53 insertions, 60 deletions
diff --git a/applets/applets.c b/applets/applets.c
index f8abb2767..ebd1ff313 100644
--- a/applets/applets.c
+++ b/applets/applets.c
@@ -44,7 +44,7 @@ static const char usage_messages[] =
44static struct BB_applet *applet_using; 44static struct BB_applet *applet_using;
45 45
46/* The -1 arises because of the {0,NULL,0,-1} entry above. */ 46/* The -1 arises because of the {0,NULL,0,-1} entry above. */
47const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1); 47const unsigned short NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
48 48
49 49
50#ifdef CONFIG_FEATURE_SUID_CONFIG 50#ifdef CONFIG_FEATURE_SUID_CONFIG
@@ -459,8 +459,6 @@ static int applet_name_compare(const void *name, const void *vapplet)
459 return strcmp(name, applet->name); 459 return strcmp(name, applet->name);
460} 460}
461 461
462extern const size_t NUM_APPLETS;
463
464struct BB_applet *find_applet_by_name(const char *name) 462struct BB_applet *find_applet_by_name(const char *name)
465{ 463{
466 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet), 464 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
@@ -469,15 +467,19 @@ struct BB_applet *find_applet_by_name(const char *name)
469 467
470void run_applet_by_name(const char *name, int argc, char **argv) 468void run_applet_by_name(const char *name, int argc, char **argv)
471{ 469{
472 if (ENABLE_FEATURE_SUID_CONFIG) parse_config_file(); 470 if (ENABLE_FEATURE_SUID_CONFIG)
471 parse_config_file();
473 472
474 if (!strncmp(name, "busybox", 7)) busybox_main(argc, argv); 473 if (!strncmp(name, "busybox", 7))
474 exit(busybox_main(argc, argv));
475 /* Do a binary search to find the applet entry given the name. */ 475 /* Do a binary search to find the applet entry given the name. */
476 applet_using = find_applet_by_name(name); 476 applet_using = find_applet_by_name(name);
477 if (applet_using) { 477 if (applet_using) {
478 applet_name = applet_using->name; 478 applet_name = applet_using->name;
479 if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage(); 479 if (argc == 2 && !strcmp(argv[1], "--help"))
480 if(ENABLE_FEATURE_SUID) check_suid(applet_using); 480 bb_show_usage();
481 exit((*(applet_using->main))(argc, argv)); 481 if (ENABLE_FEATURE_SUID)
482 check_suid(applet_using);
483 exit(applet_using->main(argc, argv));
482 } 484 }
483} 485}
diff --git a/applets/busybox.c b/applets/busybox.c
index bb9eb3af7..9ca12ac2b 100644
--- a/applets/busybox.c
+++ b/applets/busybox.c
@@ -14,8 +14,8 @@ const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
14 * this should be consistent w/ the enum, busybox.h::Location, 14 * this should be consistent w/ the enum, busybox.h::Location,
15 * or else... 15 * or else...
16 */ 16 */
17static const char usr_bin [] ="/usr/bin"; 17static const char usr_bin [] = "/usr/bin";
18static const char usr_sbin[] ="/usr/sbin"; 18static const char usr_sbin[] = "/usr/sbin";
19 19
20static const char* const install_dir[] = { 20static const char* const install_dir[] = {
21 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */ 21 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
@@ -26,25 +26,25 @@ static const char* const install_dir[] = {
26}; 26};
27 27
28/* abstract link() */ 28/* abstract link() */
29typedef int (*__link_f)(const char *, const char *); 29typedef int (*link_func)(const char *, const char *);
30 30
31/* create (sym)links for each applet */ 31/* create (sym)links for each applet */
32static void install_links(const char *busybox, int use_symbolic_links) 32static void install_links(const char *busybox, int use_symbolic_links)
33{ 33{
34 __link_f Link = link; 34 link_func lf = link;
35
36 char *fpc; 35 char *fpc;
37 int i; 36 int i;
38 int rc; 37 int rc;
39 38
40 if (use_symbolic_links) 39 if (use_symbolic_links)
41 Link = symlink; 40 lf = symlink;
42 41
43 for (i = 0; applets[i].name != NULL; i++) { 42 for (i = 0; applets[i].name != NULL; i++) {
44 fpc = concat_path_file( 43 fpc = concat_path_file(
45 install_dir[applets[i].location], applets[i].name); 44 install_dir[applets[i].location],
46 rc = Link(busybox, fpc); 45 applets[i].name);
47 if (rc!=0 && errno!=EEXIST) { 46 rc = lf(busybox, fpc);
47 if (rc != 0 && errno != EEXIST) {
48 bb_perror_msg("%s", fpc); 48 bb_perror_msg("%s", fpc);
49 } 49 }
50 free(fpc); 50 free(fpc);
@@ -59,10 +59,11 @@ int main(int argc, char **argv)
59{ 59{
60 const char *s; 60 const char *s;
61 61
62 applet_name=argv[0]; 62 applet_name = argv[0];
63 if (*applet_name == '-') applet_name++; 63 if (*applet_name == '-')
64 for (s = applet_name; *s ;) 64 applet_name++;
65 if (*(s++) == '/') applet_name = s; 65 while ((s = strchr(applet_name, '/')))
66 applet_name = s + 1;
66 67
67 /* Set locale for everybody except 'init' */ 68 /* Set locale for everybody except 'init' */
68 if (ENABLE_LOCALE_SUPPORT && getpid() != 1) 69 if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
@@ -81,44 +82,41 @@ int busybox_main(int argc, char **argv)
81 */ 82 */
82 if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) { 83 if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
83 int use_symbolic_links = 0; 84 int use_symbolic_links = 0;
84 int rc = 0;
85 char *busybox; 85 char *busybox;
86 86
87 /* to use symlinks, or not to use symlinks... */ 87 /* to use symlinks, or not to use symlinks... */
88 if (argc > 2) { 88 if (argc > 2)
89 if ((strcmp(argv[2], "-s") == 0)) { 89 if (strcmp(argv[2], "-s") == 0)
90 use_symbolic_links = 1; 90 use_symbolic_links = 1;
91 }
92 }
93 91
94 /* link */ 92 /* link */
95// XXX: FIXME: this is broken. Why not just use argv[0] ? 93// XXX: FIXME: this is broken. Why not just use argv[0] ?
96 busybox = xreadlink("/proc/self/exe"); 94 busybox = xreadlink("/proc/self/exe");
97 if (busybox) { 95 if (!busybox)
98 install_links(busybox, use_symbolic_links); 96 return 1;
97 install_links(busybox, use_symbolic_links);
98 if (ENABLE_FEATURE_CLEAN_UP)
99 free(busybox); 99 free(busybox);
100 } else { 100 return 0;
101 rc = 1;
102 }
103 return rc;
104 } 101 }
105 102
106 /* Deal with --help. (Also print help when called with no arguments) */ 103 /* Deal with --help. (Also print help when called with no arguments) */
107 104
108 if (argc==1 || !strcmp(argv[1],"--help") ) { 105 if (argc == 1 || !strcmp(argv[1], "--help") ) {
109 if (argc>2) { 106 if (argc > 2) {
110 applet_name = argv[2]; 107 applet_name = argv[2];
111 run_applet_by_name(applet_name, 2, argv); 108 run_applet_by_name(applet_name, 2, argv);
112 } else { 109 } else {
113 const struct BB_applet *a; 110 const struct BB_applet *a;
114 int col, output_width; 111 int col, output_width;
115 112
113 output_width = 80 - sizeof("start-stop-daemon, ") - 8;
116 if (ENABLE_FEATURE_AUTOWIDTH) { 114 if (ENABLE_FEATURE_AUTOWIDTH) {
117 /* Obtain the terminal width. */ 115 /* Obtain the terminal width. */
118 get_terminal_width_height(0, &output_width, NULL); 116 get_terminal_width_height(0, &output_width, NULL);
119 /* leading tab and room to wrap */ 117 /* leading tab and room to wrap */
120 output_width -= sizeof("start-stop-daemon, ") + 8; 118 output_width -= sizeof("start-stop-daemon, ") + 8;
121 } else output_width = 80 - sizeof("start-stop-daemon, ") - 8; 119 }
122 120
123 printf("%s\n" 121 printf("%s\n"
124 "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n" 122 "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n"
@@ -130,19 +128,19 @@ int busybox_main(int argc, char **argv)
130 "\tlink to busybox for each function they wish to use and BusyBox\n" 128 "\tlink to busybox for each function they wish to use and BusyBox\n"
131 "\twill act like whatever it was invoked as!\n" 129 "\twill act like whatever it was invoked as!\n"
132 "\nCurrently defined functions:\n", bb_msg_full_version); 130 "\nCurrently defined functions:\n", bb_msg_full_version);
133 131 col = 0;
134 col=0;
135 for(a = applets; a->name;) { 132 for(a = applets; a->name;) {
136 col += printf("%s%s", (col ? ", " : "\t"), (a++)->name); 133 col += printf("%s%s", (col ? ", " : "\t"), a->name);
134 a++;
137 if (col > output_width && a->name) { 135 if (col > output_width && a->name) {
138 printf(",\n"); 136 puts(",");
139 col = 0; 137 col = 0;
140 } 138 }
141 } 139 }
142 printf("\n\n"); 140 puts("\n");
143 exit(0); 141 return 0;
144 } 142 }
145 } else run_applet_by_name(argv[1], argc-1, argv+1); 143 } else run_applet_by_name(argv[1], argc - 1, argv + 1);
146 144
147 bb_error_msg_and_die("applet not found"); 145 bb_error_msg_and_die("applet not found");
148} 146}
diff --git a/include/busybox.h b/include/busybox.h
index d20337ff1..a7a0f4915 100644
--- a/include/busybox.h
+++ b/include/busybox.h
@@ -9,7 +9,7 @@
9 9
10#include "libbb.h" 10#include "libbb.h"
11 11
12/* order matters: used as index into "install_dir[]" in busybox.c */ 12/* order matters: used as index into "install_dir[]" in busybox.c */
13enum Location { 13enum Location {
14 _BB_DIR_ROOT = 0, 14 _BB_DIR_ROOT = 0,
15 _BB_DIR_BIN, 15 _BB_DIR_BIN,
@@ -31,8 +31,9 @@ struct BB_applet {
31 __extension__ enum SUIDRoot need_suid:4; 31 __extension__ enum SUIDRoot need_suid:4;
32}; 32};
33 33
34/* From busybox.c */ 34/* From busybox.c and applet.c */
35extern const struct BB_applet applets[]; 35extern const struct BB_applet applets[];
36extern const unsigned short NUM_APPLETS;
36 37
37/* Automagically pull in all the applet function prototypes and 38/* Automagically pull in all the applet function prototypes and
38 * applet usage strings. These are all of the form: 39 * applet usage strings. These are all of the form:
diff --git a/shell/ash.c b/shell/ash.c
index 2de61dad9..e8f7d30bd 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -11893,17 +11893,11 @@ static int helpcmd(int argc, char **argv)
11893 } 11893 }
11894 } 11894 }
11895#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL 11895#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
11896 { 11896 for (i = 0; i < NUM_APPLETS; i++) {
11897 extern const struct BB_applet applets[]; 11897 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), applets[i].name);
11898 extern const size_t NUM_APPLETS; 11898 if (col > 60) {
11899 11899 out1fmt("\n");
11900 for (i = 0; i < NUM_APPLETS; i++) { 11900 col = 0;
11901
11902 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), applets[i].name);
11903 if (col > 60) {
11904 out1fmt("\n");
11905 col = 0;
11906 }
11907 } 11901 }
11908 } 11902 }
11909#endif 11903#endif
diff --git a/shell/msh.c b/shell/msh.c
index 45ca3df81..d4f534551 100644
--- a/shell/msh.c
+++ b/shell/msh.c
@@ -3243,8 +3243,8 @@ static int dohelp(struct op *t)
3243 int col; 3243 int col;
3244 const struct builtincmd *x; 3244 const struct builtincmd *x;
3245 3245
3246 printf("\nBuilt-in commands:\n"); 3246 puts("\nBuilt-in commands:\n"
3247 printf("-------------------\n"); 3247 "-------------------");
3248 3248
3249 for (col = 0, x = builtincmds; x->builtinfunc != NULL; x++) { 3249 for (col = 0, x = builtincmds; x->builtinfunc != NULL; x++) {
3250 if (!x->name) 3250 if (!x->name)
@@ -3259,8 +3259,6 @@ static int dohelp(struct op *t)
3259 { 3259 {
3260 int i; 3260 int i;
3261 const struct BB_applet *applet; 3261 const struct BB_applet *applet;
3262 extern const struct BB_applet applets[];
3263 extern const size_t NUM_APPLETS;
3264 3262
3265 for (i = 0, applet = applets; i < NUM_APPLETS; applet++, i++) { 3263 for (i = 0, applet = applets; i < NUM_APPLETS; applet++, i++) {
3266 if (!applet->name) 3264 if (!applet->name)
@@ -3274,7 +3272,7 @@ static int dohelp(struct op *t)
3274 } 3272 }
3275 } 3273 }
3276#endif 3274#endif
3277 printf("\n\n"); 3275 puts("\n");
3278 return EXIT_SUCCESS; 3276 return EXIT_SUCCESS;
3279} 3277}
3280 3278