1 /* Directory entry code for Window platforms.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2, or (at your option) any later version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 GNU Make; see the file COPYING. If not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
19 #include <sys/types.h>
28 opendir(const char* pDirName
)
40 if (stat(pDirName
, &sb
) != 0) {
44 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
) {
49 /* allocate a DIR structure to return */
50 pDir
= (DIR *) malloc(sizeof (DIR));
55 /* input directory name length */
56 nBufferLen
= strlen(pDirName
);
58 /* copy input directory name to DIR buffer */
59 strcpy(pDir
->dir_pDirectoryName
, pDirName
);
61 /* point to end of the copied directory name */
62 pEndDirName
= &pDir
->dir_pDirectoryName
[nBufferLen
- 1];
64 /* if directory name did not end in '/' or '\', add '/' */
65 if ((*pEndDirName
!= '/') && (*pEndDirName
!= '\\')) {
70 /* now append the wildcard character to the buffer */
76 /* other values defaulted */
77 pDir
->dir_nNumFiles
= 0;
78 pDir
->dir_hDirHandle
= INVALID_HANDLE_VALUE
;
79 pDir
->dir_ulCookie
= __DIRENT_COOKIE
;
87 /* got a valid pointer? */
93 /* sanity check that this is a DIR pointer */
94 if (pDir
->dir_ulCookie
!= __DIRENT_COOKIE
) {
99 /* close the WINDOWS32 directory handle */
100 if (pDir
->dir_hDirHandle
!= INVALID_HANDLE_VALUE
)
101 FindClose(pDir
->dir_hDirHandle
);
111 WIN32_FIND_DATA wfdFindData
;
118 /* sanity check that this is a DIR pointer */
119 if (pDir
->dir_ulCookie
!= __DIRENT_COOKIE
) {
124 if (pDir
->dir_nNumFiles
== 0) {
125 pDir
->dir_hDirHandle
= FindFirstFile(pDir
->dir_pDirectoryName
, &wfdFindData
);
126 if (pDir
->dir_hDirHandle
== INVALID_HANDLE_VALUE
)
128 } else if (!FindNextFile(pDir
->dir_hDirHandle
, &wfdFindData
))
131 /* bump count for next call to readdir() or telldir() */
132 pDir
->dir_nNumFiles
++;
134 /* fill in struct dirent values */
135 pDir
->dir_sdReturn
.d_ino
= -1;
136 strcpy(pDir
->dir_sdReturn
.d_name
, wfdFindData
.cFileName
);
138 return &pDir
->dir_sdReturn
;
149 /* sanity check that this is a DIR pointer */
150 if (pDir
->dir_ulCookie
!= __DIRENT_COOKIE
) {
155 /* close the WINDOWS32 directory handle */
156 if (pDir
->dir_hDirHandle
!= INVALID_HANDLE_VALUE
)
157 if (!FindClose(pDir
->dir_hDirHandle
))
160 /* reset members which control readdir() */
161 pDir
->dir_hDirHandle
= INVALID_HANDLE_VALUE
;
162 pDir
->dir_nNumFiles
= 0;
175 /* sanity check that this is a DIR pointer */
176 if (pDir
->dir_ulCookie
!= __DIRENT_COOKIE
) {
181 /* return number of times readdir() called */
182 return pDir
->dir_nNumFiles
;
186 seekdir(DIR* pDir
, long nPosition
)
191 /* sanity check that this is a DIR pointer */
192 if (pDir
->dir_ulCookie
!= __DIRENT_COOKIE
)
195 /* go back to beginning of directory */
198 /* loop until we have found position we care about */
199 for (--nPosition
; nPosition
&& readdir(pDir
); nPosition
--);
201 /* flag invalid nPosition value */