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>
53 g_build_path (const gchar *separator, const gchar *first_element, ...)
56 const char *s, *p, *next;
60 g_return_val_if_fail (separator != NULL, NULL);
61 g_return_val_if_fail (first_element != NULL, NULL);
63 result = g_string_sized_new (48);
65 slen = strlen (separator);
67 va_start (args, first_element);
68 for (s = first_element; s != NULL; s = next){
69 next = va_arg (args, char *);
72 if (next && p - slen > s){
73 for (; strncmp (p-slen, separator, slen) == 0; ){
77 g_string_append_len (result, s, p - s);
80 g_string_append (result, separator);
82 for (; strncmp (next, separator, slen) == 0; )
86 g_string_append_c (result, 0);
89 return g_string_free (result, FALSE);
93 g_path_get_dirname (const gchar *filename)
97 g_return_val_if_fail (filename != NULL, NULL);
99 p = strrchr (filename, G_DIR_SEPARATOR);
101 return g_strdup (".");
103 return g_strdup ("/");
104 count = p - filename;
105 r = g_malloc (count + 1);
106 strncpy (r, filename, count);
113 g_path_get_basename (const char *filename)
116 g_return_val_if_fail (filename != NULL, NULL);
118 /* Empty filename -> . */
120 return g_strdup (".");
122 /* No separator -> filename */
123 r = strrchr (filename, G_DIR_SEPARATOR);
125 return g_strdup (filename);
127 /* Trailing slash, remove component */
129 char *copy = g_strdup (filename);
130 copy [r-filename] = 0;
131 r = strrchr (copy, G_DIR_SEPARATOR);
135 return g_strdup ("/");
137 r = g_strdup (&r[1]);
142 return g_strdup (&r[1]);
146 g_path_is_absolute (const char *filename)
148 g_return_val_if_fail (filename != NULL, FALSE);
149 return (*filename == '/');
153 g_find_program_in_path (const gchar *program)
155 char *p = g_strdup (g_getenv ("PATH"));
157 gchar *curdir = NULL;
160 g_return_val_if_fail (program != NULL, NULL);
162 if (x == NULL || *x == '\0') {
163 curdir = g_get_current_dir ();
167 while ((l = strtok_r (x, G_SEARCHPATH_SEPARATOR_S, &save)) != NULL){
171 probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program, NULL);
172 if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
185 g_get_current_dir (void)
188 char *buffer = NULL, *r;
192 buffer = g_realloc (buffer, s);
193 r = getcwd (buffer, s);
194 fail = (r == NULL && errno == ERANGE);
203 #if defined (G_OS_UNIX)
205 static pthread_mutex_t home_lock = PTHREAD_MUTEX_INITIALIZER;
206 static const gchar *home_dir;
208 /* Give preference to /etc/passwd than HOME */
210 g_get_home_dir (void)
212 if (home_dir == NULL){
215 pthread_mutex_lock (&home_lock);
216 if (home_dir == NULL){
217 struct passwd pwbuf, *track;
224 while (getpwent_r (&pwbuf, buf, sizeof (buf), &track) == 0){
225 if (pwbuf.pw_uid == uid){
226 home_dir = g_strdup (pwbuf.pw_dir);
231 if (home_dir == NULL)
232 home_dir = g_getenv ("HOME");
233 pthread_mutex_unlock (&home_lock);
239 #elif defined (G_OS_WIN32)
243 g_get_home_dir (void)
246 const gchar *drive = g_getenv ("HOMEDRIVE");
247 const gchar *path = g_getenv ("HOMEPATH");
248 gchar *home_dir = NULL;
251 home_dir = malloc(strlen(drive) + strlen(path) +1);
253 sprintf(home_dir, "%s%s", drive, path);
263 g_get_home_dir (void)
265 g_error ("%s", "g_get_home_dir not implemented on this platform");
271 static const char *tmp_dir;
274 static pthread_mutex_t tmp_lock = PTHREAD_MUTEX_INITIALIZER;
280 if (tmp_dir == NULL){
282 pthread_mutex_lock (&tmp_lock);
284 if (tmp_dir == NULL){
285 tmp_dir = g_getenv ("TMPDIR");
286 if (tmp_dir == NULL){
287 tmp_dir = g_getenv ("TMP");
288 if (tmp_dir == NULL){
289 tmp_dir = g_getenv ("TEMP");
291 #if defined (G_OS_WIN32)
292 tmp_dir = "C:\\temp";
300 pthread_mutex_unlock (&tmp_lock);
307 g_get_user_name (void)
309 return g_getenv ("USER");
315 g_set_prgname (const gchar *prgname)
317 name = g_strdup (prgname);