File: pathfind.c

package info (click to toggle)
autogen 5.3.5-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,216 kB
  • ctags: 2,706
  • sloc: ansic: 17,527; sh: 11,794; awk: 629; makefile: 556; lisp: 164; yacc: 50
file content (286 lines) | stat: -rw-r--r-- 7,729 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*  -*- Mode: C -*-  */

/* pathfind.c --- find a FILE  MODE along PATH */

/*
 * Author:           Gary V Vaughan <gvaughan@oranda.demon.co.uk>
 * Created:          Tue Jun 24 15:07:31 1997
 * Last Modified:    $Date: 2000/04/04 13:22:36 $
 *            by:    Bruce Korb <bkorb@gnu.org>
 *
 * $Id: pathfind.c,v 2.2 2000/04/04 13:22:36 bkorb Exp $
 */

/* Code: */

#include "compat.h"
#ifndef HAVE_PATHFIND

#ifndef STATIC
#  ifdef DEBUG
#    define STATIC
#  else
#    define STATIC static
#  endif
#endif

STATIC char* make_absolute( const char *string, const char *dot_path );
STATIC char* canonicalize_pathname( char *path );
STATIC char* extract_colon_unit( char* dir, const char *string, int *p_index );


/*
 * pathfind looks for a a file with name FILENAME and MODE access
 * along colon delimited PATH, and returns the full pathname as a
 * string, or NULL if not found.
 *          Letter    Meaning
 *          r         readable
 *          w         writable
 *          x         executable
 *          f         normal file    (NOT IMPLEMENTED)
 *          b         block special    (NOT IMPLEMENTED)
 *          c         character special    (NOT IMPLEMENTED)
 *          d         directory        (NOT IMPLEMENTED)
 *          p         FIFO (pipe)    (NOT IMPLEMENTED)
 *          u         set user ID bit    (NOT IMPLEMENTED)
 *          g         set group ID bit    (NOT IMPLEMENTED)
 *          k         sticky bit    (NOT IMPLEMENTED)
 *          s         size nonzero    (NOT IMPLEMENTED)
 */
     char*
pathfind( const char*  path,
          const char*  fileName,
          const char*  mode )
{
    int   p_index   = 0;
    int   mode_bits = 0;
    char* pathName  = NULL;
    char  zPath[ MAXPATHLEN + 1 ];

    if (strchr( mode, 'r' )) mode_bits |= R_OK;
    if (strchr( mode, 'w' )) mode_bits |= W_OK;
    if (strchr( mode, 'x' )) mode_bits |= X_OK;

    /*
     *  FOR each non-null entry in the colon-separated path, DO ...
     */
    for (;;) {
        DIR*  dirP;
        char* colon_unit = extract_colon_unit( zPath, path, &p_index );

        /*
         *  IF no more entries, THEN quit
         */
        if (colon_unit == NULL)
            break;

        dirP = opendir( colon_unit );

        /*
         *  IF the directory is inaccessable, THEN next directory
         */
        if (dirP == NULL)
            continue;

        /*
         *  FOR every entry in the given directory, ...
         */
        for (;;) {
            struct dirent *entP = readdir( dirP );

            if (entP == (struct dirent*)NULL)
                break;

            /*
             *  IF the file name matches the one we are looking for, ...
             */
            if (strcmp( entP->d_name, fileName ) == 0) {
                char* pzFullName = make_absolute( fileName, colon_unit);

                /*
                 *  Make sure we can access it in the way we want
                 */
                if (access( pzFullName, mode_bits ) >= 0) {
                    /*
                     *  We can, so normalize the name and return it below
                     */
                    pathName = canonicalize_pathname( pzFullName );
                }

                free( (void*)pzFullName );
                break;
            }
        }
        
        closedir( dirP );

        if (pathName != NULL)
            break;
    }

    return pathName;
}

/*
 * Turn STRING  (a pathname) into an  absolute  pathname, assuming  that
 * DOT_PATH contains the symbolic location of  `.'.  This always returns
 * a new string, even if STRING was an absolute pathname to begin with.
 */
