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