exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / opendir.c
blobdf76807631ead321e66355252660d04914521875
1 /* Start reading the entries of a directory.
2 Copyright (C) 2006-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <dirent.h>
22 #include <errno.h>
23 #include <stddef.h>
25 #if HAVE_OPENDIR
27 /* Override opendir(), to keep track of the open file descriptors.
28 Needed because there is a function dirfd(). */
30 #else
32 # include "filename.h"
34 #endif
36 #include <stdlib.h>
37 #include <string.h>
39 #if GNULIB_defined_DIR
40 # include "dirent-private.h"
41 #endif
43 #if REPLACE_FCHDIR
44 # include <unistd.h>
45 #endif
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
57 DIR *
58 opendir (const char *dir_name)
59 #undef opendir
61 #if HAVE_DIRENT_H /* equivalent to HAVE_OPENDIR */
62 DIR *dirp;
64 # if GNULIB_defined_DIR
65 # undef DIR
67 dirp = (struct gl_directory *) malloc (sizeof (struct gl_directory));
68 if (dirp == NULL)
70 errno = ENOMEM;
71 return NULL;
74 DIR *real_dirp = opendir (dir_name);
75 if (real_dirp == NULL)
77 int saved_errno = errno;
78 free (dirp);
79 errno = saved_errno;
80 return NULL;
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
91 #else
93 char dir_name_mask[MAX_PATH + 1 + 1 + 1];
94 int status;
95 HANDLE current;
96 WIN32_FIND_DATA entry;
97 struct gl_directory *dirp;
99 if (dir_name[0] == '\0')
101 errno = ENOENT;
102 return NULL;
105 /* Make the dir_name absolute, so that we continue reading the same
106 directory if the current directory changed between this opendir()
107 call and a subsequent rewinddir() call. */
108 if (!GetFullPathName (dir_name, MAX_PATH, dir_name_mask, NULL))
110 errno = EINVAL;
111 return NULL;
114 /* Append the mask.
115 "*" and "*.*" appear to be equivalent. */
117 char *p;
119 p = dir_name_mask + strlen (dir_name_mask);
120 if (p > dir_name_mask && !ISSLASH (p[-1]))
121 *p++ = '\\';
122 *p++ = '*';
123 *p = '\0';
126 /* Start searching the directory. */
127 status = -1;
128 current = FindFirstFile (dir_name_mask, &entry);
129 if (current == INVALID_HANDLE_VALUE)
131 switch (GetLastError ())
133 case ERROR_FILE_NOT_FOUND:
134 status = -2;
135 break;
136 case ERROR_PATH_NOT_FOUND:
137 errno = ENOENT;
138 return NULL;
139 case ERROR_DIRECTORY:
140 errno = ENOTDIR;
141 return NULL;
142 case ERROR_ACCESS_DENIED:
143 errno = EACCES;
144 return NULL;
145 default:
146 errno = EIO;
147 return NULL;
151 /* Allocate the result. */
152 dirp =
153 (struct gl_directory *)
154 malloc (offsetof (struct gl_directory, dir_name_mask[0])
155 + strlen (dir_name_mask) + 1);
156 if (dirp == NULL)
158 if (current != INVALID_HANDLE_VALUE)
159 FindClose (current);
160 errno = ENOMEM;
161 return NULL;
163 dirp->fd_to_close = -1;
164 dirp->status = status;
165 dirp->current = current;
166 if (status == -1)
167 memcpy (&dirp->entry, &entry, sizeof (WIN32_FIND_DATA));
168 strcpy (dirp->dir_name_mask, dir_name_mask);
170 #endif
172 #if REPLACE_FCHDIR
174 int fd = dirfd (dirp);
175 if (0 <= fd && _gl_register_fd (fd, dir_name) != fd)
177 int saved_errno = errno;
178 closedir (dirp);
179 errno = saved_errno;
180 return NULL;
183 #endif
185 return dirp;