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.
43 g_build_path (const gchar *separator, const gchar *first_element, ...)
45 const char *elem, *next, *endptr;
51 g_return_val_if_fail (separator != NULL, NULL);
53 path = g_string_sized_new (48);
54 slen = strlen (separator);
56 va_start (args, first_element);
57 for (elem = first_element; elem != NULL; elem = next) {
58 /* trim any trailing separators from @elem */
59 endptr = elem + strlen (elem);
62 while (endptr >= elem + slen) {
63 if (strncmp (endptr - slen, separator, slen) != 0)
70 /* append elem, not including any trailing separators */
72 g_string_append_len (path, elem, endptr - elem);
74 /* get the next element */
76 if (!(next = va_arg (args, char *)))
79 /* remove leading separators */
80 while (!strncmp (next, separator, slen))
82 } while (*next == '\0');
85 g_string_append_len (path, separator, slen);
89 return g_string_free (path, FALSE);
93 strrchr_seperator (const gchar* filename)
100 p = strrchr (filename, G_DIR_SEPARATOR);
102 p2 = strrchr (filename, '/');
111 g_path_get_dirname (const gchar *filename)
115 g_return_val_if_fail (filename != NULL, NULL);
117 p = strrchr_seperator (filename);
119 return g_strdup (".");
121 return g_strdup ("/");
122 count = p - filename;
123 r = g_malloc (count + 1);
124 strncpy (r, filename, count);
131 g_path_get_basename (const char *filename)
134 g_return_val_if_fail (filename != NULL, NULL);
136 /* Empty filename -> . */
138 return g_strdup (".");
140 /* No separator -> filename */
141 r = strrchr_seperator (filename);
143 return g_strdup (filename);
145 /* Trailing slash, remove component */
147 char *copy = g_strdup (filename);
148 copy [r-filename] = 0;
149 r = strrchr_seperator (copy);
153 return g_strdup ("/");
155 r = g_strdup (&r[1]);
160 return g_strdup (&r[1]);
163 #ifndef HAVE_STRTOK_R
164 // This is from BSD's strtok_r
167 strtok_r(char *s, const char *delim, char **last)
173 if (s == NULL && (s = *last) == NULL)
177 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
181 for (spanp = (char *)delim; (sc = *spanp++) != 0; ){
186 if (c == 0){ /* no non-delimiter characters */
193 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
194 * Note that delim must have one NUL; we stop if we see that, too.
198 spanp = (char *)delim;
200 if ((sc = *spanp++) == c) {
218 g_find_program_in_path (const gchar *program)
222 gchar *curdir = NULL;
226 char *suffix_list[5] = {".exe",".cmd",".bat",".com",NULL};
231 g_return_val_if_fail (program != NULL, NULL);
232 x = p = g_strdup (g_getenv ("PATH"));
234 if (x == NULL || *x == '\0') {
235 curdir = g_get_current_dir ();
240 /* see if program already has a suffix */
243 while (!hasSuffix && suffix_list[listx]) {
244 hasSuffix = g_str_has_suffix(program,suffix_list[listx++]);
248 while ((l = strtok_r (x, G_SEARCHPATH_SEPARATOR_S, &save)) != NULL){
252 probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program, NULL);
253 if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
261 /* check for program with a suffix attached */
264 while (suffix_list[listx]) {
265 program_exe = g_strjoin(NULL,program,suffix_list[listx],NULL);
266 probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program_exe, NULL);
267 if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
270 g_free (program_exe);
275 g_free (program_exe);
288 g_set_prgname (const gchar *prgname)
290 name = g_strdup (prgname);
300 g_ensure_directory_exists (const gchar *filename)
303 gchar *dir_utf8 = g_path_get_dirname (filename);
305 gunichar2 *dir_utf16 = NULL;
308 if (!dir_utf8 || !dir_utf8 [0])
311 dir_utf16 = g_utf8_to_utf16 (dir_utf8, strlen (dir_utf8), NULL, NULL, NULL);
319 /* make life easy and only use one directory seperator */
335 gboolean bRet = FALSE;
336 p = wcschr (p, '\\');
339 retval = _wmkdir (dir_utf16);
340 if (retval != 0 && errno != EEXIST) {
353 gchar *dir = g_path_get_dirname (filename);
357 if (!dir || !dir [0]) {
362 if (stat (dir, &sbuf) == 0 && S_ISDIR (sbuf.st_mode)) {
375 retval = mkdir (dir, 0777);
376 if (retval != 0 && errno != EEXIST) {