source: trunk/third/libgnome/libgnome/gnome-util.c @ 18320

Revision 18320, 5.6 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18319, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
3 * Copyright (C) 1999, 2000 Red Hat, Inc.
4 * All rights reserved.
5 *
6 * This file is part of the Gnome Library.
7 *
8 * The Gnome Library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * The Gnome Library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
20 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23/*
24  @NOTATION@
25 */
26
27/*
28 *
29 * Gnome utility routines.
30 * (C)  1997, 1998, 1999 the Free Software Foundation.
31 *
32 * Author: Miguel de Icaza,
33 */
34#include <config.h>
35
36/* needed for S_ISLNK with 'gcc -ansi -pedantic' on GNU/Linux */
37#ifndef _BSD_SOURCE
38#  define _BSD_SOURCE 1
39#endif
40#include <sys/types.h>
41
42#include <errno.h>
43#include <stdarg.h>
44#include <stdlib.h>
45#include <stdio.h>
46#include <string.h>
47#include <unistd.h>
48#include <glib.h>
49#include <sys/stat.h>
50#include <pwd.h>
51#include <limits.h>
52#include <libgnome/gnome-program.h>
53#include <libgnome/gnome-util.h>
54
55/**
56 * gnome_util_user_shell:
57 *
58 * Retrieves the user's preferred shell.
59 *
60 * Returns: A newly allocated string that is the path to the shell.
61 */
62char *
63gnome_util_user_shell (void)
64{
65        struct passwd *pw;
66        int i;
67        const char *shell;
68        static char *shells [] = {
69                /* Note that on some systems shells can also
70                 * be installed in /usr/bin */
71                "/bin/bash", "/usr/bin/bash",
72                "/bin/zsh", "/usr/bin/zsh",
73                "/bin/tcsh", "/usr/bin/tcsh",
74                "/bin/ksh", "/usr/bin/ksh",
75                "/bin/csh", "/bin/sh", 0
76        };
77
78        if (geteuid () == getuid () &&
79            getegid () == getgid ()) {
80                /* only in non-setuid */
81                if ((shell = g_getenv ("SHELL"))){
82                        return g_strdup (shell);
83                }
84        }
85        pw = getpwuid(getuid());
86        if (pw && pw->pw_shell) {
87                return g_strdup (pw->pw_shell);
88        }
89
90        for (i = 0; shells [i]; i++) {
91                if (access (shells [i], X_OK) == 0) {
92                        return g_strdup (shells[i]);
93                }
94        }
95
96        /* If /bin/sh doesn't exist, your system is truly broken.  */
97        abort ();
98
99        /* Placate compiler.  */
100        return NULL;
101}
102
103/**
104 * g_extension_pointer:
105 * @path: A filename or file path.
106 *
107 * Extracts the extension from the end of a filename (the part after the final
108 * '.' in the filename).
109 *
110 * Returns: A pointer to the extension part of the filename, or a
111 * pointer to the end of the string if the filename does not
112 * have an extension.
113 */
114const char *
115g_extension_pointer (const char * path)
116{
117        char * s, * t;
118       
119        g_return_val_if_fail(path != NULL, NULL);
120
121        /* get the dot in the last element of the path */
122        t = strrchr(path, G_DIR_SEPARATOR);
123        if (t != NULL)
124                s = strrchr(t, '.');
125        else
126                s = strrchr(path, '.');
127       
128        if (s == NULL)
129                return path + strlen(path); /* There is no extension. */
130        else {
131                ++s;      /* pass the . */
132                return s;
133        }
134}
135
136/**
137 * gnome_setenv:
138 * @name: An environment variable name.
139 * @value: The value to assign to the environment variable.
140 * @overwrite: If %TRUE, overwrite the existing @name variable in the
141 * environment.
142 *
143 * Adds "@name=@value" to the environment. Note that on systems without setenv,
144 * this leaks memory so please do not use inside a loop or anything like that.
145 * The semantics are the same as the glibc setenv() (if setenv() exists, it is
146 * used).
147 *
148 * If @overwrite is %FALSE and the variable already exists in the environment,
149 * then %0 is returned and the value is not changed.
150 *
151 * Returns: %0 on success, %-1 on error
152 *
153 **/
154int
155gnome_setenv (const char *name, const char *value, gboolean overwrite)
156{
157#if defined (HAVE_SETENV)
158        return setenv (name, value != NULL ? value : "", overwrite);
159#else
160        char *string;
161       
162        if (! overwrite && g_getenv (name) != NULL) {
163                return 0;
164        }
165       
166        /* This results in a leak when you overwrite existing
167         * settings. It would be fairly easy to fix this by keeping
168         * our own parallel array or hash table.
169         */
170        string = g_strconcat (name, "=", value, NULL);
171        return putenv (string);
172#endif
173}
174
175/**
176 * gnome_unsetenv:
177 * @name: The environment variable to unset.
178 *
179 * Description: Removes @name from the environment.
180 * In case there is no native implementation of unsetenv,
181 * this could cause leaks depending on the implementation of
182 * environment.
183 *
184 **/
185void
186gnome_unsetenv (const char *name)
187{
188#if defined (HAVE_SETENV)
189        unsetenv (name);
190#else
191        extern char **environ;
192        int i, len;
193
194        len = strlen (name);
195       
196        /* Mess directly with the environ array.
197         * This seems to be the only portable way to do this.
198         */
199        for (i = 0; environ[i] != NULL; i++) {
200                if (strncmp (environ[i], name, len) == 0
201                    && environ[i][len + 1] == '=') {
202                        break;
203                }
204        }
205        while (environ[i] != NULL) {
206                environ[i] = environ[i + 1];
207                i++;
208        }
209#endif
210}
211
212/**
213 * gnome_clearenv:
214 *
215 * Description: Clears out the environment completely.
216 * In case there is no native implementation of clearenv,
217 * this could cause leaks depending on the implementation
218 * of environment.
219 *
220 **/
221void
222gnome_clearenv (void)
223{
224#ifdef HAVE_CLEARENV
225        clearenv ();
226#else
227        extern char **environ;
228        environ[0] = NULL;
229#endif
230}
231
232
233/* Deprecated: */
234/**
235 * gnome_is_program_in_path:
236 * @program: A program name.
237 *
238 * Deprecated, use #g_find_program_in_path
239 *
240 * Returns: %NULL if program is not on the path or a string
241 * allocated with g_malloc() with the full path name of the program
242 * found.
243 */
244
Note: See TracBrowser for help on using the repository browser.