klogd: code de-obfuscation with small code size reduction
[oweals/busybox.git] / applets / busybox.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * BusyBox' main applet dispatcher.
4  *
5  * Licensed under GPLv2, see file LICENSE in this tarball for details.
6  */
7 #include "busybox.h"
8
9 const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
10
11 #ifdef CONFIG_FEATURE_INSTALLER
12 /*
13  * directory table
14  *              this should be consistent w/ the enum, busybox.h::Location,
15  *              or else...
16  */
17 static const char usr_bin [] = "/usr/bin";
18 static const char usr_sbin[] = "/usr/sbin";
19
20 static const char* const install_dir[] = {
21         &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
22         &usr_bin [4], /* "/bin" */
23         &usr_sbin[4], /* "/sbin" */
24         usr_bin,
25         usr_sbin
26 };
27
28 /* abstract link() */
29 typedef int (*link_func)(const char *, const char *);
30
31 /* create (sym)links for each applet */
32 static void install_links(const char *busybox, int use_symbolic_links)
33 {
34         link_func lf = link;
35         char *fpc;
36         int i;
37         int rc;
38
39         if (use_symbolic_links)
40                 lf = symlink;
41
42         for (i = 0; applets[i].name != NULL; i++) {
43                 fpc = concat_path_file(
44                                 install_dir[applets[i].location],
45                                 applets[i].name);
46                 rc = lf(busybox, fpc);
47                 if (rc != 0 && errno != EEXIST) {
48                         bb_perror_msg("%s", fpc);
49                 }
50                 free(fpc);
51         }
52 }
53
54 #else
55 #define install_links(x,y)
56 #endif /* CONFIG_FEATURE_INSTALLER */
57
58 int main(int argc, char **argv)
59 {
60         const char *s;
61
62         applet_name = argv[0];
63         if (*applet_name == '-')
64                 applet_name++;
65         while ((s = strchr(applet_name, '/')))
66                 applet_name = s + 1;
67
68         /* Set locale for everybody except 'init' */
69         if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
70                 setlocale(LC_ALL, "");
71
72         run_applet_by_name(applet_name, argc, argv);
73         bb_error_msg_and_die("applet not found");
74 }
75
76 int busybox_main(int argc, char **argv);
77 int busybox_main(int argc, char **argv)
78 {
79         /*
80          * This style of argument parsing doesn't scale well
81          * in the event that busybox starts wanting more --options.
82          * If someone has a cleaner approach, by all means implement it.
83          */
84         if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
85                 int use_symbolic_links = 0;
86                 char *busybox;
87
88                 /* to use symlinks, or not to use symlinks... */
89                 if (argc > 2)
90                         if (strcmp(argv[2], "-s") == 0)
91                                 use_symbolic_links = 1;
92
93                 /* link */
94 // XXX: FIXME: this is broken. Why not just use argv[0] ?
95                 busybox = xmalloc_readlink_or_warn("/proc/self/exe");
96                 if (!busybox)
97                         return 1;
98                 install_links(busybox, use_symbolic_links);
99                 if (ENABLE_FEATURE_CLEAN_UP)
100                         free(busybox);
101                 return 0;
102         }
103
104         /* Deal with --help.  (Also print help when called with no arguments) */
105
106         if (argc == 1 || !strcmp(argv[1], "--help") ) {
107                 if (argc > 2) {
108                         applet_name = argv[2];
109                         run_applet_by_name(applet_name, 2, argv);
110                 } else {
111                         const struct BB_applet *a;
112                         int col, output_width;
113
114                         output_width = 80 - sizeof("start-stop-daemon, ") - 8;
115                         if (ENABLE_FEATURE_AUTOWIDTH) {
116                                 /* Obtain the terminal width.  */
117                                 get_terminal_width_height(0, &output_width, NULL);
118                                 /* leading tab and room to wrap */
119                                 output_width -= sizeof("start-stop-daemon, ") + 8;
120                         }
121
122                         printf("%s\n"
123                                "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n"
124                                "Licensed under GPLv2.  See source distribution for full notice.\n\n"
125                                "Usage: busybox [function] [arguments]...\n"
126                                "   or: [function] [arguments]...\n\n"
127                                "\tBusyBox is a multi-call binary that combines many common Unix\n"
128                                "\tutilities into a single executable.  Most people will create a\n"
129                                "\tlink to busybox for each function they wish to use and BusyBox\n"
130                                "\twill act like whatever it was invoked as!\n"
131                                "\nCurrently defined functions:\n", bb_msg_full_version);
132                         col = 0;
133                         for (a = applets; a->name;) {
134                                 col += printf("%s%s", (col ? ", " : "\t"), a->name);
135                                 a++;
136                                 if (col > output_width && a->name) {
137                                         puts(",");
138                                         col = 0;
139                                 }
140                         }
141                         puts("\n");
142                         return 0;
143                 }
144         } else run_applet_by_name(argv[1], argc - 1, argv + 1);
145
146         bb_error_msg_and_die("applet not found");
147 }