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