Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 2 | /* |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 3 | * BusyBox' main applet dispatcher. |
| 4 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 6 | */ |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 7 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <string.h> |
Eric Andersen | 90ca284 | 2001-01-27 08:32:57 +0000 | [diff] [blame] | 10 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 11 | #include <errno.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 13 | #if ENABLE_LOCALE_SUPPORT |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 14 | #include <locale.h> |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 15 | #else |
| 16 | #define setlocale(x,y) |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 17 | #endif |
| 18 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 19 | const char *bb_applet_name ATTRIBUTE_EXTERNALLY_VISIBLE; |
Erik Andersen | 05df239 | 2000-01-13 04:43:48 +0000 | [diff] [blame] | 20 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 21 | #ifdef CONFIG_FEATURE_INSTALLER |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 22 | /* |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 23 | * directory table |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 24 | * this should be consistent w/ the enum, busybox.h::Location, |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 25 | * or else... |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 26 | */ |
Eric Andersen | fcffa2c | 2002-04-06 05:17:57 +0000 | [diff] [blame] | 27 | static const char usr_bin [] ="/usr/bin"; |
| 28 | static const char usr_sbin[] ="/usr/sbin"; |
| 29 | |
| 30 | static 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 Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /* abstract link() */ |
| 39 | typedef int (*__link_f)(const char *, const char *); |
| 40 | |
| 41 | /* create (sym)links for each applet */ |
Eric Andersen | c5949f6 | 2000-09-25 20:35:54 +0000 | [diff] [blame] | 42 | static void install_links(const char *busybox, int use_symbolic_links) |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 43 | { |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 44 | __link_f Link = link; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 45 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 46 | char *fpc; |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 47 | int i; |
Eric Andersen | c5949f6 | 2000-09-25 20:35:54 +0000 | [diff] [blame] | 48 | int rc; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 49 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 50 | if (use_symbolic_links) |
Eric Andersen | 90ca284 | 2001-01-27 08:32:57 +0000 | [diff] [blame] | 51 | Link = symlink; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 52 | |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 53 | for (i = 0; applets[i].name != NULL; i++) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 54 | 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | bb_perror_msg("%s", fpc); |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 59 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 60 | free(fpc); |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 61 | } |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 64 | #else |
| 65 | #define install_links(x,y) |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 66 | #endif /* CONFIG_FEATURE_INSTALLER */ |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 67 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 68 | int main(int argc, char **argv) |
| 69 | { |
Matt Kraai | f2cc276 | 2001-02-01 19:21:20 +0000 | [diff] [blame] | 70 | const char *s; |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 71 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 72 | 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 Kraai | 449377a | 2001-08-27 15:02:32 +0000 | [diff] [blame] | 76 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 77 | /* Set locale for everybody except `init' */ |
"Vladimir N. Oleynik" | 7407868 | 2005-09-29 08:19:04 +0000 | [diff] [blame] | 78 | if(ENABLE_LOCALE_SUPPORT && getpid() != 1) |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 79 | setlocale(LC_ALL, ""); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 80 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | run_applet_by_name(bb_applet_name, argc, argv); |
| 82 | bb_error_msg_and_die("applet not found"); |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 85 | int busybox_main(int argc, char **argv) |
| 86 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 87 | /* |
| 88 | * This style of argument parsing doesn't scale well |
John Beppu | 27b5924 | 2000-06-27 04:56:45 +0000 | [diff] [blame] | 89 | * in the event that busybox starts wanting more --options. |
| 90 | * If someone has a cleaner approach, by all means implement it. |
| 91 | */ |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 92 | if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) { |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 93 | int use_symbolic_links = 0; |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 94 | int rc = 0; |
| 95 | char *busybox; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 96 | |
John Beppu | 27b5924 | 2000-06-27 04:56:45 +0000 | [diff] [blame] | 97 | /* to use symlinks, or not to use symlinks... */ |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 98 | if (argc > 2) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 99 | if ((strcmp(argv[2], "-s") == 0)) { |
| 100 | use_symbolic_links = 1; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 103 | |
| 104 | /* link */ |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 105 | busybox = xreadlink("/proc/self/exe"); |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 106 | if (busybox) { |
| 107 | install_links(busybox, use_symbolic_links); |
| 108 | free(busybox); |
| 109 | } else { |
| 110 | rc = 1; |
| 111 | } |
| 112 | return rc; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 113 | } |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 114 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 115 | /* Deal with --help. (Also print help when called with no arguments) */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 116 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 117 | if (argc==1 || !strcmp(argv[1],"--help") ) { |
"Vladimir N. Oleynik" | 10a1fe6 | 2005-09-05 11:25:27 +0000 | [diff] [blame] | 118 | if (argc>2) { |
| 119 | run_applet_by_name(bb_applet_name=argv[2], 2, argv); |
| 120 | } else { |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 121 | const struct BB_applet *a; |
| 122 | int col, output_width; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 123 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 124 | 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 Riker | b1ffba0 | 2003-11-07 19:37:20 +0000 | [diff] [blame] | 130 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 131 | 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 139 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 140 | 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | } |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 148 | printf("\n\n"); |
| 149 | exit(0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 150 | } |
Rob Landley | 9fb272a | 2006-05-07 01:44:23 +0000 | [diff] [blame] | 151 | } else run_applet_by_name(argv[1], argc-1, argv+1); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 152 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 153 | bb_error_msg_and_die("applet not found"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 154 | } |