1 /* Start reading the entries of a directory.
2 Copyright (C) 2006-2016 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* Override opendir(), to keep track of the open file descriptors.
28 Needed because there is a function dirfd(). */
34 # include "dirent-private.h"
35 # include "filename.h"
49 opendir (const char *dir_name
)
55 dirp
= opendir (dir_name
);
61 int fd
= open (dir_name
, O_RDONLY
);
62 if (fd
== -1 || _gl_register_dirp_fd (fd
, dirp
))
64 int saved_errno
= errno
;
77 char dir_name_mask
[MAX_PATH
+ 1 + 1 + 1];
80 WIN32_FIND_DATA entry
;
81 struct gl_directory
*dirp
;
83 if (dir_name
[0] == '\0')
89 /* Make the dir_name absolute, so that we continue reading the same
90 directory if the current directory changed between this opendir()
91 call and a subsequent rewinddir() call. */
92 if (!GetFullPathName (dir_name
, MAX_PATH
, dir_name_mask
, NULL
))
99 "*" and "*.*" appear to be equivalent. */
103 p
= dir_name_mask
+ strlen (dir_name_mask
);
104 if (p
> dir_name_mask
&& !ISSLASH (p
[-1]))
110 /* Start searching the directory. */
112 current
= FindFirstFile (dir_name_mask
, &entry
);
113 if (current
== INVALID_HANDLE_VALUE
)
115 switch (GetLastError ())
117 case ERROR_FILE_NOT_FOUND
:
120 case ERROR_PATH_NOT_FOUND
:
123 case ERROR_DIRECTORY
:
126 case ERROR_ACCESS_DENIED
:
135 /* Allocate the result. */
137 (struct gl_directory
*)
138 malloc (offsetof (struct gl_directory
, dir_name_mask
[0])
139 + strlen (dir_name_mask
) + 1);
142 if (current
!= INVALID_HANDLE_VALUE
)
147 dirp
->status
= status
;
148 dirp
->current
= current
;
150 memcpy (&dirp
->entry
, &entry
, sizeof (WIN32_FIND_DATA
));
151 strcpy (dirp
->dir_name_mask
, dir_name_mask
);
157 int fd
= dirfd (dirp
);
158 if (0 <= fd
&& _gl_register_fd (fd
, dir_name
) != fd
)
160 int saved_errno
= errno
;