STATIC char*
make_absolute( const char *string, const char *dot_path )
{
    char *result;
    int result_len;
  
    if (!dot_path || *string == '/') {
        result = strdup( string );
    } else {
        if (dot_path && dot_path[0]) {
            result = malloc( 2 + strlen( dot_path ) + strlen( string ) );
            strcpy( result, dot_path );
            result_len = strlen( result );
            if (result[result_len - 1] != '/') {
                result[result_len++] = '/';
                result[result_len] = '\0';
            }    
        } else {
            result = malloc( 3 + strlen( string ) );
            result[0] = '.'; result[1] = '/'; result[2] = '\0';
            result_len = 2;
        }

        strcpy( result + result_len, string );
    }

    return result;
}

/*
 * Canonicalize PATH, and return a  new path.  The new path differs from
 * PATH in that:
 *
 *    Multiple `/'s     are collapsed to a single `/'.
 *    Leading `./'s     are removed.
 *    Trailing `/.'s    are removed.
 *    Trailing `/'s     are removed.
 *    Non-leading `../'s and trailing `..'s are handled by removing
 *                    portions of the path.
 */
STATIC char*
canonicalize_pathname( char *path )
{
    int i, start;
    char stub_char, *result;

    /* The result cannot be larger than the input PATH. */
    result = strdup( path );

    stub_char = (*path == '/') ? '/' : '.';

    /* Walk along RESULT looking for things to compact. */
    i = 0;
    while (result[i]) {
        while (result[i] != '\0' && result[i] != '/')
            i++;

        start = i++;

        /* If we didn't find any  slashes, then there is nothing left to
         * do.
         */
        if (!result[start])
            break;

        /* Handle multiple `/'s in a row. */
        while (result[i] == '/')
            i++;

#if !defined (apollo)
        if ((start + 1) != i)
#else
        if ((start + 1) != i && (start != 0 || i != 2))
#endif /* apollo */
        {
            strcpy( result + start + 1, result + i );
            i = start + 1;
        }

        /* Handle backquoted `/'. */
        if (start > 0 && result[start - 1] == '\\')
            continue;

        /* Check for trailing `/', and `.' by itself. */
        if ((start && !result[i])
            || (result[i] == '.' && !result[i+1])) {
            result[--i] = '\0';
            break;
        }

        /* Check for `../', `./' or trailing `.' by itself. */
        if (result[i] == '.') {
            /* Handle `./'. */
            if (result[i + 1] == '/') {
                strcpy( result + i, result + i + 1 );
                i = (start < 0) ? 0 : start;
                continue;
            }

            /* Handle `../' or trailing `..' by itself. */
            if (result[i + 1] == '.' &&
                (result[i + 2] == '/' || !result[i + 2])) {
                while (--start > -1 && result[start] != '/')
                    ;
                strcpy( result + start + 1, result + i + 2 );
                i = (start < 0) ? 0 : start;
                continue;
            }
        }
    }

    if (!*result) {
        *result = stub_char;
        result[1] = '\0';
    }

    return result;
}

/*
 * Given a  string containing units of information separated  by colons,
 * return the next one  pointed to by (P_INDEX), or NULL if there are no
 * more.  Advance (P_INDEX) to the character after the colon.
 */
STATIC char*
extract_colon_unit( char* pzDir, const char *string, int *p_index )
{
    char*  pzDest = pzDir;
    const char*  pzSrc  = string + *p_index;

    if (  (string == NULL)
       || (*p_index >= (int) strlen( string )))
        return NULL;

    while (*pzSrc == ':')
        pzSrc++;

    for (;;) {
        char ch = (*pzDest = *pzSrc);
        if (ch == NUL)
            break;
        if (ch == ':') {
            *pzDest = NUL;
            break;
        }
        pzDest++;
        pzSrc++;
    }

    if (*pzDir == NUL)
        return NULL;

    *p_index = (pzSrc - string);
    return pzDir;
}

#endif /* HAVE_PATHFIND */
/* pathfind.c ends here */