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