VirtualBox

source: kBuild/vendor/gnumake/3.82-cvs/w32/compat/dirent.c

Last change on this file was 2580, checked in by bird, 12 years ago

Importing the make-3-82 CVS tag with --auto-props but no keywords.

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1/* Directory entry code for Window platforms.
2Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
32006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4This file is part of GNU Make.
5
6GNU Make is free software; you can redistribute it and/or modify it under the
7terms of the GNU General Public License as published by the Free Software
8Foundation; either version 3 of the License, or (at your option) any later
9version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16this program. If not, see <http://www.gnu.org/licenses/>. */
17
18
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"
26
27
28DIR*
29opendir(const char* pDirName)
30{
31 struct stat sb;
32 DIR* pDir;
33 char* pEndDirName;
34 int nBufferLen;
35
36 /* sanity checks */
37 if (!pDirName) {
38 errno = EINVAL;
39 return NULL;
40 }
41 if (stat(pDirName, &sb) != 0) {
42 errno = ENOENT;
43 return NULL;
44 }
45 if ((sb.st_mode & S_IFMT) != S_IFDIR) {
46 errno = ENOTDIR;
47 return NULL;
48 }
49
50 /* allocate a DIR structure to return */
51 pDir = (DIR *) malloc(sizeof (DIR));
52
53 if (!pDir)
54 return NULL;
55
56 /* input directory name length */
57 nBufferLen = strlen(pDirName);
58
59 /* copy input directory name to DIR buffer */
60 strcpy(pDir->dir_pDirectoryName, pDirName);
61
62 /* point to end of the copied directory name */
63 pEndDirName = &pDir->dir_pDirectoryName[nBufferLen - 1];
64
65 /* if directory name did not end in '/' or '\', add '/' */
66 if ((*pEndDirName != '/') && (*pEndDirName != '\\')) {
67 pEndDirName++;
68 *pEndDirName = '/';
69 }
70
71 /* now append the wildcard character to the buffer */
72 pEndDirName++;
73 *pEndDirName = '*';
74 pEndDirName++;
75 *pEndDirName = '\0';
76
77 /* other values defaulted */
78 pDir->dir_nNumFiles = 0;
79 pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
80 pDir->dir_ulCookie = __DIRENT_COOKIE;
81
82 return pDir;
83}
84
85void
86closedir(DIR *pDir)
87{
88 /* got a valid pointer? */
89 if (!pDir) {
90 errno = EINVAL;
91 return;
92 }
93
94 /* sanity check that this is a DIR pointer */
95 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
96 errno = EINVAL;
97 return;
98 }
99
100 /* close the WINDOWS32 directory handle */
101 if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
102 FindClose(pDir->dir_hDirHandle);
103
104 free(pDir);
105
106 return;
107}
108
109struct dirent *
110readdir(DIR* pDir)
111{
112 WIN32_FIND_DATA wfdFindData;
113
114 if (!pDir) {
115 errno = EINVAL;
116 return NULL;
117 }
118
119 /* sanity check that this is a DIR pointer */
120 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
121 errno = EINVAL;
122 return NULL;
123 }
124
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;
131
132 /* bump count for next call to readdir() or telldir() */
133 pDir->dir_nNumFiles++;
134
135 /* fill in struct dirent values */
136 pDir->dir_sdReturn.d_ino = (ino_t)-1;
137 strcpy(pDir->dir_sdReturn.d_name, wfdFindData.cFileName);
138
139 return &pDir->dir_sdReturn;
140}
141
142void
143rewinddir(DIR* pDir)
144{
145 if (!pDir) {
146 errno = EINVAL;
147 return;
148 }
149
150 /* sanity check that this is a DIR pointer */
151 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
152 errno = EINVAL;
153 return;
154 }
155
156 /* close the WINDOWS32 directory handle */
157 if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
158 if (!FindClose(pDir->dir_hDirHandle))
159 errno = EBADF;
160
161 /* reset members which control readdir() */
162 pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
163 pDir->dir_nNumFiles = 0;
164
165 return;
166}
167
168int
169telldir(DIR* pDir)
170{
171 if (!pDir) {
172 errno = EINVAL;
173 return -1;
174 }
175
176 /* sanity check that this is a DIR pointer */
177 if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
178 errno = EINVAL;
179 return -1;
180 }
181
182 /* return number of times readdir() called */
183 return pDir->dir_nNumFiles;
184}
185
186void
187seekdir(DIR* pDir, long nPosition)
188{
189 if (!pDir)
190 return;
191
192 /* sanity check that this is a DIR pointer */
193 if (pDir->dir_ulCookie != __DIRENT_COOKIE)
194 return;
195
196 /* go back to beginning of directory */
197 rewinddir(pDir);
198
199 /* loop until we have found position we care about */
200 for (--nPosition; nPosition && readdir(pDir); nPosition--);
201
202 /* flag invalid nPosition value */
203 if (nPosition)
204 errno = EINVAL;
205
206 return;
207}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette