Debian Codegraph @

lib/opendir.c

raw
1 /* Start reading the entries of a directory.
2 Copyright (C) 2006-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 HAVE_OPENDIR
26
27 /* Override opendir(), to keep track of the open file descriptors.
28 Needed because there is a function dirfd(). */
29
30 #else
31
32 # include "filename.h"
33
34 #endif
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 #if GNULIB_defined_DIR
40 # include "dirent-private.h"
41 #endif
42
43 #if REPLACE_FCHDIR
44 # include <unistd.h>
45 #endif
46
47 #if defined _WIN32 && ! defined __CYGWIN__
48 /* Don't assume that UNICODE is not defined. */
49 # undef WIN32_FIND_DATA
50 # define WIN32_FIND_DATA WIN32_FIND_DATAA
51 # undef GetFullPathName
52 # define GetFullPathName GetFullPathNameA
53 # undef FindFirstFile
54 # define FindFirstFile FindFirstFileA
55 #endif
56
57 DIR *
58 opendir (const char *dir_name)
59 #undef opendir
60 {
61 #if HAVE_DIRENT_H /* equivalent to HAVE_OPENDIR */
62 DIR *dirp;
63
64 # if GNULIB_defined_DIR
65 # undef DIR
66
67 dirp = (struct gl_directory *) malloc (sizeof (struct gl_directory));
68 if (dirp == NULL)
69 {
70 errno = ENOMEM;
71 return NULL;
72 }
73
74 DIR *real_dirp = opendir (dir_name);
75 if (real_dirp == NULL)
76 {
77 int saved_errno = errno;
78 free (dirp);
79 errno = saved_errno;
80 return NULL;
81 }
82
83 dirp->fd_to_close = -1;
84 dirp->real_dirp = real_dirp;
85 # else
86 dirp = opendir (dir_name);
87 if (dirp == NULL)
88 return NULL;
89 # endif
90
91 #else
92
93 if (dir_name[0] == '\0')
94 {
95 errno = ENOENT;
96 return NULL;
97 }
98
99 /* Make the dir_name absolute, so that we continue reading the same
100 directory if the current directory changed between this opendir()
101 call and a subsequent rewinddir() call. */
102 char dir_name_mask[MAX_PATH + 1 + 1 + 1];
103 if (!GetFullPathName (dir_name, MAX_PATH, dir_name_mask, NULL))
104 {
105 errno = EINVAL;
106 return NULL;
107 }
108
109 /* Append the mask.
110 "*" and "*.*" appear to be equivalent. */
111 {
112 char *p;
113
114 p = dir_name_mask + strlen (dir_name_mask);
115 if (p > dir_name_mask && !ISSLASH (p[-1]))
116 *p++ = '\\';
117 *p++ = '*';
118 *p = '\0';
119 }
120
121 /* Start searching the directory. */
122 int status = -1;
123 WIN32_FIND_DATA entry;
124 HANDLE current = FindFirstFile (dir_name_mask, &entry);
125 if (current == INVALID_HANDLE_VALUE)
126 {
127 switch (GetLastError ())
128 {
129 case ERROR_FILE_NOT_FOUND:
130 status = -2;
131 break;
132 case ERROR_PATH_NOT_FOUND:
133 errno = ENOENT;
134 return NULL;
135 case ERROR_DIRECTORY:
136 errno = ENOTDIR;
137 return NULL;
138 case ERROR_ACCESS_DENIED:
139 errno = EACCES;
140 return NULL;
141 default:
142 errno = EIO;
143 return NULL;
144 }
145 }
146
147 /* Allocate the result. */
148 struct gl_directory *dirp =
149 (struct gl_directory *)
150 malloc (offsetof (struct gl_directory, dir_name_mask[0])
151 + strlen (dir_name_mask) + 1);
152 if (dirp == NULL)
153 {
154 if (current != INVALID_HANDLE_VALUE)
155 FindClose (current);
156 errno = ENOMEM;
157 return NULL;
158 }
159 dirp->fd_to_close = -1;
160 dirp->status = status;
161 dirp->current = current;
162 if (status == -1)
163 memcpy (&dirp->entry, &entry, sizeof (WIN32_FIND_DATA));
164 strcpy (dirp->dir_name_mask, dir_name_mask);
165
166 #endif
167
168 #if REPLACE_FCHDIR
169 {
170 int fd = dirfd (dirp);
171 if (0 <= fd && _gl_register_fd (fd, dir_name) != fd)
172 {
173 int saved_errno = errno;
174 closedir (dirp);
175 errno = saved_errno;
176 return NULL;
177 }
178 }
179 #endif
180
181 return dirp;
182 }