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 #include "dirent_on_windows.h"
11
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 #include "allocation.h"
18
19 #if defined(__cplusplus)
20 extern "C" {
21 #endif
22
23 typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */
24
25 struct DIR {
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
opendir(const VkAllocationCallbacks * pAllocator,const char * name)32 DIR *opendir(const VkAllocationCallbacks *pAllocator, const char *name) {
33 DIR *dir = 0;
34
35 if (name && name[0]) {
36 size_t base_length = strlen(name);
37 const char *all = /* search pattern must end with suitable wildcard */
38 strchr("/\\", name[base_length - 1]) ? "*" : "/*";
39 size_t full_length = base_length + strlen(all) + 1;
40
41 if ((dir = (DIR *)loader_alloc(pAllocator, sizeof *dir, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)) != 0 &&
42 (dir->name = (char *)loader_calloc(pAllocator, full_length, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)) != 0) {
43 loader_strncpy(dir->name, full_length, name, base_length);
44 loader_strncat(dir->name, full_length, all, strlen(all));
45
46 if ((dir->handle = (handle_type)_findfirst(dir->name, &dir->info)) != -1) {
47 dir->result.d_name = 0;
48 } else /* rollback */
49 {
50 loader_free(pAllocator, dir->name);
51 loader_free(pAllocator, dir);
52 dir = 0;
53 }
54 } else /* rollback */
55 {
56 loader_free(pAllocator, dir);
57 dir = 0;
58 errno = ENOMEM;
59 }
60 } else {
61 errno = EINVAL;
62 }
63
64 return dir;
65 }
66
closedir(const VkAllocationCallbacks * pAllocator,DIR * dir)67 int closedir(const VkAllocationCallbacks *pAllocator, DIR *dir) {
68 int result = -1;
69
70 if (dir) {
71 if (dir->handle != -1) {
72 result = _findclose(dir->handle);
73 }
74
75 loader_free(pAllocator, dir->name);
76 loader_free(pAllocator, dir);
77 }
78
79 if (result == -1) /* map all errors to EBADF */
80 {
81 errno = EBADF;
82 }
83
84 return result;
85 }
86
readdir(DIR * dir)87 struct dirent *readdir(DIR *dir) {
88 struct dirent *result = 0;
89
90 if (dir && dir->handle != -1) {
91 if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) {
92 result = &dir->result;
93 result->d_name = dir->info.name;
94 }
95 // _findnext sets errno to ENOENT when no more matching files could be found, does not indicate an error
96 if (errno == ENOENT) {
97 errno = 0;
98 }
99 } else {
100 errno = EBADF;
101 }
102
103 return result;
104 }
105
rewinddir(DIR * dir)106 void rewinddir(DIR *dir) {
107 if (dir && dir->handle != -1) {
108 _findclose(dir->handle);
109 dir->handle = (handle_type)_findfirst(dir->name, &dir->info);
110 dir->result.d_name = 0;
111 } else {
112 errno = EBADF;
113 }
114 }
115
116 #if defined(__cplusplus)
117 }
118 #endif
119
120 /*
121
122 Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved.
123 Copyright (c) 2015 The Khronos Group Inc.
124 Copyright (c) 2015 Valve Corporation
125 Copyright (c) 2015 LunarG, Inc.
126 Permission to use, copy, modify, and distribute this software and its
127 documentation for any purpose is hereby granted without fee, provided
128 that this copyright and permissions notice appear in all copies and
129 derivatives.
130
131 This software is supplied "as is" without express or implied warranty.
132
133 But that said, if there are any problems please get in touch.
134
135 */
136