3
/* pathfind.c --- find a FILE MODE along PATH */
6
* Author: Gary V Vaughan <gvaughan@oranda.demon.co.uk>
7
* Created: Tue Jun 24 15:07:31 1997
8
* Last Modified: $Date: 2006/09/24 02:11:16 $
11
* $Id: pathfind.c,v 4.9 2006/09/24 02:11:16 bkorb Exp $
18
#if defined(__windows__) && !defined(__CYGWIN__)
20
pathfind( char const* path,
28
static char* make_absolute( char const *string, char const *dot_path );
29
static char* canonicalize_pathname( char *path );
30
static char* extract_colon_unit( char* dir, char const *string, int *p_index );
33
/*=export_func pathfind
35
* what: fild a file in a list of directories
37
* ifndef: HAVE_PATHFIND
39
* arg: + char const* + path + colon separated list of search directories +
40
* arg: + char const* + file + the name of the file to look for +
41
* arg: + char const* + mode + the mode bits that must be set to match +
44
* ret_desc: the path to the located file
48
* pathfind looks for a a file with name "FILE" and "MODE" access
49
* along colon delimited "PATH", and returns the full pathname as a
50
* string, or NULL if not found. If "FILE" contains a slash, then
51
* it is treated as a relative or absolute path and "PATH" is ignored.
53
* @strong{NOTE}: this function is compiled into @file{libopts} only if
54
* it is not natively supplied.
56
* The "MODE" argument is a string of option letters chosen from the
63
* f normal file (NOT IMPLEMENTED)
64
* b block special (NOT IMPLEMENTED)
65
* c character special (NOT IMPLEMENTED)
66
* d directory (NOT IMPLEMENTED)
67
* p FIFO (pipe) (NOT IMPLEMENTED)
68
* u set user ID bit (NOT IMPLEMENTED)
69
* g set group ID bit (NOT IMPLEMENTED)
70
* k sticky bit (NOT IMPLEMENTED)
71
* s size nonzero (NOT IMPLEMENTED)
75
* To find the "ls" command using the "PATH" environment variable:
78
* char* pz_ls = pathfind( getenv("PATH"), "ls", "rx" );
79
* <<do whatever with pz_ls>>
82
* The path is allocated with @code{malloc(3C)}, so you must @code{free(3C)}
83
* the result. Also, do not use unimplemented file modes. :-)
85
* err: returns NULL if the file is not found.
88
pathfind( char const* path,
94
char* pathName = NULL;
95
char zPath[ MAXPATHLEN + 1 ];
97
if (strchr( mode, 'r' )) mode_bits |= R_OK;
98
if (strchr( mode, 'w' )) mode_bits |= W_OK;
99
if (strchr( mode, 'x' )) mode_bits |= X_OK;
102
* FOR each non-null entry in the colon-separated path, DO ...
106
char* colon_unit = extract_colon_unit( zPath, path, &p_index );
109
* IF no more entries, THEN quit
111
if (colon_unit == NULL)
114
dirP = opendir( colon_unit );
117
* IF the directory is inaccessable, THEN next directory
123
* FOR every entry in the given directory, ...
126
struct dirent *entP = readdir( dirP );
128
if (entP == (struct dirent*)NULL)
132
* IF the file name matches the one we are looking for, ...
134
if (strcmp( entP->d_name, fileName ) == 0) {
135
char* pzFullName = make_absolute( fileName, colon_unit);
138
* Make sure we can access it in the way we want
140
if (access( pzFullName, mode_bits ) >= 0) {
142
* We can, so normalize the name and return it below
144
pathName = canonicalize_pathname( pzFullName );
147
free( (void*)pzFullName );
154
if (pathName != NULL)
162
* Turn STRING (a pathname) into an absolute pathname, assuming that
163
* DOT_PATH contains the symbolic location of `.'. This always returns
164
* a new string, even if STRING was an absolute pathname to begin with.
167
make_absolute( char const *string, char const *dot_path )
172
if (!dot_path || *string == '/') {
173
result = strdup( string );
175
if (dot_path && dot_path[0]) {
176
result = malloc( 2 + strlen( dot_path ) + strlen( string ) );
177
strcpy( result, dot_path );
178
result_len = strlen( result );
179
if (result[result_len - 1] != '/') {
180
result[result_len++] = '/';
181
result[result_len] = '\0';
184
result = malloc( 3 + strlen( string ) );
185
result[0] = '.'; result[1] = '/'; result[2] = '\0';
189
strcpy( result + result_len, string );
196
* Canonicalize PATH, and return a new path. The new path differs from
199
* Multiple `/'s are collapsed to a single `/'.
200
* Leading `./'s are removed.
201
* Trailing `/.'s are removed.
202
* Trailing `/'s are removed.
203
* Non-leading `../'s and trailing `..'s are handled by removing
204
* portions of the path.
207
canonicalize_pathname( char *path )
210
char stub_char, *result;
212
/* The result cannot be larger than the input PATH. */
213
result = strdup( path );
215
stub_char = (*path == '/') ? '/' : '.';
217
/* Walk along RESULT looking for things to compact. */
220
while (result[i] != '\0' && result[i] != '/')
225
/* If we didn't find any slashes, then there is nothing left to
231
/* Handle multiple `/'s in a row. */
232
while (result[i] == '/')
235
#if !defined (apollo)
236
if ((start + 1) != i)
238
if ((start + 1) != i && (start != 0 || i != 2))
241
strcpy( result + start + 1, result + i );
245
/* Handle backquoted `/'. */
246
if (start > 0 && result[start - 1] == '\\')
249
/* Check for trailing `/', and `.' by itself. */
250
if ((start && !result[i])
251
|| (result[i] == '.' && !result[i+1])) {
256
/* Check for `../', `./' or trailing `.' by itself. */
257
if (result[i] == '.') {
259
if (result[i + 1] == '/') {
260
strcpy( result + i, result + i + 1 );
261
i = (start < 0) ? 0 : start;
265
/* Handle `../' or trailing `..' by itself. */
266
if (result[i + 1] == '.' &&
267
(result[i + 2] == '/' || !result[i + 2])) {
268
while (--start > -1 && result[start] != '/')
270
strcpy( result + start + 1, result + i + 2 );
271
i = (start < 0) ? 0 : start;
286
* Given a string containing units of information separated by colons,
287
* return the next one pointed to by (P_INDEX), or NULL if there are no
288
* more. Advance (P_INDEX) to the character after the colon.
291
extract_colon_unit( char* pzDir, char const *string, int *p_index )
293
char* pzDest = pzDir;
299
if ((unsigned)ix >= strlen( string ))
303
char const* pzSrc = string + ix;
305
while (*pzSrc == ':') pzSrc++;
308
char ch = (*(pzDest++) = *(pzSrc++));
316
if ((pzDest - pzDir) >= MAXPATHLEN)
329
#endif /* __windows__ / __CYGWIN__ */
330
#endif /* HAVE_PATHFIND */
335
* c-file-style: "stroustrup"
336
* indent-tabs-mode: nil
338
* end of compat/pathfind.c */