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" |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 8 | #ifdef CONFIG_LOCALE_SUPPORT |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 9 | #include <locale.h> |
| 10 | #endif |
| 11 | |
Eric Andersen | 0f0c0b4 | 2001-04-03 17:05:01 +0000 | [diff] [blame] | 12 | int been_there_done_that = 0; /* Also used in applets.c */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 13 | const char *bb_applet_name; |
Erik Andersen | 05df239 | 2000-01-13 04:43:48 +0000 | [diff] [blame] | 14 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 15 | #ifdef CONFIG_FEATURE_INSTALLER |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 16 | /* |
| 17 | * directory table |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 18 | * this should be consistent w/ the enum, busybox.h::Location, |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 19 | * or else... |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 20 | */ |
Eric Andersen | fcffa2c | 2002-04-06 05:17:57 +0000 | [diff] [blame] | 21 | static const char usr_bin [] ="/usr/bin"; |
| 22 | static const char usr_sbin[] ="/usr/sbin"; |
| 23 | |
| 24 | static const char* const install_dir[] = { |
| 25 | &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */ |
| 26 | &usr_bin [4], /* "/bin" */ |
| 27 | &usr_sbin[4], /* "/sbin" */ |
| 28 | usr_bin, |
| 29 | usr_sbin |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | /* abstract link() */ |
| 33 | typedef int (*__link_f)(const char *, const char *); |
| 34 | |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 35 | /* |
| 36 | * Where in the filesystem is this busybox? |
| 37 | * [return] |
| 38 | * malloc'd string w/ full pathname of busybox's location |
| 39 | * NULL on failure |
| 40 | */ |
Eric Andersen | fcffa2c | 2002-04-06 05:17:57 +0000 | [diff] [blame] | 41 | static inline char *busybox_fullpath(void) |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 42 | { |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 43 | return xreadlink("/proc/self/exe"); |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 44 | } |
| 45 | |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 46 | /* create (sym)links for each applet */ |
Eric Andersen | c5949f6 | 2000-09-25 20:35:54 +0000 | [diff] [blame] | 47 | static void install_links(const char *busybox, int use_symbolic_links) |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 48 | { |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 49 | __link_f Link = link; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 50 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 51 | char *fpc; |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 52 | int i; |
Eric Andersen | c5949f6 | 2000-09-25 20:35:54 +0000 | [diff] [blame] | 53 | int rc; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 54 | |
Eric Andersen | 90ca284 | 2001-01-27 08:32:57 +0000 | [diff] [blame] | 55 | if (use_symbolic_links) |
| 56 | Link = symlink; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 57 | |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 58 | for (i = 0; applets[i].name != NULL; i++) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 59 | fpc = concat_path_file( |
| 60 | install_dir[applets[i].location], applets[i].name); |
| 61 | rc = Link(busybox, fpc); |
| 62 | if (rc!=0 && errno!=EEXIST) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | bb_perror_msg("%s", fpc); |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 64 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 65 | free(fpc); |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 66 | } |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 69 | #endif /* CONFIG_FEATURE_INSTALLER */ |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 70 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 71 | int main(int argc, char **argv) |
| 72 | { |
Matt Kraai | f2cc276 | 2001-02-01 19:21:20 +0000 | [diff] [blame] | 73 | const char *s; |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 74 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_applet_name = argv[0]; |
Matt Kraai | 449377a | 2001-08-27 15:02:32 +0000 | [diff] [blame] | 76 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | if (bb_applet_name[0] == '-') |
| 78 | bb_applet_name++; |
Matt Kraai | 449377a | 2001-08-27 15:02:32 +0000 | [diff] [blame] | 79 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | for (s = bb_applet_name; *s != '\0';) { |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 81 | if (*s++ == '/') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | bb_applet_name = s; |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 85 | #ifdef CONFIG_LOCALE_SUPPORT |
| 86 | #ifdef CONFIG_INIT |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 87 | if(getpid()!=1) /* Do not set locale for `init' */ |
Eric Andersen | 4819c3d | 2001-05-13 00:33:16 +0000 | [diff] [blame] | 88 | #endif |
| 89 | { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 90 | setlocale(LC_ALL, ""); |
Eric Andersen | 4819c3d | 2001-05-13 00:33:16 +0000 | [diff] [blame] | 91 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | run_applet_by_name(bb_applet_name, argc, argv); |
| 95 | bb_error_msg_and_die("applet not found"); |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | int busybox_main(int argc, char **argv) |
| 100 | { |
| 101 | int col = 0, len, i; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 102 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 103 | #ifdef CONFIG_FEATURE_INSTALLER |
John Beppu | 27b5924 | 2000-06-27 04:56:45 +0000 | [diff] [blame] | 104 | /* |
| 105 | * This style of argument parsing doesn't scale well |
| 106 | * in the event that busybox starts wanting more --options. |
| 107 | * If someone has a cleaner approach, by all means implement it. |
| 108 | */ |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 109 | if (argc > 1 && (strcmp(argv[1], "--install") == 0)) { |
| 110 | int use_symbolic_links = 0; |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 111 | int rc = 0; |
| 112 | char *busybox; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 113 | |
John Beppu | 27b5924 | 2000-06-27 04:56:45 +0000 | [diff] [blame] | 114 | /* to use symlinks, or not to use symlinks... */ |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 115 | if (argc > 2) { |
| 116 | if ((strcmp(argv[2], "-s") == 0)) { |
| 117 | use_symbolic_links = 1; |
| 118 | } |
| 119 | } |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 120 | |
| 121 | /* link */ |
| 122 | busybox = busybox_fullpath(); |
| 123 | if (busybox) { |
| 124 | install_links(busybox, use_symbolic_links); |
| 125 | free(busybox); |
| 126 | } else { |
| 127 | rc = 1; |
| 128 | } |
| 129 | return rc; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 130 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 131 | #endif /* CONFIG_FEATURE_INSTALLER */ |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 132 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 133 | argc--; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 134 | |
Eric Andersen | 5e09b6e | 2000-12-08 19:03:12 +0000 | [diff] [blame] | 135 | /* If we've already been here once, exit now */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 136 | if (been_there_done_that == 1 || argc < 1) { |
Erik Andersen | bcd6177 | 2000-05-13 06:33:19 +0000 | [diff] [blame] | 137 | const struct BB_applet *a = applets; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 138 | |
Pavel Roskin | 9c5fcc3 | 2000-07-17 23:45:12 +0000 | [diff] [blame] | 139 | fprintf(stderr, "%s\n\n" |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 140 | "Usage: busybox [function] [arguments]...\n" |
| 141 | " or: [function] [arguments]...\n\n" |
John Beppu | b4f8606 | 2000-04-13 03:36:01 +0000 | [diff] [blame] | 142 | "\tBusyBox is a multi-call binary that combines many common Unix\n" |
| 143 | "\tutilities into a single executable. Most people will create a\n" |
| 144 | "\tlink to busybox for each function they wish to use, and BusyBox\n" |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 145 | "\twill act like whatever it was invoked as.\n" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 146 | "\nCurrently defined functions:\n", bb_msg_full_version); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | |
| 148 | while (a->name != 0) { |
| 149 | col += |
| 150 | fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "), |
| 151 | (a++)->name); |
| 152 | if (col > 60 && a->name != 0) { |
| 153 | fprintf(stderr, ",\n"); |
| 154 | col = 0; |
| 155 | } |
| 156 | } |
| 157 | fprintf(stderr, "\n\n"); |
Mark Whitley | 0167718 | 2001-03-02 17:47:17 +0000 | [diff] [blame] | 158 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 159 | } |
Eric Andersen | 5e09b6e | 2000-12-08 19:03:12 +0000 | [diff] [blame] | 160 | |
| 161 | /* Flag that we've been here already */ |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 162 | been_there_done_that = 1; |
Eric Andersen | 5e09b6e | 2000-12-08 19:03:12 +0000 | [diff] [blame] | 163 | |
Matt Kraai | 8abc78a | 2000-12-15 00:35:22 +0000 | [diff] [blame] | 164 | /* Move the command line down a notch */ |
| 165 | len = argv[argc] + strlen(argv[argc]) - argv[1]; |
| 166 | memmove(argv[0], argv[1], len); |
| 167 | memset(argv[0] + len, 0, argv[1] - argv[0]); |
| 168 | |
| 169 | /* Fix up the argv pointers */ |
| 170 | len = argv[1] - argv[0]; |
Eric Andersen | 90ca284 | 2001-01-27 08:32:57 +0000 | [diff] [blame] | 171 | memmove(argv, argv + 1, sizeof(char *) * (argc + 1)); |
Matt Kraai | 8abc78a | 2000-12-15 00:35:22 +0000 | [diff] [blame] | 172 | for (i = 0; i < argc; i++) |
| 173 | argv[i] -= len; |
Eric Andersen | 5e09b6e | 2000-12-08 19:03:12 +0000 | [diff] [blame] | 174 | |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 175 | return (main(argc, argv)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 176 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | Local Variables: |
| 180 | c-file-style: "linux" |
| 181 | c-basic-offset: 4 |
| 182 | tab-width: 4 |
| 183 | End: |
| 184 | */ |