blob: 833e8d37292ccb8fa603aa43d311182692a256b8 [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"
Rob Landley8a7a6782005-09-05 04:13:33 +00008#if ENABLE_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +00009#include <locale.h>
Rob Landley8a7a6782005-09-05 04:13:33 +000010#else
11#define setlocale(x,y)
Eric Andersene5dfced2001-04-09 22:48:12 +000012#endif
13
Manuel Novoa III cad53642003-03-19 09:13:01 +000014const char *bb_applet_name;
Erik Andersen05df2392000-01-13 04:43:48 +000015
Eric Andersenbdfd0d72001-10-24 05:00:29 +000016#ifdef CONFIG_FEATURE_INSTALLER
Eric Andersenc7bda1c2004-03-15 08:29:22 +000017/*
John Beppu8f425db2000-06-27 04:50:02 +000018 * 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 */
Eric Andersenfcffa2c2002-04-06 05:17:57 +000022static const char usr_bin [] ="/usr/bin";
23static const char usr_sbin[] ="/usr/sbin";
24
25static const char* const install_dir[] = {
26 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
27 &usr_bin [4], /* "/bin" */
28 &usr_sbin[4], /* "/sbin" */
29 usr_bin,
30 usr_sbin
John Beppu8f425db2000-06-27 04:50:02 +000031};
32
33/* abstract link() */
34typedef int (*__link_f)(const char *, const char *);
35
36/* create (sym)links for each applet */
Eric Andersenc5949f62000-09-25 20:35:54 +000037static void install_links(const char *busybox, int use_symbolic_links)
John Beppu8f425db2000-06-27 04:50:02 +000038{
John Beppueb028332000-06-28 00:55:31 +000039 __link_f Link = link;
John Beppu8f425db2000-06-27 04:50:02 +000040
Eric Andersene5dfced2001-04-09 22:48:12 +000041 char *fpc;
John Beppueb028332000-06-28 00:55:31 +000042 int i;
Eric Andersenc5949f62000-09-25 20:35:54 +000043 int rc;
John Beppu8f425db2000-06-27 04:50:02 +000044
Eric Andersenc7bda1c2004-03-15 08:29:22 +000045 if (use_symbolic_links)
Eric Andersen90ca2842001-01-27 08:32:57 +000046 Link = symlink;
John Beppu8f425db2000-06-27 04:50:02 +000047
John Beppueb028332000-06-28 00:55:31 +000048 for (i = 0; applets[i].name != NULL; i++) {
Eric Andersene5dfced2001-04-09 22:48:12 +000049 fpc = concat_path_file(
50 install_dir[applets[i].location], applets[i].name);
51 rc = Link(busybox, fpc);
52 if (rc!=0 && errno!=EEXIST) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 bb_perror_msg("%s", fpc);
John Beppu8f425db2000-06-27 04:50:02 +000054 }
Eric Andersene5dfced2001-04-09 22:48:12 +000055 free(fpc);
John Beppueb028332000-06-28 00:55:31 +000056 }
John Beppu8f425db2000-06-27 04:50:02 +000057}
58
Rob Landley8a7a6782005-09-05 04:13:33 +000059#else
60#define install_links(x,y)
Eric Andersenbdfd0d72001-10-24 05:00:29 +000061#endif /* CONFIG_FEATURE_INSTALLER */
John Beppu8f425db2000-06-27 04:50:02 +000062
Eric Andersencc8ed391999-10-05 16:24:54 +000063int main(int argc, char **argv)
64{
Matt Kraaif2cc2762001-02-01 19:21:20 +000065 const char *s;
Eric Andersenf5d5e772001-01-24 23:34:48 +000066
Rob Landleyb766c392005-09-04 11:10:37 +000067 bb_applet_name=argv[0];
68 if (*bb_applet_name == '-') bb_applet_name++;
69 for (s = bb_applet_name; *s ;)
70 if (*(s++) == '/') bb_applet_name = s;
Matt Kraai449377a2001-08-27 15:02:32 +000071
Rob Landleyb766c392005-09-04 11:10:37 +000072 /* Set locale for everybody except `init' */
73 if(ENABLE_LOCALE_SUPPORT && (!ENABLE_INIT || getpid()==1))
Eric Andersene5dfced2001-04-09 22:48:12 +000074 setlocale(LC_ALL, "");
Eric Andersene5dfced2001-04-09 22:48:12 +000075
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 run_applet_by_name(bb_applet_name, argc, argv);
77 bb_error_msg_and_die("applet not found");
Eric Andersenf5d5e772001-01-24 23:34:48 +000078}
79
Eric Andersenf5d5e772001-01-24 23:34:48 +000080int busybox_main(int argc, char **argv)
81{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000082 /*
83 * This style of argument parsing doesn't scale well
John Beppu27b59242000-06-27 04:56:45 +000084 * in the event that busybox starts wanting more --options.
85 * If someone has a cleaner approach, by all means implement it.
86 */
Rob Landleyb766c392005-09-04 11:10:37 +000087 if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
John Beppu8f425db2000-06-27 04:50:02 +000088 int use_symbolic_links = 0;
John Beppu7cdc76d2000-06-28 00:41:26 +000089 int rc = 0;
90 char *busybox;
John Beppu8f425db2000-06-27 04:50:02 +000091
John Beppu27b59242000-06-27 04:56:45 +000092 /* to use symlinks, or not to use symlinks... */
John Beppu8f425db2000-06-27 04:50:02 +000093 if (argc > 2) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000094 if ((strcmp(argv[2], "-s") == 0)) {
95 use_symbolic_links = 1;
John Beppu8f425db2000-06-27 04:50:02 +000096 }
97 }
John Beppu7cdc76d2000-06-28 00:41:26 +000098
99 /* link */
Rob Landleyb766c392005-09-04 11:10:37 +0000100 busybox = xreadlink("/proc/self/exe");
John Beppu7cdc76d2000-06-28 00:41:26 +0000101 if (busybox) {
102 install_links(busybox, use_symbolic_links);
103 free(busybox);
104 } else {
105 rc = 1;
106 }
107 return rc;
John Beppu8f425db2000-06-27 04:50:02 +0000108 }
John Beppu8f425db2000-06-27 04:50:02 +0000109
Rob Landleyb766c392005-09-04 11:10:37 +0000110 /* Deal with --help. (Also print help when called with no arguments) */
111
112 if (argc==1 || !strcmp(argv[1],"--help") ) {
"Vladimir N. Oleynik"10a1fe62005-09-05 11:25:27 +0000113 if (argc>2) {
114 run_applet_by_name(bb_applet_name=argv[2], 2, argv);
115 } else {
Rob Landleyb766c392005-09-04 11:10:37 +0000116 const struct BB_applet *a;
117 int col, output_width;
Eric Andersencc8ed391999-10-05 16:24:54 +0000118
Rob Landleyb766c392005-09-04 11:10:37 +0000119 if (ENABLE_FEATURE_AUTOWIDTH) {
120 /* Obtain the terminal width. */
121 get_terminal_width_height(0, &output_width, NULL);
122 /* leading tab and room to wrap */
123 output_width -= 20;
124 } else output_width = 60;
Tim Rikerb1ffba02003-11-07 19:37:20 +0000125
Rob Landleyb766c392005-09-04 11:10:37 +0000126 printf("%s\n\n"
127 "Usage: busybox [function] [arguments]...\n"
128 " or: [function] [arguments]...\n\n"
129 "\tBusyBox is a multi-call binary that combines many common Unix\n"
130 "\tutilities into a single executable. Most people will create a\n"
131 "\tlink to busybox for each function they wish to use and BusyBox\n"
132 "\twill act like whatever it was invoked as!\n"
133 "\nCurrently defined functions:\n", bb_msg_full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134
Rob Landleyb766c392005-09-04 11:10:37 +0000135 col=0;
136 for(a = applets; a->name;) {
137 col += printf("%s%s", (col ? ", " : "\t"), (a++)->name);
138 if (col > output_width && a->name) {
139 printf(",\n");
140 col = 0;
141 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 }
Rob Landleyb766c392005-09-04 11:10:37 +0000143 printf("\n\n");
144 exit(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 }
Rob Landleyb766c392005-09-04 11:10:37 +0000146 } else run_applet_by_name(bb_applet_name=argv[1], argc-1, argv+1);
147
148 bb_error_msg_and_die("applet not found");
Eric Andersencc8ed391999-10-05 16:24:54 +0000149}
Erik Andersen029011b2000-03-04 21:19:32 +0000150
151/*
152Local Variables:
153c-file-style: "linux"
154c-basic-offset: 4
155tab-width: 4
156End:
157*/