Debian Codegraph @

lib/readdir.c

raw
1 /* Read the next entry of a directory.
2 Copyright (C) 2011-2026 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 /* Specification. */
20 #include <dirent.h>
21
22 #include <errno.h>
23 #include <stddef.h>
24
25 #if GNULIB_defined_DIR
26 # include "dirent-private.h"
27 #endif
28
29 /* Don't assume that UNICODE is not defined. */
30 #undef FindNextFile
31 #define FindNextFile FindNextFileA
32
33 struct dirent *
34 readdir (DIR *dirp)
35 #undef readdir
36 {
37 #if HAVE_DIRENT_H /* equivalent to HAVE_READDIR */
38 return readdir (dirp->real_dirp);
39 #else
40 /* There is no need to add code to produce entries for "." and "..".
41 According to the POSIX:2008 section "4.12 Pathname Resolution"
42 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html>
43 "." and ".." are syntactic entities.
44 POSIX also says:
45 "If entries for dot or dot-dot exist, one entry shall be returned
46 for dot and one entry shall be returned for dot-dot; otherwise,
47 they shall not be returned." */
48
49 switch (dirp->status)
50 {
51 case -2:
52 /* End of directory already reached. */
53 return NULL;
54 case -1:
55 break;
56 case 0:
57 if (!FindNextFile (dirp->current, &dirp->entry))
58 {
59 switch (GetLastError ())
60 {
61 case ERROR_NO_MORE_FILES:
62 dirp->status = -2;
63 return NULL;
64 default:
65 errno = EIO;
66 return NULL;
67 }
68 }
69 break;
70 default:
71 errno = dirp->status;
72 return NULL;
73 }
74
75 dirp->status = 0;
76
77 char type;
78 if (dirp->entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
79 type = DT_DIR;
80 else if (dirp->entry.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
81 type = DT_LNK;
82 else if ((dirp->entry.dwFileAttributes
83 & ~(FILE_ATTRIBUTE_READONLY
84 | FILE_ATTRIBUTE_HIDDEN
85 | FILE_ATTRIBUTE_SYSTEM
86 | FILE_ATTRIBUTE_ARCHIVE
87 | FILE_ATTRIBUTE_NORMAL
88 | FILE_ATTRIBUTE_TEMPORARY
89 | FILE_ATTRIBUTE_SPARSE_FILE
90 | FILE_ATTRIBUTE_COMPRESSED
91 | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
92 | FILE_ATTRIBUTE_ENCRYPTED)) == 0)
93 /* Devices like COM1, LPT1, NUL would also have the attributes 0x20 but
94 they cannot occur here. */
95 type = DT_REG;
96 else
97 type = DT_UNKNOWN;
98
99 /* Reuse the memory of dirp->entry for the result. */
100 struct dirent *result =
101 (struct dirent *)
102 ((char *) dirp->entry.cFileName - offsetof (struct dirent, d_name[0]));
103 result->d_type = type;
104
105 return result;
106 #endif
107 }