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