Blame src/eggshell.c

Packit d370c2
/*
Packit d370c2
 * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
Packit d370c2
 * Copyright (C) 1999, 2000 Red Hat, Inc.
Packit d370c2
 * All rights reserved.
Packit d370c2
 *
Packit d370c2
 * This file is part of the Gnome Library.
Packit d370c2
 *
Packit d370c2
 * This program is free software: you can redistribute it and/or modify
Packit d370c2
 * it under the terms of the GNU General Public License as published by
Packit d370c2
 * the Free Software Foundation, either version 3 of the License, or
Packit d370c2
 * (at your option) any later version.
Packit d370c2
 *
Packit d370c2
 * This program is distributed in the hope that it will be useful,
Packit d370c2
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit d370c2
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit d370c2
 * GNU General Public License for more details.
Packit d370c2
 *
Packit d370c2
 * You should have received a copy of the GNU General Public License
Packit d370c2
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit d370c2
 */
Packit d370c2
/*
Packit d370c2
  @NOTATION@
Packit d370c2
 */
Packit d370c2
Packit d370c2
/*
Packit d370c2
 *
Packit d370c2
 * Gnome utility routines.
Packit d370c2
 * (C)  1997, 1998, 1999 the Free Software Foundation.
Packit d370c2
 *
Packit d370c2
 * Author: Miguel de Icaza,
Packit d370c2
 */
Packit d370c2
Packit d370c2
#include <config.h>
Packit d370c2
Packit d370c2
#include "eggshell.h"
Packit d370c2
Packit d370c2
#include <stdlib.h>
Packit d370c2
#include <stdio.h>
Packit d370c2
#include <string.h>
Packit d370c2
#include <unistd.h>
Packit d370c2
#ifndef G_OS_WIN32
Packit d370c2
#include <pwd.h>
Packit d370c2
#endif
Packit d370c2
Packit d370c2
#include <glib.h>
Packit d370c2
Packit d370c2
/**
Packit d370c2
 * egg_shell:
Packit d370c2
 * @shell: the value of the SHELL env variable
Packit d370c2
 *
Packit d370c2
 * Retrieves the user's preferred shell.
Packit d370c2
 *
Packit d370c2
 * Returns: A newly allocated string that is the path to the shell.
Packit d370c2
 */
Packit d370c2
char *
Packit d370c2
egg_shell (const char *shell)
Packit d370c2
{
Packit d370c2
#ifndef G_OS_WIN32
Packit d370c2
	struct passwd *pw;
Packit d370c2
	int i;
Packit d370c2
	static const char shells [][14] = {
Packit d370c2
		/* Note that on some systems shells can also
Packit d370c2
		 * be installed in /usr/bin */
Packit d370c2
		"/bin/bash", "/usr/bin/bash",
Packit d370c2
		"/bin/zsh", "/usr/bin/zsh",
Packit d370c2
		"/bin/tcsh", "/usr/bin/tcsh",
Packit d370c2
		"/bin/ksh", "/usr/bin/ksh",
Packit d370c2
		"/bin/csh", "/bin/sh"
Packit d370c2
	};
Packit d370c2
Packit d370c2
	if (geteuid () == getuid () &&
Packit d370c2
	    getegid () == getgid ()) {
Packit d370c2
		/* only in non-setuid */
Packit d370c2
		if (shell != NULL) {
Packit d370c2
			if (access (shell, X_OK) == 0) {
Packit d370c2
				return g_strdup (shell);
Packit d370c2
			}
Packit d370c2
		}
Packit d370c2
	}
Packit d370c2
	pw = getpwuid(getuid());
Packit d370c2
	if (pw && pw->pw_shell) {
Packit d370c2
		if (access (pw->pw_shell, X_OK) == 0) {
Packit d370c2
			return g_strdup (pw->pw_shell);
Packit d370c2
		}
Packit d370c2
	}
Packit d370c2
Packit d370c2
	for (i = 0; i != G_N_ELEMENTS (shells); i++) {
Packit d370c2
		if (access (shells [i], X_OK) == 0) {
Packit d370c2
			return g_strdup (shells[i]);
Packit d370c2
		}
Packit d370c2
	}
Packit d370c2
Packit d370c2
	/* If /bin/sh doesn't exist, your system is truly broken.  */
Packit d370c2
	g_assert_not_reached ();
Packit d370c2
Packit d370c2
	/* Placate compiler.  */
Packit d370c2
	return NULL;
Packit d370c2
#else
Packit d370c2
	/* g_find_program_in_path() always looks also in the Windows
Packit d370c2
	 * and System32 directories, so it should always find either cmd.exe
Packit d370c2
	 * or command.com.
Packit d370c2
	 */
Packit d370c2
	char *retval = g_find_program_in_path ("cmd.exe");
Packit d370c2
Packit d370c2
	if (retval == NULL)
Packit d370c2
		retval = g_find_program_in_path ("command.com");
Packit d370c2
Packit d370c2
	g_assert (retval != NULL);
Packit d370c2
Packit d370c2
	return retval;
Packit d370c2
#endif
Packit d370c2
}