-
Notifications
You must be signed in to change notification settings - Fork 0
/
minirent.h
136 lines (110 loc) · 3.61 KB
/
minirent.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2021 Alexey Kutepov <reximkut@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ============================================================
//
// minirent — 0.0.1 — A subset of dirent interface for Windows.
//
// https://github.com/tsoding/minirent
//
// ============================================================
//
// ChangeLog (https://semver.org/ is implied)
//
// 0.0.1 First Official Release
#ifndef MINIRENT_H_
#define MINIRENT_H_
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
struct dirent
{
char d_name[MAX_PATH+1];
};
typedef struct DIR DIR;
DIR *opendir(const char *dirpath);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);
#endif // MINIRENT_H_
#ifdef MINIRENT_IMPLEMENTATION
struct DIR
{
HANDLE hFind;
WIN32_FIND_DATA data;
struct dirent *dirent;
};
DIR *opendir(const char *dirpath)
{
assert(dirpath);
char buffer[MAX_PATH];
snprintf(buffer, MAX_PATH, "%s\\*", dirpath);
DIR *dir = (DIR*)calloc(1, sizeof(DIR));
dir->hFind = FindFirstFile(buffer, &dir->data);
if (dir->hFind == INVALID_HANDLE_VALUE) {
// TODO: opendir should set errno accordingly on FindFirstFile fail
// https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror
errno = ENOSYS;
goto fail;
}
return dir;
fail:
if (dir) {
free(dir);
}
return NULL;
}
struct dirent *readdir(DIR *dirp)
{
assert(dirp);
if (dirp->dirent == NULL) {
dirp->dirent = (struct dirent*)calloc(1, sizeof(struct dirent));
} else {
if(!FindNextFile(dirp->hFind, &dirp->data)) {
if (GetLastError() != ERROR_NO_MORE_FILES) {
// TODO: readdir should set errno accordingly on FindNextFile fail
// https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror
errno = ENOSYS;
}
return NULL;
}
}
memset(dirp->dirent->d_name, 0, sizeof(dirp->dirent->d_name));
strncpy(
dirp->dirent->d_name,
dirp->data.cFileName,
sizeof(dirp->dirent->d_name) - 1);
return dirp->dirent;
}
int closedir(DIR *dirp)
{
assert(dirp);
if(!FindClose(dirp->hFind)) {
// TODO: closedir should set errno accordingly on FindClose fail
// https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror
errno = ENOSYS;
return -1;
}
if (dirp->dirent) {
free(dirp->dirent);
}
free(dirp);
return 0;
}
#endif // MINIRENT_IMPLEMENTATION