Repair defaults for new titlebar button style
[wmaker-crm.git] / WINGs / findfile.c
blob9aad1d74c361b271444406d1e55d8ea4407252a9
1 /*
2 * Window Maker miscelaneous function library
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "wconfig.h"
23 #include "WUtil.h"
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <pwd.h>
30 #include <limits.h>
32 #ifndef PATH_MAX
33 #define PATH_MAX 1024
34 #endif
36 char *wgethomedir()
38 char *home = getenv("HOME");
39 struct passwd *user;
41 if (home)
42 return home;
44 user = getpwuid(getuid());
45 if (!user) {
46 wsyserror(_("could not get password entry for UID %i"), getuid());
47 return "/";
49 if (!user->pw_dir) {
50 return "/";
51 } else {
52 return user->pw_dir;
56 static char *getuserhomedir(const char *username)
58 struct passwd *user;
60 user = getpwnam(username);
61 if (!user) {
62 wsyserror(_("could not get password entry for user %s"), username);
63 return NULL;
65 if (!user->pw_dir) {
66 return "/";
67 } else {
68 return user->pw_dir;
72 char *wexpandpath(char *path)
74 char *origpath = path;
75 char buffer2[PATH_MAX + 2];
76 char buffer[PATH_MAX + 2];
77 int i;
79 memset(buffer, 0, PATH_MAX + 2);
81 if (*path == '~') {
82 char *home;
84 path++;
85 if (*path == '/' || *path == 0) {
86 home = wgethomedir();
87 if (strlen(home) > PATH_MAX)
88 goto error;
89 strcat(buffer, home);
90 } else {
91 int j;
92 j = 0;
93 while (*path != 0 && *path != '/') {
94 if (j > PATH_MAX)
95 goto error;
96 buffer2[j++] = *path;
97 buffer2[j] = 0;
98 path++;
100 home = getuserhomedir(buffer2);
101 if (!home || strlen(home) > PATH_MAX)
102 goto error;
103 strcat(buffer, home);
107 i = strlen(buffer);
109 while (*path != 0 && i <= PATH_MAX) {
110 char *tmp;
112 if (*path == '$') {
113 int j = 0;
114 path++;
115 /* expand $(HOME) or $HOME style environment variables */
116 if (*path == '(') {
117 path++;
118 while (*path != 0 && *path != ')') {
119 if (j > PATH_MAX)
120 goto error;
121 buffer2[j++] = *(path++);
122 buffer2[j] = 0;
124 if (*path == ')') {
125 path++;
126 tmp = getenv(buffer2);
127 } else {
128 tmp = NULL;
130 if (!tmp) {
131 if ((i += strlen(buffer2) + 2) > PATH_MAX)
132 goto error;
133 buffer[i] = 0;
134 strcat(buffer, "$(");
135 strcat(buffer, buffer2);
136 if (*(path-1)==')') {
137 if (++i > PATH_MAX)
138 goto error;
139 strcat(buffer, ")");
141 } else {
142 if ((i += strlen(tmp)) > PATH_MAX)
143 goto error;
144 strcat(buffer, tmp);
146 } else {
147 while (*path != 0 && *path != '/') {
148 if (j > PATH_MAX)
149 goto error;
150 buffer2[j++] = *(path++);
151 buffer2[j] = 0;
153 tmp = getenv(buffer2);
154 if (!tmp) {
155 if ((i += strlen(buffer2) + 1) > PATH_MAX)
156 goto error;
157 strcat(buffer, "$");
158 strcat(buffer, buffer2);
159 } else {
160 if ((i += strlen(tmp)) > PATH_MAX)
161 goto error;
162 strcat(buffer, tmp);
165 } else {
166 buffer[i++] = *path;
167 path++;
171 if (*path!=0)
172 goto error;
174 return wstrdup(buffer);
176 error:
177 errno = ENAMETOOLONG;
178 wsyserror(_("could not expand %s"), origpath);
179 /* FIXME: too many functions handle a return value of NULL incorrectly */
180 exit(1);
183 /* return address of next char != tok or end of string whichever comes first */
184 static char *skipchar(char *string, char tok)
186 while (*string != 0 && *string == tok)
187 string++;
189 return string;
192 /* return address of next char == tok or end of string whichever comes first */
193 static char *nextchar(char *string, char tok)
195 while (*string != 0 && *string != tok)
196 string++;
198 return string;
202 *----------------------------------------------------------------------
203 * findfile--
204 * Finds a file in a : separated list of paths. ~ expansion is also
205 * done.
207 * Returns:
208 * The complete path for the file (in a newly allocated string) or
209 * NULL if the file was not found.
211 * Side effects:
212 * A new string is allocated. It must be freed later.
214 *----------------------------------------------------------------------
216 char *wfindfile(char *paths, char *file)
218 char *path;
219 char *tmp, *tmp2;
220 int len, flen;
221 char *fullpath;
223 if (!file)
224 return NULL;
226 if (*file == '/' || *file == '~' || *file == '$' || !paths || *paths == 0) {
227 if (access(file, F_OK) < 0) {
228 fullpath = wexpandpath(file);
229 if (!fullpath)
230 return NULL;
232 if (access(fullpath, F_OK) < 0) {
233 wfree(fullpath);
234 return NULL;
235 } else {
236 return fullpath;
238 } else {
239 return wstrdup(file);
243 flen = strlen(file);
244 tmp = paths;
245 while (*tmp) {
246 tmp = skipchar(tmp, ':');
247 if (*tmp == 0)
248 break;
249 tmp2 = nextchar(tmp, ':');
250 len = tmp2 - tmp;
251 path = wmalloc(len + flen + 2);
252 path = memcpy(path, tmp, len);
253 path[len] = 0;
254 if (path[len - 1] != '/')
255 strcat(path, "/");
256 strcat(path, file);
257 fullpath = wexpandpath(path);
258 wfree(path);
259 if (fullpath) {
260 if (access(fullpath, F_OK) == 0) {
261 return fullpath;
263 wfree(fullpath);
265 tmp = tmp2;
268 return NULL;
271 char *wfindfileinlist(char **path_list, char *file)
273 int i;
274 char *path;
275 int len, flen;
276 char *fullpath;
278 if (!file)
279 return NULL;
281 if (*file == '/' || *file == '~' || !path_list) {
282 if (access(file, F_OK) < 0) {
283 fullpath = wexpandpath(file);
284 if (!fullpath)
285 return NULL;
287 if (access(fullpath, F_OK) < 0) {
288 wfree(fullpath);
289 return NULL;
290 } else {
291 return fullpath;
293 } else {
294 return wstrdup(file);
298 flen = strlen(file);
299 for (i = 0; path_list[i] != NULL; i++) {
300 len = strlen(path_list[i]);
301 path = wmalloc(len + flen + 2);
302 path = memcpy(path, path_list[i], len);
303 path[len] = 0;
304 strcat(path, "/");
305 strcat(path, file);
306 /* expand tilde */
307 fullpath = wexpandpath(path);
308 wfree(path);
309 if (fullpath) {
310 /* check if file exists */
311 if (access(fullpath, F_OK) == 0) {
312 return fullpath;
314 wfree(fullpath);
317 return NULL;
320 char *wfindfileinarray(WMPropList * array, char *file)
322 int i;
323 char *path;
324 int len, flen;
325 char *fullpath;
327 if (!file)
328 return NULL;
330 if (*file == '/' || *file == '~' || !array) {
331 if (access(file, F_OK) < 0) {
332 fullpath = wexpandpath(file);
333 if (!fullpath)
334 return NULL;
336 if (access(fullpath, F_OK) < 0) {
337 wfree(fullpath);
338 return NULL;
339 } else {
340 return fullpath;
342 } else {
343 return wstrdup(file);
347 flen = strlen(file);
348 for (i = 0; i < WMGetPropListItemCount(array); i++) {
349 WMPropList *prop;
350 char *p;
352 prop = WMGetFromPLArray(array, i);
353 if (!prop)
354 continue;
355 p = WMGetFromPLString(prop);
357 len = strlen(p);
358 path = wmalloc(len + flen + 2);
359 path = memcpy(path, p, len);
360 path[len] = 0;
361 strcat(path, "/");
362 strcat(path, file);
363 /* expand tilde */
364 fullpath = wexpandpath(path);
365 wfree(path);
366 if (fullpath) {
367 /* check if file exists */
368 if (access(fullpath, F_OK) == 0) {
369 return fullpath;
371 wfree(fullpath);
374 return NULL;