1 /* vi: set sw=4 ts=4: */
12 #define bb_need_full_version
13 #define BB_DECLARE_EXTERN
16 static int been_there_done_that = 0;
19 const char *applet_name;
21 #ifdef BB_FEATURE_INSTALLER
24 * this should be consistent w/ the enum, busybox.h::Location,
27 static char* install_dir[] = {
36 typedef int (*__link_f)(const char *, const char *);
39 * Where in the filesystem is this busybox?
41 * malloc'd string w/ full pathname of busybox's location
44 static char *busybox_fullpath()
52 sprintf(proc, "/proc/%d/exe", pid);
53 len = readlink(proc, path, 256);
57 error_msg("%s: %s\n", proc, strerror(errno));
63 /* create (sym)links for each applet */
64 static void install_links(const char *busybox, int use_symbolic_links)
72 if (use_symbolic_links) Link = symlink;
74 for (i = 0; applets[i].name != NULL; i++) {
75 sprintf ( command, "%s/%s",
76 install_dir[applets[i].location],
78 rc = Link(busybox, command);
81 error_msg("%s: %s\n", command, strerror(errno));
86 #endif /* BB_FEATURE_INSTALLER */
88 int main(int argc, char **argv)
90 struct BB_applet search_applet, *applet;
92 applet_name = "busybox";
94 #ifdef BB_FEATURE_INSTALLER
96 * This style of argument parsing doesn't scale well
97 * in the event that busybox starts wanting more --options.
98 * If someone has a cleaner approach, by all means implement it.
100 if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
101 int use_symbolic_links = 0;
105 /* to use symlinks, or not to use symlinks... */
107 if ((strcmp(argv[2], "-s") == 0)) {
108 use_symbolic_links = 1;
113 busybox = busybox_fullpath();
115 install_links(busybox, use_symbolic_links);
122 #endif /* BB_FEATURE_INSTALLER */
124 for (s = applet_name = argv[0]; *s != '\0';) {
129 *argv = (char*)applet_name;
132 /* Add in a special case hack -- whenever **argv == '-'
133 * (i.e. '-su' or '-sh') always invoke the shell */
134 if (**argv == '-' && *(*argv+1)!= '-') {
135 exit(((*(shell_main)) (argc, argv)));
139 /* Do a binary search to find the applet entry given the name. */
140 search_applet.name = applet_name;
141 applet = bsearch(&search_applet, applets, NUM_APPLETS,
142 sizeof(struct BB_applet), applet_name_compare);
143 if (applet != NULL) {
144 if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0)
145 usage(applet->usage);
146 exit((*(applet->main)) (argc, argv));
149 return(busybox_main(argc, argv));
153 int busybox_main(int argc, char **argv)
160 if (been_there_done_that == 1 || argc < 1) {
161 const struct BB_applet *a = applets;
163 fprintf(stderr, "%s\n\n"
164 "Usage: busybox [function] [arguments]...\n"
165 " or: [function] [arguments]...\n\n"
166 "\tBusyBox is a multi-call binary that combines many common Unix\n"
167 "\tutilities into a single executable. Most people will create a\n"
168 "\tlink to busybox for each function they wish to use, and BusyBox\n"
169 "\twill act like whatever it was invoked as.\n"
170 "\nCurrently defined functions:\n", full_version);
172 while (a->name != 0) {
174 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
176 if (col > 60 && a->name != 0) {
177 fprintf(stderr, ",\n");
181 fprintf(stderr, "\n\n");
184 /* If we've already been here once, exit now */
185 been_there_done_that = 1;
186 return (main(argc, argv));
191 c-file-style: "linux"