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