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.
37 char *home = getenv("HOME");
43 user = getpwuid(getuid());
45 wsyserror(_("could not get password entry for UID %i"), getuid());
55 static char *getuserhomedir(char *username)
59 user = getpwnam(username);
61 wsyserror(_("could not get password entry for user %s"), username);
71 char *wexpandpath(char *path)
73 char buffer2[PATH_MAX + 2];
74 char buffer[PATH_MAX + 2];
77 memset(buffer, 0, PATH_MAX + 2);
83 if (*path == '/' || *path == 0) {
89 while (*path != 0 && *path != '/') {
94 home = getuserhomedir(buffer2);
109 /* expand $(HOME) or $HOME style environment variables */
112 while (*path != 0 && *path != ')') {
113 buffer2[j++] = *(path++);
118 tmp = getenv(buffer2);
121 strcat(buffer, "$(");
122 strcat(buffer, buffer2);
124 i += strlen(buffer2) + 3;
130 while (*path != 0 && *path != '/') {
131 buffer2[j++] = *(path++);
134 tmp = getenv(buffer2);
137 strcat(buffer, buffer2);
138 i += strlen(buffer2) + 1;
150 return wstrdup(buffer);
153 /* return address of next char != tok or end of string whichever comes first */
154 static char *skipchar(char *string, char tok)
156 while (*string != 0 && *string == tok)
162 /* return address of next char == tok or end of string whichever comes first */
163 static char *nextchar(char *string, char tok)
165 while (*string != 0 && *string != tok)
172 *----------------------------------------------------------------------
174 * Finds a file in a : separated list of paths. ~ expansion is also
178 * The complete path for the file (in a newly allocated string) or
179 * NULL if the file was not found.
182 * A new string is allocated. It must be freed later.
184 *----------------------------------------------------------------------
186 char *wfindfile(char *paths, char *file)
196 if (*file == '/' || *file == '~' || *file == '$' || !paths || *paths == 0) {
197 if (access(file, F_OK) < 0) {
198 fullpath = wexpandpath(file);
202 if (access(fullpath, F_OK) < 0) {
209 return wstrdup(file);
216 tmp = skipchar(tmp, ':');
219 tmp2 = nextchar(tmp, ':');
221 path = wmalloc(len + flen + 2);
222 path = memcpy(path, tmp, len);
224 if (path[len - 1] != '/')
227 fullpath = wexpandpath(path);
230 if (access(fullpath, F_OK) == 0) {
241 char *wfindfileinlist(char **path_list, char *file)
251 if (*file == '/' || *file == '~' || !path_list) {
252 if (access(file, F_OK) < 0) {
253 fullpath = wexpandpath(file);
257 if (access(fullpath, F_OK) < 0) {
264 return wstrdup(file);
269 for (i = 0; path_list[i] != NULL; i++) {
270 len = strlen(path_list[i]);
271 path = wmalloc(len + flen + 2);
272 path = memcpy(path, path_list[i], len);
277 fullpath = wexpandpath(path);
280 /* check if file exists */
281 if (access(fullpath, F_OK) == 0) {
290 char *wfindfileinarray(WMPropList * array, char *file)
300 if (*file == '/' || *file == '~' || !array) {
301 if (access(file, F_OK) < 0) {
302 fullpath = wexpandpath(file);
306 if (access(fullpath, F_OK) < 0) {
313 return wstrdup(file);
318 for (i = 0; i < WMGetPropListItemCount(array); i++) {
322 prop = WMGetFromPLArray(array, i);
325 p = WMGetFromPLString(prop);
328 path = wmalloc(len + flen + 2);
329 path = memcpy(path, p, len);
334 fullpath = wexpandpath(path);
337 /* check if file exists */
338 if (access(fullpath, F_OK) == 0) {