shell.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Provides a functions for working with shell.
  3. Copyright (C) 2006-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2015.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file shell.c
  20. * \brief Source: provides a functions for working with shell.
  21. */
  22. #include <config.h>
  23. #include <pwd.h> /* for username in xterm title */
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "global.h"
  28. #include "util.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. /*** forward declarations (file scope functions) *************************************************/
  33. /*** file scope variables ************************************************************************/
  34. static char rp_shell[PATH_MAX];
  35. /* --------------------------------------------------------------------------------------------- */
  36. /*** file scope functions ************************************************************************/
  37. /* --------------------------------------------------------------------------------------------- */
  38. /**
  39. * Get a system shell.
  40. *
  41. * @return newly allocated mc_shell_t object with shell name
  42. */
  43. static mc_shell_t *
  44. mc_shell_get_installed_in_system (void)
  45. {
  46. mc_shell_t *mc_shell;
  47. mc_shell = g_new0 (mc_shell_t, 1);
  48. /* 3rd choice: look for existing shells supported as MC subshells. */
  49. if (access ("/bin/bash", X_OK) == 0)
  50. mc_shell->path = g_strdup ("/bin/bash");
  51. else if (access ("/bin/ash", X_OK) == 0)
  52. mc_shell->path = g_strdup ("/bin/ash");
  53. else if (access ("/bin/dash", X_OK) == 0)
  54. mc_shell->path = g_strdup ("/bin/dash");
  55. else if (access ("/bin/busybox", X_OK) == 0)
  56. mc_shell->path = g_strdup ("/bin/busybox");
  57. else if (access ("/bin/zsh", X_OK) == 0)
  58. mc_shell->path = g_strdup ("/bin/zsh");
  59. else if (access ("/bin/tcsh", X_OK) == 0)
  60. mc_shell->path = g_strdup ("/bin/tcsh");
  61. else if (access ("/bin/csh", X_OK) == 0)
  62. mc_shell->path = g_strdup ("/bin/csh");
  63. /* No fish as fallback because it is so much different from other shells and
  64. * in a way exotic (even though user-friendly by name) that we should not
  65. * present it as a subshell without the user's explicit intention. We rather
  66. * will not use a subshell but just a command line.
  67. * else if (access("/bin/fish", X_OK) == 0)
  68. * mc_global.tty.shell = g_strdup ("/bin/fish");
  69. */
  70. else
  71. /* Fallback and last resort: system default shell */
  72. mc_shell->path = g_strdup ("/bin/sh");
  73. return mc_shell;
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. static char *
  77. mc_shell_get_name_env (void)
  78. {
  79. const char *shell_env;
  80. char *shell_name = NULL;
  81. shell_env = g_getenv ("SHELL");
  82. if ((shell_env == NULL) || (shell_env[0] == '\0'))
  83. {
  84. /* 2nd choice: user login shell */
  85. struct passwd *pwd;
  86. pwd = getpwuid (geteuid ());
  87. if (pwd != NULL)
  88. shell_name = g_strdup (pwd->pw_shell);
  89. }
  90. else
  91. /* 1st choice: SHELL environment variable */
  92. shell_name = g_strdup (shell_env);
  93. return shell_name;
  94. }
  95. /* --------------------------------------------------------------------------------------------- */
  96. static mc_shell_t *
  97. mc_shell_get_from_env (void)
  98. {
  99. mc_shell_t *mc_shell = NULL;
  100. char *shell_name;
  101. shell_name = mc_shell_get_name_env ();
  102. if (shell_name != NULL)
  103. {
  104. mc_shell = g_new0 (mc_shell_t, 1);
  105. mc_shell->path = shell_name;
  106. }
  107. return mc_shell;
  108. }
  109. /* --------------------------------------------------------------------------------------------- */
  110. static void
  111. mc_shell_recognize_real_path (mc_shell_t *mc_shell)
  112. {
  113. if (strstr (mc_shell->path, "/zsh") != NULL || strstr (mc_shell->real_path, "/zsh") != NULL
  114. || getenv ("ZSH_VERSION") != NULL)
  115. {
  116. /* Also detects ksh symlinked to zsh */
  117. mc_shell->type = SHELL_ZSH;
  118. mc_shell->name = "zsh";
  119. }
  120. else if (strstr (mc_shell->path, "/tcsh") != NULL
  121. || strstr (mc_shell->real_path, "/tcsh") != NULL)
  122. {
  123. /* Also detects csh symlinked to tcsh */
  124. mc_shell->type = SHELL_TCSH;
  125. mc_shell->name = "tcsh";
  126. }
  127. else if (strstr (mc_shell->path, "/csh") != NULL
  128. || strstr (mc_shell->real_path, "/csh") != NULL)
  129. {
  130. mc_shell->type = SHELL_TCSH;
  131. mc_shell->name = "csh";
  132. }
  133. else if (strstr (mc_shell->path, "/fish") != NULL
  134. || strstr (mc_shell->real_path, "/fish") != NULL)
  135. {
  136. mc_shell->type = SHELL_FISH;
  137. mc_shell->name = "fish";
  138. }
  139. else if (strstr (mc_shell->path, "/dash") != NULL
  140. || strstr (mc_shell->real_path, "/dash") != NULL)
  141. {
  142. /* Debian ash (also found if symlinked to by ash/sh) */
  143. mc_shell->type = SHELL_DASH;
  144. mc_shell->name = "dash";
  145. }
  146. else if (strstr (mc_shell->real_path, "/busybox") != NULL)
  147. {
  148. /* If shell is symlinked to busybox, assume it is an ash, even though theoretically
  149. * it could also be a hush (a mini shell for non-MMU systems deactivated by default).
  150. * For simplicity's sake we assume that busybox always contains an ash, not a hush.
  151. * On embedded platforms or on server systems, /bin/sh often points to busybox.
  152. * Sometimes even bash is symlinked to busybox (CONFIG_FEATURE_BASH_IS_ASH option),
  153. * so we need to check busybox symlinks *before* checking for the name "bash"
  154. * in order to avoid that case. */
  155. mc_shell->type = SHELL_ASH_BUSYBOX;
  156. mc_shell->name = mc_shell->path;
  157. }
  158. else
  159. mc_shell->type = SHELL_NONE;
  160. }
  161. /* --------------------------------------------------------------------------------------------- */
  162. static void
  163. mc_shell_recognize_path (mc_shell_t *mc_shell)
  164. {
  165. /* If shell is not symlinked to busybox, it is safe to assume it is a real shell */
  166. if (strstr (mc_shell->path, "/bash") != NULL || getenv ("BASH") != NULL)
  167. {
  168. mc_shell->type = SHELL_BASH;
  169. mc_shell->name = "bash";
  170. }
  171. else if (strstr (mc_shell->path, "/sh") != NULL || getenv ("SH") != NULL)
  172. {
  173. mc_shell->type = SHELL_SH;
  174. mc_shell->name = "sh";
  175. }
  176. else if (strstr (mc_shell->path, "/ash") != NULL || getenv ("ASH") != NULL)
  177. {
  178. mc_shell->type = SHELL_ASH_BUSYBOX;
  179. mc_shell->name = "ash";
  180. }
  181. else
  182. mc_shell->type = SHELL_NONE;
  183. }
  184. /* --------------------------------------------------------------------------------------------- */
  185. /*** public functions ****************************************************************************/
  186. /* --------------------------------------------------------------------------------------------- */
  187. void
  188. mc_shell_init (void)
  189. {
  190. mc_shell_t *mc_shell;
  191. mc_shell = mc_shell_get_from_env ();
  192. if (mc_shell == NULL)
  193. mc_shell = mc_shell_get_installed_in_system ();
  194. mc_shell->real_path = mc_realpath (mc_shell->path, rp_shell);
  195. /* Find out what type of shell we have. Also consider real paths (resolved symlinks)
  196. * because e.g. csh might point to tcsh, ash to dash or busybox, sh to anything. */
  197. if (mc_shell->real_path != NULL)
  198. mc_shell_recognize_real_path (mc_shell);
  199. if (mc_shell->type == SHELL_NONE)
  200. mc_shell_recognize_path (mc_shell);
  201. if (mc_shell->type == SHELL_NONE)
  202. mc_global.tty.use_subshell = FALSE;
  203. mc_global.shell = mc_shell;
  204. }
  205. /* --------------------------------------------------------------------------------------------- */
  206. void
  207. mc_shell_deinit (void)
  208. {
  209. if (mc_global.shell != NULL)
  210. {
  211. g_free (mc_global.shell->path);
  212. MC_PTR_FREE (mc_global.shell);
  213. }
  214. }
  215. /* --------------------------------------------------------------------------------------------- */