annotate lib/readdir.c @ 17342:c75939cb6254

merge with default branch
author Michael Goffioul <michael.goffioul@gmail.com>
date Thu, 21 Feb 2013 14:57:31 +0000
parents e542fd46ad6f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15610
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Read the next entry of a directory.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
2 Copyright (C) 2011-2013 Free Software Foundation, Inc.
15610
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #include <config.h>
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /* Specification. */
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <dirent.h>
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <errno.h>
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <stddef.h>
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "dirent-private.h"
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 struct dirent *
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 readdir (DIR *dirp)
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 {
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 char type;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 struct dirent *result;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 /* There is no need to add code to produce entries for "." and "..".
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 According to the POSIX:2008 section "4.12 Pathname Resolution"
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html>
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 "." and ".." are syntactic entities.
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 POSIX also says:
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 "If entries for dot or dot-dot exist, one entry shall be returned
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 for dot and one entry shall be returned for dot-dot; otherwise,
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 they shall not be returned." */
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 switch (dirp->status)
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 {
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 case -2:
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 /* End of directory already reached. */
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 return NULL;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 case -1:
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 break;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 case 0:
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 if (!FindNextFile (dirp->current, &dirp->entry))
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 {
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 switch (GetLastError ())
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 {
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 case ERROR_NO_MORE_FILES:
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 dirp->status = -2;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 return NULL;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 default:
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 errno = EIO;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 return NULL;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 }
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 }
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 break;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 default:
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 errno = dirp->status;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 return NULL;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 }
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 dirp->status = 0;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 if (dirp->entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 type = DT_DIR;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 else if (dirp->entry.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 type = DT_LNK;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 else if ((dirp->entry.dwFileAttributes
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 & ~(FILE_ATTRIBUTE_READONLY
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 | FILE_ATTRIBUTE_HIDDEN
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 | FILE_ATTRIBUTE_SYSTEM
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 | FILE_ATTRIBUTE_ARCHIVE
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 | FILE_ATTRIBUTE_NORMAL
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 | FILE_ATTRIBUTE_TEMPORARY
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 | FILE_ATTRIBUTE_SPARSE_FILE
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 | FILE_ATTRIBUTE_COMPRESSED
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 | FILE_ATTRIBUTE_ENCRYPTED)) == 0)
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 /* Devices like COM1, LPT1, NUL would also have the attributes 0x20 but
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 they cannot occur here. */
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 type = DT_REG;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 else
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 type = DT_UNKNOWN;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 /* Reuse the memory of dirp->entry for the result. */
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 result =
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 (struct dirent *)
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 ((char *) dirp->entry.cFileName - offsetof (struct dirent, d_name[0]));
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 result->d_type = type;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 return result;
de2ecfd7fc4a New modules 'opendir', 'readdir', 'rewinddir', 'closedir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 }