2 * Window Maker miscelaneous function library
4 * Copyright (c) 1997 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.
22 #include "../src/config.h"
42 char *home
= getenv("HOME");
48 user
= getpwuid(getuid());
50 wsyserror(_("could not get password entry for UID %i"), getuid());
62 getuserhomedir(char *username
)
66 user
= getpwnam(username
);
68 wsyserror(_("could not get password entry for user %s"), username
);
82 wexpandpath(char *path
)
84 char buffer2
[PATH_MAX
+2];
85 char buffer
[PATH_MAX
+2];
88 memset(buffer
, 0, PATH_MAX
+2);
94 if (*path
=='/' || *path
==0) {
100 while (*path
!=0 && *path
!='/') {
101 buffer2
[j
++] = *path
;
105 home
= getuserhomedir(buffer2
);
108 strcat(buffer
, home
);
120 /* expand $(HOME) or $HOME style environment variables */
123 while (*path
!=0 && *path
!=')') {
124 buffer2
[j
++] = *(path
++);
129 tmp
= getenv(buffer2
);
132 strcat(buffer
, "$(");
133 strcat(buffer
, buffer2
);
135 i
+= strlen(buffer2
)+3;
141 while (*path
!=0 && *path
!='/') {
142 buffer2
[j
++] = *(path
++);
145 tmp
= getenv(buffer2
);
148 strcat(buffer
, buffer2
);
149 i
+= strlen(buffer2
)+1;
161 return wstrdup(buffer
);
167 *----------------------------------------------------------------------
169 * Finds a file in a : separated list of paths. ~ expansion is also
173 * The complete path for the file (in a newly allocated string) or
174 * NULL if the file was not found.
177 * A new string is allocated. It must be freed later.
179 *----------------------------------------------------------------------
182 wfindfile(char *paths
, char *file
)
193 if (*file
=='/' || *file
=='~' || *file
=='$' || !paths
) {
194 if (access(file
, F_OK
)<0) {
195 fullpath
= wexpandpath(file
);
199 if (access(fullpath
, F_OK
)<0) {
206 return wstrdup(file
);
214 len
= strcspn(tmp
, ":");
216 path
= wmalloc(len
+flen
+2);
217 path
= memcpy(path
, tmp
, len
);
221 fullpath
= wexpandpath(path
);
224 if (access(fullpath
, F_OK
)==0) {
237 wfindfileinlist(char **path_list
, char *file
)
247 if (*file
=='/' || *file
=='~' || !path_list
) {
248 if (access(file
, F_OK
)<0) {
249 fullpath
= wexpandpath(file
);
253 if (access(fullpath
, F_OK
)<0) {
260 return wstrdup(file
);
265 for (i
=0; path_list
[i
]!=NULL
; i
++) {
266 len
= strlen(path_list
[i
]);
267 path
= wmalloc(len
+flen
+2);
268 path
= memcpy(path
, path_list
[i
], len
);
273 fullpath
= wexpandpath(path
);
276 /* check if file exists */
277 if (access(fullpath
, F_OK
)==0) {
289 wfindfileinarray(proplist_t array
, char *file
)
299 if (*file
=='/' || *file
=='~' || !array
) {
300 if (access(file
, F_OK
)<0) {
301 fullpath
= wexpandpath(file
);
305 if (access(fullpath
, F_OK
)<0) {
312 return wstrdup(file
);
317 for (i
=0; i
<PLGetNumberOfElements(array
); i
++) {
321 prop
= PLGetArrayElement(array
, i
);
324 p
= PLGetString(prop
);
327 path
= wmalloc(len
+flen
+2);
328 path
= memcpy(path
, p
, len
);
333 fullpath
= wexpandpath(path
);
336 /* check if file exists */
337 if (access(fullpath
, F_OK
)==0) {