1 /*
2 
3     Implementation of POSIX directory browsing functions and types for Win32.
4 
5     Author:  Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
6     History: Created March 1997. Updated June 2003 and July 2012.
7     Rights:  See end of file.
8 
9 */
10 
11 #include "precompiled.h"
12 
13 #include "dirent.h"
14 #include <errno.h>
15 #include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #ifdef __cplusplus
20 extern "C"
21 {
22 #endif
23 
24 typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */
25 
26 struct DIR
27 {
28     handle_type         handle; /* -1 for failed rewind */
29     struct _finddata_t  info;
30     struct dirent       result; /* d_name null iff first time */
31     char                *name;  /* null-terminated char string */
32 };
33 
opendir(const char * name)34 DIR *opendir(const char *name)
35 {
36     DIR *dir = 0;
37 
38     if(name && name[0])
39     {
40         size_t base_length = strlen(name);
41         const char *all = /* search pattern must end with suitable wildcard */
42             strchr("/\\", name[base_length - 1]) ? "*" : "/*";
43 
44         if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
45            (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
46         {
47             strcat(strcpy(dir->name, name), all);
48 
49             if((dir->handle =
50                 (handle_type) _findfirst(dir->name, &dir->info)) != -1)
51             {
52                 dir->result.d_name = 0;
53             }
54             else /* rollback */
55             {
56                 free(dir->name);
57                 free(dir);
58                 dir = 0;
59             }
60         }
61         else /* rollback */
62         {
63             free(dir);
64             dir   = 0;
65             errno = ENOMEM;
66         }
67     }
68     else
69     {
70         errno = EINVAL;
71     }
72 
73     return dir;
74 }
75 
closedir(DIR * dir)76 int closedir(DIR *dir)
77 {
78     int result = -1;
79 
80     if(dir)
81     {
82         if(dir->handle != -1)
83         {
84             result = _findclose(dir->handle);
85         }
86 
87         free(dir->name);
88         free(dir);
89     }
90 
91     if(result == -1) /* map all errors to EBADF */
92     {
93         errno = EBADF;
94     }
95 
96     return result;
97 }
98 
readdir(DIR * dir)99 struct dirent *readdir(DIR *dir)
100 {
101     struct dirent *result = 0;
102 
103     if(dir && dir->handle != -1)
104     {
105         if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
106         {
107             result         = &dir->result;
108             result->d_name = dir->info.name;
109         }
110     }
111     else
112     {
113         errno = EBADF;
114     }
115 
116     return result;
117 }
118 
rewinddir(DIR * dir)119 void rewinddir(DIR *dir)
120 {
121     if(dir && dir->handle != -1)
122     {
123         _findclose(dir->handle);
124         dir->handle = (handle_type) _findfirst(dir->name, &dir->info);
125         dir->result.d_name = 0;
126     }
127     else
128     {
129         errno = EBADF;
130     }
131 }
132 
133 #ifdef __cplusplus
134 }
135 #endif
136 
137 /*
138 
139     Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved.
140 
141     Permission to use, copy, modify, and distribute this software and its
142     documentation for any purpose is hereby granted without fee, provided
143     that this copyright and permissions notice appear in all copies and
144     derivatives.
145 
146     This software is supplied "as is" without express or implied warranty.
147 
148     But that said, if there are any problems please get in touch.
149 
150 */
151