420c9c0e5d350ffec83ae5431056ca289917006a
[oweals/busybox.git] / applets / busybox.c
1 /* vi: set sw=4 ts=4: */
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include "busybox.h"
8 #ifdef CONFIG_LOCALE_SUPPORT
9 #include <locale.h>
10 #endif
11
12 const char *bb_applet_name;
13
14 #ifdef CONFIG_FEATURE_INSTALLER
15 /*
16  * directory table
17  *              this should be consistent w/ the enum, busybox.h::Location,
18  *              or else...
19  */
20 static const char usr_bin [] ="/usr/bin";
21 static const char usr_sbin[] ="/usr/sbin";
22
23 static const char* const install_dir[] = {
24         &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
25         &usr_bin [4], /* "/bin" */
26         &usr_sbin[4], /* "/sbin" */
27         usr_bin,
28         usr_sbin
29 };
30
31 /* abstract link() */
32 typedef int (*__link_f)(const char *, const char *);
33
34 /* create (sym)links for each applet */
35 static void install_links(const char *busybox, int use_symbolic_links)
36 {
37         __link_f Link = link;
38
39         char *fpc;
40         int i;
41         int rc;
42
43         if (use_symbolic_links)
44                 Link = symlink;
45
46         for (i = 0; applets[i].name != NULL; i++) {
47                 fpc = concat_path_file(
48                         install_dir[applets[i].location], applets[i].name);
49                 rc = Link(busybox, fpc);
50                 if (rc!=0 && errno!=EEXIST) {
51                         bb_perror_msg("%s", fpc);
52                 }
53                 free(fpc);
54         }
55 }
56
57 #endif /* CONFIG_FEATURE_INSTALLER */
58
59 int main(int argc, char **argv)
60 {
61         const char *s;
62
63         bb_applet_name=argv[0];
64         if (*bb_applet_name == '-') bb_applet_name++;
65         for (s = bb_applet_name; *s ;)
66                 if (*(s++) == '/') bb_applet_name = s;
67
68         /* Set locale for everybody except `init' */
69         if(ENABLE_LOCALE_SUPPORT && (!ENABLE_INIT || getpid()==1))
70                 setlocale(LC_ALL, "");
71
72         run_applet_by_name(bb_applet_name, argc, argv);
73         bb_error_msg_and_die("applet not found");
74 }
75
76 int busybox_main(int argc, char **argv)
77 {
78         /*
79          * This style of argument parsing doesn't scale well
80          * in the event that busybox starts wanting more --options.
81          * If someone has a cleaner approach, by all means implement it.
82          */
83         if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
84                 int use_symbolic_links = 0;
85                 int rc = 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                 }
94
95                 /* link */
96                 busybox = xreadlink("/proc/self/exe");
97                 if (busybox) {
98                         install_links(busybox, use_symbolic_links);
99                         free(busybox);
100                 } else {
101                         rc = 1;
102                 }
103                 return rc;
104         }
105
106         /* Deal with --help.  (Also print help when called with no arguments) */
107         
108         if (argc==1 || !strcmp(argv[1],"--help") ) {
109                 if (argc>2) run_applet_by_name(bb_applet_name=argv[2], argc, argv);
110                 else {
111                         const struct BB_applet *a;
112                         int col, output_width;
113
114                         if (ENABLE_FEATURE_AUTOWIDTH) {
115                                 /* Obtain the terminal width.  */
116                                 get_terminal_width_height(0, &output_width, NULL);
117                                 /* leading tab and room to wrap */
118                                 output_width -= 20;
119                         } else output_width = 60;
120
121                         printf("%s\n\n"
122                                "Usage: busybox [function] [arguments]...\n"
123                                "   or: [function] [arguments]...\n\n"
124                                "\tBusyBox is a multi-call binary that combines many common Unix\n"
125                                "\tutilities into a single executable.  Most people will create a\n"
126                                "\tlink to busybox for each function they wish to use and BusyBox\n"
127                                "\twill act like whatever it was invoked as!\n"
128                                "\nCurrently defined functions:\n", bb_msg_full_version);
129
130                         col=0;
131                         for(a = applets; a->name;) {
132                                 col += printf("%s%s", (col ? ", " : "\t"), (a++)->name);
133                                 if (col > output_width && a->name) {
134                                         printf(",\n");
135                                         col = 0;
136                                 }
137                         }
138                         printf("\n\n");
139                         exit(0);
140                 }
141         } else run_applet_by_name(bb_applet_name=argv[1], argc-1, argv+1);
142         
143         bb_error_msg_and_die("applet not found");
144 }
145
146 /*
147 Local Variables:
148 c-file-style: "linux"
149 c-basic-offset: 4
150 tab-width: 4
151 End:
152 */