fix overflow bug, try2
[wmaker-crm.git] / WINGs / findfile.c
blobc5f9b939ee5e1d2def7a78a60684fee51b0aecde
1 /*
2 * Window Maker miscelaneous function library
3 *
4 * Copyright (c) 1997-2002 Alfredo K. Kojima
5 *
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.
22 #include "wconfig.h"
24 #include "WUtil.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
37 char*
38 wgethomedir()
40 char *home = getenv("HOME");
41 struct passwd *user;
43 if (home)
44 return home;
46 user = getpwuid(getuid());
47 if (!user) {
48 wsyserror(_("could not get password entry for UID %i"), getuid());
49 return "/";
51 if (!user->pw_dir) {
52 return "/";
53 } else {
54 return user->pw_dir;
59 static char*
60 getuserhomedir(char *username)
62 struct passwd *user;
64 user = getpwnam(username);
65 if (!user) {
66 wsyserror(_("could not get password entry for user %s"), username);
67 return NULL;
69 if (!user->pw_dir) {
70 return "/";
71 } else {
72 return user->pw_dir;
79 char*
80 wexpandpath(char *path)
82 char buffer2[PATH_MAX+2];
83 char buffer[PATH_MAX+2];
84 int i;
86 memset(buffer, 0, PATH_MAX+2);
88 if (*path=='~') {
89 char *home;
91 path++;
92 if (*path=='/' || *path==0) {
93 home = wgethomedir();
94 strcat(buffer, home);
95 } else {
96 int j;
97 j = 0;
98 while (*path!=0 && *path!='/') {
99 buffer2[j++] = *path;
100 buffer2[j] = 0;
101 path++;
103 home = getuserhomedir(buffer2);
104 if (!home)
105 return NULL;
106 strcat(buffer, home);
110 i = strlen(buffer);
112 while (*path!=0) {
113 char *tmp;
115 if (*path=='$') {
116 int j = 0;
117 path++;
118 /* expand $(HOME) or $HOME style environment variables */
119 if (*path=='(') {
120 path++;
121 while (*path!=0 && *path!=')') {
122 buffer2[j++] = *(path++);
123 buffer2[j] = 0;
125 if (*path==')')
126 path++;
127 tmp = getenv(buffer2);
128 if (!tmp) {
129 buffer[i] = 0;
130 strcat(buffer, "$(");
131 strcat(buffer, buffer2);
132 strcat(buffer, ")");
133 i += strlen(buffer2)+3;
134 } else {
135 strcat(buffer, tmp);
136 i += strlen(tmp);
138 } else {
139 while (*path!=0 && *path!='/') {
140 buffer2[j++] = *(path++);
141 buffer2[j] = 0;
143 tmp = getenv(buffer2);
144 if (!tmp) {
145 strcat(buffer, "$");
146 strcat(buffer, buffer2);
147 i += strlen(buffer2)+1;
148 } else {
149 strcat(buffer, tmp);
150 i += strlen(tmp);
153 } else {
154 buffer[i++] = *path;
155 path++;
159 return wstrdup(buffer);
163 /* return address of next char != tok or end of string whichever comes first */
164 static char*
165 skipchar(char *string, char tok)
167 while(*string!=0 && *string==tok)
168 string++;
170 return string;
174 /* return address of next char == tok or end of string whichever comes first */
175 static char*
176 nextchar(char *string, char tok)
178 while(*string!=0 && *string!=tok)
179 string++;
181 return string;
186 *----------------------------------------------------------------------
187 * findfile--
188 * Finds a file in a : separated list of paths. ~ expansion is also
189 * done.
191 * Returns:
192 * The complete path for the file (in a newly allocated string) or
193 * NULL if the file was not found.
195 * Side effects:
196 * A new string is allocated. It must be freed later.
198 *----------------------------------------------------------------------
200 char*
201 wfindfile(char *paths, char *file)
203 char *path;
204 char *tmp, *tmp2;
205 int len, flen;
206 char *fullpath;
208 if (!file)
209 return NULL;
211 if (*file=='/' || *file=='~' || *file=='$' || !paths || *paths==0) {
212 if (access(file, F_OK)<0) {
213 fullpath = wexpandpath(file);
214 if (!fullpath)
215 return NULL;
217 if (access(fullpath, F_OK)<0) {
218 wfree(fullpath);
219 return NULL;
220 } else {
221 return fullpath;
223 } else {
224 return wstrdup(file);
228 flen = strlen(file);
229 tmp = paths;
230 while (*tmp) {
231 tmp = skipchar(tmp, ':');
232 if (*tmp==0)
233 break;
234 tmp2 = nextchar(tmp, ':');
235 len = tmp2 - tmp;
236 path = wmalloc(len+flen+2);
237 path = memcpy(path, tmp, len);
238 path[len]=0;
239 if (path[len-1] != '/')
240 strcat(path, "/");
241 strcat(path, file);
242 fullpath = wexpandpath(path);
243 wfree(path);
244 if (fullpath) {
245 if (access(fullpath, F_OK)==0) {
246 return fullpath;
248 wfree(fullpath);
250 tmp = tmp2;
253 return NULL;
257 char*
258 wfindfileinlist(char **path_list, char *file)
260 int i;
261 char *path;
262 int len, flen;
263 char *fullpath;
265 if (!file)
266 return NULL;
268 if (*file=='/' || *file=='~' || !path_list) {
269 if (access(file, F_OK)<0) {
270 fullpath = wexpandpath(file);
271 if (!fullpath)
272 return NULL;
274 if (access(fullpath, F_OK)<0) {
275 wfree(fullpath);
276 return NULL;
277 } else {
278 return fullpath;
280 } else {
281 return wstrdup(file);
285 flen = strlen(file);
286 for (i=0; path_list[i]!=NULL; i++) {
287 len = strlen(path_list[i]);
288 path = wmalloc(len+flen+2);
289 path = memcpy(path, path_list[i], len);
290 path[len]=0;
291 strcat(path, "/");
292 strcat(path, file);
293 /* expand tilde */
294 fullpath = wexpandpath(path);
295 wfree(path);
296 if (fullpath) {
297 /* check if file exists */
298 if (access(fullpath, F_OK)==0) {
299 return fullpath;
301 wfree(fullpath);
304 return NULL;
309 char*
310 wfindfileinarray(WMPropList *array, char *file)
312 int i;
313 char *path;
314 int len, flen;
315 char *fullpath;
317 if (!file)
318 return NULL;
320 if (*file=='/' || *file=='~' || !array) {
321 if (access(file, F_OK)<0) {
322 fullpath = wexpandpath(file);
323 if (!fullpath)
324 return NULL;
326 if (access(fullpath, F_OK)<0) {
327 wfree(fullpath);
328 return NULL;
329 } else {
330 return fullpath;
332 } else {
333 return wstrdup(file);
337 flen = strlen(file);
338 for (i=0; i<WMGetPropListItemCount(array); i++) {
339 WMPropList *prop;
340 char *p;
342 prop = WMGetFromPLArray(array, i);
343 if (!prop)
344 continue;
345 p = WMGetFromPLString(prop);
347 len = strlen(p);
348 path = wmalloc(len+flen+2);
349 path = memcpy(path, p, len);
350 path[len]=0;
351 strcat(path, "/");
352 strcat(path, file);
353 /* expand tilde */
354 fullpath = wexpandpath(path);
355 wfree(path);
356 if (fullpath) {
357 /* check if file exists */
358 if (access(fullpath, F_OK)==0) {
359 return fullpath;
361 wfree(fullpath);
364 return NULL;