1 /* vi: set sw=4 ts=4: */
8 #define bb_need_full_version
9 #define BB_DECLARE_EXTERN
12 static int been_there_done_that = 0;
15 const char *applet_name;
17 #ifdef BB_FEATURE_INSTALLER
20 * this should be consistent w/ the enum, busybox.h::Location,
23 static char* install_dir[] = {
32 typedef int (*__link_f)(const char *, const char *);
35 * Where in the filesystem is this busybox?
37 * malloc'd string w/ full pathname of busybox's location
40 static char *busybox_fullpath()
48 sprintf(proc, "/proc/%d/exe", pid);
49 len = readlink(proc, path, 256);
53 errorMsg("%s: %s\n", proc, strerror(errno));
59 /* create (sym)links for each applet */
60 static void install_links(const char *busybox, int use_symbolic_links)
68 if (use_symbolic_links) Link = symlink;
70 for (i = 0; applets[i].name != NULL; i++) {
71 sprintf ( command, "%s/%s",
72 install_dir[applets[i].location],
74 rc = Link(busybox, command);
77 errorMsg("%s: %s\n", command, strerror(errno));
82 #endif /* BB_FEATURE_INSTALLER */
84 int main(int argc, char **argv)
86 struct BB_applet search_applet, *applet;
88 applet_name = "busybox";
90 #ifdef BB_FEATURE_INSTALLER
92 * This style of argument parsing doesn't scale well
93 * in the event that busybox starts wanting more --options.
94 * If someone has a cleaner approach, by all means implement it.
96 if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
97 int use_symbolic_links = 0;
101 /* to use symlinks, or not to use symlinks... */
103 if ((strcmp(argv[2], "-s") == 0)) {
104 use_symbolic_links = 1;
109 busybox = busybox_fullpath();
111 install_links(busybox, use_symbolic_links);
118 #endif /* BB_FEATURE_INSTALLER */
120 for (s = applet_name = argv[0]; *s != '\0';) {
125 *argv = (char*)applet_name;
128 /* Add in a special case hack -- whenever **argv == '-'
129 * (i.e. '-su' or '-sh') always invoke the shell */
130 if (**argv == '-' && *(*argv+1)!= '-') {
131 exit(((*(shell_main)) (argc, argv)));
135 /* Do a binary search to find the applet entry given the name. */
136 search_applet.name = applet_name;
137 applet = bsearch(&search_applet, applets, NUM_APPLETS,
138 sizeof(struct BB_applet), applet_name_compare);
139 if (applet != NULL) {
140 if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0)
141 usage(applet->usage);
142 exit((*(applet->main)) (argc, argv));
145 return(busybox_main(argc, argv));
149 int busybox_main(int argc, char **argv)
156 if (been_there_done_that == 1 || argc < 1) {
157 const struct BB_applet *a = applets;
159 fprintf(stderr, "%s\n\n"
160 "Usage: busybox [function] [arguments]...\n"
161 " or: [function] [arguments]...\n\n"
162 "\tBusyBox is a multi-call binary that combines many common Unix\n"
163 "\tutilities into a single executable. Most people will create a\n"
164 "\tlink to busybox for each function they wish to use, and BusyBox\n"
165 "\twill act like whatever it was invoked as.\n"
166 "\nCurrently defined functions:\n", full_version);
168 while (a->name != 0) {
170 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
172 if (col > 60 && a->name != 0) {
173 fprintf(stderr, ",\n");
177 fprintf(stderr, "\n\n");
180 /* If we've already been here once, exit now */
181 been_there_done_that = 1;
182 return (main(argc, argv));
187 c-file-style: "linux"