blob: badd53d79de9a7c28dc34fbf1c46903d1f96e8b1 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002#include <stdio.h>
3#include <string.h>
Eric Andersen90ca2842001-01-27 08:32:57 +00004#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +00005#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +00006#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +00007#include "busybox.h"
Eric Andersene5dfced2001-04-09 22:48:12 +00008#ifdef BB_LOCALE_SUPPORT
9#include <locale.h>
10#endif
11
Eric Andersen0f0c0b42001-04-03 17:05:01 +000012int been_there_done_that = 0; /* Also used in applets.c */
Eric Andersen501c88b2000-07-28 15:14:45 +000013const char *applet_name;
Erik Andersen05df2392000-01-13 04:43:48 +000014
John Beppu8f425db2000-06-27 04:50:02 +000015#ifdef BB_FEATURE_INSTALLER
16/*
17 * directory table
Eric Andersen3570a342000-09-25 21:45:58 +000018 * this should be consistent w/ the enum, busybox.h::Location,
John Beppueb028332000-06-28 00:55:31 +000019 * or else...
John Beppu8f425db2000-06-27 04:50:02 +000020 */
21static char* install_dir[] = {
John Beppueb028332000-06-28 00:55:31 +000022 "/",
23 "/bin",
24 "/sbin",
25 "/usr/bin",
26 "/usr/sbin",
John Beppu8f425db2000-06-27 04:50:02 +000027};
28
29/* abstract link() */
30typedef int (*__link_f)(const char *, const char *);
31
John Beppu7cdc76d2000-06-28 00:41:26 +000032/*
33 * Where in the filesystem is this busybox?
34 * [return]
35 * malloc'd string w/ full pathname of busybox's location
36 * NULL on failure
37 */
38static char *busybox_fullpath()
39{
John Beppueb028332000-06-28 00:55:31 +000040 char proc[256];
John Beppu7cdc76d2000-06-28 00:41:26 +000041
Mark Whitley8a633262001-04-30 18:17:00 +000042 sprintf(proc, "/proc/%d/exe", getpid());
43 return xreadlink(proc);
John Beppu7cdc76d2000-06-28 00:41:26 +000044}
45
John Beppu8f425db2000-06-27 04:50:02 +000046/* create (sym)links for each applet */
Eric Andersenc5949f62000-09-25 20:35:54 +000047static void install_links(const char *busybox, int use_symbolic_links)
John Beppu8f425db2000-06-27 04:50:02 +000048{
John Beppueb028332000-06-28 00:55:31 +000049 __link_f Link = link;
John Beppu8f425db2000-06-27 04:50:02 +000050
Eric Andersene5dfced2001-04-09 22:48:12 +000051 char *fpc;
John Beppueb028332000-06-28 00:55:31 +000052 int i;
Eric Andersenc5949f62000-09-25 20:35:54 +000053 int rc;
John Beppu8f425db2000-06-27 04:50:02 +000054
Eric Andersen90ca2842001-01-27 08:32:57 +000055 if (use_symbolic_links)
56 Link = symlink;
John Beppu8f425db2000-06-27 04:50:02 +000057
John Beppueb028332000-06-28 00:55:31 +000058 for (i = 0; applets[i].name != NULL; i++) {
Eric Andersene5dfced2001-04-09 22:48:12 +000059 fpc = concat_path_file(
60 install_dir[applets[i].location], applets[i].name);
61 rc = Link(busybox, fpc);
62 if (rc!=0 && errno!=EEXIST) {
63 perror_msg("%s", fpc);
John Beppu8f425db2000-06-27 04:50:02 +000064 }
Eric Andersene5dfced2001-04-09 22:48:12 +000065 free(fpc);
John Beppueb028332000-06-28 00:55:31 +000066 }
John Beppu8f425db2000-06-27 04:50:02 +000067}
68
John Beppu8f425db2000-06-27 04:50:02 +000069#endif /* BB_FEATURE_INSTALLER */
70
Eric Andersencc8ed391999-10-05 16:24:54 +000071int main(int argc, char **argv)
72{
Matt Kraaif2cc2762001-02-01 19:21:20 +000073 const char *s;
Eric Andersenf5d5e772001-01-24 23:34:48 +000074
75 for (s = applet_name = argv[0]; *s != '\0';) {
76 if (*s++ == '/')
77 applet_name = s;
78 }
79
80#ifdef BB_SH
81 /* Add in a special case hack -- whenever **argv == '-'
82 * (i.e. '-su' or '-sh') always invoke the shell */
83 if (**argv == '-' && *(*argv+1)!= '-') {
Eric Andersenba372622001-03-20 17:39:53 +000084 applet_name = "sh";
Eric Andersenf5d5e772001-01-24 23:34:48 +000085 }
86#endif
87
Eric Andersene5dfced2001-04-09 22:48:12 +000088#ifdef BB_LOCALE_SUPPORT
89 if(getpid()!=1) /* Do not set locale for `init' */
90 setlocale(LC_ALL, "");
91#endif
92
Eric Andersen67991cf2001-02-14 21:23:06 +000093 run_applet_by_name(applet_name, argc, argv);
Matt Kraaidd19c692001-01-31 19:00:21 +000094 error_msg_and_die("applet not found");
Eric Andersenf5d5e772001-01-24 23:34:48 +000095}
96
97
98int busybox_main(int argc, char **argv)
99{
100 int col = 0, len, i;
Eric Andersencc8ed391999-10-05 16:24:54 +0000101
John Beppu8f425db2000-06-27 04:50:02 +0000102#ifdef BB_FEATURE_INSTALLER
John Beppu27b59242000-06-27 04:56:45 +0000103 /*
104 * This style of argument parsing doesn't scale well
105 * in the event that busybox starts wanting more --options.
106 * If someone has a cleaner approach, by all means implement it.
107 */
John Beppu8f425db2000-06-27 04:50:02 +0000108 if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
109 int use_symbolic_links = 0;
John Beppu7cdc76d2000-06-28 00:41:26 +0000110 int rc = 0;
111 char *busybox;
John Beppu8f425db2000-06-27 04:50:02 +0000112
John Beppu27b59242000-06-27 04:56:45 +0000113 /* to use symlinks, or not to use symlinks... */
John Beppu8f425db2000-06-27 04:50:02 +0000114 if (argc > 2) {
115 if ((strcmp(argv[2], "-s") == 0)) {
116 use_symbolic_links = 1;
117 }
118 }
John Beppu7cdc76d2000-06-28 00:41:26 +0000119
120 /* link */
121 busybox = busybox_fullpath();
122 if (busybox) {
123 install_links(busybox, use_symbolic_links);
124 free(busybox);
125 } else {
126 rc = 1;
127 }
128 return rc;
John Beppu8f425db2000-06-27 04:50:02 +0000129 }
130#endif /* BB_FEATURE_INSTALLER */
131
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 argc--;
Eric Andersencc8ed391999-10-05 16:24:54 +0000133
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000134 /* If we've already been here once, exit now */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 if (been_there_done_that == 1 || argc < 1) {
Erik Andersenbcd61772000-05-13 06:33:19 +0000136 const struct BB_applet *a = applets;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137
Pavel Roskin9c5fcc32000-07-17 23:45:12 +0000138 fprintf(stderr, "%s\n\n"
Erik Andersen330fd2b2000-05-19 05:35:19 +0000139 "Usage: busybox [function] [arguments]...\n"
140 " or: [function] [arguments]...\n\n"
John Beppub4f86062000-04-13 03:36:01 +0000141 "\tBusyBox is a multi-call binary that combines many common Unix\n"
142 "\tutilities into a single executable. Most people will create a\n"
143 "\tlink to busybox for each function they wish to use, and BusyBox\n"
Erik Andersen330fd2b2000-05-19 05:35:19 +0000144 "\twill act like whatever it was invoked as.\n"
Pavel Roskin9c5fcc32000-07-17 23:45:12 +0000145 "\nCurrently defined functions:\n", full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146
147 while (a->name != 0) {
148 col +=
149 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
150 (a++)->name);
151 if (col > 60 && a->name != 0) {
152 fprintf(stderr, ",\n");
153 col = 0;
154 }
155 }
156 fprintf(stderr, "\n\n");
Mark Whitley01677182001-03-02 17:47:17 +0000157 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000158 }
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000159
160 /* Flag that we've been here already */
Eric Andersenb6106152000-06-19 17:25:40 +0000161 been_there_done_that = 1;
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000162
Matt Kraai8abc78a2000-12-15 00:35:22 +0000163 /* Move the command line down a notch */
164 len = argv[argc] + strlen(argv[argc]) - argv[1];
165 memmove(argv[0], argv[1], len);
166 memset(argv[0] + len, 0, argv[1] - argv[0]);
167
168 /* Fix up the argv pointers */
169 len = argv[1] - argv[0];
Eric Andersen90ca2842001-01-27 08:32:57 +0000170 memmove(argv, argv + 1, sizeof(char *) * (argc + 1));
Matt Kraai8abc78a2000-12-15 00:35:22 +0000171 for (i = 0; i < argc; i++)
172 argv[i] -= len;
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000173
Eric Andersenb6106152000-06-19 17:25:40 +0000174 return (main(argc, argv));
Eric Andersencc8ed391999-10-05 16:24:54 +0000175}
Erik Andersen029011b2000-03-04 21:19:32 +0000176
177/*
178Local Variables:
179c-file-style: "linux"
180c-basic-offset: 4
181tab-width: 4
182End:
183*/