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