Updating to version 0.20.2
[wmaker-crm.git] / WINGs / findfile.c
blobe0f7453a14a8d9bf8a6ba23de3fc37e662eba7b8
1 /*
2 * Window Maker miscelaneous function library
3 *
4 * Copyright (c) 1997 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 "../src/config.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);
165 *----------------------------------------------------------------------
166 * findfile--
167 * Finds a file in a : separated list of paths. ~ expansion is also
168 * done.
170 * Returns:
171 * The complete path for the file (in a newly allocated string) or
172 * NULL if the file was not found.
174 * Side effects:
175 * A new string is allocated. It must be freed later.
177 *----------------------------------------------------------------------
179 char*
180 wfindfile(char *paths, char *file)
182 char *path;
183 char *tmp;
184 int done;
185 int len, flen;
186 char *fullpath;
188 if (!file)
189 return NULL;
191 if (*file=='/' || *file=='~' || *file=='$' || !paths) {
192 if (access(file, R_OK)<0) {
193 fullpath = wexpandpath(file);
194 if (!fullpath)
195 return NULL;
197 if (access(fullpath, R_OK)<0) {
198 free(fullpath);
199 return NULL;
200 } else {
201 return fullpath;
203 } else {
204 return wstrdup(file);
208 flen = strlen(file);
209 tmp = paths;
210 done = 0;
211 while (!done) {
212 len = strcspn(tmp, ":");
213 if (len==0) done=1;
214 path = wmalloc(len+flen+2);
215 path = memcpy(path, tmp, len);
216 path[len]=0;
217 strcat(path, "/");
218 strcat(path, file);
219 fullpath = wexpandpath(path);
220 free(path);
221 if (fullpath) {
222 if (access(fullpath, R_OK)==0) {
223 return fullpath;
225 free(fullpath);
227 tmp=&(tmp[len+1]);
228 if (*tmp==0) break;
230 return NULL;
234 char*
235 wfindfileinlist(char **path_list, char *file)
237 int i;
238 char *path;
239 int len, flen;
240 char *fullpath;
242 if (!file)
243 return NULL;
245 if (*file=='/' || *file=='~' || !path_list) {
246 if (access(file, R_OK)<0) {
247 fullpath = wexpandpath(file);
248 if (!fullpath)
249 return NULL;
251 if (access(fullpath, R_OK)<0) {
252 free(fullpath);
253 return NULL;
254 } else {
255 return fullpath;
257 } else {
258 return wstrdup(file);
262 flen = strlen(file);
263 for (i=0; path_list[i]!=NULL; i++) {
264 len = strlen(path_list[i]);
265 path = wmalloc(len+flen+2);
266 path = memcpy(path, path_list[i], len);
267 path[len]=0;
268 strcat(path, "/");
269 strcat(path, file);
270 /* expand tilde */
271 fullpath = wexpandpath(path);
272 free(path);
273 if (fullpath) {
274 /* check if file is readable */
275 if (access(fullpath, R_OK)==0) {
276 return fullpath;
278 free(fullpath);
281 return NULL;