- if (!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
- /* Do a binary search to find the applet entry given the name. */
- applet_using = find_applet_by_name(name);
- if (applet_using) {
- applet_name = applet_using->name;
- if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage();
- if(ENABLE_FEATURE_SUID) check_suid(applet_using);
- exit((*(applet_using->main))(argc, argv));
+ /* Deal with --help. Also print help when called with no arguments */
+
+ if (argc == 1 || !strcmp(argv[1], "--help") ) {
+ if (argc > 2) {
+ /* set name for proper "<name>: applet not found" */
+ applet_name = argv[2];
+ run_applet_and_exit(applet_name, 2, argv);
+ } else {
+ const struct bb_applet *a;
+ int col, output_width;
+
+ output_width = 80 - sizeof("start-stop-daemon, ") - 8;
+ if (ENABLE_FEATURE_AUTOWIDTH) {
+ /* Obtain the terminal width. */
+ get_terminal_width_height(0, &output_width, NULL);
+ /* leading tab and room to wrap */
+ output_width -= sizeof("start-stop-daemon, ") + 8;
+ }
+
+ printf("%s\n"
+ "Copyright (C) 1998-2006 Erik Andersen, Rob Landley, and others.\n"
+ "Licensed under GPLv2. See source distribution for full notice.\n"
+ "\n"
+ "Usage: busybox [function] [arguments]...\n"
+ " or: [function] [arguments]...\n"
+ "\n"
+ "\tBusyBox is a multi-call binary that combines many common Unix\n"
+ "\tutilities into a single executable. Most people will create a\n"
+ "\tlink to busybox for each function they wish to use and BusyBox\n"
+ "\twill act like whatever it was invoked as!\n"
+ "\nCurrently defined functions:\n", bb_msg_full_version);
+ col = 0;
+ a = applets;
+ while (a->name) {
+ col += printf("%s%s", (col ? ", " : "\t"), a->name);
+ a++;
+ if (col > output_width && a->name) {
+ puts(",");
+ col = 0;
+ }
+ }
+ puts("\n");
+ return 0;
+ }
+ } else {
+ /* we want "<argv[1]>: applet not found", not "busybox: ..." */
+ applet_name = argv[1];
+ run_applet_and_exit(argv[1], argc - 1, argv + 1);
+ }
+
+ bb_error_msg_and_die("applet not found");
+}
+
+void run_current_applet_and_exit(int argc, char **argv)
+{
+ /* Reinit some shared global data */
+ optind = 1;
+ xfunc_error_retval = EXIT_FAILURE;
+
+ applet_name = current_applet->name;
+ if (argc == 2 && !strcmp(argv[1], "--help"))
+ bb_show_usage();
+ if (ENABLE_FEATURE_SUID)
+ check_suid(current_applet);
+ exit(current_applet->main(argc, argv));
+}
+
+void run_applet_and_exit(const char *name, int argc, char **argv)
+{
+ current_applet = find_applet_by_name(name);
+ if (current_applet)
+ run_current_applet_and_exit(argc, argv);
+ if (!strncmp(name, "busybox", 7))
+ exit(busybox_main(argc, argv));
+}
+
+
+int main(int argc, char **argv)
+{
+ const char *s;
+
+#if !BB_MMU
+ /* NOMMU re-exec trick sets high-order bit in first byte of name */
+ if (argv[0][0] & 0x80) {
+ re_execed = 1;
+ argv[0][0] &= 0x7f;