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 | */ |
---|
62 | char * |
---|
63 | gnome_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", NULL |
---|
76 | }; |
---|
77 | |
---|
78 | if (geteuid () == getuid () && |
---|
79 | getegid () == getgid ()) { |
---|
80 | /* only in non-setuid */ |
---|
81 | if ((shell = g_getenv ("SHELL"))){ |
---|
82 | if (access (shell, X_OK) == 0) { |
---|
83 | return g_strdup (shell); |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | pw = getpwuid(getuid()); |
---|
88 | if (pw && pw->pw_shell) { |
---|
89 | if (access (pw->pw_shell, X_OK) == 0) { |
---|
90 | return g_strdup (pw->pw_shell); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | for (i = 0; shells [i]; i++) { |
---|
95 | if (access (shells [i], X_OK) == 0) { |
---|
96 | return g_strdup (shells[i]); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | /* If /bin/sh doesn't exist, your system is truly broken. */ |
---|
101 | abort (); |
---|
102 | |
---|
103 | /* Placate compiler. */ |
---|
104 | return NULL; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * g_extension_pointer: |
---|
109 | * @path: A filename or file path. |
---|
110 | * |
---|
111 | * Extracts the extension from the end of a filename (the part after the final |
---|
112 | * '.' in the filename). |
---|
113 | * |
---|
114 | * Returns: A pointer to the extension part of the filename, or a |
---|
115 | * pointer to the end of the string if the filename does not |
---|
116 | * have an extension. |
---|
117 | */ |
---|
118 | const char * |
---|
119 | g_extension_pointer (const char * path) |
---|
120 | { |
---|
121 | char * s, * t; |
---|
122 | |
---|
123 | g_return_val_if_fail(path != NULL, NULL); |
---|
124 | |
---|
125 | /* get the dot in the last element of the path */ |
---|
126 | t = strrchr(path, G_DIR_SEPARATOR); |
---|
127 | if (t != NULL) |
---|
128 | s = strrchr(t, '.'); |
---|
129 | else |
---|
130 | s = strrchr(path, '.'); |
---|
131 | |
---|
132 | if (s == NULL) |
---|
133 | return path + strlen(path); /* There is no extension. */ |
---|
134 | else { |
---|
135 | ++s; /* pass the . */ |
---|
136 | return s; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * gnome_setenv: |
---|
142 | * @name: An environment variable name. |
---|
143 | * @value: The value to assign to the environment variable. |
---|
144 | * @overwrite: If %TRUE, overwrite the existing @name variable in the |
---|
145 | * environment. |
---|
146 | * |
---|
147 | * Adds "@name=@value" to the environment. Note that on systems without setenv, |
---|
148 | * this leaks memory so please do not use inside a loop or anything like that. |
---|
149 | * The semantics are the same as the glibc setenv() (if setenv() exists, it is |
---|
150 | * used). |
---|
151 | * |
---|
152 | * If @overwrite is %FALSE and the variable already exists in the environment, |
---|
153 | * then %0 is returned and the value is not changed. |
---|
154 | * |
---|
155 | * Returns: %0 on success, %-1 on error |
---|
156 | * |
---|
157 | **/ |
---|
158 | int |
---|
159 | gnome_setenv (const char *name, const char *value, gboolean overwrite) |
---|
160 | { |
---|
161 | #if defined (HAVE_SETENV) |
---|
162 | return setenv (name, value != NULL ? value : "", overwrite); |
---|
163 | #else |
---|
164 | char *string; |
---|
165 | |
---|
166 | if (! overwrite && g_getenv (name) != NULL) { |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | /* This results in a leak when you overwrite existing |
---|
171 | * settings. It would be fairly easy to fix this by keeping |
---|
172 | * our own parallel array or hash table. |
---|
173 | */ |
---|
174 | string = g_strconcat (name, "=", value, NULL); |
---|
175 | return putenv (string); |
---|
176 | #endif |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * gnome_unsetenv: |
---|
181 | * @name: The environment variable to unset. |
---|
182 | * |
---|
183 | * Description: Removes @name from the environment. |
---|
184 | * In case there is no native implementation of unsetenv, |
---|
185 | * this could cause leaks depending on the implementation of |
---|
186 | * environment. |
---|
187 | * |
---|
188 | **/ |
---|
189 | void |
---|
190 | gnome_unsetenv (const char *name) |
---|
191 | { |
---|
192 | #if defined (HAVE_UNSETENV) |
---|
193 | unsetenv (name); |
---|
194 | #else |
---|
195 | extern char **environ; |
---|
196 | int i, len; |
---|
197 | |
---|
198 | len = strlen (name); |
---|
199 | |
---|
200 | /* Mess directly with the environ array. |
---|
201 | * This seems to be the only portable way to do this. |
---|
202 | */ |
---|
203 | for (i = 0; environ[i] != NULL; i++) { |
---|
204 | if (strncmp (environ[i], name, len) == 0 |
---|
205 | && environ[i][len + 1] == '=') { |
---|
206 | break; |
---|
207 | } |
---|
208 | } |
---|
209 | while (environ[i] != NULL) { |
---|
210 | environ[i] = environ[i + 1]; |
---|
211 | i++; |
---|
212 | } |
---|
213 | #endif |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * gnome_clearenv: |
---|
218 | * |
---|
219 | * Description: Clears out the environment completely. |
---|
220 | * In case there is no native implementation of clearenv, |
---|
221 | * this could cause leaks depending on the implementation |
---|
222 | * of environment. |
---|
223 | * |
---|
224 | **/ |
---|
225 | void |
---|
226 | gnome_clearenv (void) |
---|
227 | { |
---|
228 | #ifdef HAVE_CLEARENV |
---|
229 | clearenv (); |
---|
230 | #else |
---|
231 | extern char **environ; |
---|
232 | environ[0] = NULL; |
---|
233 | #endif |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | /* Deprecated: */ |
---|
238 | /** |
---|
239 | * gnome_is_program_in_path: |
---|
240 | * @program: A program name. |
---|
241 | * |
---|
242 | * Deprecated, use #g_find_program_in_path |
---|
243 | * |
---|
244 | * Returns: %NULL if program is not on the path or a string |
---|
245 | * allocated with g_malloc() with the full path name of the program |
---|
246 | * found. |
---|
247 | */ |
---|
248 | |
---|