2 * Window Maker miscelaneous function library
4 * Copyright (c) 1997-2002 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.
40 char *home
= getenv("HOME");
46 user
= getpwuid(getuid());
48 wsyserror(_("could not get password entry for UID %i"), getuid());
60 getuserhomedir(char *username
)
64 user
= getpwnam(username
);
66 wsyserror(_("could not get password entry for user %s"), username
);
80 wexpandpath(char *path
)
82 char buffer2
[PATH_MAX
+2];
83 char buffer
[PATH_MAX
+2];
86 memset(buffer
, 0, PATH_MAX
+2);
92 if (*path
=='/' || *path
==0) {
98 while (*path
!=0 && *path
!='/') {
103 home
= getuserhomedir(buffer2
);
106 strcat(buffer
, home
);
118 /* expand $(HOME) or $HOME style environment variables */
121 while (*path
!=0 && *path
!=')') {
122 buffer2
[j
++] = *(path
++);
127 tmp
= getenv(buffer2
);
130 strcat(buffer
, "$(");
131 strcat(buffer
, buffer2
);
133 i
+= strlen(buffer2
)+3;
139 while (*path
!=0 && *path
!='/') {
140 buffer2
[j
++] = *(path
++);
143 tmp
= getenv(buffer2
);
146 strcat(buffer
, buffer2
);
147 i
+= strlen(buffer2
)+1;
159 return wstrdup(buffer
);
163 /* return address of next char != tok or end of string whichever comes first */
165 skipchar(char *string
, char tok
)
167 while(*string
!=0 && *string
==tok
)
174 /* return address of next char == tok or end of string whichever comes first */
176 nextchar(char *string
, char tok
)
178 while(*string
!=0 && *string
!=tok
)
186 *----------------------------------------------------------------------
188 * Finds a file in a : separated list of paths. ~ expansion is also
192 * The complete path for the file (in a newly allocated string) or
193 * NULL if the file was not found.
196 * A new string is allocated. It must be freed later.
198 *----------------------------------------------------------------------
201 wfindfile(char *paths
, char *file
)
211 if (*file
=='/' || *file
=='~' || *file
=='$' || !paths
|| *paths
==0) {
212 if (access(file
, F_OK
)<0) {
213 fullpath
= wexpandpath(file
);
217 if (access(fullpath
, F_OK
)<0) {
224 return wstrdup(file
);
231 tmp
= skipchar(tmp
, ':');
234 tmp2
= nextchar(tmp
, ':');
236 path
= wmalloc(len
+flen
+2);
237 path
= memcpy(path
, tmp
, len
);
239 if (path
[len
-1] != '/')
242 fullpath
= wexpandpath(path
);
245 if (access(fullpath
, F_OK
)==0) {
258 wfindfileinlist(char **path_list
, char *file
)
268 if (*file
=='/' || *file
=='~' || !path_list
) {
269 if (access(file
, F_OK
)<0) {
270 fullpath
= wexpandpath(file
);
274 if (access(fullpath
, F_OK
)<0) {
281 return wstrdup(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
);
294 fullpath
= wexpandpath(path
);
297 /* check if file exists */
298 if (access(fullpath
, F_OK
)==0) {
310 wfindfileinarray(WMPropList
*array
, char *file
)
320 if (*file
=='/' || *file
=='~' || !array
) {
321 if (access(file
, F_OK
)<0) {
322 fullpath
= wexpandpath(file
);
326 if (access(fullpath
, F_OK
)<0) {
333 return wstrdup(file
);
338 for (i
=0; i
<WMGetPropListItemCount(array
); i
++) {
342 prop
= WMGetFromPLArray(array
, i
);
345 p
= WMGetFromPLString(prop
);
348 path
= wmalloc(len
+flen
+2);
349 path
= memcpy(path
, p
, len
);
354 fullpath
= wexpandpath(path
);
357 /* check if file exists */
358 if (access(fullpath
, F_OK
)==0) {