2 * Portable Utility Functions
5 * Miguel de Icaza (miguel@novell.com)
7 * (C) 2006 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #include <sys/types.h>
37 g_build_path (const gchar *separator, const gchar *first_element, ...)
40 const char *s, *p, *next;
44 g_return_val_if_fail (separator != NULL, NULL);
45 g_return_val_if_fail (first_element != NULL, NULL);
47 result = g_string_sized_new (48);
49 slen = strlen (separator);
51 va_start (args, first_element);
52 for (s = first_element; s != NULL; s = next){
53 next = va_arg (args, char *);
56 if (next && p - slen > s){
57 for (; strncmp (p-slen, separator, slen) == 0; ){
61 g_string_append_len (result, s, p - s);
64 g_string_append (result, separator);
66 for (; strncmp (next, separator, slen) == 0; )
70 g_string_append_c (result, 0);
73 return g_string_free (result, FALSE);
77 g_path_get_dirname (const gchar *filename)
81 g_return_val_if_fail (filename != NULL, NULL);
83 p = strrchr (filename, G_DIR_SEPARATOR);
85 return g_strdup (".");
87 return g_strdup ("/");
89 r = g_malloc (count + 1);
90 strncpy (r, filename, count);
97 g_path_get_basename (const char *filename)
100 g_return_val_if_fail (filename != NULL, NULL);
102 /* Empty filename -> . */
104 return g_strdup (".");
106 /* No separator -> filename */
107 r = strrchr (filename, G_DIR_SEPARATOR);
109 return g_strdup (filename);
111 /* Trailing slash, remove component */
113 char *copy = g_strdup (filename);
114 copy [r-filename] = 0;
115 r = strrchr (copy, G_DIR_SEPARATOR);
119 return g_strdup ("/");
121 r = g_strdup (&r[1]);
126 return g_strdup (&r[1]);
130 g_path_is_absolute (const char *filename)
132 g_return_val_if_fail (filename != NULL, FALSE);
133 return (*filename == '/');
137 g_find_program_in_path (const gchar *program)
139 char *p = g_strdup (getenv ("PATH"));
141 gchar *curdir = NULL;
144 g_return_val_if_fail (program != NULL, NULL);
146 if (x == NULL || *x == '\0') {
147 curdir = g_get_current_dir ();
151 while ((l = strtok_r (x, G_SEARCHPATH_SEPARATOR_S, &save)) != NULL){
155 probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program, NULL);
156 if (access (probe_path, X_OK) == 0){
169 g_get_current_dir (void)
172 char *buffer = NULL, *r;
176 buffer = g_realloc (buffer, s);
177 r = getcwd (buffer, s);
178 fail = (r == NULL && errno == ERANGE);
187 static pthread_mutex_t home_lock = PTHREAD_MUTEX_INITIALIZER;
188 static char *home_dir;
190 /* Give preference to /etc/passwd than HOME */
192 g_get_home_dir (void)
194 if (home_dir == NULL){
197 pthread_mutex_lock (&home_lock);
198 if (home_dir == NULL){
199 struct passwd pwbuf, *track;
206 while (getpwent_r (&pwbuf, buf, sizeof (buf), &track) == 0){
207 if (pwbuf.pw_uid == uid){
208 home_dir = g_strdup (pwbuf.pw_dir);
213 if (home_dir == NULL)
214 home_dir = getenv ("HOME");
215 pthread_mutex_unlock (&home_lock);
221 static char *tmp_dir;
222 static pthread_mutex_t tmp_lock = PTHREAD_MUTEX_INITIALIZER;
227 if (tmp_dir == NULL){
228 pthread_mutex_lock (&tmp_lock);
229 if (tmp_dir == NULL){
230 tmp_dir = getenv ("TMPDIR");
231 if (tmp_dir == NULL){
232 tmp_dir = getenv ("TMP");
233 if (tmp_dir == NULL){
234 tmp_dir = getenv ("TEMP");
240 pthread_mutex_unlock (&tmp_lock);
246 g_get_user_name (void)
248 return getenv ("USER");
254 g_set_prgname (const gchar *prgname)
256 name = g_strdup (prgname);