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.
38 char *home
= getenv("HOME");
44 user
= getpwuid(getuid());
46 wsyserror(_("could not get password entry for UID %i"), getuid());
56 static char *getuserhomedir(const char *username
)
60 user
= getpwnam(username
);
62 wsyserror(_("could not get password entry for user %s"), username
);
72 char *wexpandpath(char *path
)
74 char *origpath
= path
;
75 char buffer2
[PATH_MAX
+ 2];
76 char buffer
[PATH_MAX
+ 2];
79 memset(buffer
, 0, PATH_MAX
+ 2);
85 if (*path
== '/' || *path
== 0) {
87 if (strlen(home
) > PATH_MAX
)
93 while (*path
!= 0 && *path
!= '/') {
100 home
= getuserhomedir(buffer2
);
101 if (!home
|| strlen(home
) > PATH_MAX
)
103 strcat(buffer
, home
);
109 while (*path
!= 0 && i
<= PATH_MAX
) {
115 /* expand $(HOME) or $HOME style environment variables */
118 while (*path
!= 0 && *path
!= ')') {
121 buffer2
[j
++] = *(path
++);
126 tmp
= getenv(buffer2
);
131 if ((i
+= strlen(buffer2
) + 2) > PATH_MAX
)
134 strcat(buffer
, "$(");
135 strcat(buffer
, buffer2
);
136 if (*(path
-1)==')') {
142 if ((i
+= strlen(tmp
)) > PATH_MAX
)
147 while (*path
!= 0 && *path
!= '/') {
150 buffer2
[j
++] = *(path
++);
153 tmp
= getenv(buffer2
);
155 if ((i
+= strlen(buffer2
) + 1) > PATH_MAX
)
158 strcat(buffer
, buffer2
);
160 if ((i
+= strlen(tmp
)) > PATH_MAX
)
174 return wstrdup(buffer
);
177 errno
= ENAMETOOLONG
;
178 wsyserror(_("could not expand %s"), origpath
);
179 /* FIXME: too many functions handle a return value of NULL incorrectly */
183 /* return address of next char != tok or end of string whichever comes first */
184 static char *skipchar(char *string
, char tok
)
186 while (*string
!= 0 && *string
== tok
)
192 /* return address of next char == tok or end of string whichever comes first */
193 static char *nextchar(char *string
, char tok
)
195 while (*string
!= 0 && *string
!= tok
)
202 *----------------------------------------------------------------------
204 * Finds a file in a : separated list of paths. ~ expansion is also
208 * The complete path for the file (in a newly allocated string) or
209 * NULL if the file was not found.
212 * A new string is allocated. It must be freed later.
214 *----------------------------------------------------------------------
216 char *wfindfile(char *paths
, char *file
)
226 if (*file
== '/' || *file
== '~' || *file
== '$' || !paths
|| *paths
== 0) {
227 if (access(file
, F_OK
) < 0) {
228 fullpath
= wexpandpath(file
);
232 if (access(fullpath
, F_OK
) < 0) {
239 return wstrdup(file
);
246 tmp
= skipchar(tmp
, ':');
249 tmp2
= nextchar(tmp
, ':');
251 path
= wmalloc(len
+ flen
+ 2);
252 path
= memcpy(path
, tmp
, len
);
254 if (path
[len
- 1] != '/')
257 fullpath
= wexpandpath(path
);
260 if (access(fullpath
, F_OK
) == 0) {
271 char *wfindfileinlist(char **path_list
, char *file
)
281 if (*file
== '/' || *file
== '~' || !path_list
) {
282 if (access(file
, F_OK
) < 0) {
283 fullpath
= wexpandpath(file
);
287 if (access(fullpath
, F_OK
) < 0) {
294 return wstrdup(file
);
299 for (i
= 0; path_list
[i
] != NULL
; i
++) {
300 len
= strlen(path_list
[i
]);
301 path
= wmalloc(len
+ flen
+ 2);
302 path
= memcpy(path
, path_list
[i
], len
);
307 fullpath
= wexpandpath(path
);
310 /* check if file exists */
311 if (access(fullpath
, F_OK
) == 0) {
320 char *wfindfileinarray(WMPropList
* array
, char *file
)
330 if (*file
== '/' || *file
== '~' || !array
) {
331 if (access(file
, F_OK
) < 0) {
332 fullpath
= wexpandpath(file
);
336 if (access(fullpath
, F_OK
) < 0) {
343 return wstrdup(file
);
348 for (i
= 0; i
< WMGetPropListItemCount(array
); i
++) {
352 prop
= WMGetFromPLArray(array
, i
);
355 p
= WMGetFromPLString(prop
);
358 path
= wmalloc(len
+ flen
+ 2);
359 path
= memcpy(path
, p
, len
);
364 fullpath
= wexpandpath(path
);
367 /* check if file exists */
368 if (access(fullpath
, F_OK
) == 0) {