blob: a92ddbd0ea00e393feac1c9932d473b770c45291 [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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00007#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +00008#include <stdio.h>
9#include <string.h>
Eric Andersen90ca2842001-01-27 08:32:57 +000010#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000011#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000012#include <stdlib.h>
Rob Landley8a7a6782005-09-05 04:13:33 +000013#if ENABLE_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +000014#include <locale.h>
Rob Landley8a7a6782005-09-05 04:13:33 +000015#else
16#define setlocale(x,y)
Eric Andersene5dfced2001-04-09 22:48:12 +000017#endif
18
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000019const char *bb_applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
Erik Andersen05df2392000-01-13 04:43:48 +000020
Eric Andersenbdfd0d72001-10-24 05:00:29 +000021#ifdef CONFIG_FEATURE_INSTALLER
Eric Andersenc7bda1c2004-03-15 08:29:22 +000022/*
John Beppu8f425db2000-06-27 04:50:02 +000023 * directory table
Eric Andersen3570a342000-09-25 21:45:58 +000024 * this should be consistent w/ the enum, busybox.h::Location,
John Beppueb028332000-06-28 00:55:31 +000025 * or else...
John Beppu8f425db2000-06-27 04:50:02 +000026 */
Eric Andersenfcffa2c2002-04-06 05:17:57 +000027static const char usr_bin [] ="/usr/bin";
28static const char usr_sbin[] ="/usr/sbin";
29
30static const char* const install_dir[] = {
31 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
32 &usr_bin [4], /* "/bin" */
33 &usr_sbin[4], /* "/sbin" */
34 usr_bin,
35 usr_sbin
John Beppu8f425db2000-06-27 04:50:02 +000036};
37
38/* abstract link() */
39typedef int (*__link_f)(const char *, const char *);
40
41/* create (sym)links for each applet */
Eric Andersenc5949f62000-09-25 20:35:54 +000042static void install_links(const char *busybox, int use_symbolic_links)
John Beppu8f425db2000-06-27 04:50:02 +000043{
John Beppueb028332000-06-28 00:55:31 +000044 __link_f Link = link;
John Beppu8f425db2000-06-27 04:50:02 +000045
Eric Andersene5dfced2001-04-09 22:48:12 +000046 char *fpc;
John Beppueb028332000-06-28 00:55:31 +000047 int i;
Eric Andersenc5949f62000-09-25 20:35:54 +000048 int rc;
John Beppu8f425db2000-06-27 04:50:02 +000049
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050 if (use_symbolic_links)
Eric Andersen90ca2842001-01-27 08:32:57 +000051 Link = symlink;
John Beppu8f425db2000-06-27 04:50:02 +000052
John Beppueb028332000-06-28 00:55:31 +000053 for (i = 0; applets[i].name != NULL; i++) {
Eric Andersene5dfced2001-04-09 22:48:12 +000054 fpc = concat_path_file(
55 install_dir[applets[i].location], applets[i].name);
56 rc = Link(busybox, fpc);
57 if (rc!=0 && errno!=EEXIST) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 bb_perror_msg("%s", fpc);
John Beppu8f425db2000-06-27 04:50:02 +000059 }
Eric Andersene5dfced2001-04-09 22:48:12 +000060 free(fpc);
John Beppueb028332000-06-28 00:55:31 +000061 }
John Beppu8f425db2000-06-27 04:50:02 +000062}
63
Rob Landley8a7a6782005-09-05 04:13:33 +000064#else
65#define install_links(x,y)
Eric Andersenbdfd0d72001-10-24 05:00:29 +000066#endif /* CONFIG_FEATURE_INSTALLER */
John Beppu8f425db2000-06-27 04:50:02 +000067
Eric Andersencc8ed391999-10-05 16:24:54 +000068int main(int argc, char **argv)
69{
Matt Kraaif2cc2762001-02-01 19:21:20 +000070 const char *s;
Eric Andersenf5d5e772001-01-24 23:34:48 +000071
Rob Landleyb766c392005-09-04 11:10:37 +000072 bb_applet_name=argv[0];
73 if (*bb_applet_name == '-') bb_applet_name++;
74 for (s = bb_applet_name; *s ;)
75 if (*(s++) == '/') bb_applet_name = s;
Matt Kraai449377a2001-08-27 15:02:32 +000076
Rob Landleyb766c392005-09-04 11:10:37 +000077 /* Set locale for everybody except `init' */
"Vladimir N. Oleynik"74078682005-09-29 08:19:04 +000078 if(ENABLE_LOCALE_SUPPORT && getpid() != 1)
Eric Andersene5dfced2001-04-09 22:48:12 +000079 setlocale(LC_ALL, "");
Eric Andersene5dfced2001-04-09 22:48:12 +000080
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 run_applet_by_name(bb_applet_name, argc, argv);
82 bb_error_msg_and_die("applet not found");
Eric Andersenf5d5e772001-01-24 23:34:48 +000083}
84
Eric Andersenf5d5e772001-01-24 23:34:48 +000085int busybox_main(int argc, char **argv)
86{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087 /*
88 * This style of argument parsing doesn't scale well
John Beppu27b59242000-06-27 04:56:45 +000089 * in the event that busybox starts wanting more --options.
90 * If someone has a cleaner approach, by all means implement it.
91 */
Rob Landleyb766c392005-09-04 11:10:37 +000092 if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
John Beppu8f425db2000-06-27 04:50:02 +000093 int use_symbolic_links = 0;
John Beppu7cdc76d2000-06-28 00:41:26 +000094 int rc = 0;
95 char *busybox;
John Beppu8f425db2000-06-27 04:50:02 +000096
John Beppu27b59242000-06-27 04:56:45 +000097 /* to use symlinks, or not to use symlinks... */
John Beppu8f425db2000-06-27 04:50:02 +000098 if (argc > 2) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000099 if ((strcmp(argv[2], "-s") == 0)) {
100 use_symbolic_links = 1;
John Beppu8f425db2000-06-27 04:50:02 +0000101 }
102 }
John Beppu7cdc76d2000-06-28 00:41:26 +0000103
104 /* link */
Rob Landleyb766c392005-09-04 11:10:37 +0000105 busybox = xreadlink("/proc/self/exe");
John Beppu7cdc76d2000-06-28 00:41:26 +0000106 if (busybox) {
107 install_links(busybox, use_symbolic_links);
108 free(busybox);
109 } else {
110 rc = 1;
111 }
112 return rc;
John Beppu8f425db2000-06-27 04:50:02 +0000113 }
John Beppu8f425db2000-06-27 04:50:02 +0000114
Rob Landleyb766c392005-09-04 11:10:37 +0000115 /* Deal with --help. (Also print help when called with no arguments) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000116
Rob Landleyb766c392005-09-04 11:10:37 +0000117 if (argc==1 || !strcmp(argv[1],"--help") ) {
"Vladimir N. Oleynik"10a1fe62005-09-05 11:25:27 +0000118 if (argc>2) {
119 run_applet_by_name(bb_applet_name=argv[2], 2, argv);
120 } else {
Rob Landleyb766c392005-09-04 11:10:37 +0000121 const struct BB_applet *a;
122 int col, output_width;
Eric Andersencc8ed391999-10-05 16:24:54 +0000123
Rob Landleyb766c392005-09-04 11:10:37 +0000124 if (ENABLE_FEATURE_AUTOWIDTH) {
125 /* Obtain the terminal width. */
126 get_terminal_width_height(0, &output_width, NULL);
127 /* leading tab and room to wrap */
128 output_width -= 20;
129 } else output_width = 60;
Tim Rikerb1ffba02003-11-07 19:37:20 +0000130
Rob Landleyb766c392005-09-04 11:10:37 +0000131 printf("%s\n\n"
132 "Usage: busybox [function] [arguments]...\n"
133 " or: [function] [arguments]...\n\n"
134 "\tBusyBox is a multi-call binary that combines many common Unix\n"
135 "\tutilities into a single executable. Most people will create a\n"
136 "\tlink to busybox for each function they wish to use and BusyBox\n"
137 "\twill act like whatever it was invoked as!\n"
138 "\nCurrently defined functions:\n", bb_msg_full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139
Rob Landleyb766c392005-09-04 11:10:37 +0000140 col=0;
141 for(a = applets; a->name;) {
142 col += printf("%s%s", (col ? ", " : "\t"), (a++)->name);
143 if (col > output_width && a->name) {
144 printf(",\n");
145 col = 0;
146 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 }
Rob Landleyb766c392005-09-04 11:10:37 +0000148 printf("\n\n");
149 exit(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000150 }
Rob Landley9fb272a2006-05-07 01:44:23 +0000151 } else run_applet_by_name(argv[1], argc-1, argv+1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000152
Rob Landleyb766c392005-09-04 11:10:37 +0000153 bb_error_msg_and_die("applet not found");
Eric Andersencc8ed391999-10-05 16:24:54 +0000154}