5 /* pathfind.c --- find a FILE MODE along PATH */
8 * Author: Gary V Vaughan <gvaughan@oranda.demon.co.uk>
9 * Time-stamp: "2006-09-23 19:46:16 bkorb"
12 * Id: 8ce7ddfe2378f0b75c91c0ab348a6ad81634fb01
19 #if defined(__windows__) && !defined(__CYGWIN__)
21 pathfind( char const* path
,
29 static char* make_absolute( char const *string
, char const *dot_path
);
30 static char* canonicalize_pathname( char *path
);
31 static char* extract_colon_unit( char* dir
, char const *string
, int *p_index
);
34 /*=export_func pathfind
36 * what: fild a file in a list of directories
38 * ifndef: HAVE_PATHFIND
40 * arg: + char const* + path + colon separated list of search directories +
41 * arg: + char const* + file + the name of the file to look for +
42 * arg: + char const* + mode + the mode bits that must be set to match +
45 * ret_desc: the path to the located file
49 * pathfind looks for a a file with name "FILE" and "MODE" access
50 * along colon delimited "PATH", and returns the full pathname as a
51 * string, or NULL if not found. If "FILE" contains a slash, then
52 * it is treated as a relative or absolute path and "PATH" is ignored.
54 * @strong{NOTE}: this function is compiled into @file{libopts} only if
55 * it is not natively supplied.
57 * The "MODE" argument is a string of option letters chosen from the
64 * f normal file (NOT IMPLEMENTED)
65 * b block special (NOT IMPLEMENTED)
66 * c character special (NOT IMPLEMENTED)
67 * d directory (NOT IMPLEMENTED)
68 * p FIFO (pipe) (NOT IMPLEMENTED)
69 * u set user ID bit (NOT IMPLEMENTED)
70 * g set group ID bit (NOT IMPLEMENTED)
71 * k sticky bit (NOT IMPLEMENTED)
72 * s size nonzero (NOT IMPLEMENTED)
76 * To find the "ls" command using the "PATH" environment variable:
79 * char* pz_ls = pathfind( getenv("PATH"), "ls", "rx" );
80 * <<do whatever with pz_ls>>
83 * The path is allocated with @code{malloc(3C)}, so you must @code{free(3C)}
84 * the result. Also, do not use unimplemented file modes. :-)
86 * err: returns NULL if the file is not found.
89 pathfind( char const* path
,
95 char* pathName
= NULL
;
96 char zPath
[ AG_PATH_MAX
+ 1 ];
98 if (strchr( mode
, 'r' )) mode_bits
|= R_OK
;
99 if (strchr( mode
, 'w' )) mode_bits
|= W_OK
;
100 if (strchr( mode
, 'x' )) mode_bits
|= X_OK
;
103 * FOR each non-null entry in the colon-separated path, DO ...
107 char* colon_unit
= extract_colon_unit( zPath
, path
, &p_index
);
110 * IF no more entries, THEN quit
112 if (colon_unit
== NULL
)
115 dirP
= opendir( colon_unit
);
118 * IF the directory is inaccessable, THEN next directory
124 * FOR every entry in the given directory, ...
127 struct dirent
*entP
= readdir( dirP
);
129 if (entP
== (struct dirent
*)NULL
)
133 * IF the file name matches the one we are looking for, ...
135 if (strcmp( entP
->d_name
, fileName
) == 0) {
136 char* pzFullName
= make_absolute( fileName
, colon_unit
);
139 * Make sure we can access it in the way we want
141 if (access( pzFullName
, mode_bits
) >= 0) {
143 * We can, so normalize the name and return it below
145 pathName
= canonicalize_pathname( pzFullName
);
148 free( (void*)pzFullName
);
155 if (pathName
!= NULL
)
163 * Turn STRING (a pathname) into an absolute pathname, assuming that
164 * DOT_PATH contains the symbolic location of `.'. This always returns
165 * a new string, even if STRING was an absolute pathname to begin with.
168 make_absolute( char const *string
, char const *dot_path
)
173 if (!dot_path
|| *string
== '/') {
174 result
= strdup( string
);
176 if (dot_path
&& dot_path
[0]) {
177 result
= malloc( 2 + strlen( dot_path
) + strlen( string
) );
178 strcpy( result
, dot_path
);
179 result_len
= strlen( result
);
180 if (result
[result_len
- 1] != '/') {
181 result
[result_len
++] = '/';
182 result
[result_len
] = '\0';
185 result
= malloc( 3 + strlen( string
) );
186 result
[0] = '.'; result
[1] = '/'; result
[2] = '\0';
190 strcpy( result
+ result_len
, string
);
197 * Canonicalize PATH, and return a new path. The new path differs from
200 * Multiple `/'s are collapsed to a single `/'.
201 * Leading `./'s are removed.
202 * Trailing `/.'s are removed.
203 * Trailing `/'s are removed.
204 * Non-leading `../'s and trailing `..'s are handled by removing
205 * portions of the path.
208 canonicalize_pathname( char *path
)
211 char stub_char
, *result
;
213 /* The result cannot be larger than the input PATH. */
214 result
= strdup( path
);
216 stub_char
= (*path
== '/') ? '/' : '.';
218 /* Walk along RESULT looking for things to compact. */
221 while (result
[i
] != '\0' && result
[i
] != '/')
226 /* If we didn't find any slashes, then there is nothing left to
232 /* Handle multiple `/'s in a row. */
233 while (result
[i
] == '/')
236 #if !defined (apollo)
237 if ((start
+ 1) != i
)
239 if ((start
+ 1) != i
&& (start
!= 0 || i
!= 2))
242 strcpy( result
+ start
+ 1, result
+ i
);
246 /* Handle backquoted `/'. */
247 if (start
> 0 && result
[start
- 1] == '\\')
250 /* Check for trailing `/', and `.' by itself. */
251 if ((start
&& !result
[i
])
252 || (result
[i
] == '.' && !result
[i
+1])) {
257 /* Check for `../', `./' or trailing `.' by itself. */
258 if (result
[i
] == '.') {
260 if (result
[i
+ 1] == '/') {
261 strcpy( result
+ i
, result
+ i
+ 1 );
262 i
= (start
< 0) ? 0 : start
;
266 /* Handle `../' or trailing `..' by itself. */
267 if (result
[i
+ 1] == '.' &&
268 (result
[i
+ 2] == '/' || !result
[i
+ 2])) {
269 while (--start
> -1 && result
[start
] != '/')
271 strcpy( result
+ start
+ 1, result
+ i
+ 2 );
272 i
= (start
< 0) ? 0 : start
;
287 * Given a string containing units of information separated by colons,
288 * return the next one pointed to by (P_INDEX), or NULL if there are no
289 * more. Advance (P_INDEX) to the character after the colon.
292 extract_colon_unit( char* pzDir
, char const *string
, int *p_index
)
294 char* pzDest
= pzDir
;
300 if ((unsigned)ix
>= strlen( string
))
304 char const* pzSrc
= string
+ ix
;
306 while (*pzSrc
== ':') pzSrc
++;
309 char ch
= (*(pzDest
++) = *(pzSrc
++));
317 if ((pzDest
- pzDir
) >= AG_PATH_MAX
)
330 #endif /* __windows__ / __CYGWIN__ */
331 #endif /* HAVE_PATHFIND */
336 * c-file-style: "stroustrup"
337 * indent-tabs-mode: nil
339 * end of compat/pathfind.c */