blob: 9db26df271858d6f1fa070b26a2d8bb1bc39feae [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 Andersen8c725e62000-11-30 00:27:06 +00008
Pavel Roskin9c5fcc32000-07-17 23:45:12 +00009#define bb_need_full_version
10#define BB_DECLARE_EXTERN
11#include "messages.c"
12
Eric Andersene5dfced2001-04-09 22:48:12 +000013#ifdef BB_LOCALE_SUPPORT
14#include <locale.h>
15#endif
16
Eric Andersen0f0c0b42001-04-03 17:05:01 +000017int been_there_done_that = 0; /* Also used in applets.c */
Eric Andersen501c88b2000-07-28 15:14:45 +000018const char *applet_name;
Erik Andersen05df2392000-01-13 04:43:48 +000019
John Beppu8f425db2000-06-27 04:50:02 +000020#ifdef BB_FEATURE_INSTALLER
21/*
22 * directory table
Eric Andersen3570a342000-09-25 21:45:58 +000023 * this should be consistent w/ the enum, busybox.h::Location,
John Beppueb028332000-06-28 00:55:31 +000024 * or else...
John Beppu8f425db2000-06-27 04:50:02 +000025 */
26static char* install_dir[] = {
John Beppueb028332000-06-28 00:55:31 +000027 "/",
28 "/bin",
29 "/sbin",
30 "/usr/bin",
31 "/usr/sbin",
John Beppu8f425db2000-06-27 04:50:02 +000032};
33
34/* abstract link() */
35typedef int (*__link_f)(const char *, const char *);
36
John Beppu7cdc76d2000-06-28 00:41:26 +000037/*
38 * Where in the filesystem is this busybox?
39 * [return]
40 * malloc'd string w/ full pathname of busybox's location
41 * NULL on failure
42 */
43static char *busybox_fullpath()
44{
John Beppueb028332000-06-28 00:55:31 +000045 pid_t pid;
46 char path[256];
47 char proc[256];
48 int len;
John Beppu7cdc76d2000-06-28 00:41:26 +000049
50 pid = getpid();
51 sprintf(proc, "/proc/%d/exe", pid);
52 len = readlink(proc, path, 256);
53 if (len != -1) {
54 path[len] = 0;
55 } else {
Matt Kraai1fa1ade2000-12-18 03:57:16 +000056 perror_msg("%s", proc);
John Beppu7cdc76d2000-06-28 00:41:26 +000057 return NULL;
58 }
59 return strdup(path);
60}
61
John Beppu8f425db2000-06-27 04:50:02 +000062/* create (sym)links for each applet */
Eric Andersenc5949f62000-09-25 20:35:54 +000063static void install_links(const char *busybox, int use_symbolic_links)
John Beppu8f425db2000-06-27 04:50:02 +000064{
John Beppueb028332000-06-28 00:55:31 +000065 __link_f Link = link;
John Beppu8f425db2000-06-27 04:50:02 +000066
Eric Andersene5dfced2001-04-09 22:48:12 +000067 char *fpc;
John Beppueb028332000-06-28 00:55:31 +000068 int i;
Eric Andersenc5949f62000-09-25 20:35:54 +000069 int rc;
John Beppu8f425db2000-06-27 04:50:02 +000070
Eric Andersen90ca2842001-01-27 08:32:57 +000071 if (use_symbolic_links)
72 Link = symlink;
John Beppu8f425db2000-06-27 04:50:02 +000073
John Beppueb028332000-06-28 00:55:31 +000074 for (i = 0; applets[i].name != NULL; i++) {
Eric Andersene5dfced2001-04-09 22:48:12 +000075 fpc = concat_path_file(
76 install_dir[applets[i].location], applets[i].name);
77 rc = Link(busybox, fpc);
78 if (rc!=0 && errno!=EEXIST) {
79 perror_msg("%s", fpc);
John Beppu8f425db2000-06-27 04:50:02 +000080 }
Eric Andersene5dfced2001-04-09 22:48:12 +000081 free(fpc);
John Beppueb028332000-06-28 00:55:31 +000082 }
John Beppu8f425db2000-06-27 04:50:02 +000083}
84
John Beppu8f425db2000-06-27 04:50:02 +000085#endif /* BB_FEATURE_INSTALLER */
86
Eric Andersencc8ed391999-10-05 16:24:54 +000087int main(int argc, char **argv)
88{
Matt Kraaif2cc2762001-02-01 19:21:20 +000089 const char *s;
Eric Andersenf5d5e772001-01-24 23:34:48 +000090
91 for (s = applet_name = argv[0]; *s != '\0';) {
92 if (*s++ == '/')
93 applet_name = s;
94 }
95
96#ifdef BB_SH
97 /* Add in a special case hack -- whenever **argv == '-'
98 * (i.e. '-su' or '-sh') always invoke the shell */
99 if (**argv == '-' && *(*argv+1)!= '-') {
Eric Andersenba372622001-03-20 17:39:53 +0000100 applet_name = "sh";
Eric Andersenf5d5e772001-01-24 23:34:48 +0000101 }
102#endif
103
Eric Andersene5dfced2001-04-09 22:48:12 +0000104#ifdef BB_LOCALE_SUPPORT
105 if(getpid()!=1) /* Do not set locale for `init' */
106 setlocale(LC_ALL, "");
107#endif
108
Eric Andersen67991cf2001-02-14 21:23:06 +0000109 run_applet_by_name(applet_name, argc, argv);
Matt Kraaidd19c692001-01-31 19:00:21 +0000110 error_msg_and_die("applet not found");
Eric Andersenf5d5e772001-01-24 23:34:48 +0000111}
112
113
114int busybox_main(int argc, char **argv)
115{
116 int col = 0, len, i;
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
John Beppu8f425db2000-06-27 04:50:02 +0000118#ifdef BB_FEATURE_INSTALLER
John Beppu27b59242000-06-27 04:56:45 +0000119 /*
120 * This style of argument parsing doesn't scale well
121 * in the event that busybox starts wanting more --options.
122 * If someone has a cleaner approach, by all means implement it.
123 */
John Beppu8f425db2000-06-27 04:50:02 +0000124 if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
125 int use_symbolic_links = 0;
John Beppu7cdc76d2000-06-28 00:41:26 +0000126 int rc = 0;
127 char *busybox;
John Beppu8f425db2000-06-27 04:50:02 +0000128
John Beppu27b59242000-06-27 04:56:45 +0000129 /* to use symlinks, or not to use symlinks... */
John Beppu8f425db2000-06-27 04:50:02 +0000130 if (argc > 2) {
131 if ((strcmp(argv[2], "-s") == 0)) {
132 use_symbolic_links = 1;
133 }
134 }
John Beppu7cdc76d2000-06-28 00:41:26 +0000135
136 /* link */
137 busybox = busybox_fullpath();
138 if (busybox) {
139 install_links(busybox, use_symbolic_links);
140 free(busybox);
141 } else {
142 rc = 1;
143 }
144 return rc;
John Beppu8f425db2000-06-27 04:50:02 +0000145 }
146#endif /* BB_FEATURE_INSTALLER */
147
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 argc--;
Eric Andersencc8ed391999-10-05 16:24:54 +0000149
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000150 /* If we've already been here once, exit now */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 if (been_there_done_that == 1 || argc < 1) {
Erik Andersenbcd61772000-05-13 06:33:19 +0000152 const struct BB_applet *a = applets;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153
Pavel Roskin9c5fcc32000-07-17 23:45:12 +0000154 fprintf(stderr, "%s\n\n"
Erik Andersen330fd2b2000-05-19 05:35:19 +0000155 "Usage: busybox [function] [arguments]...\n"
156 " or: [function] [arguments]...\n\n"
John Beppub4f86062000-04-13 03:36:01 +0000157 "\tBusyBox is a multi-call binary that combines many common Unix\n"
158 "\tutilities into a single executable. Most people will create a\n"
159 "\tlink to busybox for each function they wish to use, and BusyBox\n"
Erik Andersen330fd2b2000-05-19 05:35:19 +0000160 "\twill act like whatever it was invoked as.\n"
Pavel Roskin9c5fcc32000-07-17 23:45:12 +0000161 "\nCurrently defined functions:\n", full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162
163 while (a->name != 0) {
164 col +=
165 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
166 (a++)->name);
167 if (col > 60 && a->name != 0) {
168 fprintf(stderr, ",\n");
169 col = 0;
170 }
171 }
172 fprintf(stderr, "\n\n");
Mark Whitley01677182001-03-02 17:47:17 +0000173 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000174 }
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000175
176 /* Flag that we've been here already */
Eric Andersenb6106152000-06-19 17:25:40 +0000177 been_there_done_that = 1;
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000178
Matt Kraai8abc78a2000-12-15 00:35:22 +0000179 /* Move the command line down a notch */
180 len = argv[argc] + strlen(argv[argc]) - argv[1];
181 memmove(argv[0], argv[1], len);
182 memset(argv[0] + len, 0, argv[1] - argv[0]);
183
184 /* Fix up the argv pointers */
185 len = argv[1] - argv[0];
Eric Andersen90ca2842001-01-27 08:32:57 +0000186 memmove(argv, argv + 1, sizeof(char *) * (argc + 1));
Matt Kraai8abc78a2000-12-15 00:35:22 +0000187 for (i = 0; i < argc; i++)
188 argv[i] -= len;
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000189
Eric Andersenb6106152000-06-19 17:25:40 +0000190 return (main(argc, argv));
Eric Andersencc8ed391999-10-05 16:24:54 +0000191}
Erik Andersen029011b2000-03-04 21:19:32 +0000192
193/*
194Local Variables:
195c-file-style: "linux"
196c-basic-offset: 4
197tab-width: 4
198End:
199*/