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