1 /* vi: set sw=4 ts=4: */
14 #define bb_need_full_version
15 #define BB_DECLARE_EXTERN
18 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 perror_msg("%s", proc);
63 /* create (sym)links for each applet */
64 static void install_links(const char *busybox, int use_symbolic_links)
72 if (use_symbolic_links)
75 for (i = 0; applets[i].name != NULL; i++) {
76 sprintf ( command, "%s/%s",
77 install_dir[applets[i].location], applets[i].name);
78 rc = Link(busybox, command);
81 perror_msg("%s", command);
86 #endif /* BB_FEATURE_INSTALLER */
88 int main(int argc, char **argv)
90 struct BB_applet *applet;
93 for (s = applet_name = argv[0]; *s != '\0';) {
99 /* Add in a special case hack -- whenever **argv == '-'
100 * (i.e. '-su' or '-sh') always invoke the shell */
101 if (**argv == '-' && *(*argv+1)!= '-') {
102 exit(((*(shell_main)) (argc, argv)));
106 /* Do a binary search to find the applet entry given the name. */
107 if ((applet = find_applet_by_name(applet_name)) != NULL) {
108 if (applet->usage && argv[1] && strcmp(argv[1], "--help") == 0)
109 usage(applet->usage);
110 exit((*(applet->main)) (argc, argv));
113 error_msg_and_die("applet not found");
117 int busybox_main(int argc, char **argv)
121 #ifdef BB_FEATURE_INSTALLER
123 * This style of argument parsing doesn't scale well
124 * in the event that busybox starts wanting more --options.
125 * If someone has a cleaner approach, by all means implement it.
127 if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
128 int use_symbolic_links = 0;
132 /* to use symlinks, or not to use symlinks... */
134 if ((strcmp(argv[2], "-s") == 0)) {
135 use_symbolic_links = 1;
140 busybox = busybox_fullpath();
142 install_links(busybox, use_symbolic_links);
149 #endif /* BB_FEATURE_INSTALLER */
153 /* If we've already been here once, exit now */
154 if (been_there_done_that == 1 || argc < 1) {
155 const struct BB_applet *a = applets;
157 fprintf(stderr, "%s\n\n"
158 "Usage: busybox [function] [arguments]...\n"
159 " or: [function] [arguments]...\n\n"
160 "\tBusyBox is a multi-call binary that combines many common Unix\n"
161 "\tutilities into a single executable. Most people will create a\n"
162 "\tlink to busybox for each function they wish to use, and BusyBox\n"
163 "\twill act like whatever it was invoked as.\n"
164 "\nCurrently defined functions:\n", full_version);
166 while (a->name != 0) {
168 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
170 if (col > 60 && a->name != 0) {
171 fprintf(stderr, ",\n");
175 fprintf(stderr, "\n\n");
179 /* Flag that we've been here already */
180 been_there_done_that = 1;
182 /* Move the command line down a notch */
183 len = argv[argc] + strlen(argv[argc]) - argv[1];
184 memmove(argv[0], argv[1], len);
185 memset(argv[0] + len, 0, argv[1] - argv[0]);
187 /* Fix up the argv pointers */
188 len = argv[1] - argv[0];
189 memmove(argv, argv + 1, sizeof(char *) * (argc + 1));
190 for (i = 0; i < argc; i++)
193 return (main(argc, argv));
198 c-file-style: "linux"