wrlib: return NULL if XImage could not be taken, for consistency
[wmaker-crm.git] / WINGs / findfile.c
blob21ed7e2272489741b436d58658bd6dbe1248a885
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., 51 Franklin St, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
22 #include "wconfig.h"
24 #include "WUtil.h"
26 #include <sys/stat.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <pwd.h>
33 #include <limits.h>
35 #ifndef PATH_MAX
36 #define PATH_MAX 1024
37 #endif
40 char *wgethomedir()
42 static char *home = NULL;
43 char *tmp;
44 struct passwd *user;
46 if (home)
47 return home;
49 #ifdef HAVE_SECURE_GETENV
50 tmp = secure_getenv("HOME");
51 #else
52 tmp = getenv("HOME");
53 #endif
54 if (tmp) {
55 home = wstrdup(tmp);
56 return home;
59 user = getpwuid(getuid());
60 if (!user) {
61 werror(_("could not get password entry for UID %i"), getuid());
62 home = "/";
63 return home;
66 if (!user->pw_dir)
67 home = "/";
68 else
69 home = wstrdup(user->pw_dir);
71 return home;
75 * Return the home directory for the specified used
77 * If user not found, returns NULL, otherwise always returns a path that is
78 * statically stored.
80 * Please note you must use the path before any other call to 'getpw*' or it
81 * may be erased. This is a design choice to avoid duplication considering
82 * the use case for this function.
84 static const char *getuserhomedir(const char *username)
86 static const char default_home[] = "/";
87 struct passwd *user;
89 user = getpwnam(username);
90 if (!user) {
91 werror(_("could not get password entry for user %s"), username);
92 return NULL;
94 if (!user->pw_dir)
95 return default_home;
96 else
97 return user->pw_dir;
101 char *wexpandpath(const char *path)
103 const char *origpath = path;
104 char buffer2[PATH_MAX + 2];
105 char buffer[PATH_MAX + 2];
106 int i;
108 memset(buffer, 0, PATH_MAX + 2);
110 if (*path == '~') {
111 const char *home;
113 path++;
114 if (*path == '/' || *path == 0) {
115 home = wgethomedir();
116 if (strlen(home) > PATH_MAX ||
117 wstrlcpy(buffer, home, sizeof(buffer)) >= sizeof(buffer))
118 goto error;
119 } else {
120 int j;
121 j = 0;
122 while (*path != 0 && *path != '/') {
123 if (j > PATH_MAX)
124 goto error;
125 buffer2[j++] = *path;
126 buffer2[j] = 0;
127 path++;
129 home = getuserhomedir(buffer2);
130 if (!home || wstrlcat(buffer, home, sizeof(buffer)) >= sizeof(buffer))
131 goto error;
135 i = strlen(buffer);
137 while (*path != 0 && i <= PATH_MAX) {
138 char *tmp;
140 if (*path == '$') {
141 int j;
143 path++;
144 /* expand $(HOME) or $HOME style environment variables */
145 if (*path == '(') {
146 path++;
147 j = 0;
148 while (*path != 0 && *path != ')') {
149 if (j > PATH_MAX)
150 goto error;
151 buffer2[j++] = *(path++);
153 buffer2[j] = 0;
154 if (*path == ')') {
155 path++;
156 tmp = getenv(buffer2);
157 } else {
158 tmp = NULL;
160 if (!tmp) {
161 if ((i += strlen(buffer2) + 2) > PATH_MAX)
162 goto error;
163 buffer[i] = 0;
164 if (wstrlcat(buffer, "$(", sizeof(buffer)) >= sizeof(buffer) ||
165 wstrlcat(buffer, buffer2, sizeof(buffer)) >= sizeof(buffer))
166 goto error;
167 if (*(path-1)==')') {
168 if (++i > PATH_MAX ||
169 wstrlcat(buffer, ")", sizeof(buffer)) >= sizeof(buffer))
170 goto error;
172 } else {
173 if ((i += strlen(tmp)) > PATH_MAX ||
174 wstrlcat(buffer, tmp, sizeof(buffer)) >= sizeof(buffer))
175 goto error;
177 } else {
178 j = 0;
179 while (*path != 0 && *path != '/') {
180 if (j > PATH_MAX)
181 goto error;
182 buffer2[j++] = *(path++);
184 buffer2[j] = 0;
185 tmp = getenv(buffer2);
186 if (!tmp) {
187 if ((i += strlen(buffer2) + 1) > PATH_MAX ||
188 wstrlcat(buffer, "$", sizeof(buffer)) >= sizeof(buffer) ||
189 wstrlcat(buffer, buffer2, sizeof(buffer)) >= sizeof(buffer))
190 goto error;
191 } else {
192 if ((i += strlen(tmp)) > PATH_MAX ||
193 wstrlcat(buffer, tmp, sizeof(buffer)) >= sizeof(buffer))
194 goto error;
197 } else {
198 buffer[i++] = *path;
199 path++;
203 if (*path!=0)
204 goto error;
206 return wstrdup(buffer);
208 error:
209 errno = ENAMETOOLONG;
210 werror(_("could not expand %s"), origpath);
212 return NULL;
215 /* return address of next char != tok or end of string whichever comes first */
216 static const char *skipchar(const char *string, char tok)
218 while (*string != 0 && *string == tok)
219 string++;
221 return string;
224 /* return address of next char == tok or end of string whichever comes first */
225 static const char *nextchar(const char *string, char tok)
227 while (*string != 0 && *string != tok)
228 string++;
230 return string;
234 *----------------------------------------------------------------------
235 * findfile--
236 * Finds a file in a : separated list of paths. ~ expansion is also
237 * done.
239 * Returns:
240 * The complete path for the file (in a newly allocated string) or
241 * NULL if the file was not found.
243 * Side effects:
244 * A new string is allocated. It must be freed later.
246 *----------------------------------------------------------------------
248 char *wfindfile(const char *paths, const char *file)
250 char *path;
251 const char *tmp, *tmp2;
252 int len, flen;
253 char *fullpath;
255 if (!file)
256 return NULL;
258 if (*file == '/' || *file == '~' || *file == '$' || !paths || *paths == 0) {
259 if (access(file, F_OK) < 0) {
260 fullpath = wexpandpath(file);
261 if (!fullpath)
262 return NULL;
264 if (access(fullpath, F_OK) < 0) {
265 wfree(fullpath);
266 return NULL;
267 } else {
268 return fullpath;
270 } else {
271 return wstrdup(file);
275 flen = strlen(file);
276 tmp = paths;
277 while (*tmp) {
278 tmp = skipchar(tmp, ':');
279 if (*tmp == 0)
280 break;
281 tmp2 = nextchar(tmp, ':');
282 len = tmp2 - tmp;
283 path = wmalloc(len + flen + 2);
284 path = memcpy(path, tmp, len);
285 path[len] = 0;
286 if (path[len - 1] != '/' &&
287 wstrlcat(path, "/", len + flen + 2) >= len + flen + 2) {
288 wfree(path);
289 return NULL;
292 if (wstrlcat(path, file, len + flen + 2) >= len + flen + 2) {
293 wfree(path);
294 return NULL;
297 fullpath = wexpandpath(path);
298 wfree(path);
300 if (fullpath) {
301 if (access(fullpath, F_OK) == 0) {
302 return fullpath;
304 wfree(fullpath);
306 tmp = tmp2;
309 return NULL;
312 char *wfindfileinlist(char *const *path_list, const char *file)
314 int i;
315 char *path;
316 int len, flen;
317 char *fullpath;
319 if (!file)
320 return NULL;
322 if (*file == '/' || *file == '~' || !path_list) {
323 if (access(file, F_OK) < 0) {
324 fullpath = wexpandpath(file);
325 if (!fullpath)
326 return NULL;
328 if (access(fullpath, F_OK) < 0) {
329 wfree(fullpath);
330 return NULL;
331 } else {
332 return fullpath;
334 } else {
335 return wstrdup(file);
339 flen = strlen(file);
340 for (i = 0; path_list[i] != NULL; i++) {
341 len = strlen(path_list[i]);
342 path = wmalloc(len + flen + 2);
343 path = memcpy(path, path_list[i], len);
344 path[len] = 0;
345 if (wstrlcat(path, "/", len + flen + 2) >= len + flen + 2 ||
346 wstrlcat(path, file, len + flen + 2) >= len + flen + 2) {
347 wfree(path);
348 return NULL;
350 /* expand tilde */
351 fullpath = wexpandpath(path);
352 wfree(path);
353 if (fullpath) {
354 /* check if file exists */
355 if (access(fullpath, F_OK) == 0) {
356 return fullpath;
358 wfree(fullpath);
362 return NULL;
365 char *wfindfileinarray(WMPropList *array, const char *file)
367 int i;
368 char *path;
369 int len, flen;
370 char *fullpath;
372 if (!file)
373 return NULL;
375 if (*file == '/' || *file == '~' || !array) {
376 if (access(file, F_OK) < 0) {
377 fullpath = wexpandpath(file);
378 if (!fullpath)
379 return NULL;
381 if (access(fullpath, F_OK) < 0) {
382 wfree(fullpath);
383 return NULL;
384 } else {
385 return fullpath;
387 } else {
388 return wstrdup(file);
392 flen = strlen(file);
393 for (i = 0; i < WMGetPropListItemCount(array); i++) {
394 WMPropList *prop;
395 char *p;
397 prop = WMGetFromPLArray(array, i);
398 if (!prop)
399 continue;
400 p = WMGetFromPLString(prop);
402 len = strlen(p);
403 path = wmalloc(len + flen + 2);
404 path = memcpy(path, p, len);
405 path[len] = 0;
406 if (wstrlcat(path, "/", len + flen + 2) >= len + flen + 2 ||
407 wstrlcat(path, file, len + flen + 2) >= len + flen + 2) {
408 wfree(path);
409 return NULL;
411 /* expand tilde */
412 fullpath = wexpandpath(path);
413 wfree(path);
414 if (fullpath) {
415 /* check if file exists */
416 if (access(fullpath, F_OK) == 0) {
417 return fullpath;
419 wfree(fullpath);
422 return NULL;
425 int wcopy_file(const char *dir, const char *src_file, const char *dest_file)
427 FILE *src, *dst;
428 size_t nread, nwritten;
429 char *dstpath;
430 struct stat st;
431 char buf[4096];
433 /* only to a directory */
434 if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode))
435 return -1;
436 /* only copy files */
437 if (stat(src_file, &st) != 0 || !S_ISREG(st.st_mode))
438 return -1;
440 do {
441 src = fopen(src_file, "rb");
442 } while ((src == NULL) && (errno == EINTR));
443 if (src == NULL) {
444 werror(_("Could not open input file \"%s\""), src_file);
445 return -1;
448 dstpath = wstrconcat(dir, dest_file);
449 do {
450 dst = fopen(dstpath, "wb");
451 } while ((dst == NULL) && (errno == EINTR));
452 if (dst == NULL) {
453 werror(_("Could not create target file \"%s\""), dstpath);
454 wfree(dstpath);
455 fclose(src);
456 return -1;
459 do {
460 nread = fread(buf, 1, sizeof(buf), src);
461 if (ferror(src))
462 break;
464 nwritten = fwrite(buf, 1, nread, dst);
465 if (ferror(dst) || feof(src) || nread != nwritten)
466 break;
468 } while (1);
470 if (ferror(src) || ferror(dst))
471 unlink(dstpath);
473 fclose(src);
474 fchmod(fileno(dst), st.st_mode);
475 fsync(fileno(dst));
476 if (fclose(dst))
477 wwarning("error occured during fclose(\"%s\")", dstpath);
478 wfree(dstpath);
480 return 0;