busybox.c 3.8 KB

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