OSDN Git Service

Correct improper naming of assembly language source files.
[mingw/mingw-org-wsl.git] / mingwrt / mingwex / dirent.c
1 /*
2  * dirent.c
3  * This file has no copyright assigned and is placed in the Public Domain.
4  * This file is a part of the mingw-runtime package.
5  * No warranty is given; refer to the file DISCLAIMER within the package.
6  *
7  * Derived from DIRLIB.C by Matt J. Weinstein
8  * This note appears in the DIRLIB.H
9  * DIRLIB.H by M. J. Weinstein   Released to public domain 1-Jan-89
10  *
11  * Updated by Jeremy Bettis <jeremy@hksys.com>
12  * Significantly revised and rewinddir, seekdir and telldir added by Colin
13  * Peters <colin@fu.is.saga-u.ac.jp>
14  *
15  */
16
17 #include <stdlib.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <io.h>
21 #include <direct.h>
22 #include <dirent.h>
23
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h> /* for GetFileAttributes */
26
27 #include <tchar.h>
28 #define SUFFIX  _T("*")
29 #define SLASH   _T("\\")
30
31
32 /* Helper for opendir().  */
33 static inline unsigned _tGetFileAttributes (const _TCHAR * tPath)
34 {
35 #ifdef _UNICODE
36   /* GetFileAttributesW does not work on W9x, so convert to ANSI */
37   if (_osver & 0x8000)
38     {
39       char aPath [MAX_PATH];
40       WideCharToMultiByte (CP_ACP, 0, tPath, -1, aPath, MAX_PATH, NULL,
41                            NULL);
42       return GetFileAttributesA (aPath);
43     }
44   return GetFileAttributesW (tPath);
45 #else
46   return GetFileAttributesA (tPath);
47 #endif
48 }
49
50 /*
51  * opendir
52  *
53  * Returns a pointer to a DIR structure appropriately filled in to begin
54  * searching a directory.
55  */
56 _TDIR *
57 _topendir (const _TCHAR *szPath)
58 {
59   _TDIR *nd;
60   unsigned int rc;
61   _TCHAR szFullPath[MAX_PATH];
62
63   errno = 0;
64
65   if (!szPath)
66     {
67       errno = EFAULT;
68       return (_TDIR *) 0;
69     }
70
71   if (szPath[0] == _T('\0'))
72     {
73       errno = ENOTDIR;
74       return (_TDIR *) 0;
75     }
76
77   /* Attempt to determine if the given path really is a directory. */
78   rc = _tGetFileAttributes (szPath);
79   if (rc == (unsigned int)-1)
80     {
81       /* call GetLastError for more error info */
82       errno = ENOENT;
83       return (_TDIR *) 0;
84     }
85   if (!(rc & FILE_ATTRIBUTE_DIRECTORY))
86     {
87       /* Error, entry exists but not a directory. */
88       errno = ENOTDIR;
89       return (_TDIR *) 0;
90     }
91
92   /* Make an absolute pathname.  */
93   _tfullpath (szFullPath, szPath, MAX_PATH);
94
95   /* Allocate enough space to store DIR structure and the complete
96    * directory path given. */
97   nd = (_TDIR *) malloc (sizeof (_TDIR) + (_tcslen (szFullPath)
98                                            + _tcslen (SLASH)
99                                            + _tcslen (SUFFIX) + 1)
100                                           * sizeof (_TCHAR));
101
102   if (!nd)
103     {
104       /* Error, out of memory. */
105       errno = ENOMEM;
106       return (_TDIR *) 0;
107     }
108
109   /* Create the search expression. */
110   _tcscpy (nd->dd_name, szFullPath);
111
112   /* Add on a slash if the path does not end with one. */
113   if (nd->dd_name[0] != _T('\0')
114       && _tcsrchr (nd->dd_name, _T('/')) != nd->dd_name
115                                             + _tcslen (nd->dd_name) - 1
116       && _tcsrchr (nd->dd_name, _T('\\')) != nd->dd_name
117                                              + _tcslen (nd->dd_name) - 1)
118     {
119       _tcscat (nd->dd_name, SLASH);
120     }
121
122   /* Add on the search pattern */
123   _tcscat (nd->dd_name, SUFFIX);
124
125   /* Initialize handle to -1 so that a premature closedir doesn't try
126    * to call _findclose on it. */
127   nd->dd_handle = -1;
128
129   /* Initialize the status. */
130   nd->dd_stat = 0;
131
132   /* Initialize the dirent structure. ino and reclen are invalid under
133    * Win32, and name simply points at the appropriate part of the
134    * findfirst_t structure. */
135   nd->dd_dir.d_ino = 0;
136   nd->dd_dir.d_reclen = 0;
137   nd->dd_dir.d_namlen = 0;
138   memset (nd->dd_dir.d_name, 0, FILENAME_MAX);
139
140   return nd;
141 }
142
143
144 /*
145  * readdir
146  *
147  * Return a pointer to a dirent structure filled with the information on the
148  * next entry in the directory.
149  */
150 struct _tdirent *
151 _treaddir (_TDIR * dirp)
152 {
153   errno = 0;
154
155   /* Check for valid DIR struct. */
156   if (!dirp)
157     {
158       errno = EFAULT;
159       return (struct _tdirent *) 0;
160     }
161
162   if (dirp->dd_stat < 0)
163     {
164       /* We have already returned all files in the directory
165        * (or the structure has an invalid dd_stat). */
166       return (struct _tdirent *) 0;
167     }
168   else if (dirp->dd_stat == 0)
169     {
170       /* We haven't started the search yet. */
171       /* Start the search */
172       dirp->dd_handle = _tfindfirst (dirp->dd_name, &(dirp->dd_dta));
173
174       if (dirp->dd_handle == -1)
175         {
176           /* Whoops! Seems there are no files in that
177            * directory. */
178           dirp->dd_stat = -1;
179         }
180       else
181         {
182           dirp->dd_stat = 1;
183         }
184     }
185   else
186     {
187       /* Get the next search entry. */
188       if (_tfindnext (dirp->dd_handle, &(dirp->dd_dta)))
189         {
190           /* We are off the end or otherwise error.
191              _findnext sets errno to ENOENT if no more file
192              Undo this. */
193           DWORD winerr = GetLastError ();
194           if (winerr == ERROR_NO_MORE_FILES)
195             errno = 0;
196           _findclose (dirp->dd_handle);
197           dirp->dd_handle = -1;
198           dirp->dd_stat = -1;
199         }
200       else
201         {
202           /* Update the status to indicate the correct
203            * number. */
204           dirp->dd_stat++;
205         }
206     }
207
208   if (dirp->dd_stat > 0)
209     {
210       /* Successfully got an entry. Everything about the file is
211        * already appropriately filled in except the length of the
212        * file name. */
213       dirp->dd_dir.d_namlen = _tcslen (dirp->dd_dta.name);
214       _tcscpy (dirp->dd_dir.d_name, dirp->dd_dta.name);
215       return &dirp->dd_dir;
216     }
217
218   return (struct _tdirent *) 0;
219 }
220
221
222 /*
223  * closedir
224  *
225  * Frees up resources allocated by opendir.
226  */
227 int
228 _tclosedir (_TDIR * dirp)
229 {
230   int rc;
231
232   errno = 0;
233   rc = 0;
234
235   if (!dirp)
236     {
237       errno = EFAULT;
238       return -1;
239     }
240
241   if (dirp->dd_handle != -1)
242     {
243       rc = _findclose (dirp->dd_handle);
244     }
245
246   /* Delete the dir structure. */
247   free (dirp);
248
249   return rc;
250 }
251
252 /*
253  * rewinddir
254  *
255  * Return to the beginning of the directory "stream". We simply call findclose
256  * and then reset things like an opendir.
257  */
258 void
259 _trewinddir (_TDIR * dirp)
260 {
261   errno = 0;
262
263   if (!dirp)
264     {
265       errno = EFAULT;
266       return;
267     }
268
269   if (dirp->dd_handle != -1)
270     {
271       _findclose (dirp->dd_handle);
272     }
273
274   dirp->dd_handle = -1;
275   dirp->dd_stat = 0;
276 }
277
278 /*
279  * telldir
280  *
281  * Returns the "position" in the "directory stream" which can be used with
282  * seekdir to go back to an old entry. We simply return the value in stat.
283  */
284 long
285 _ttelldir (_TDIR * dirp)
286 {
287   errno = 0;
288
289   if (!dirp)
290     {
291       errno = EFAULT;
292       return -1;
293     }
294   return dirp->dd_stat;
295 }
296
297 /*
298  * seekdir
299  *
300  * Seek to an entry previously returned by telldir. We rewind the directory
301  * and call readdir repeatedly until either dd_stat is the position number
302  * or -1 (off the end). This is not perfect, in that the directory may
303  * have changed while we weren't looking. But that is probably the case with
304  * any such system.
305  */
306 void
307 _tseekdir (_TDIR * dirp, long lPos)
308 {
309   errno = 0;
310
311   if (!dirp)
312     {
313       errno = EFAULT;
314       return;
315     }
316
317   if (lPos < -1)
318     {
319       /* Seeking to an invalid position. */
320       errno = EINVAL;
321       return;
322     }
323   else if (lPos == -1)
324     {
325       /* Seek past end. */
326       if (dirp->dd_handle != -1)
327         {
328           _findclose (dirp->dd_handle);
329         }
330       dirp->dd_handle = -1;
331       dirp->dd_stat = -1;
332     }
333   else
334     {
335       /* Rewind and read forward to the appropriate index. */
336       _trewinddir (dirp);
337
338       while ((dirp->dd_stat < lPos) && _treaddir (dirp))
339         ;
340     }
341 }