blob: 25bb36d9c007b0e2724bb4ab77db0631df30a515 [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/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
Eric Andersencc8ed391999-10-05 16:24:54 +00005#include <stdio.h>
6#include <string.h>
Eric Andersen90ca2842001-01-27 08:32:57 +00007#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +00008#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +00009#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000010#include "busybox.h"
Rob Landley8a7a6782005-09-05 04:13:33 +000011#if ENABLE_LOCALE_SUPPORT
Eric Andersene5dfced2001-04-09 22:48:12 +000012#include <locale.h>
Rob Landley8a7a6782005-09-05 04:13:33 +000013#else
14#define setlocale(x,y)
Eric Andersene5dfced2001-04-09 22:48:12 +000015#endif
16
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000017const char *bb_applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
Erik Andersen05df2392000-01-13 04:43:48 +000018
Eric Andersenbdfd0d72001-10-24 05:00:29 +000019#ifdef CONFIG_FEATURE_INSTALLER
Eric Andersenc7bda1c2004-03-15 08:29:22 +000020/*
John Beppu8f425db2000-06-27 04:50:02 +000021 * directory table
Eric Andersen3570a342000-09-25 21:45:58 +000022 * this should be consistent w/ the enum, busybox.h::Location,
John Beppueb028332000-06-28 00:55:31 +000023 * or else...
John Beppu8f425db2000-06-27 04:50:02 +000024 */
Eric Andersenfcffa2c2002-04-06 05:17:57 +000025static const char usr_bin [] ="/usr/bin";
26static const char usr_sbin[] ="/usr/sbin";
27
28static const char* const install_dir[] = {
29 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
30 &usr_bin [4], /* "/bin" */
31 &usr_sbin[4], /* "/sbin" */
32 usr_bin,
33 usr_sbin
John Beppu8f425db2000-06-27 04:50:02 +000034};
35
36/* abstract link() */
37typedef int (*__link_f)(const char *, const char *);
38
39/* create (sym)links for each applet */
Eric Andersenc5949f62000-09-25 20:35:54 +000040static void install_links(const char *busybox, int use_symbolic_links)
John Beppu8f425db2000-06-27 04:50:02 +000041{
John Beppueb028332000-06-28 00:55:31 +000042 __link_f Link = link;
John Beppu8f425db2000-06-27 04:50:02 +000043
Eric Andersene5dfced2001-04-09 22:48:12 +000044 char *fpc;
John Beppueb028332000-06-28 00:55:31 +000045 int i;
Eric Andersenc5949f62000-09-25 20:35:54 +000046 int rc;
John Beppu8f425db2000-06-27 04:50:02 +000047
Eric Andersenc7bda1c2004-03-15 08:29:22 +000048 if (use_symbolic_links)
Eric Andersen90ca2842001-01-27 08:32:57 +000049 Link = symlink;
John Beppu8f425db2000-06-27 04:50:02 +000050
John Beppueb028332000-06-28 00:55:31 +000051 for (i = 0; applets[i].name != NULL; i++) {
Eric Andersene5dfced2001-04-09 22:48:12 +000052 fpc = concat_path_file(
53 install_dir[applets[i].location], applets[i].name);
54 rc = Link(busybox, fpc);
55 if (rc!=0 && errno!=EEXIST) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 bb_perror_msg("%s", fpc);
John Beppu8f425db2000-06-27 04:50:02 +000057 }
Eric Andersene5dfced2001-04-09 22:48:12 +000058 free(fpc);
John Beppueb028332000-06-28 00:55:31 +000059 }
John Beppu8f425db2000-06-27 04:50:02 +000060}
61
Rob Landley8a7a6782005-09-05 04:13:33 +000062#else
63#define install_links(x,y)
Eric Andersenbdfd0d72001-10-24 05:00:29 +000064#endif /* CONFIG_FEATURE_INSTALLER */
John Beppu8f425db2000-06-27 04:50:02 +000065
Eric Andersencc8ed391999-10-05 16:24:54 +000066int main(int argc, char **argv)
67{
Matt Kraaif2cc2762001-02-01 19:21:20 +000068 const char *s;
Eric Andersenf5d5e772001-01-24 23:34:48 +000069
Rob Landleyb766c392005-09-04 11:10:37 +000070 bb_applet_name=argv[0];
71 if (*bb_applet_name == '-') bb_applet_name++;
72 for (s = bb_applet_name; *s ;)
73 if (*(s++) == '/') bb_applet_name = s;
Matt Kraai449377a2001-08-27 15:02:32 +000074
Rob Landleyb766c392005-09-04 11:10:37 +000075 /* Set locale for everybody except `init' */
"Vladimir N. Oleynik"74078682005-09-29 08:19:04 +000076 if(ENABLE_LOCALE_SUPPORT && getpid() != 1)
Eric Andersene5dfced2001-04-09 22:48:12 +000077 setlocale(LC_ALL, "");
Eric Andersene5dfced2001-04-09 22:48:12 +000078
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 run_applet_by_name(bb_applet_name, argc, argv);
80 bb_error_msg_and_die("applet not found");
Eric Andersenf5d5e772001-01-24 23:34:48 +000081}
82
Eric Andersenf5d5e772001-01-24 23:34:48 +000083int busybox_main(int argc, char **argv)
84{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000085 /*
86 * This style of argument parsing doesn't scale well
John Beppu27b59242000-06-27 04:56:45 +000087 * in the event that busybox starts wanting more --options.
88 * If someone has a cleaner approach, by all means implement it.
89 */
Rob Landleyb766c392005-09-04 11:10:37 +000090 if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
John Beppu8f425db2000-06-27 04:50:02 +000091 int use_symbolic_links = 0;
John Beppu7cdc76d2000-06-28 00:41:26 +000092 int rc = 0;
93 char *busybox;
John Beppu8f425db2000-06-27 04:50:02 +000094
John Beppu27b59242000-06-27 04:56:45 +000095 /* to use symlinks, or not to use symlinks... */
John Beppu8f425db2000-06-27 04:50:02 +000096 if (argc > 2) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000097 if ((strcmp(argv[2], "-s") == 0)) {
98 use_symbolic_links = 1;
John Beppu8f425db2000-06-27 04:50:02 +000099 }
100 }
John Beppu7cdc76d2000-06-28 00:41:26 +0000101
102 /* link */
Rob Landleyb766c392005-09-04 11:10:37 +0000103 busybox = xreadlink("/proc/self/exe");
John Beppu7cdc76d2000-06-28 00:41:26 +0000104 if (busybox) {
105 install_links(busybox, use_symbolic_links);
106 free(busybox);
107 } else {
108 rc = 1;
109 }
110 return rc;
John Beppu8f425db2000-06-27 04:50:02 +0000111 }
John Beppu8f425db2000-06-27 04:50:02 +0000112
Rob Landleyb766c392005-09-04 11:10:37 +0000113 /* Deal with --help. (Also print help when called with no arguments) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000114
Rob Landleyb766c392005-09-04 11:10:37 +0000115 if (argc==1 || !strcmp(argv[1],"--help") ) {
"Vladimir N. Oleynik"10a1fe62005-09-05 11:25:27 +0000116 if (argc>2) {
117 run_applet_by_name(bb_applet_name=argv[2], 2, argv);
118 } else {
Rob Landleyb766c392005-09-04 11:10:37 +0000119 const struct BB_applet *a;
120 int col, output_width;
Eric Andersencc8ed391999-10-05 16:24:54 +0000121
Rob Landleyb766c392005-09-04 11:10:37 +0000122 if (ENABLE_FEATURE_AUTOWIDTH) {
123 /* Obtain the terminal width. */
124 get_terminal_width_height(0, &output_width, NULL);
125 /* leading tab and room to wrap */
126 output_width -= 20;
127 } else output_width = 60;
Tim Rikerb1ffba02003-11-07 19:37:20 +0000128
Rob Landleyb766c392005-09-04 11:10:37 +0000129 printf("%s\n\n"
130 "Usage: busybox [function] [arguments]...\n"
131 " or: [function] [arguments]...\n\n"
132 "\tBusyBox is a multi-call binary that combines many common Unix\n"
133 "\tutilities into a single executable. Most people will create a\n"
134 "\tlink to busybox for each function they wish to use and BusyBox\n"
135 "\twill act like whatever it was invoked as!\n"
136 "\nCurrently defined functions:\n", bb_msg_full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137
Rob Landleyb766c392005-09-04 11:10:37 +0000138 col=0;
139 for(a = applets; a->name;) {
140 col += printf("%s%s", (col ? ", " : "\t"), (a++)->name);
141 if (col > output_width && a->name) {
142 printf(",\n");
143 col = 0;
144 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 }
Rob Landleyb766c392005-09-04 11:10:37 +0000146 printf("\n\n");
147 exit(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 }
Rob Landley9fb272a2006-05-07 01:44:23 +0000149 } else run_applet_by_name(argv[1], argc-1, argv+1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000150
Rob Landleyb766c392005-09-04 11:10:37 +0000151 bb_error_msg_and_die("applet not found");
Eric Andersencc8ed391999-10-05 16:24:54 +0000152}