blob: b4939e19d319a5dea4a3fbea596e7317921ec70f [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 pid_t pid;
41 char path[256];
42 char proc[256];
43 int len;
John Beppu7cdc76d2000-06-28 00:41:26 +000044
45 pid = getpid();
46 sprintf(proc, "/proc/%d/exe", pid);
47 len = readlink(proc, path, 256);
48 if (len != -1) {
49 path[len] = 0;
50 } else {
Matt Kraai1fa1ade2000-12-18 03:57:16 +000051 perror_msg("%s", proc);
John Beppu7cdc76d2000-06-28 00:41:26 +000052 return NULL;
53 }
54 return strdup(path);
55}
56
John Beppu8f425db2000-06-27 04:50:02 +000057/* create (sym)links for each applet */
Eric Andersenc5949f62000-09-25 20:35:54 +000058static void install_links(const char *busybox, int use_symbolic_links)
John Beppu8f425db2000-06-27 04:50:02 +000059{
John Beppueb028332000-06-28 00:55:31 +000060 __link_f Link = link;
John Beppu8f425db2000-06-27 04:50:02 +000061
Eric Andersene5dfced2001-04-09 22:48:12 +000062 char *fpc;
John Beppueb028332000-06-28 00:55:31 +000063 int i;
Eric Andersenc5949f62000-09-25 20:35:54 +000064 int rc;
John Beppu8f425db2000-06-27 04:50:02 +000065
Eric Andersen90ca2842001-01-27 08:32:57 +000066 if (use_symbolic_links)
67 Link = symlink;
John Beppu8f425db2000-06-27 04:50:02 +000068
John Beppueb028332000-06-28 00:55:31 +000069 for (i = 0; applets[i].name != NULL; i++) {
Eric Andersene5dfced2001-04-09 22:48:12 +000070 fpc = concat_path_file(
71 install_dir[applets[i].location], applets[i].name);
72 rc = Link(busybox, fpc);
73 if (rc!=0 && errno!=EEXIST) {
74 perror_msg("%s", fpc);
John Beppu8f425db2000-06-27 04:50:02 +000075 }
Eric Andersene5dfced2001-04-09 22:48:12 +000076 free(fpc);
John Beppueb028332000-06-28 00:55:31 +000077 }
John Beppu8f425db2000-06-27 04:50:02 +000078}
79
John Beppu8f425db2000-06-27 04:50:02 +000080#endif /* BB_FEATURE_INSTALLER */
81
Eric Andersencc8ed391999-10-05 16:24:54 +000082int main(int argc, char **argv)
83{
Matt Kraaif2cc2762001-02-01 19:21:20 +000084 const char *s;
Eric Andersenf5d5e772001-01-24 23:34:48 +000085
86 for (s = applet_name = argv[0]; *s != '\0';) {
87 if (*s++ == '/')
88 applet_name = s;
89 }
90
91#ifdef BB_SH
92 /* Add in a special case hack -- whenever **argv == '-'
93 * (i.e. '-su' or '-sh') always invoke the shell */
94 if (**argv == '-' && *(*argv+1)!= '-') {
Eric Andersenba372622001-03-20 17:39:53 +000095 applet_name = "sh";
Eric Andersenf5d5e772001-01-24 23:34:48 +000096 }
97#endif
98
Eric Andersene5dfced2001-04-09 22:48:12 +000099#ifdef BB_LOCALE_SUPPORT
100 if(getpid()!=1) /* Do not set locale for `init' */
101 setlocale(LC_ALL, "");
102#endif
103
Eric Andersen67991cf2001-02-14 21:23:06 +0000104 run_applet_by_name(applet_name, argc, argv);
Matt Kraaidd19c692001-01-31 19:00:21 +0000105 error_msg_and_die("applet not found");
Eric Andersenf5d5e772001-01-24 23:34:48 +0000106}
107
108
109int busybox_main(int argc, char **argv)
110{
111 int col = 0, len, i;
Eric Andersencc8ed391999-10-05 16:24:54 +0000112
John Beppu8f425db2000-06-27 04:50:02 +0000113#ifdef BB_FEATURE_INSTALLER
John Beppu27b59242000-06-27 04:56:45 +0000114 /*
115 * This style of argument parsing doesn't scale well
116 * in the event that busybox starts wanting more --options.
117 * If someone has a cleaner approach, by all means implement it.
118 */
John Beppu8f425db2000-06-27 04:50:02 +0000119 if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
120 int use_symbolic_links = 0;
John Beppu7cdc76d2000-06-28 00:41:26 +0000121 int rc = 0;
122 char *busybox;
John Beppu8f425db2000-06-27 04:50:02 +0000123
John Beppu27b59242000-06-27 04:56:45 +0000124 /* to use symlinks, or not to use symlinks... */
John Beppu8f425db2000-06-27 04:50:02 +0000125 if (argc > 2) {
126 if ((strcmp(argv[2], "-s") == 0)) {
127 use_symbolic_links = 1;
128 }
129 }
John Beppu7cdc76d2000-06-28 00:41:26 +0000130
131 /* link */
132 busybox = busybox_fullpath();
133 if (busybox) {
134 install_links(busybox, use_symbolic_links);
135 free(busybox);
136 } else {
137 rc = 1;
138 }
139 return rc;
John Beppu8f425db2000-06-27 04:50:02 +0000140 }
141#endif /* BB_FEATURE_INSTALLER */
142
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 argc--;
Eric Andersencc8ed391999-10-05 16:24:54 +0000144
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000145 /* If we've already been here once, exit now */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 if (been_there_done_that == 1 || argc < 1) {
Erik Andersenbcd61772000-05-13 06:33:19 +0000147 const struct BB_applet *a = applets;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148
Pavel Roskin9c5fcc32000-07-17 23:45:12 +0000149 fprintf(stderr, "%s\n\n"
Erik Andersen330fd2b2000-05-19 05:35:19 +0000150 "Usage: busybox [function] [arguments]...\n"
151 " or: [function] [arguments]...\n\n"
John Beppub4f86062000-04-13 03:36:01 +0000152 "\tBusyBox is a multi-call binary that combines many common Unix\n"
153 "\tutilities into a single executable. Most people will create a\n"
154 "\tlink to busybox for each function they wish to use, and BusyBox\n"
Erik Andersen330fd2b2000-05-19 05:35:19 +0000155 "\twill act like whatever it was invoked as.\n"
Pavel Roskin9c5fcc32000-07-17 23:45:12 +0000156 "\nCurrently defined functions:\n", full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157
158 while (a->name != 0) {
159 col +=
160 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
161 (a++)->name);
162 if (col > 60 && a->name != 0) {
163 fprintf(stderr, ",\n");
164 col = 0;
165 }
166 }
167 fprintf(stderr, "\n\n");
Mark Whitley01677182001-03-02 17:47:17 +0000168 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000169 }
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000170
171 /* Flag that we've been here already */
Eric Andersenb6106152000-06-19 17:25:40 +0000172 been_there_done_that = 1;
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000173
Matt Kraai8abc78a2000-12-15 00:35:22 +0000174 /* Move the command line down a notch */
175 len = argv[argc] + strlen(argv[argc]) - argv[1];
176 memmove(argv[0], argv[1], len);
177 memset(argv[0] + len, 0, argv[1] - argv[0]);
178
179 /* Fix up the argv pointers */
180 len = argv[1] - argv[0];
Eric Andersen90ca2842001-01-27 08:32:57 +0000181 memmove(argv, argv + 1, sizeof(char *) * (argc + 1));
Matt Kraai8abc78a2000-12-15 00:35:22 +0000182 for (i = 0; i < argc; i++)
183 argv[i] -= len;
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000184
Eric Andersenb6106152000-06-19 17:25:40 +0000185 return (main(argc, argv));
Eric Andersencc8ed391999-10-05 16:24:54 +0000186}
Erik Andersen029011b2000-03-04 21:19:32 +0000187
188/*
189Local Variables:
190c-file-style: "linux"
191c-basic-offset: 4
192tab-width: 4
193End:
194*/