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