Update copyright notices.
[make.git] / w32 / compat / dirent.c
blobe31dc8d309d8529636d44f678108982e639718bd
1 /* Directory entry code for Window platforms.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2008, 2009, 2010, 2011, 2012 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
9 version.
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/>. */
19 #include <config.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "dirent.h"
28 DIR*
29 opendir(const char* pDirName)
31 struct stat sb;
32 DIR* pDir;
33 char* pEndDirName;
34 int nBufferLen;
36 /* sanity checks */
37 if (!pDirName) {
38 errno = EINVAL;
39 return NULL;
41 if (stat(pDirName, &sb) != 0) {
42 errno = ENOENT;
43 return NULL;
45 if ((sb.st_mode & S_IFMT) != S_IFDIR) {
46 errno = ENOTDIR;
47 return NULL;
50 /* allocate a DIR structure to return */
51 pDir = (DIR *) malloc(sizeof (DIR));
53 if (!pDir)
54 return NULL;
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 != '\\')) {
67 pEndDirName++;
68 *pEndDirName = '/';
71 /* now append the wildcard character to the buffer */
72 pEndDirName++;
73 *pEndDirName = '*';
74 pEndDirName++;
75 *pEndDirName = '\0';
77 /* other values defaulted */
78 pDir->dir_nNumFiles = 0;
79 pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
80 pDir->dir_ulCookie = __DIRENT_COOKIE;
82 return pDir;
85 void
86 closedir(DIR *pDir)
88 /* got a valid pointer? */
89 if (!pDir) {
90 errno = EINVAL;
91 return;
94 /* sanity check that this is a DIR pointer */
95 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
96 errno = EINVAL;
97 return;
100 /* close the WINDOWS32 directory handle */
101 if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
102 FindClose(pDir->dir_hDirHandle);
104 free(pDir);
106 return;
109 struct dirent *
110 readdir(DIR* pDir)
112 WIN32_FIND_DATA wfdFindData;
114 if (!pDir) {
115 errno = EINVAL;
116 return NULL;
119 /* sanity check that this is a DIR pointer */
120 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
121 errno = EINVAL;
122 return NULL;
125 if (pDir->dir_nNumFiles == 0) {
126 pDir->dir_hDirHandle = FindFirstFile(pDir->dir_pDirectoryName, &wfdFindData);
127 if (pDir->dir_hDirHandle == INVALID_HANDLE_VALUE)
128 return NULL;
129 } else if (!FindNextFile(pDir->dir_hDirHandle, &wfdFindData))
130 return NULL;
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;
142 void
143 rewinddir(DIR* pDir)
145 if (!pDir) {
146 errno = EINVAL;
147 return;
150 /* sanity check that this is a DIR pointer */
151 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
152 errno = EINVAL;
153 return;
156 /* close the WINDOWS32 directory handle */
157 if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
158 if (!FindClose(pDir->dir_hDirHandle))
159 errno = EBADF;
161 /* reset members which control readdir() */
162 pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
163 pDir->dir_nNumFiles = 0;
165 return;
169 telldir(DIR* pDir)
171 if (!pDir) {
172 errno = EINVAL;
173 return -1;
176 /* sanity check that this is a DIR pointer */
177 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
178 errno = EINVAL;
179 return -1;
182 /* return number of times readdir() called */
183 return pDir->dir_nNumFiles;
186 void
187 seekdir(DIR* pDir, long nPosition)
189 if (!pDir)
190 return;
192 /* sanity check that this is a DIR pointer */
193 if (pDir->dir_ulCookie != __DIRENT_COOKIE)
194 return;
196 /* go back to beginning of directory */
197 rewinddir(pDir);
199 /* loop until we have found position we care about */
200 for (--nPosition; nPosition && readdir(pDir); nPosition--);
202 /* flag invalid nPosition value */
203 if (nPosition)
204 errno = EINVAL;
206 return;