process utilities related style cleanup
[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 *bb_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_f)(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_f Link = link;
35
36         char *fpc;
37         int i;
38         int rc;
39
40         if (use_symbolic_links)
41                 Link = symlink;
42
43         for (i = 0; applets[i].name != NULL; i++) {
44                 fpc = concat_path_file(
45                         install_dir[applets[i].location], applets[i].name);
46                 rc = Link(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         bb_applet_name=argv[0];
63         if (*bb_applet_name == '-') bb_applet_name++;
64         for (s = bb_applet_name; *s ;)
65                 if (*(s++) == '/') bb_applet_name = s;
66
67         /* Set locale for everybody except `init' */
68         if(ENABLE_LOCALE_SUPPORT && getpid() != 1)
69                 setlocale(LC_ALL, "");
70
71         run_applet_by_name(bb_applet_name, argc, argv);
72         bb_error_msg_and_die("applet not found");
73 }
74
75 int busybox_main(int argc, char **argv)
76 {
77         /*
78          * This style of argument parsing doesn't scale well
79          * in the event that busybox starts wanting more --options.
80          * If someone has a cleaner approach, by all means implement it.
81          */
82         if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
83                 int use_symbolic_links = 0;
84                 int rc = 0;
85                 char *busybox;
86
87                 /* to use symlinks, or not to use symlinks... */
88                 if (argc > 2) {
89                         if ((strcmp(argv[2], "-s") == 0)) {
90                                 use_symbolic_links = 1;
91                         }
92                 }
93
94                 /* link */
95                 busybox = xreadlink("/proc/self/exe");
96                 if (busybox) {
97                         install_links(busybox, use_symbolic_links);
98                         free(busybox);
99                 } else {
100                         rc = 1;
101                 }
102                 return rc;
103         }
104
105         /* Deal with --help.  (Also print help when called with no arguments) */
106
107         if (argc==1 || !strcmp(argv[1],"--help") ) {
108                 if (argc>2) {
109                         run_applet_by_name(bb_applet_name=argv[2], 2, 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"
122                                    "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n"
123                                    "Licensed under GPLv2.  See source distribution for full notice.\n\n"
124                                "Usage: busybox [function] [arguments]...\n"
125                                "   or: [function] [arguments]...\n\n"
126                                "\tBusyBox is a multi-call binary that combines many common Unix\n"
127                                "\tutilities into a single executable.  Most people will create a\n"
128                                "\tlink to busybox for each function they wish to use and BusyBox\n"
129                                "\twill act like whatever it was invoked as!\n"
130                                "\nCurrently defined functions:\n", bb_msg_full_version);
131
132                         col=0;
133                         for(a = applets; a->name;) {
134                                 col += printf("%s%s", (col ? ", " : "\t"), (a++)->name);
135                                 if (col > output_width && a->name) {
136                                         printf(",\n");
137                                         col = 0;
138                                 }
139                         }
140                         printf("\n\n");
141                         exit(0);
142                 }
143         } else run_applet_by_name(argv[1], argc-1, argv+1);
144
145         bb_error_msg_and_die("applet not found");
146 }