1 /* Directory entry code for Window platforms.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 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 3 of the License, or (at your option) any later
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <sys/types.h>
29 opendir(const char* pDirName)
41 if (stat(pDirName, &sb) != 0) {
45 if ((sb.st_mode & S_IFMT) != S_IFDIR) {
50 /* allocate a DIR structure to return */
51 pDir = (DIR *) malloc(sizeof (DIR));
56 /* input directory name length */
57 nBufferLen = strlen(pDirName);
59 /* copy input directory name to DIR buffer */
60 strcpy(pDir->dir_pDirectoryName, pDirName);
62 /* point to end of the copied directory name */
63 pEndDirName = &pDir->dir_pDirectoryName[nBufferLen - 1];
65 /* if directory name did not end in '/' or '\', add '/' */
66 if ((*pEndDirName != '/') && (*pEndDirName != '\\')) {
71 /* now append the wildcard character to the buffer */
77 /* other values defaulted */
78 pDir->dir_nNumFiles = 0;
79 pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
80 pDir->dir_ulCookie = __DIRENT_COOKIE;
88 /* got a valid pointer? */
94 /* sanity check that this is a DIR pointer */
95 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
100 /* close the WINDOWS32 directory handle */
101 if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
102 FindClose(pDir->dir_hDirHandle);
112 WIN32_FIND_DATA wfdFindData;
119 /* sanity check that this is a DIR pointer */
120 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
125 if (pDir->dir_nNumFiles == 0) {
126 pDir->dir_hDirHandle = FindFirstFile(pDir->dir_pDirectoryName, &wfdFindData);
127 if (pDir->dir_hDirHandle == INVALID_HANDLE_VALUE)
129 } else if (!FindNextFile(pDir->dir_hDirHandle, &wfdFindData))
132 /* bump count for next call to readdir() or telldir() */
133 pDir->dir_nNumFiles++;
135 /* fill in struct dirent values */
136 pDir->dir_sdReturn.d_ino = (ino_t)-1;
137 strcpy(pDir->dir_sdReturn.d_name, wfdFindData.cFileName);
139 return &pDir->dir_sdReturn;
150 /* sanity check that this is a DIR pointer */
151 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
156 /* close the WINDOWS32 directory handle */
157 if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
158 if (!FindClose(pDir->dir_hDirHandle))
161 /* reset members which control readdir() */
162 pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
163 pDir->dir_nNumFiles = 0;
176 /* sanity check that this is a DIR pointer */
177 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
182 /* return number of times readdir() called */
183 return pDir->dir_nNumFiles;
187 seekdir(DIR* pDir, long nPosition)
192 /* sanity check that this is a DIR pointer */
193 if (pDir->dir_ulCookie != __DIRENT_COOKIE)
196 /* go back to beginning of directory */
199 /* loop until we have found position we care about */
200 for (--nPosition; nPosition && readdir(pDir); nPosition--);
202 /* flag invalid nPosition value */