blob: 0387d79b7c9b968bcc49ae9a4c01dcc9bf5fcc3c [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00002/*
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00003 * BusyBox' main applet dispatcher.
4 *
Rob Landleyeb84a422006-09-20 21:41:13 +00005 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00006 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00007#include "busybox.h"
Eric Andersene5dfced2001-04-09 22:48:12 +00008
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00009const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
Erik Andersen05df2392000-01-13 04:43:48 +000010
Eric Andersenbdfd0d72001-10-24 05:00:29 +000011#ifdef CONFIG_FEATURE_INSTALLER
Eric Andersenc7bda1c2004-03-15 08:29:22 +000012/*
John Beppu8f425db2000-06-27 04:50:02 +000013 * directory table
Eric Andersen3570a342000-09-25 21:45:58 +000014 * this should be consistent w/ the enum, busybox.h::Location,
John Beppueb028332000-06-28 00:55:31 +000015 * or else...
John Beppu8f425db2000-06-27 04:50:02 +000016 */
Denis Vlasenko0ee39992006-12-24 15:23:28 +000017static const char usr_bin [] = "/usr/bin";
18static const char usr_sbin[] = "/usr/sbin";
Eric Andersenfcffa2c2002-04-06 05:17:57 +000019
20static const char* const install_dir[] = {
21 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
22 &usr_bin [4], /* "/bin" */
23 &usr_sbin[4], /* "/sbin" */
24 usr_bin,
25 usr_sbin
John Beppu8f425db2000-06-27 04:50:02 +000026};
27
28/* abstract link() */
Denis Vlasenko0ee39992006-12-24 15:23:28 +000029typedef int (*link_func)(const char *, const char *);
John Beppu8f425db2000-06-27 04:50:02 +000030
31/* create (sym)links for each applet */
Eric Andersenc5949f62000-09-25 20:35:54 +000032static void install_links(const char *busybox, int use_symbolic_links)
John Beppu8f425db2000-06-27 04:50:02 +000033{
Denis Vlasenko0ee39992006-12-24 15:23:28 +000034 link_func lf = link;
Eric Andersene5dfced2001-04-09 22:48:12 +000035 char *fpc;
John Beppueb028332000-06-28 00:55:31 +000036 int i;
Eric Andersenc5949f62000-09-25 20:35:54 +000037 int rc;
John Beppu8f425db2000-06-27 04:50:02 +000038
Eric Andersenc7bda1c2004-03-15 08:29:22 +000039 if (use_symbolic_links)
Denis Vlasenko0ee39992006-12-24 15:23:28 +000040 lf = symlink;
John Beppu8f425db2000-06-27 04:50:02 +000041
John Beppueb028332000-06-28 00:55:31 +000042 for (i = 0; applets[i].name != NULL; i++) {
Eric Andersene5dfced2001-04-09 22:48:12 +000043 fpc = concat_path_file(
Denis Vlasenko0ee39992006-12-24 15:23:28 +000044 install_dir[applets[i].location],
45 applets[i].name);
46 rc = lf(busybox, fpc);
47 if (rc != 0 && errno != EEXIST) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 bb_perror_msg("%s", fpc);
John Beppu8f425db2000-06-27 04:50:02 +000049 }
Eric Andersene5dfced2001-04-09 22:48:12 +000050 free(fpc);
John Beppueb028332000-06-28 00:55:31 +000051 }
John Beppu8f425db2000-06-27 04:50:02 +000052}
53
Rob Landley8a7a6782005-09-05 04:13:33 +000054#else
55#define install_links(x,y)
Eric Andersenbdfd0d72001-10-24 05:00:29 +000056#endif /* CONFIG_FEATURE_INSTALLER */
John Beppu8f425db2000-06-27 04:50:02 +000057
Eric Andersencc8ed391999-10-05 16:24:54 +000058int main(int argc, char **argv)
59{
Matt Kraaif2cc2762001-02-01 19:21:20 +000060 const char *s;
Eric Andersenf5d5e772001-01-24 23:34:48 +000061
Denis Vlasenko0ee39992006-12-24 15:23:28 +000062 applet_name = argv[0];
63 if (*applet_name == '-')
64 applet_name++;
65 while ((s = strchr(applet_name, '/')))
66 applet_name = s + 1;
Matt Kraai449377a2001-08-27 15:02:32 +000067
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +000068 /* Set locale for everybody except 'init' */
69 if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
Eric Andersene5dfced2001-04-09 22:48:12 +000070 setlocale(LC_ALL, "");
Eric Andersene5dfced2001-04-09 22:48:12 +000071
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000072 run_applet_by_name(applet_name, argc, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 bb_error_msg_and_die("applet not found");
Eric Andersenf5d5e772001-01-24 23:34:48 +000074}
75
Denis Vlasenko06af2162007-02-03 17:28:39 +000076int busybox_main(int argc, char **argv);
Eric Andersenf5d5e772001-01-24 23:34:48 +000077int busybox_main(int argc, char **argv)
78{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000079 /*
80 * This style of argument parsing doesn't scale well
John Beppu27b59242000-06-27 04:56:45 +000081 * in the event that busybox starts wanting more --options.
82 * If someone has a cleaner approach, by all means implement it.
83 */
Rob Landleyb766c392005-09-04 11:10:37 +000084 if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
John Beppu8f425db2000-06-27 04:50:02 +000085 int use_symbolic_links = 0;
John Beppu7cdc76d2000-06-28 00:41:26 +000086 char *busybox;
John Beppu8f425db2000-06-27 04:50:02 +000087
John Beppu27b59242000-06-27 04:56:45 +000088 /* to use symlinks, or not to use symlinks... */
Denis Vlasenko0ee39992006-12-24 15:23:28 +000089 if (argc > 2)
90 if (strcmp(argv[2], "-s") == 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000091 use_symbolic_links = 1;
John Beppu7cdc76d2000-06-28 00:41:26 +000092
93 /* link */
Bernhard Reutner-Fischerb40bdb32006-11-22 18:40:06 +000094// XXX: FIXME: this is broken. Why not just use argv[0] ?
Denis Vlasenko6ca04442007-02-11 16:19:28 +000095 busybox = xmalloc_readlink_or_warn("/proc/self/exe");
Denis Vlasenko0ee39992006-12-24 15:23:28 +000096 if (!busybox)
97 return 1;
98 install_links(busybox, use_symbolic_links);
99 if (ENABLE_FEATURE_CLEAN_UP)
John Beppu7cdc76d2000-06-28 00:41:26 +0000100 free(busybox);
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000101 return 0;
John Beppu8f425db2000-06-27 04:50:02 +0000102 }
John Beppu8f425db2000-06-27 04:50:02 +0000103
Rob Landleyb766c392005-09-04 11:10:37 +0000104 /* Deal with --help. (Also print help when called with no arguments) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000105
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000106 if (argc == 1 || !strcmp(argv[1], "--help") ) {
107 if (argc > 2) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000108 applet_name = argv[2];
109 run_applet_by_name(applet_name, 2, argv);
"Vladimir N. Oleynik"10a1fe62005-09-05 11:25:27 +0000110 } else {
Rob Landleyb766c392005-09-04 11:10:37 +0000111 const struct BB_applet *a;
112 int col, output_width;
Eric Andersencc8ed391999-10-05 16:24:54 +0000113
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000114 output_width = 80 - sizeof("start-stop-daemon, ") - 8;
Rob Landleyb766c392005-09-04 11:10:37 +0000115 if (ENABLE_FEATURE_AUTOWIDTH) {
116 /* Obtain the terminal width. */
117 get_terminal_width_height(0, &output_width, NULL);
118 /* leading tab and room to wrap */
Denis Vlasenko01c27fc2006-10-05 21:10:53 +0000119 output_width -= sizeof("start-stop-daemon, ") + 8;
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000120 }
Tim Rikerb1ffba02003-11-07 19:37:20 +0000121
Rob Landleyeb84a422006-09-20 21:41:13 +0000122 printf("%s\n"
Denis Vlasenko01c27fc2006-10-05 21:10:53 +0000123 "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n"
124 "Licensed under GPLv2.  See source distribution for full notice.\n\n"
Rob Landleyb766c392005-09-04 11:10:37 +0000125 "Usage: busybox [function] [arguments]...\n"
126 " or: [function] [arguments]...\n\n"
127 "\tBusyBox is a multi-call binary that combines many common Unix\n"
128 "\tutilities into a single executable. Most people will create a\n"
129 "\tlink to busybox for each function they wish to use and BusyBox\n"
130 "\twill act like whatever it was invoked as!\n"
131 "\nCurrently defined functions:\n", bb_msg_full_version);
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000132 col = 0;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000133 for (a = applets; a->name;) {
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000134 col += printf("%s%s", (col ? ", " : "\t"), a->name);
135 a++;
Rob Landleyb766c392005-09-04 11:10:37 +0000136 if (col > output_width && a->name) {
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000137 puts(",");
Rob Landleyb766c392005-09-04 11:10:37 +0000138 col = 0;
139 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 }
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000141 puts("\n");
142 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 }
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000144 } else run_applet_by_name(argv[1], argc - 1, argv + 1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000145
Rob Landleyb766c392005-09-04 11:10:37 +0000146 bb_error_msg_and_die("applet not found");
Eric Andersencc8ed391999-10-05 16:24:54 +0000147}