~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to lib/dirent.c

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2010-05-04 11:15:49 UTC
  • mfrom: (1.3.1 upstream)
  • mto: (20.3.1 squeeze) (21.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20100504111549-1apjh2g5sndki4te
Tags: upstream-3.1.3
ImportĀ upstreamĀ versionĀ 3.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 *  it under the terms of the GNU General Public License as published by
22
22
 *  the Free Software Foundation; either version 2 of the License, or
23
23
 *  (at your option) any later version.
24
 
 *  
 
24
 *
25
25
 *  This program is distributed in the hope that it will be useful,
26
26
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27
27
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
28
 *  GNU General Public License for more details.
29
 
 *  
 
29
 *
30
30
 *  You should have received a copy of the GNU General Public License
31
31
 *  along with this program; if not, write to the Free Software
32
32
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
38
38
 * This file is a part of the mingw-runtime package.
39
39
 * No warranty is given; refer to the file DISCLAIMER within the package.
40
40
 *
41
 
 * Derived from DIRLIB.C by Matt J. Weinstein 
 
41
 * Derived from DIRLIB.C by Matt J. Weinstein
42
42
 * This note appears in the DIRLIB.H
43
43
 * DIRLIB.H by M. J. Weinstein   Released to public domain 1-Jan-89
44
44
 *
45
45
 * Updated by Jeremy Bettis <jeremy@hksys.com>
46
46
 * Significantly revised and rewinddir, seekdir and telldir added by Colin
47
47
 * Peters <colin@fu.is.saga-u.ac.jp>
48
 
 *      
 
48
 *
49
49
 */
50
50
 
51
51
#include "util.h"
81
81
    errno = 0;
82
82
 
83
83
    if (!szPath) {
84
 
        errno = EFAULT;
85
 
        return (DIR *) 0;
 
84
        errno = EFAULT;
 
85
        return (DIR *) 0;
86
86
    }
87
87
    if (szPath[0] == '\0') {
88
 
        errno = ENOTDIR;
89
 
        return (DIR *) 0;
 
88
        errno = ENOTDIR;
 
89
        return (DIR *) 0;
90
90
    }
91
91
    /* Attempt to determine if the given path really is a directory. */
92
92
    rc = GetFileAttributes(szPath);
93
93
    if (rc == (unsigned int) -1) {
94
 
        /* call GetLastError for more error info */
95
 
        errno = ENOENT;
96
 
        return (DIR *) 0;
 
94
        /* call GetLastError for more error info */
 
95
        errno = ENOENT;
 
96
        return (DIR *) 0;
97
97
    }
98
98
    if (!(rc & FILE_ATTRIBUTE_DIRECTORY)) {
99
 
        /* Error, entry exists but not a directory. */
100
 
        errno = ENOTDIR;
101
 
        return (DIR *) 0;
 
99
        /* Error, entry exists but not a directory. */
 
100
        errno = ENOTDIR;
 
101
        return (DIR *) 0;
102
102
    }
103
103
    /* Make an absolute pathname.  */
104
104
    _fullpath(szFullPath, szPath, MAX_PATH);
106
106
    /* Allocate enough space to store DIR structure and the complete
107
107
     * directory path given. */
108
108
    nd = (DIR *) malloc(sizeof(DIR) + (strlen(szFullPath)
109
 
            + strlen(SLASH)
110
 
            + strlen(SUFFIX) + 1)
111
 
        * sizeof(CHAR));
 
109
                                       + strlen(SLASH)
 
110
                                       + strlen(SUFFIX) + 1)
 
111
                        * sizeof(CHAR));
112
112
 
113
113
    if (!nd) {
114
 
        /* Error, out of memory. */
115
 
        errno = ENOMEM;
116
 
        return (DIR *) 0;
 
114
        /* Error, out of memory. */
 
115
        errno = ENOMEM;
 
116
        return (DIR *) 0;
117
117
    }
118
118
    /* Create the search expression. */
119
119
    strcpy(nd->dd_name, szFullPath);
