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, ...)
45 const char *s, *p, *next;
49 g_return_val_if_fail (separator != NULL, NULL);
51 if (first_element == NULL)
54 result = g_string_sized_new (48);
56 slen = strlen (separator);
58 va_start (args, first_element);
59 for (s = first_element; s != NULL; s = next){
60 next = va_arg (args, char *);
63 if (next && p - slen > s){
64 for (; strncmp (p-slen, separator, slen) == 0; ){
68 g_string_append_len (result, s, p - s);
71 int reslen = strlen (result->str);
73 if (reslen >= slen && strncmp (separator, result->str + reslen - slen, slen))
74 g_string_append (result, separator);
76 for (; strncmp (next, separator, slen) == 0; )
80 g_string_append_c (result, 0);
83 return g_string_free (result, FALSE);
87 g_path_get_dirname (const gchar *filename)
91 g_return_val_if_fail (filename != NULL, NULL);
93 p = strrchr (filename, G_DIR_SEPARATOR);
95 return g_strdup (".");
97 return g_strdup ("/");
99 r = g_malloc (count + 1);
100 strncpy (r, filename, count);
107 g_path_get_basename (const char *filename)
110 g_return_val_if_fail (filename != NULL, NULL);
112 /* Empty filename -> . */
114 return g_strdup (".");
116 /* No separator -> filename */
117 r = strrchr (filename, G_DIR_SEPARATOR);
119 return g_strdup (filename);
121 /* Trailing slash, remove component */
123 char *copy = g_strdup (filename);
124 copy [r-filename] = 0;
125 r = strrchr (copy, G_DIR_SEPARATOR);
129 return g_strdup ("/");
131 r = g_strdup (&r[1]);
136 return g_strdup (&r[1]);
139 #ifndef HAVE_STRTOK_R
140 // This is from BSD's strtok_r
143 strtok_r(char *s, const char *delim, char **last)
149 if (s == NULL && (s = *last) == NULL)
153 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
157 for (spanp = (char *)delim; (sc = *spanp++) != 0; ){
162 if (c == 0){ /* no non-delimiter characters */
169 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
170 * Note that delim must have one NUL; we stop if we see that, too.
174 spanp = (char *)delim;
176 if ((sc = *spanp++) == c) {
194 g_find_program_in_path (const gchar *program)
196 char *p = g_strdup (g_getenv ("PATH"));
198 gchar *curdir = NULL;
202 char *suffix_list[5] = {".exe",".cmd",".bat",".com",NULL};
207 g_return_val_if_fail (program != NULL, NULL);
209 if (x == NULL || *x == '\0') {
210 curdir = g_get_current_dir ();
215 /* see if program already has a suffix */
218 while (!hasSuffix && suffix_list[listx]) {
219 hasSuffix = g_str_has_suffix(program,suffix_list[listx++]);
223 while ((l = strtok_r (x, G_SEARCHPATH_SEPARATOR_S, &save)) != NULL){
227 probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program, NULL);
228 if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
236 /* check for program with a suffix attached */
239 while (suffix_list[listx]) {
240 program_exe = g_strjoin(NULL,program,suffix_list[listx],NULL);
241 probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program_exe, NULL);
242 if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
245 g_free (program_exe);
250 g_free (program_exe);
263 g_set_prgname (const gchar *prgname)
265 name = g_strdup (prgname);