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