1 /* vi: set sw=4 ts=4: */
9 #define bb_need_full_version
10 #define BB_DECLARE_EXTERN
13 #ifdef BB_LOCALE_SUPPORT
17 int been_there_done_that = 0; /* Also used in applets.c */
18 const char *applet_name;
20 #ifdef BB_FEATURE_INSTALLER
23 * this should be consistent w/ the enum, busybox.h::Location,
26 static char* install_dir[] = {
35 typedef int (*__link_f)(const char *, const char *);
38 * Where in the filesystem is this busybox?
40 * malloc'd string w/ full pathname of busybox's location
43 static char *busybox_fullpath()
51 sprintf(proc, "/proc/%d/exe", pid);
52 len = readlink(proc, path, 256);
56 perror_msg("%s", proc);
62 /* create (sym)links for each applet */
63 static void install_links(const char *busybox, int use_symbolic_links)
71 if (use_symbolic_links)
74 for (i = 0; applets[i].name != NULL; i++) {
75 fpc = concat_path_file(
76 install_dir[applets[i].location], applets[i].name);
77 rc = Link(busybox, fpc);
78 if (rc!=0 && errno!=EEXIST) {
79 perror_msg("%s", fpc);
85 #endif /* BB_FEATURE_INSTALLER */
87 int main(int argc, char **argv)
91 for (s = applet_name = argv[0]; *s != '\0';) {
97 /* Add in a special case hack -- whenever **argv == '-'
98 * (i.e. '-su' or '-sh') always invoke the shell */
99 if (**argv == '-' && *(*argv+1)!= '-') {
104 #ifdef BB_LOCALE_SUPPORT
105 if(getpid()!=1) /* Do not set locale for `init' */
106 setlocale(LC_ALL, "");
109 run_applet_by_name(applet_name, argc, argv);
110 error_msg_and_die("applet not found");
114 int busybox_main(int argc, char **argv)
118 #ifdef BB_FEATURE_INSTALLER
120 * This style of argument parsing doesn't scale well
121 * in the event that busybox starts wanting more --options.
122 * If someone has a cleaner approach, by all means implement it.
124 if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
125 int use_symbolic_links = 0;
129 /* to use symlinks, or not to use symlinks... */
131 if ((strcmp(argv[2], "-s") == 0)) {
132 use_symbolic_links = 1;
137 busybox = busybox_fullpath();
139 install_links(busybox, use_symbolic_links);
146 #endif /* BB_FEATURE_INSTALLER */
150 /* If we've already been here once, exit now */
151 if (been_there_done_that == 1 || argc < 1) {
152 const struct BB_applet *a = applets;
154 fprintf(stderr, "%s\n\n"
155 "Usage: busybox [function] [arguments]...\n"
156 " or: [function] [arguments]...\n\n"
157 "\tBusyBox is a multi-call binary that combines many common Unix\n"
158 "\tutilities into a single executable. Most people will create a\n"
159 "\tlink to busybox for each function they wish to use, and BusyBox\n"
160 "\twill act like whatever it was invoked as.\n"
161 "\nCurrently defined functions:\n", full_version);
163 while (a->name != 0) {
165 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
167 if (col > 60 && a->name != 0) {
168 fprintf(stderr, ",\n");
172 fprintf(stderr, "\n\n");
176 /* Flag that we've been here already */
177 been_there_done_that = 1;
179 /* Move the command line down a notch */
180 len = argv[argc] + strlen(argv[argc]) - argv[1];
181 memmove(argv[0], argv[1], len);
182 memset(argv[0] + len, 0, argv[1] - argv[0]);
184 /* Fix up the argv pointers */
185 len = argv[1] - argv[0];
186 memmove(argv, argv + 1, sizeof(char *) * (argc + 1));
187 for (i = 0; i < argc; i++)
190 return (main(argc, argv));
195 c-file-style: "linux"