Give unique names to s390 assembler opcode tests.
[binutils-gdb.git] / gnulib / import / opendir.c
blobfc6b455a62afa25ef874d998a96c46a390258f62
1 /* Start reading the entries of a directory.
2 Copyright (C) 2006-2022 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 <stdlib.h>
34 # include "dirent-private.h"
35 # include "filename.h"
37 #endif
39 #if REPLACE_FCHDIR
40 # include <unistd.h>
41 #endif
43 #ifdef __KLIBC__
44 # include <io.h>
45 # include <fcntl.h>
46 #endif
48 #if defined _WIN32 && ! defined __CYGWIN__
49 /* Don't assume that UNICODE is not defined. */
50 # undef WIN32_FIND_DATA
51 # define WIN32_FIND_DATA WIN32_FIND_DATAA
52 # undef GetFullPathName
53 # define GetFullPathName GetFullPathNameA
54 # undef FindFirstFile
55 # define FindFirstFile FindFirstFileA
56 #endif
58 DIR *
59 opendir (const char *dir_name)
61 #if HAVE_OPENDIR
62 # undef opendir
63 DIR *dirp;
65 dirp = opendir (dir_name);
66 if (dirp == NULL)
67 return NULL;
69 # ifdef __KLIBC__
71 int fd = open (dir_name, O_RDONLY);
72 if (fd == -1 || _gl_register_dirp_fd (fd, dirp))
74 int saved_errno = errno;
76 close (fd);
77 closedir (dirp);
79 errno = saved_errno;
81 return NULL;
84 # endif
85 #else
87 char dir_name_mask[MAX_PATH + 1 + 1 + 1];
88 int status;
89 HANDLE current;
90 WIN32_FIND_DATA entry;
91 struct gl_directory *dirp;
93 if (dir_name[0] == '\0')
95 errno = ENOENT;
96 return NULL;
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 if (!GetFullPathName (dir_name, MAX_PATH, dir_name_mask, NULL))
104 errno = EINVAL;
105 return NULL;
108 /* Append the mask.
109 "*" and "*.*" appear to be equivalent. */
111 char *p;
113 p = dir_name_mask + strlen (dir_name_mask);
114 if (p > dir_name_mask && !ISSLASH (p[-1]))
115 *p++ = '\\';
116 *p++ = '*';
117 *p = '\0';
120 /* Start searching the directory. */
121 status = -1;
122 current = FindFirstFile (dir_name_mask, &entry);
123 if (current == INVALID_HANDLE_VALUE)
125 switch (GetLastError ())
127 case ERROR_FILE_NOT_FOUND:
128 status = -2;
129 break;
130 case ERROR_PATH_NOT_FOUND:
131 errno = ENOENT;
132 return NULL;
133 case ERROR_DIRECTORY:
134 errno = ENOTDIR;
135 return NULL;
136 case ERROR_ACCESS_DENIED:
137 errno = EACCES;
138 return NULL;
139 default:
140 errno = EIO;
141 return NULL;
145 /* Allocate the result. */
146 dirp =
147 (struct gl_directory *)
148 malloc (offsetof (struct gl_directory, dir_name_mask[0])
149 + strlen (dir_name_mask) + 1);
150 if (dirp == NULL)
152 if (current != INVALID_HANDLE_VALUE)
153 FindClose (current);
154 errno = ENOMEM;
155 return NULL;
157 dirp->status = status;
158 dirp->current = current;
159 if (status == -1)
160 memcpy (&dirp->entry, &entry, sizeof (WIN32_FIND_DATA));
161 strcpy (dirp->dir_name_mask, dir_name_mask);
163 #endif
165 #if REPLACE_FCHDIR
167 int fd = dirfd (dirp);
168 if (0 <= fd && _gl_register_fd (fd, dir_name) != fd)
170 int saved_errno = errno;
171 closedir (dirp);
172 errno = saved_errno;
173 return NULL;
176 #endif
178 return dirp;