120
120
 
121
121
    /* Add on a slash if the path does not end with one. */
122
122
    if (nd->dd_name[0] != '\0'
123
 
        && strchr(nd->dd_name, '/') != nd->dd_name
124
 
        + strlen(nd->dd_name) - 1
125
 
        && strchr(nd->dd_name, '\\') != nd->dd_name
126
 
        + strlen(nd->dd_name) - 1) {
127
 
        strcat(nd->dd_name, SLASH);
 
123
            && strchr(nd->dd_name, '/') != nd->dd_name
 
124
            + strlen(nd->dd_name) - 1
 
125
            && strchr(nd->dd_name, '\\') != nd->dd_name
 
126
            + strlen(nd->dd_name) - 1) {
 
127
        strcat(nd->dd_name, SLASH);
128
128
    }
129
129
    /* Add on the search pattern */
130
130
    strcat(nd->dd_name, SUFFIX);
155
155
 * next entry in the directory.
156
156
 */
157
157
struct dirent *
158
 
readdir(DIR * dirp)
159
 
{
 
158
readdir(DIR * dirp) {
160
159
    errno = 0;
161
160
 
162
161
    /* Check for valid DIR struct. */
163
162
    if (!dirp) {
164
 
        errno = EFAULT;
165
 
        return (struct dirent *) 0;
 
163
        errno = EFAULT;
 
164
        return (struct dirent *) 0;
166
165
    }
167
166
    if (dirp->dd_stat < 0) {
168
 
        /* We have already returned all files in the directory
169
 
         * (or the structure has an invalid dd_stat). */
170
 
        return (struct dirent *) 0;
 
167
        /* We have already returned all files in the directory
 
168
         * (or the structure has an invalid dd_stat). */
 
169
        return (struct dirent *) 0;
171
170
    } else if (dirp->dd_stat == 0) {
172
 
        /* We haven't started the search yet. */
173
 
        /* Start the search */
174
 
        dirp->dd_handle = _findfirst(dirp->dd_name, &(dirp->dd_dta));
 
171
        /* We haven't started the search yet. */
 
172
        /* Start the search */
 
173
        dirp->dd_handle = _findfirst(dirp->dd_name, &(dirp->dd_dta));
175
174
 
176
 
        if (dirp->dd_handle == -1) {
177
 
            /* Whoops! Seems there are no files in that
178
 
             * directory. */
179
 
            dirp->dd_stat = -1;
180
 
        } else {
181
 
            dirp->dd_stat = 1;
182
 
        }
 
175
        if (dirp->dd_handle == -1) {
 
176
            /* Whoops! Seems there are no files in that
 
177
             * directory. */
 
178
            dirp->dd_stat = -1;
 
179
        } else {
 
180
            dirp->dd_stat = 1;
 
181
        }
183
182
    } else {
184
 
        /* Get the next search entry. */
185
 
        if (_findnext(dirp->dd_handle, &(dirp->dd_dta))) {
186
 
            /* We are off the end or otherwise error.     
187
 
             * _findnext sets errno to ENOENT if no more file
188
 
             * Undo this. */
189
 
            DWORD winerr = GetLastError();
190
 
            if (winerr == ERROR_NO_MORE_FILES)
191
 
                errno = 0;
192
 
            _findclose(dirp->dd_handle);
193
 
            dirp->dd_handle = -1;
194
 
            dirp->dd_stat = -1;
195
 
        } else {
196
 
            /* Update the status to indicate the correct
197
 
             * number. */
198
 
            dirp->dd_stat++;
199
 
        }
 
183
        /* Get the next search entry. */
 
184
        if (_findnext(dirp->dd_handle, &(dirp->dd_dta))) {
 
185
            /* We are off the end or otherwise error.
 
186
             * _findnext sets errno to ENOENT if no more file
 
187
             * Undo this. */
 
188
            DWORD winerr = GetLastError();
 
189
            if (winerr == ERROR_NO_MORE_FILES)
 
190
                errno = 0;
 
191
            _findclose(dirp->dd_handle);
 
192
            dirp->dd_handle = -1;
 
193
            dirp->dd_stat = -1;
 
194
        } else {
 
195
            /* Update the status to indicate the correct
 
196
             * number. */
 
197
            dirp->dd_stat++;
 
198
        }
200
199
    }
201
200
 
202
201
    if (dirp->dd_stat > 0) {
203
 
        /* Successfully got an entry. Everything about the file is
204
 
         * already appropriately filled in except the length of the
205
 
         * file name. */
206
 
        dirp->dd_dir.d_namlen = strlen(dirp->dd_dta.name);
207
 
        strcpy(dirp->dd_dir.d_name, dirp->dd_dta.name);
208
 
        return &dirp->dd_dir;
 
202
        /* Successfully got an entry. Everything about the file is
 
203
         * already appropriately filled in except the length of the
 
204
         * file name. */
 
205
        dirp->dd_dir.d_namlen = strlen(dirp->dd_dta.name);
 
206
        strcpy(dirp->dd_dir.d_name, dirp->dd_dta.name);
 
207
        return &dirp->dd_dir;
209
208
    }
210
209
    return (struct dirent *) 0;
211
210
}
225
224
    rc = 0;
226
225
 
227
226
    if (!dirp) {
228
 
        errno = EFAULT;
229
 
        return -1;
 
227
        errno = EFAULT;
 
228
        return -1;
230
229
    }
231
230
    if (dirp->dd_handle != -1) {
232
 
        rc = _findclose(dirp->dd_handle);
 
231
        rc = _findclose(dirp->dd_handle);
233
232
    }
234
233
    /* Delete the dir structure. */
235
234
    free(dirp);
249
248
    errno = 0;
250
249
 
251
250
    if (!dirp) {
252
 
        errno = EFAULT;
253
 
        return;
 
251
        errno = EFAULT;
 
252
        return;
254
253
    }
255
254
    if (dirp->dd_handle != -1) {
256
 
        _findclose(dirp->dd_handle);
 
255
        _findclose(dirp->dd_handle);
257
256
    }
258
257
    dirp->dd_handle = -1;
259
258
    dirp->dd_stat = 0;
271
270
    errno = 0;
272
271
 
273
272
    if (!dirp) {
274
 
        errno = EFAULT;
275
 
        return -1;
 
273
        errno = EFAULT;
 
274
        return -1;
276
275
    }
277
276
    return dirp->dd_stat;
278
277
}
292
291
    errno = 0;
