blob: 79634f99e97a85853d856e1f300afff9e0b37dab [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
Eric Andersenf5d5e772001-01-24 23:34:48 +000076int busybox_main(int argc, char **argv)
77{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000078 /*
79 * This style of argument parsing doesn't scale well
John Beppu27b59242000-06-27 04:56:45 +000080 * in the event that busybox starts wanting more --options.
81 * If someone has a cleaner approach, by all means implement it.
82 */
Rob Landleyb766c392005-09-04 11:10:37 +000083 if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
John Beppu8f425db2000-06-27 04:50:02 +000084 int use_symbolic_links = 0;
John Beppu7cdc76d2000-06-28 00:41:26 +000085 char *busybox;
John Beppu8f425db2000-06-27 04:50:02 +000086
John Beppu27b59242000-06-27 04:56:45 +000087 /* to use symlinks, or not to use symlinks... */
Denis Vlasenko0ee39992006-12-24 15:23:28 +000088 if (argc > 2)
89 if (strcmp(argv[2], "-s") == 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000090 use_symbolic_links = 1;
John Beppu7cdc76d2000-06-28 00:41:26 +000091
92 /* link */
Bernhard Reutner-Fischerb40bdb32006-11-22 18:40:06 +000093// XXX: FIXME: this is broken. Why not just use argv[0] ?
Rob Landleyb766c392005-09-04 11:10:37 +000094 busybox = xreadlink("/proc/self/exe");
Denis Vlasenko0ee39992006-12-24 15:23:28 +000095 if (!busybox)
96 return 1;
97 install_links(busybox, use_symbolic_links);
98 if (ENABLE_FEATURE_CLEAN_UP)
John Beppu7cdc76d2000-06-28 00:41:26 +000099 free(busybox);
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000100 return 0;
John Beppu8f425db2000-06-27 04:50:02 +0000101 }
John Beppu8f425db2000-06-27 04:50:02 +0000102
Rob Landleyb766c392005-09-04 11:10:37 +0000103 /* Deal with --help. (Also print help when called with no arguments) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000104
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000105 if (argc == 1 || !strcmp(argv[1], "--help") ) {
106 if (argc > 2) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000107 applet_name = argv[2];
108 run_applet_by_name(applet_name, 2, argv);
"Vladimir N. Oleynik"10a1fe62005-09-05 11:25:27 +0000109 } else {
Rob Landleyb766c392005-09-04 11:10:37 +0000110 const struct BB_applet *a;
111 int col, output_width;
Eric Andersencc8ed391999-10-05 16:24:54 +0000112
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000113 output_width = 80 - sizeof("start-stop-daemon, ") - 8;
Rob Landleyb766c392005-09-04 11:10:37 +0000114 if (ENABLE_FEATURE_AUTOWIDTH) {
115 /* Obtain the terminal width. */
116 get_terminal_width_height(0, &output_width, NULL);
117 /* leading tab and room to wrap */
Denis Vlasenko01c27fc2006-10-05 21:10:53 +0000118 output_width -= sizeof("start-stop-daemon, ") + 8;
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000119 }
Tim Rikerb1ffba02003-11-07 19:37:20 +0000120
Rob Landleyeb84a422006-09-20 21:41:13 +0000121 printf("%s\n"
Denis Vlasenko01c27fc2006-10-05 21:10:53 +0000122 "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n"
123 "Licensed under GPLv2.  See source distribution for full notice.\n\n"
Rob Landleyb766c392005-09-04 11:10:37 +0000124 "Usage: busybox [function] [arguments]...\n"
125 " or: [function] [arguments]...\n\n"
126 "\tBusyBox is a multi-call binary that combines many common Unix\n"
127 "\tutilities into a single executable. Most people will create a\n"
128 "\tlink to busybox for each function they wish to use and BusyBox\n"
129 "\twill act like whatever it was invoked as!\n"
130 "\nCurrently defined functions:\n", bb_msg_full_version);
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000131 col = 0;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000132 for (a = applets; a->name;) {
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000133 col += printf("%s%s", (col ? ", " : "\t"), a->name);
134 a++;
Rob Landleyb766c392005-09-04 11:10:37 +0000135 if (col > output_width && a->name) {
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000136 puts(",");
Rob Landleyb766c392005-09-04 11:10:37 +0000137 col = 0;
138 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139 }
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000140 puts("\n");
141 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 }
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000143 } else run_applet_by_name(argv[1], argc - 1, argv + 1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000144
Rob Landleyb766c392005-09-04 11:10:37 +0000145 bb_error_msg_and_die("applet not found");
Eric Andersencc8ed391999-10-05 16:24:54 +0000146}