blob: 5085556d6b55915b8557b608482fc807874b2793 [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 Andersen0f0c0b42001-04-03 17:05:01 +000013int been_there_done_that = 0; /* Also used in applets.c */
Eric Andersen501c88b2000-07-28 15:14:45 +000014const char *applet_name;
Erik Andersen05df2392000-01-13 04:43:48 +000015
John Beppu8f425db2000-06-27 04:50:02 +000016#ifdef BB_FEATURE_INSTALLER
17/*
18 * directory table
Eric Andersen3570a342000-09-25 21:45:58 +000019 * this should be consistent w/ the enum, busybox.h::Location,
John Beppueb028332000-06-28 00:55:31 +000020 * or else...
John Beppu8f425db2000-06-27 04:50:02 +000021 */
22static char* install_dir[] = {
John Beppueb028332000-06-28 00:55:31 +000023 "/",
24 "/bin",
25 "/sbin",
26 "/usr/bin",
27 "/usr/sbin",
John Beppu8f425db2000-06-27 04:50:02 +000028};
29
30/* abstract link() */
31typedef int (*__link_f)(const char *, const char *);
32
John Beppu7cdc76d2000-06-28 00:41:26 +000033/*
34 * Where in the filesystem is this busybox?
35 * [return]
36 * malloc'd string w/ full pathname of busybox's location
37 * NULL on failure
38 */
39static char *busybox_fullpath()
40{
John Beppueb028332000-06-28 00:55:31 +000041 pid_t pid;
42 char path[256];
43 char proc[256];
44 int len;
John Beppu7cdc76d2000-06-28 00:41:26 +000045
46 pid = getpid();
47 sprintf(proc, "/proc/%d/exe", pid);
48 len = readlink(proc, path, 256);
49 if (len != -1) {
50 path[len] = 0;
51 } else {
Matt Kraai1fa1ade2000-12-18 03:57:16 +000052 perror_msg("%s", proc);
John Beppu7cdc76d2000-06-28 00:41:26 +000053 return NULL;
54 }
55 return strdup(path);
56}
57
John Beppu8f425db2000-06-27 04:50:02 +000058/* create (sym)links for each applet */
Eric Andersenc5949f62000-09-25 20:35:54 +000059static void install_links(const char *busybox, int use_symbolic_links)
John Beppu8f425db2000-06-27 04:50:02 +000060{
John Beppueb028332000-06-28 00:55:31 +000061 __link_f Link = link;
John Beppu8f425db2000-06-27 04:50:02 +000062
John Beppueb028332000-06-28 00:55:31 +000063 char command[256];
64 int i;
Eric Andersenc5949f62000-09-25 20:35:54 +000065 int rc;
John Beppu8f425db2000-06-27 04:50:02 +000066
Eric Andersen90ca2842001-01-27 08:32:57 +000067 if (use_symbolic_links)
68 Link = symlink;
John Beppu8f425db2000-06-27 04:50:02 +000069
John Beppueb028332000-06-28 00:55:31 +000070 for (i = 0; applets[i].name != NULL; i++) {
Eric Andersenc5949f62000-09-25 20:35:54 +000071 sprintf ( command, "%s/%s",
Eric Andersen90ca2842001-01-27 08:32:57 +000072 install_dir[applets[i].location], applets[i].name);
Eric Andersenc5949f62000-09-25 20:35:54 +000073 rc = Link(busybox, command);
74
John Beppu8f425db2000-06-27 04:50:02 +000075 if (rc) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +000076 perror_msg("%s", command);
John Beppu8f425db2000-06-27 04:50:02 +000077 }
John Beppueb028332000-06-28 00:55:31 +000078 }
John Beppu8f425db2000-06-27 04:50:02 +000079}
80
John Beppu8f425db2000-06-27 04:50:02 +000081#endif /* BB_FEATURE_INSTALLER */
82
Eric Andersencc8ed391999-10-05 16:24:54 +000083int main(int argc, char **argv)
84{
Matt Kraaif2cc2762001-02-01 19:21:20 +000085 const char *s;
Eric Andersenf5d5e772001-01-24 23:34:48 +000086
87 for (s = applet_name = argv[0]; *s != '\0';) {
88 if (*s++ == '/')
89 applet_name = s;
90 }
91
92#ifdef BB_SH
93 /* Add in a special case hack -- whenever **argv == '-'
94 * (i.e. '-su' or '-sh') always invoke the shell */
95 if (**argv == '-' && *(*argv+1)!= '-') {
Eric Andersenba372622001-03-20 17:39:53 +000096 applet_name = "sh";
Eric Andersenf5d5e772001-01-24 23:34:48 +000097 }
98#endif
99
Eric Andersen67991cf2001-02-14 21:23:06 +0000100 run_applet_by_name(applet_name, argc, argv);
Matt Kraaidd19c692001-01-31 19:00:21 +0000101 error_msg_and_die("applet not found");
Eric Andersenf5d5e772001-01-24 23:34:48 +0000102}
103
104
105int busybox_main(int argc, char **argv)
106{
107 int col = 0, len, i;
Eric Andersencc8ed391999-10-05 16:24:54 +0000108
John Beppu8f425db2000-06-27 04:50:02 +0000109#ifdef BB_FEATURE_INSTALLER
John Beppu27b59242000-06-27 04:56:45 +0000110 /*
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.
114 */
John Beppu8f425db2000-06-27 04:50:02 +0000115 if (argc > 1 && (strcmp(argv[1], "--install") == 0)) {
116 int use_symbolic_links = 0;
John Beppu7cdc76d2000-06-28 00:41:26 +0000117 int rc = 0;
118 char *busybox;
John Beppu8f425db2000-06-27 04:50:02 +0000119
John Beppu27b59242000-06-27 04:56:45 +0000120 /* to use symlinks, or not to use symlinks... */
John Beppu8f425db2000-06-27 04:50:02 +0000121 if (argc > 2) {
122 if ((strcmp(argv[2], "-s") == 0)) {
123 use_symbolic_links = 1;
124 }
125 }
John Beppu7cdc76d2000-06-28 00:41:26 +0000126
127 /* link */
128 busybox = busybox_fullpath();
129 if (busybox) {
130 install_links(busybox, use_symbolic_links);
131 free(busybox);
132 } else {
133 rc = 1;
134 }
135 return rc;
John Beppu8f425db2000-06-27 04:50:02 +0000136 }
137#endif /* BB_FEATURE_INSTALLER */
138
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139 argc--;
Eric Andersencc8ed391999-10-05 16:24:54 +0000140
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000141 /* If we've already been here once, exit now */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 if (been_there_done_that == 1 || argc < 1) {
Erik Andersenbcd61772000-05-13 06:33:19 +0000143 const struct BB_applet *a = applets;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144
Pavel Roskin9c5fcc32000-07-17 23:45:12 +0000145 fprintf(stderr, "%s\n\n"
Erik Andersen330fd2b2000-05-19 05:35:19 +0000146 "Usage: busybox [function] [arguments]...\n"
147 " or: [function] [arguments]...\n\n"
John Beppub4f86062000-04-13 03:36:01 +0000148 "\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"
Erik Andersen330fd2b2000-05-19 05:35:19 +0000151 "\twill act like whatever it was invoked as.\n"
Pavel Roskin9c5fcc32000-07-17 23:45:12 +0000152 "\nCurrently defined functions:\n", full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153
154 while (a->name != 0) {
155 col +=
156 fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "),
157 (a++)->name);
158 if (col > 60 && a->name != 0) {
159 fprintf(stderr, ",\n");
160 col = 0;
161 }
162 }
163 fprintf(stderr, "\n\n");
Mark Whitley01677182001-03-02 17:47:17 +0000164 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000165 }
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000166
167 /* Flag that we've been here already */
Eric Andersenb6106152000-06-19 17:25:40 +0000168 been_there_done_that = 1;
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000169
Matt Kraai8abc78a2000-12-15 00:35:22 +0000170 /* 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]);
174
175 /* Fix up the argv pointers */
176 len = argv[1] - argv[0];
Eric Andersen90ca2842001-01-27 08:32:57 +0000177 memmove(argv, argv + 1, sizeof(char *) * (argc + 1));
Matt Kraai8abc78a2000-12-15 00:35:22 +0000178 for (i = 0; i < argc; i++)
179 argv[i] -= len;
Eric Andersen5e09b6e2000-12-08 19:03:12 +0000180
Eric Andersenb6106152000-06-19 17:25:40 +0000181 return (main(argc, argv));
Eric Andersencc8ed391999-10-05 16:24:54 +0000182}
Erik Andersen029011b2000-03-04 21:19:32 +0000183
184/*
185Local Variables:
186c-file-style: "linux"
187c-basic-offset: 4
188tab-width: 4
189End:
190*/