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