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);
150 if (filename[0] != '\0' && filename[1] != '\0' && filename[1] == ':' &&
151 filename[2] != '\0' && (filename[2] == '\\' || filename[2] == '/'))
156 return (*filename == '/');
161 g_find_program_in_path (const gchar *program)
163 char *p = g_strdup (g_getenv ("PATH"));
165 gchar *curdir = NULL;
168 g_return_val_if_fail (program != NULL, NULL);
170 if (x == NULL || *x == '\0') {
171 curdir = g_get_current_dir ();
175 while ((l = strtok_r (x, G_SEARCHPATH_SEPARATOR_S, &save)) != NULL){
179 probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program, NULL);
180 if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
193 g_get_current_dir (void)
196 char *buffer = NULL, *r;
200 buffer = g_realloc (buffer, s);
201 r = getcwd (buffer, s);
202 fail = (r == NULL && errno == ERANGE);
211 #if defined (G_OS_UNIX)
213 static pthread_mutex_t home_lock = PTHREAD_MUTEX_INITIALIZER;
214 static const gchar *home_dir;
216 /* Give preference to /etc/passwd than HOME */
218 g_get_home_dir (void)
220 if (home_dir == NULL){
221 pthread_mutex_lock (&home_lock);
222 if (home_dir == NULL){
223 #ifdef HAVE_GETPWENT_R
224 struct passwd pwbuf, *track;
232 while (getpwent_r (&pwbuf, buf, sizeof (buf), &track) == 0){
233 if (pwbuf.pw_uid == uid){
234 home_dir = g_strdup (pwbuf.pw_dir);
240 if (home_dir == NULL)
241 home_dir = g_getenv ("HOME");
242 pthread_mutex_unlock (&home_lock);
248 #elif defined (G_OS_WIN32)
252 g_get_home_dir (void)
255 const gchar *drive = g_getenv ("HOMEDRIVE");
256 const gchar *path = g_getenv ("HOMEPATH");
257 gchar *home_dir = NULL;
260 home_dir = malloc(strlen(drive) + strlen(path) +1);
262 sprintf(home_dir, "%s%s", drive, path);
272 g_get_home_dir (void)
274 g_error ("%s", "g_get_home_dir not implemented on this platform");
280 static const char *tmp_dir;
283 static pthread_mutex_t tmp_lock = PTHREAD_MUTEX_INITIALIZER;
289 if (tmp_dir == NULL){
291 pthread_mutex_lock (&tmp_lock);
293 if (tmp_dir == NULL){
294 tmp_dir = g_getenv ("TMPDIR");
295 if (tmp_dir == NULL){
296 tmp_dir = g_getenv ("TMP");
297 if (tmp_dir == NULL){
298 tmp_dir = g_getenv ("TEMP");
300 #if defined (G_OS_WIN32)
301 tmp_dir = "C:\\temp";
309 pthread_mutex_unlock (&tmp_lock);
316 g_get_user_name (void)
318 const char * retName = g_getenv ("USER");
319 #if defined (G_OS_WIN32)
321 retName = g_getenv ("USERNAME");
329 g_set_prgname (const gchar *prgname)
331 name = g_strdup (prgname);