3
0

busybox.c 3.7 KB

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