blob: 420c9c0e5d350ffec83ae5431056ca289917006a [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 Andersenbdfd0d72001-10-24 05:00:29 +00008#ifdef CONFIG_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +00009#include <locale.h>
10#endif
11
Manuel Novoa III cad53642003-03-19 09:13:01 +000012const char *bb_applet_name;
Erik Andersen05df2392000-01-13 04:43:48 +000013
Eric Andersenbdfd0d72001-10-24 05:00:29 +000014#ifdef CONFIG_FEATURE_INSTALLER
Eric Andersenc7bda1c2004-03-15 08:29:22 +000015/*
John Beppu8f425db2000-06-27 04:50:02 +000016 * directory table
Eric Andersen3570a342000-09-25 21:45:58 +000017 * this should be consistent w/ the enum, busybox.h::Location,
John Beppueb028332000-06-28 00:55:31 +000018 * or else...
John Beppu8f425db2000-06-27 04:50:02 +000019 */
Eric Andersenfcffa2c2002-04-06 05:17:57 +000020static const char usr_bin [] ="/usr/bin";
21static const char usr_sbin[] ="/usr/sbin";
22
23static const char* const install_dir[] = {
24 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
25 &usr_bin [4], /* "/bin" */
26 &usr_sbin[4], /* "/sbin" */
27 usr_bin,
28 usr_sbin
John Beppu8f425db2000-06-27 04:50:02 +000029};
30
31/* abstract link() */
32typedef int (*__link_f)(const char *, const char *);
33
34/* create (sym)links for each applet */
Eric Andersenc5949f62000-09-25 20:35:54 +000035static void install_links(const char *busybox, int use_symbolic_links)
John Beppu8f425db2000-06-27 04:50:02 +000036{
John Beppueb028332000-06-28 00:55:31 +000037 __link_f Link = link;
John Beppu8f425db2000-06-27 04:50:02 +000038
Eric Andersene5dfced2001-04-09 22:48:12 +000039 char *fpc;
John Beppueb028332000-06-28 00:55:31 +000040 int i;
Eric Andersenc5949f62000-09-25 20:35:54 +000041 int rc;
John Beppu8f425db2000-06-27 04:50:02 +000042
Eric Andersenc7bda1c2004-03-15 08:29:22 +000043 if (use_symbolic_links)
Eric Andersen90ca2842001-01-27 08:32:57 +000044 Link = symlink;
John Beppu8f425db2000-06-27 04:50:02 +000045
John Beppueb028332000-06-28 00:55:31 +000046 for (i = 0; applets[i].name != NULL; i++) {
Eric Andersene5dfced2001-04-09 22:48:12 +000047 fpc = concat_path_file(
48 install_dir[applets[i].location], applets[i].name);
49 rc = Link(busybox, fpc);
50 if (rc!=0 && errno!=EEXIST) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 bb_perror_msg("%s", fpc);
John Beppu8f425db2000-06-27 04:50:02 +000052 }
Eric Andersene5dfced2001-04-09 22:48:12 +000053 free(fpc);
John Beppueb028332000-06-28 00:55:31 +000054 }
John Beppu8f425db2000-06-27 04:50:02 +000055}
56
Eric Andersenbdfd0d72001-10-24 05:00:29 +000057#endif /* CONFIG_FEATURE_INSTALLER */
John Beppu8f425db2000-06-27 04:50:02 +000058
Eric Andersencc8ed391999-10-05 16:24:54 +000059int main(int argc, char **argv)
60{
Matt Kraaif2cc2762001-02-01 19:21:20 +000061 const char *s;
Eric Andersenf5d5e772001-01-24 23:34:48 +000062
Rob Landleyb766c392005-09-04 11:10:37 +000063 bb_applet_name=argv[0];
64 if (*bb_applet_name == '-') bb_applet_name++;
65 for (s = bb_applet_name; *s ;)
66 if (*(s++) == '/') bb_applet_name = s;
Matt Kraai449377a2001-08-27 15:02:32 +000067
Rob Landleyb766c392005-09-04 11:10:37 +000068 /* Set locale for everybody except `init' */
69 if(ENABLE_LOCALE_SUPPORT && (!ENABLE_INIT || getpid()==1))
Eric Andersene5dfced2001-04-09 22:48:12 +000070 setlocale(LC_ALL, "");
Eric Andersene5dfced2001-04-09 22:48:12 +000071
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 run_applet_by_name(bb_applet_name, argc, argv);
73 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 int rc = 0;
86 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... */
John Beppu8f425db2000-06-27 04:50:02 +000089 if (argc > 2) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000090 if ((strcmp(argv[2], "-s") == 0)) {
91 use_symbolic_links = 1;
John Beppu8f425db2000-06-27 04:50:02 +000092 }
93 }
John Beppu7cdc76d2000-06-28 00:41:26 +000094
95 /* link */
Rob Landleyb766c392005-09-04 11:10:37 +000096 busybox = xreadlink("/proc/self/exe");
John Beppu7cdc76d2000-06-28 00:41:26 +000097 if (busybox) {
98 install_links(busybox, use_symbolic_links);
99 free(busybox);
100 } else {
101 rc = 1;
102 }
103 return rc;
John Beppu8f425db2000-06-27 04:50:02 +0000104 }
John Beppu8f425db2000-06-27 04:50:02 +0000105
Rob Landleyb766c392005-09-04 11:10:37 +0000106 /* Deal with --help. (Also print help when called with no arguments) */
107
108 if (argc==1 || !strcmp(argv[1],"--help") ) {
109 if (argc>2) run_applet_by_name(bb_applet_name=argv[2], argc, argv);
110 else {
111 const struct BB_applet *a;
112 int col, output_width;
Eric Andersencc8ed391999-10-05 16:24:54 +0000113
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 */
118 output_width -= 20;
119 } else output_width = 60;
Tim Rikerb1ffba02003-11-07 19:37:20 +0000120
Rob Landleyb766c392005-09-04 11:10:37 +0000121 printf("%s\n\n"
122 "Usage: busybox [function] [arguments]...\n"
123 " or: [function] [arguments]...\n\n"
124 "\tBusyBox is a multi-call binary that combines many common Unix\n"
125 "\tutilities into a single executable. Most people will create a\n"
126 "\tlink to busybox for each function they wish to use and BusyBox\n"
127 "\twill act like whatever it was invoked as!\n"
128 "\nCurrently defined functions:\n", bb_msg_full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129
Rob Landleyb766c392005-09-04 11:10:37 +0000130 col=0;
131 for(a = applets; a->name;) {
132 col += printf("%s%s", (col ? ", " : "\t"), (a++)->name);
133 if (col > output_width && a->name) {
134 printf(",\n");
135 col = 0;
136 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 }
Rob Landleyb766c392005-09-04 11:10:37 +0000138 printf("\n\n");
139 exit(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 }
Rob Landleyb766c392005-09-04 11:10:37 +0000141 } else run_applet_by_name(bb_applet_name=argv[1], argc-1, argv+1);
142
143 bb_error_msg_and_die("applet not found");
Eric Andersencc8ed391999-10-05 16:24:54 +0000144}
Erik Andersen029011b2000-03-04 21:19:32 +0000145
146/*
147Local Variables:
148c-file-style: "linux"
149c-basic-offset: 4
150tab-width: 4
151End:
152*/