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.
42 g_build_path (const gchar *separator, const gchar *first_element, ...)
44 const char *elem, *next, *endptr;
50 g_return_val_if_fail (separator != NULL, NULL);
52 path = g_string_sized_new (48);
53 slen = strlen (separator);
55 va_start (args, first_element);
56 for (elem = first_element; elem != NULL; elem = next) {
57 /* trim any trailing separators from @elem */
58 endptr = elem + strlen (elem);
61 while (endptr >= elem + slen) {
62 if (strncmp (endptr - slen, separator, slen) != 0)
69 /* append elem, not including any trailing separators */
71 g_string_append_len (path, elem, endptr - elem);
73 /* get the next element */
75 if (!(next = va_arg (args, char *)))
78 /* remove leading separators */
79 while (!strncmp (next, separator, slen))
81 } while (*next == '\0');
84 g_string_append_len (path, separator, slen);
88 return g_string_free (path, FALSE);
92 g_path_get_dirname (const gchar *filename)
96 g_return_val_if_fail (filename != NULL, NULL);
98 p = strrchr (filename, G_DIR_SEPARATOR);
100 return g_strdup (".");
102 return g_strdup ("/");
103 count = p - filename;
104 r = g_malloc (count + 1);
105 strncpy (r, filename, count);
112 g_path_get_basename (const char *filename)
115 g_return_val_if_fail (filename != NULL, NULL);
117 /* Empty filename -> . */
119 return g_strdup (".");
121 /* No separator -> filename */
122 r = strrchr (filename, G_DIR_SEPARATOR);
124 return g_strdup (filename);
126 /* Trailing slash, remove component */
128 char *copy = g_strdup (filename);
129 copy [r-filename] = 0;
130 r = strrchr (copy, G_DIR_SEPARATOR);
134 return g_strdup ("/");
136 r = g_strdup (&r[1]);
141 return g_strdup (&r[1]);
144 #ifndef HAVE_STRTOK_R
145 // This is from BSD's strtok_r
148 strtok_r(char *s, const char *delim, char **last)
154 if (s == NULL && (s = *last) == NULL)
158 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
162 for (spanp = (char *)delim; (sc = *spanp++) != 0; ){
167 if (c == 0){ /* no non-delimiter characters */
174 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
175 * Note that delim must have one NUL; we stop if we see that, too.
179 spanp = (char *)delim;
181 if ((sc = *spanp++) == c) {
199 g_find_program_in_path (const gchar *program)
201 char *p = g_strdup (g_getenv ("PATH"));
203 gchar *curdir = NULL;
207 char *suffix_list[5] = {".exe",".cmd",".bat",".com",NULL};
212 g_return_val_if_fail (program != NULL, NULL);
214 if (x == NULL || *x == '\0') {
215 curdir = g_get_current_dir ();
220 /* see if program already has a suffix */
223 while (!hasSuffix && suffix_list[listx]) {
224 hasSuffix = g_str_has_suffix(program,suffix_list[listx++]);
228 while ((l = strtok_r (x, G_SEARCHPATH_SEPARATOR_S, &save)) != NULL){
232 probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program, NULL);
233 if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
241 /* check for program with a suffix attached */
244 while (suffix_list[listx]) {
245 program_exe = g_strjoin(NULL,program,suffix_list[listx],NULL);
246 probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program_exe, NULL);
247 if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
250 g_free (program_exe);
255 g_free (program_exe);
268 g_set_prgname (const gchar *prgname)
270 name = g_strdup (prgname);