293
292
 
294
293
    if (!dirp) {
295
 
        errno = EFAULT;
296
 
        return;
 
294
        errno = EFAULT;
 
295
        return;
297
296
    }
298
297
    if (lPos < -1) {
299
 
        /* Seeking to an invalid position. */
300
 
        errno = EINVAL;
301
 
        return;
 
298
        /* Seeking to an invalid position. */
 
299
        errno = EINVAL;
 
300
        return;
302
301
    } else if (lPos == -1) {
303
 
        /* Seek past end. */
304
 
        if (dirp->dd_handle != -1) {
305
 
            _findclose(dirp->dd_handle);
306
 
        }
307
 
        dirp->dd_handle = -1;
308
 
        dirp->dd_stat = -1;
 
302
        /* Seek past end. */
 
303
        if (dirp->dd_handle != -1) {
 
304
            _findclose(dirp->dd_handle);
 
305
        }
 
306
        dirp->dd_handle = -1;
 
307
        dirp->dd_stat = -1;
309
308
    } else {
310
 
        /* Rewind and read forward to the appropriate index. */
311
 
        rewinddir(dirp);
 
309
        /* Rewind and read forward to the appropriate index. */
 
310
        rewinddir(dirp);
312
311
 
313
 
        while ((dirp->dd_stat < lPos) && readdir(dirp));
 
312
        while ((dirp->dd_stat < lPos) && readdir(dirp));
314
313
    }
315
314
}
316
315
#endif /* _SQUID_MSWIN_ */