Ticket #3561: (tree_store_mark_checked): fix meleak.
[midnight-commander.git] / lib / shell.c
blob1227a515a601cc391c131421ff8f91b13f8379fb
1 /*
2 Provides a functions for working with shell.
4 Copyright (C) 2006-2015
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2015.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** \file shell.c
27 * \brief Source: provides a functions for working with shell.
30 #include <config.h>
32 #include <pwd.h> /* for username in xterm title */
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
37 #include "global.h"
38 #include "util.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 static char rp_shell[PATH_MAX];
51 /*** file scope functions ************************************************************************/
52 /* --------------------------------------------------------------------------------------------- */
53 /**
54 * Get a system shell.
56 * @return newly allocated string with shell name
59 static mc_shell_t *
60 mc_shell_get_installed_in_system (void)
62 mc_shell_t *mc_shell;
64 mc_shell = g_new0 (mc_shell_t, 1);
66 /* 3rd choice: look for existing shells supported as MC subshells. */
67 if (access ("/bin/bash", X_OK) == 0)
68 mc_shell->path = g_strdup ("/bin/bash");
69 else if (access ("/bin/ash", X_OK) == 0)
70 mc_shell->path = g_strdup ("/bin/ash");
71 else if (access ("/bin/dash", X_OK) == 0)
72 mc_shell->path = g_strdup ("/bin/dash");
73 else if (access ("/bin/busybox", X_OK) == 0)
74 mc_shell->path = g_strdup ("/bin/busybox");
75 else if (access ("/bin/zsh", X_OK) == 0)
76 mc_shell->path = g_strdup ("/bin/zsh");
77 else if (access ("/bin/tcsh", X_OK) == 0)
78 mc_shell->path = g_strdup ("/bin/tcsh");
79 /* No fish as fallback because it is so much different from other shells and
80 * in a way exotic (even though user-friendly by name) that we should not
81 * present it as a subshell without the user's explicit intention. We rather
82 * will not use a subshell but just a command line.
83 * else if (access("/bin/fish", X_OK) == 0)
84 * mc_global.tty.shell = g_strdup ("/bin/fish");
86 else
87 /* Fallback and last resort: system default shell */
88 mc_shell->path = g_strdup ("/bin/sh");
90 return mc_shell;
93 /* --------------------------------------------------------------------------------------------- */
95 static char *
96 mc_shell_get_name_env (void)
98 const char *shell_env;
99 char *shell_name = NULL;
101 shell_env = g_getenv ("SHELL");
102 if ((shell_env == NULL) || (shell_env[0] == '\0'))
104 /* 2nd choice: user login shell */
105 struct passwd *pwd;
107 pwd = getpwuid (geteuid ());
108 if (pwd != NULL)
109 shell_name = g_strdup (pwd->pw_shell);
111 else
112 /* 1st choice: SHELL environment variable */
113 shell_name = g_strdup (shell_env);
115 return shell_name;
118 /* --------------------------------------------------------------------------------------------- */
120 static mc_shell_t *
121 mc_shell_get_from_env (void)
123 mc_shell_t *mc_shell = NULL;
125 char *shell_name;
127 shell_name = mc_shell_get_name_env ();
129 if (shell_name != NULL)
131 mc_shell = g_new0 (mc_shell_t, 1);
132 mc_shell->path = shell_name;
135 return mc_shell;
138 /* --------------------------------------------------------------------------------------------- */
140 * Get a shell type and store in mc_shell->type variable
143 static gboolean
144 mc_shell_recognize_and_fill_type (mc_shell_t * mc_shell)
146 gboolean result = TRUE;
148 /* Find out what type of shell we have. Also consider real paths (resolved symlinks)
149 * because e.g. csh might point to tcsh, ash to dash or busybox, sh to anything. */
151 if (strstr (mc_shell->path, "/zsh") != NULL || strstr (mc_shell->real_path, "/zsh") != NULL
152 || getenv ("ZSH_VERSION") != NULL)
154 /* Also detects ksh symlinked to zsh */
155 mc_shell->type = SHELL_ZSH;
156 mc_shell->name = "zsh";
158 else if (strstr (mc_shell->path, "/tcsh") != NULL
159 || strstr (mc_shell->real_path, "/tcsh") != NULL)
161 /* Also detects csh symlinked to tcsh */
162 mc_shell->type = SHELL_TCSH;
163 mc_shell->name = "tcsh";
165 else if (strstr (mc_shell->path, "/fish") != NULL
166 || strstr (mc_shell->real_path, "/fish") != NULL)
168 mc_shell->type = SHELL_FISH;
169 mc_shell->name = "fish";
171 else if (strstr (mc_shell->path, "/dash") != NULL
172 || strstr (mc_shell->real_path, "/dash") != NULL)
174 /* Debian ash (also found if symlinked to by ash/sh) */
175 mc_shell->type = SHELL_DASH;
176 mc_shell->name = "dash";
178 else if (strstr (mc_shell->real_path, "/busybox") != NULL)
180 /* If shell is symlinked to busybox, assume it is an ash, even though theoretically
181 * it could also be a hush (a mini shell for non-MMU systems deactivated by default).
182 * For simplicity's sake we assume that busybox always contains an ash, not a hush.
183 * On embedded platforms or on server systems, /bin/sh often points to busybox.
184 * Sometimes even bash is symlinked to busybox (CONFIG_FEATURE_BASH_IS_ASH option),
185 * so we need to check busybox symlinks *before* checking for the name "bash"
186 * in order to avoid that case. */
187 mc_shell->type = SHELL_ASH_BUSYBOX;
188 mc_shell->name = mc_shell->path;
190 else if (strstr (mc_shell->path, "/bash") != NULL || getenv ("BASH") != NULL)
192 /* If bash is not symlinked to busybox, it is safe to assume it is a real bash */
193 mc_shell->type = SHELL_BASH;
194 mc_shell->name = "bash";
196 else if (strstr (mc_shell->path, "/sh") != NULL || getenv ("SH") != NULL)
198 /* If bash is not symlinked to busybox, it is safe to assume it is a real bash */
199 mc_shell->type = SHELL_SH;
200 mc_shell->name = "sh";
202 else if (strstr (mc_shell->path, "/ash") != NULL || getenv ("ASH") != NULL)
204 /* If bash is not symlinked to busybox, it is safe to assume it is a real bash */
205 mc_shell->type = SHELL_ASH_BUSYBOX;
206 mc_shell->name = "ash";
208 else
210 mc_shell->type = SHELL_NONE;
211 mc_global.tty.use_subshell = FALSE;
212 result = FALSE;
214 return result;
217 /* --------------------------------------------------------------------------------------------- */
218 /*** public functions ****************************************************************************/
219 /* --------------------------------------------------------------------------------------------- */
221 void
222 mc_shell_init (void)
224 mc_shell_t *mc_shell;
226 mc_shell = mc_shell_get_from_env ();
228 if (mc_shell == NULL)
229 mc_shell = mc_shell_get_installed_in_system ();
231 mc_shell->real_path = mc_realpath (mc_shell->path, rp_shell);
233 if (!mc_shell_recognize_and_fill_type (mc_shell))
234 mc_global.tty.use_subshell = FALSE;
236 mc_global.shell = mc_shell;
239 /* --------------------------------------------------------------------------------------------- */
241 void
242 mc_shell_deinit (void)
244 if (mc_global.shell != NULL)
246 g_free (mc_global.shell->path);
247 MC_PTR_FREE (mc_global.shell);
251 /* --------------------------------------------------------------------------------------------- */