1 /* vi: set sw=4 ts=4: */
3 * BusyBox' main applet dispatcher.
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 #if ENABLE_LOCALE_SUPPORT
16 #define setlocale(x,y)
19 const char *bb_applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
21 #ifdef CONFIG_FEATURE_INSTALLER
24 * this should be consistent w/ the enum, busybox.h::Location,
27 static const char usr_bin [] ="/usr/bin";
28 static const char usr_sbin[] ="/usr/sbin";
30 static const char* const install_dir[] = {
31 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
32 &usr_bin [4], /* "/bin" */
33 &usr_sbin[4], /* "/sbin" */
39 typedef int (*__link_f)(const char *, const char *);
41 /* create (sym)links for each applet */
42 static void install_links(const char *busybox, int use_symbolic_links)
50 if (use_symbolic_links)
53 for (i = 0; applets[i].name != NULL; i++) {
54 fpc = concat_path_file(
55 install_dir[applets[i].location], applets[i].name);
56 rc = Link(busybox, fpc);
57 if (rc!=0 && errno!=EEXIST) {
58 bb_perror_msg("%s", fpc);
65 #define install_links(x,y)
66 #endif /* CONFIG_FEATURE_INSTALLER */
68 int main(int argc, char **argv)
72 bb_applet_name=argv[0];
73 if (*bb_applet_name == '-') bb_applet_name++;
74 for (s = bb_applet_name; *s ;)
75 if (*(s++) == '/') bb_applet_name = s;
77 /* Set locale for everybody except `init' */
78 if(ENABLE_LOCALE_SUPPORT && getpid() != 1)
79 setlocale(LC_ALL, "");
81 run_applet_by_name(bb_applet_name, argc, argv);
82 bb_error_msg_and_die("applet not found");
85 int busybox_main(int argc, char **argv)
88 * This style of argument parsing doesn't scale well
89 * in the event that busybox starts wanting more --options.
90 * If someone has a cleaner approach, by all means implement it.
92 if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
93 int use_symbolic_links = 0;
97 /* to use symlinks, or not to use symlinks... */
99 if ((strcmp(argv[2], "-s") == 0)) {
100 use_symbolic_links = 1;
105 busybox = xreadlink("/proc/self/exe");
107 install_links(busybox, use_symbolic_links);
115 /* Deal with --help. (Also print help when called with no arguments) */
117 if (argc==1 || !strcmp(argv[1],"--help") ) {
119 run_applet_by_name(bb_applet_name=argv[2], 2, argv);
121 const struct BB_applet *a;
122 int col, output_width;
124 if (ENABLE_FEATURE_AUTOWIDTH) {
125 /* Obtain the terminal width. */
126 get_terminal_width_height(0, &output_width, NULL);
127 /* leading tab and room to wrap */
129 } else output_width = 60;
132 "Usage: busybox [function] [arguments]...\n"
133 " or: [function] [arguments]...\n\n"
134 "\tBusyBox is a multi-call binary that combines many common Unix\n"
135 "\tutilities into a single executable. Most people will create a\n"
136 "\tlink to busybox for each function they wish to use and BusyBox\n"
137 "\twill act like whatever it was invoked as!\n"
138 "\nCurrently defined functions:\n", bb_msg_full_version);
141 for(a = applets; a->name;) {
142 col += printf("%s%s", (col ? ", " : "\t"), (a++)->name);
143 if (col > output_width && a->name) {
151 } else run_applet_by_name(argv[1], argc-1, argv+1);
153 bb_error_msg_and_die("applet not found");