3 /* pathfind.c --- find a FILE MODE along PATH */
5 /* Author: Gary V Vaughan <gvaughan@oranda.demon.co.uk> */
10 pathfind( char const * path,
16 #if defined(__windows__) && !defined(__CYGWIN__)
18 pathfind( char const * path,
26 static char * make_absolute(char const * string, char const * dot_path);
27 static char * canonicalize_pathname(char * path);
28 static char * extract_colon_unit(char * dir, char const * string, int * p_index);
31 * local implementation of pathfind.
32 * @param[in] path colon separated list of directories
33 * @param[in] fname the name we are hunting for
34 * @param[in] mode the required file mode
35 * @returns an allocated string with the full path, or NULL
38 pathfind( char const * path,
44 char * res_path = NULL;
45 char zPath[ AG_PATH_MAX + 1 ];
47 if (strchr( mode, 'r' )) mode_bits |= R_OK;
48 if (strchr( mode, 'w' )) mode_bits |= W_OK;
49 if (strchr( mode, 'x' )) mode_bits |= X_OK;
52 * FOR each non-null entry in the colon-separated path, DO ...
56 char * colon_unit = extract_colon_unit( zPath, path, &p_index );
58 if (colon_unit == NULL)
61 dirP = opendir( colon_unit );
64 * IF the directory is inaccessable, THEN next directory
70 struct dirent *entP = readdir( dirP );
72 if (entP == (struct dirent *)NULL)
76 * IF the file name matches the one we are looking for, ...
78 if (strcmp(entP->d_name, fname) == 0) {
79 char * abs_name = make_absolute(fname, colon_unit);
82 * Make sure we can access it in the way we want
84 if (access(abs_name, mode_bits) >= 0) {
86 * We can, so normalize the name and return it below
88 res_path = canonicalize_pathname(abs_name);
106 * Turn STRING (a pathname) into an absolute pathname, assuming that
107 * DOT_PATH contains the symbolic location of `.'. This always returns
108 * a new string, even if STRING was an absolute pathname to begin with.
111 make_absolute( char const * string, char const * dot_path )
116 if (!dot_path || *string == '/') {
117 result = strdup( string );
119 if (dot_path && dot_path[0]) {
120 result = malloc( 2 + strlen( dot_path ) + strlen( string ) );
121 strcpy( result, dot_path );
122 result_len = (int)strlen(result);
123 if (result[result_len - 1] != '/') {
124 result[result_len++] = '/';
125 result[result_len] = '\0';
128 result = malloc( 3 + strlen( string ) );
129 result[0] = '.'; result[1] = '/'; result[2] = '\0';
133 strcpy( result + result_len, string );
140 * Canonicalize PATH, and return a new path. The new path differs from
143 * Multiple `/'s are collapsed to a single `/'.
144 * Leading `./'s are removed.
145 * Trailing `/.'s are removed.
146 * Trailing `/'s are removed.
147 * Non-leading `../'s and trailing `..'s are handled by removing
148 * portions of the path.
151 canonicalize_pathname( char *path )
154 char stub_char, *result;
156 /* The result cannot be larger than the input PATH. */
157 result = strdup( path );
159 stub_char = (*path == '/') ? '/' : '.';
161 /* Walk along RESULT looking for things to compact. */
164 while (result[i] != '\0' && result[i] != '/')
169 /* If we didn't find any slashes, then there is nothing left to
175 /* Handle multiple `/'s in a row. */
176 while (result[i] == '/')
179 #if !defined (apollo)
180 if ((start + 1) != i)
182 if ((start + 1) != i && (start != 0 || i != 2))
185 strcpy( result + start + 1, result + i );
189 /* Handle backquoted `/'. */
190 if (start > 0 && result[start - 1] == '\\')
193 /* Check for trailing `/', and `.' by itself. */
194 if ((start && !result[i])
195 || (result[i] == '.' && !result[i+1])) {
200 /* Check for `../', `./' or trailing `.' by itself. */
201 if (result[i] == '.') {
203 if (result[i + 1] == '/') {
204 strcpy( result + i, result + i + 1 );
205 i = (start < 0) ? 0 : start;
209 /* Handle `../' or trailing `..' by itself. */
210 if (result[i + 1] == '.' &&
211 (result[i + 2] == '/' || !result[i + 2])) {
212 while (--start > -1 && result[start] != '/')
214 strcpy( result + start + 1, result + i + 2 );
215 i = (start < 0) ? 0 : start;
230 * Given a string containing units of information separated by colons,
231 * return the next one pointed to by (P_INDEX), or NULL if there are no
232 * more. Advance (P_INDEX) to the character after the colon.
235 extract_colon_unit(char * pzDir, char const * string, int * p_index)
237 char * pzDest = pzDir;
243 if ((unsigned)ix >= strlen( string ))
247 char const * pzSrc = string + ix;
249 while (*pzSrc == ':') pzSrc++;
252 char ch = (*(pzDest++) = *(pzSrc++));
261 if ((unsigned long)(pzDest - pzDir) >= AG_PATH_MAX)
265 ix = (int)(pzSrc - string);
274 #endif /* __windows__ / __CYGWIN__ */
275 #endif /* HAVE_PATHFIND */
280 * c-file-style: "stroustrup"
281 * indent-tabs-mode: nil
283 * end of compat/pathfind.c */