busybox.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * BusyBox' main applet dispatcher.
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  6. */
  7. #include "busybox.h"
  8. const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
  9. #ifdef CONFIG_FEATURE_INSTALLER
  10. /*
  11. * directory table
  12. * this should be consistent w/ the enum, busybox.h::Location,
  13. * or else...
  14. */
  15. static const char usr_bin [] = "/usr/bin";
  16. static const char usr_sbin[] = "/usr/sbin";
  17. static const char* const install_dir[] = {
  18. &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
  19. &usr_bin [4], /* "/bin" */
  20. &usr_sbin[4], /* "/sbin" */
  21. usr_bin,
  22. usr_sbin
  23. };
  24. /* abstract link() */
  25. typedef int (*link_func)(const char *, const char *);
  26. /* create (sym)links for each applet */
  27. static void install_links(const char *busybox, int use_symbolic_links)
  28. {
  29. link_func lf = link;
  30. char *fpc;
  31. int i;
  32. int rc;
  33. if (use_symbolic_links)
  34. lf = symlink;
  35. for (i = 0; applets[i].name != NULL; i++) {
  36. fpc = concat_path_file(
  37. install_dir[applets[i].location],
  38. applets[i].name);
  39. rc = lf(busybox, fpc);
  40. if (rc != 0 && errno != EEXIST) {
  41. bb_perror_msg("%s", fpc);
  42. }
  43. free(fpc);
  44. }
  45. }
  46. #else
  47. #define install_links(x,y)
  48. #endif /* CONFIG_FEATURE_INSTALLER */
  49. int main(int argc, char **argv)
  50. {
  51. const char *s;
  52. applet_name = argv[0];
  53. if (*applet_name == '-')
  54. applet_name++;
  55. while ((s = strchr(applet_name, '/')))
  56. applet_name = s + 1;
  57. /* Set locale for everybody except 'init' */
  58. if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
  59. setlocale(LC_ALL, "");
  60. run_applet_by_name(applet_name, argc, argv);
  61. bb_error_msg_and_die("applet not found");
  62. }
  63. int busybox_main(int argc, char **argv)
  64. {
  65. /*
  66. * This style of argument parsing doesn't scale well
  67. * in the event that busybox starts wanting more --options.
  68. * If someone has a cleaner approach, by all means implement it.
  69. */
  70. if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
  71. int use_symbolic_links = 0;
  72. char *busybox;
  73. /* to use symlinks, or not to use symlinks... */
  74. if (argc > 2)
  75. if (strcmp(argv[2], "-s") == 0)
  76. use_symbolic_links = 1;
  77. /* link */
  78. // XXX: FIXME: this is broken. Why not just use argv[0] ?
  79. busybox = xreadlink("/proc/self/exe");
  80. if (!busybox)
  81. return 1;
  82. install_links(busybox, use_symbolic_links);
  83. if (ENABLE_FEATURE_CLEAN_UP)
  84. free(busybox);
  85. return 0;
  86. }
  87. /* Deal with --help. (Also print help when called with no arguments) */
  88. if (argc == 1 || !strcmp(argv[1], "--help") ) {
  89. if (argc > 2) {
  90. applet_name = argv[2];
  91. run_applet_by_name(applet_name, 2, argv);
  92. } else {
  93. const struct BB_applet *a;
  94. int col, output_width;
  95. output_width = 80 - sizeof("start-stop-daemon, ") - 8;
  96. if (ENABLE_FEATURE_AUTOWIDTH) {
  97. /* Obtain the terminal width. */
  98. get_terminal_width_height(0, &output_width, NULL);
  99. /* leading tab and room to wrap */
  100. output_width -= sizeof("start-stop-daemon, ") + 8;
  101. }
  102. printf("%s\n"
  103. "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n"
  104. "Licensed under GPLv2.  See source distribution for full notice.\n\n"
  105. "Usage: busybox [function] [arguments]...\n"
  106. " or: [function] [arguments]...\n\n"
  107. "\tBusyBox is a multi-call binary that combines many common Unix\n"
  108. "\tutilities into a single executable. Most people will create a\n"
  109. "\tlink to busybox for each function they wish to use and BusyBox\n"
  110. "\twill act like whatever it was invoked as!\n"
  111. "\nCurrently defined functions:\n", bb_msg_full_version);
  112. col = 0;
  113. for (a = applets; a->name;) {
  114. col += printf("%s%s", (col ? ", " : "\t"), a->name);
  115. a++;
  116. if (col > output_width && a->name) {
  117. puts(",");
  118. col = 0;
  119. }
  120. }
  121. puts("\n");
  122. return 0;
  123. }
  124. } else run_applet_by_name(argv[1], argc - 1, argv + 1);
  125. bb_error_msg_and_die("applet not found");
  126. }