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 | * |
Rob Landley | eb84a42 | 2006-09-20 21:41:13 +0000 | [diff] [blame] | 5 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 6 | */ |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 7 | #include "busybox.h" |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 8 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 9 | const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE; |
Erik Andersen | 05df239 | 2000-01-13 04:43:48 +0000 | [diff] [blame] | 10 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 11 | #ifdef CONFIG_FEATURE_INSTALLER |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 12 | /* |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 13 | * directory table |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 14 | * this should be consistent w/ the enum, busybox.h::Location, |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 15 | * or else... |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 16 | */ |
Eric Andersen | fcffa2c | 2002-04-06 05:17:57 +0000 | [diff] [blame] | 17 | static const char usr_bin [] ="/usr/bin"; |
| 18 | static const char usr_sbin[] ="/usr/sbin"; |
| 19 | |
| 20 | static const char* const install_dir[] = { |
| 21 | &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */ |
| 22 | &usr_bin [4], /* "/bin" */ |
| 23 | &usr_sbin[4], /* "/sbin" */ |
| 24 | usr_bin, |
| 25 | usr_sbin |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | /* abstract link() */ |
| 29 | typedef int (*__link_f)(const char *, const char *); |
| 30 | |
| 31 | /* create (sym)links for each applet */ |
Eric Andersen | c5949f6 | 2000-09-25 20:35:54 +0000 | [diff] [blame] | 32 | static void install_links(const char *busybox, int use_symbolic_links) |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 33 | { |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 34 | __link_f Link = link; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 35 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 36 | char *fpc; |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 37 | int i; |
Eric Andersen | c5949f6 | 2000-09-25 20:35:54 +0000 | [diff] [blame] | 38 | int rc; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 39 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 40 | if (use_symbolic_links) |
Eric Andersen | 90ca284 | 2001-01-27 08:32:57 +0000 | [diff] [blame] | 41 | Link = symlink; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 42 | |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 43 | for (i = 0; applets[i].name != NULL; i++) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 44 | fpc = concat_path_file( |
| 45 | install_dir[applets[i].location], applets[i].name); |
| 46 | rc = Link(busybox, fpc); |
| 47 | if (rc!=0 && errno!=EEXIST) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | bb_perror_msg("%s", fpc); |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 49 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 50 | free(fpc); |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 51 | } |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 54 | #else |
| 55 | #define install_links(x,y) |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 56 | #endif /* CONFIG_FEATURE_INSTALLER */ |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 57 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 58 | int main(int argc, char **argv) |
| 59 | { |
Matt Kraai | f2cc276 | 2001-02-01 19:21:20 +0000 | [diff] [blame] | 60 | const char *s; |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 62 | applet_name=argv[0]; |
| 63 | if (*applet_name == '-') applet_name++; |
| 64 | for (s = applet_name; *s ;) |
| 65 | if (*(s++) == '/') applet_name = s; |
Matt Kraai | 449377a | 2001-08-27 15:02:32 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | fcdb00f | 2006-11-21 00:09:37 +0000 | [diff] [blame^] | 67 | /* Set locale for everybody except 'init' */ |
| 68 | if (ENABLE_LOCALE_SUPPORT && getpid() != 1) |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 69 | setlocale(LC_ALL, ""); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 71 | run_applet_by_name(applet_name, argc, argv); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | bb_error_msg_and_die("applet not found"); |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 75 | int busybox_main(int argc, char **argv) |
| 76 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 77 | /* |
| 78 | * This style of argument parsing doesn't scale well |
John Beppu | 27b5924 | 2000-06-27 04:56:45 +0000 | [diff] [blame] | 79 | * in the event that busybox starts wanting more --options. |
| 80 | * If someone has a cleaner approach, by all means implement it. |
| 81 | */ |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 82 | if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) { |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 83 | int use_symbolic_links = 0; |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 84 | int rc = 0; |
| 85 | char *busybox; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 86 | |
John Beppu | 27b5924 | 2000-06-27 04:56:45 +0000 | [diff] [blame] | 87 | /* to use symlinks, or not to use symlinks... */ |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 88 | if (argc > 2) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 89 | if ((strcmp(argv[2], "-s") == 0)) { |
| 90 | use_symbolic_links = 1; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 93 | |
| 94 | /* link */ |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 95 | busybox = xreadlink("/proc/self/exe"); |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 96 | if (busybox) { |
| 97 | install_links(busybox, use_symbolic_links); |
| 98 | free(busybox); |
| 99 | } else { |
| 100 | rc = 1; |
| 101 | } |
| 102 | return rc; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 103 | } |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 104 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 105 | /* Deal with --help. (Also print help when called with no arguments) */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 106 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 107 | if (argc==1 || !strcmp(argv[1],"--help") ) { |
"Vladimir N. Oleynik" | 10a1fe6 | 2005-09-05 11:25:27 +0000 | [diff] [blame] | 108 | if (argc>2) { |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 109 | applet_name = argv[2]; |
| 110 | run_applet_by_name(applet_name, 2, argv); |
"Vladimir N. Oleynik" | 10a1fe6 | 2005-09-05 11:25:27 +0000 | [diff] [blame] | 111 | } else { |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 112 | const struct BB_applet *a; |
| 113 | int col, output_width; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 114 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 115 | if (ENABLE_FEATURE_AUTOWIDTH) { |
| 116 | /* Obtain the terminal width. */ |
| 117 | get_terminal_width_height(0, &output_width, NULL); |
| 118 | /* leading tab and room to wrap */ |
Denis Vlasenko | 01c27fc | 2006-10-05 21:10:53 +0000 | [diff] [blame] | 119 | output_width -= sizeof("start-stop-daemon, ") + 8; |
| 120 | } else output_width = 80 - sizeof("start-stop-daemon, ") - 8; |
Tim Riker | b1ffba0 | 2003-11-07 19:37:20 +0000 | [diff] [blame] | 121 | |
Rob Landley | eb84a42 | 2006-09-20 21:41:13 +0000 | [diff] [blame] | 122 | printf("%s\n" |
Denis Vlasenko | 01c27fc | 2006-10-05 21:10:53 +0000 | [diff] [blame] | 123 | "Copyright (C) 1998-2006 Erik Andersen, Rob Landley, and others.\n" |
| 124 | "Licensed under GPLv2. See source distribution for full notice.\n\n" |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 125 | "Usage: busybox [function] [arguments]...\n" |
| 126 | " or: [function] [arguments]...\n\n" |
| 127 | "\tBusyBox is a multi-call binary that combines many common Unix\n" |
| 128 | "\tutilities into a single executable. Most people will create a\n" |
| 129 | "\tlink to busybox for each function they wish to use and BusyBox\n" |
| 130 | "\twill act like whatever it was invoked as!\n" |
| 131 | "\nCurrently defined functions:\n", bb_msg_full_version); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 132 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 133 | col=0; |
| 134 | for(a = applets; a->name;) { |
| 135 | col += printf("%s%s", (col ? ", " : "\t"), (a++)->name); |
| 136 | if (col > output_width && a->name) { |
| 137 | printf(",\n"); |
| 138 | col = 0; |
| 139 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 140 | } |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 141 | printf("\n\n"); |
| 142 | exit(0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 143 | } |
Rob Landley | 9fb272a | 2006-05-07 01:44:23 +0000 | [diff] [blame] | 144 | } else run_applet_by_name(argv[1], argc-1, argv+1); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 145 | |
Rob Landley | b766c39 | 2005-09-04 11:10:37 +0000 | [diff] [blame] | 146 | bb_error_msg_and_die("applet not found"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 147 | } |