1 /* vi: set sw=4 ts=4: */
9 #define bb_need_full_version
10 #define BB_DECLARE_EXTERN
13 static int been_there_done_that = 0;
14 const char *applet_name;
16 #ifdef BB_FEATURE_INSTALLER
19 * this should be consistent w/ the enum, busybox.h::Location,
22 static char* install_dir[] = {
31 typedef int (*__link_f)(const char *, const char *);
34 * Where in the filesystem is this busybox?
36 * malloc'd string w/ full pathname of busybox's location
39 static char *busybox_fullpath()
47 sprintf(proc, "/proc/%d/exe", pid);
48 len = readlink(proc, path, 256);
52 perror_msg("%s", proc);
58 /* create (sym)links for each applet */
59 static void install_links(const char *busybox, int use_symbolic_links)
67 if (use_symbolic_links)
70 for (i = 0; applets[i].name != NULL; i++) {
71 sprintf ( command, "%s/%s",
72 install_dir[applets[i].location], applets[i].name);
73 rc = Link(busybox, command);
76 perror_msg("%s", command);
81 #endif /* BB_FEATURE_INSTALLER */
83 int main(int argc, char **argv)
87 for (s = applet_name = argv[0]; *s != '\0';) {
93 /* Add in a special case hack -- whenever **argv == '-'
94 * (i.e. '-su' or '-sh') always invoke the shell */
95 if (**argv == '-' && *(*argv+1)!= '-') {
100 run_applet_by_name(applet_name, argc, argv);
101 error_msg_and_die("applet not found");
105 int busybox_main(int argc, char **argv)
109 #ifdef BB_FEATURE_INSTALLER
111 * This style of argument parsing doesn't scale well
112 * in the event that busybox starts wanting more --options.
113 * If someone has a cleaner approach, by all means implement it.
115 if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
116 int use_symbolic_links = 0;
120 /* to use symlinks, or not to use symlinks... */
122 if ((strcmp(argv[2], "-s") == 0)) {
123 use_symbolic_links = 1;
128 busybox = busybox_fullpath();
130 install_links(busybox, use_symbolic_links);
137 #endif /* BB_FEATURE_INSTALLER */
141 /* If we've already been here once, exit now */
142 if (been_there_done_that == 1 || argc < 1) {
143 const struct BB_applet *a = applets;
145 fprintf(stderr, "%s\n\n"
146 "Usage: busybox [function] [arguments]...\n"
147 " or: [function] [arguments]...\n\n"
148 "\tBusyBox is a multi-call binary that combines many common Unix\n"
149 "\tutilities into a single executable. Most people will create a\n"
150 "\tlink to busybox for each function they wish to use, and BusyBox\n"
151 "\twill act like whatever it was invoked as.\n"
152 "\nCurrently defined functions:\n", full_version);
154 while (a->name != 0) {
156 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
158 if (col > 60 && a->name != 0) {
159 fprintf(stderr, ",\n");
163 fprintf(stderr, "\n\n");
167 /* Flag that we've been here already */
168 been_there_done_that = 1;
170 /* Move the command line down a notch */
171 len = argv[argc] + strlen(argv[argc]) - argv[1];
172 memmove(argv[0], argv[1], len);
173 memset(argv[0] + len, 0, argv[1] - argv[0]);
175 /* Fix up the argv pointers */
176 len = argv[1] - argv[0];
177 memmove(argv, argv + 1, sizeof(char *) * (argc + 1));
178 for (i = 0; i < argc; i++)
181 return (main(argc, argv));
186 c-file-style: "linux"