2009-01-07 Miguel de Icaza <miguel@novell.com>
[mono.git] / eglib / src / gpath.c
1 /*
2  * Portable Utility Functions
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@novell.com)
6  *
7  * (C) 2006 Novell, Inc.
8  *
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:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
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.
27  */
28 #include <config.h>
29 #include <stdio.h>
30 #include <glib.h>
31 #include <errno.h>
32
33 #ifdef G_OS_WIN32
34 #include <direct.h> 
35 #endif
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 gchar *
42 g_build_path (const gchar *separator, const gchar *first_element, ...)
43 {
44         GString *result;
45         const char *s, *p, *next;
46         size_t slen;
47         va_list args;
48         
49         g_return_val_if_fail (separator != NULL, NULL);
50         g_return_val_if_fail (first_element != NULL, NULL);
51
52         result = g_string_sized_new (48);
53
54         slen = strlen (separator);
55         
56         va_start (args, first_element);
57         for (s = first_element; s != NULL; s = next){
58                 next = va_arg (args, char *);
59                 p = (s + strlen (s));
60
61                 if (next && p - slen > s){
62                         for (; strncmp (p-slen, separator, slen) == 0; ){
63                                 p -= slen;
64                         }
65                 }
66                 g_string_append_len (result, s, p - s);
67
68                 if (next && *next){
69                         g_string_append (result, separator);
70
71                         for (; strncmp (next, separator, slen) == 0; )
72                                 next += slen;
73                 }
74         }
75         g_string_append_c (result, 0);
76         va_end (args);
77
78         return g_string_free (result, FALSE);
79 }
80
81 gchar *
82 g_path_get_dirname (const gchar *filename)
83 {
84         char *p, *r;
85         size_t count;
86         g_return_val_if_fail (filename != NULL, NULL);
87
88         p = strrchr (filename, G_DIR_SEPARATOR);
89         if (p == NULL)
90                 return g_strdup (".");
91         if (p == filename)
92                 return g_strdup ("/");
93         count = p - filename;
94         r = g_malloc (count + 1);
95         strncpy (r, filename, count);
96         r [count] = 0;
97
98         return r;
99 }
100
101 gchar *
102 g_path_get_basename (const char *filename)
103 {
104         char *r;
105         g_return_val_if_fail (filename != NULL, NULL);
106
107         /* Empty filename -> . */
108         if (!*filename)
109                 return g_strdup (".");
110
111         /* No separator -> filename */
112         r = strrchr (filename, G_DIR_SEPARATOR);
113         if (r == NULL)
114                 return g_strdup (filename);
115
116         /* Trailing slash, remove component */
117         if (r [1] == 0){
118                 char *copy = g_strdup (filename);
119                 copy [r-filename] = 0;
120                 r = strrchr (copy, G_DIR_SEPARATOR);
121
122                 if (r == NULL){
123                         g_free (copy);                  
124                         return g_strdup ("/");
125                 }
126                 r = g_strdup (&r[1]);
127                 g_free (copy);
128                 return r;
129         }
130
131         return g_strdup (&r[1]);
132 }
133
134 #ifndef HAVE_STRTOK_R
135 // This is from BSD's strtok_r
136
137 char *
138 strtok_r(char *s, const char *delim, char **last)
139 {
140         char *spanp;
141         int c, sc;
142         char *tok;
143         
144         if (s == NULL && (s = *last) == NULL)
145                 return NULL;
146         
147         /*
148          * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
149          */
150 cont:
151         c = *s++;
152         for (spanp = (char *)delim; (sc = *spanp++) != 0; ){
153                 if (c == sc)
154                         goto cont;
155         }
156
157         if (c == 0){         /* no non-delimiter characters */
158                 *last = NULL;
159                 return NULL;
160         }
161         tok = s - 1;
162
163         /*
164          * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
165          * Note that delim must have one NUL; we stop if we see that, too.
166          */
167         for (;;){
168                 c = *s++;
169                 spanp = (char *)delim;
170                 do {
171                         if ((sc = *spanp++) == c) {
172                                 if (c == 0)
173                                         s = NULL;
174                                 else {
175                                         char *w = s - 1;
176                                         *w = '\0';
177                                 }
178                                 *last = s;
179                                 return tok;
180                         }
181                 }
182                 while (sc != 0);
183         }
184         /* NOTREACHED */
185 }
186 #endif
187
188 gchar *
189 g_find_program_in_path (const gchar *program)
190 {
191         char *p = g_strdup (g_getenv ("PATH"));
192         char *x = p, *l;
193         gchar *curdir = NULL;
194         char *save;
195
196         g_return_val_if_fail (program != NULL, NULL);
197
198         if (x == NULL || *x == '\0') {
199                 curdir = g_get_current_dir ();
200                 x = curdir;
201         }
202
203         while ((l = strtok_r (x, G_SEARCHPATH_SEPARATOR_S, &save)) != NULL){
204                 char *probe_path; 
205                 
206                 x = NULL;
207                 probe_path = g_build_path (G_DIR_SEPARATOR_S, l, program, NULL);
208                 if (access (probe_path, X_OK) == 0){ /* FIXME: on windows this is just a read permissions test */
209                         g_free (curdir);
210                         g_free (p);
211                         return probe_path;
212                 }
213                 g_free (probe_path);
214         }
215         g_free (curdir);
216         g_free (p);
217         return NULL;
218 }
219
220 static char *name;
221
222 void
223 g_set_prgname (const gchar *prgname)
224 {
225         name = g_strdup (prgname);
226 }
227
228 gchar *
229 g_get_prgname (void)
230 {
231         return name;
232 }