Skip to content

Commit c5f52b8

Browse files
stinospfalcon
authored andcommitted
msvc: Add dirent.h/.c implementation
This fixes the build after adding directory iteration in d874702
1 parent 755b014 commit c5f52b8

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

windows/msvc/dirent.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "dirent.h"
28+
#include <errno.h>
29+
#include <Windows.h>
30+
31+
typedef struct DIR {
32+
HANDLE findHandle;
33+
WIN32_FIND_DATA findData;
34+
struct dirent result;
35+
} DIR;
36+
37+
DIR *opendir(const char *name) {
38+
if (!name || !*name) {
39+
errno = ENOENT;
40+
return NULL;
41+
}
42+
43+
DIR *dir = malloc(sizeof(DIR));
44+
if (!dir) {
45+
errno = ENOMEM;
46+
return NULL;
47+
}
48+
dir->result.d_ino = 0;
49+
dir->result.d_name = NULL;
50+
dir->findHandle = INVALID_HANDLE_VALUE;
51+
52+
const size_t nameLen = strlen(name);
53+
char *path = malloc(nameLen + 3); // allocate enough for adding "/*"
54+
if (!path) {
55+
free(dir);
56+
errno = ENOMEM;
57+
return NULL;
58+
}
59+
strcpy(path, name);
60+
61+
// assure path ends with wildcard
62+
const char lastChar = path[nameLen - 1];
63+
if (lastChar != '*') {
64+
const char *appendWC = (lastChar != '/' && lastChar != '\\') ? "/*" : "*";
65+
strcat(path, appendWC);
66+
}
67+
68+
// init
69+
dir->findHandle = FindFirstFile(path, &dir->findData);
70+
free(path);
71+
if (dir->findHandle == INVALID_HANDLE_VALUE) {
72+
free(dir);
73+
errno = ENOENT;
74+
return NULL;
75+
}
76+
return dir;
77+
}
78+
79+
int closedir(DIR *dir) {
80+
if (dir) {
81+
FindClose(dir->findHandle);
82+
free(dir);
83+
return 0;
84+
} else {
85+
errno = EBADF;
86+
return -1;
87+
}
88+
}
89+
90+
struct dirent *readdir(DIR *dir) {
91+
if (!dir) {
92+
errno = EBADF;
93+
return NULL;
94+
}
95+
96+
// first pass d_name is NULL so use result from FindFirstFile in opendir, else use FindNextFile
97+
if (!dir->result.d_name || FindNextFile(dir->findHandle, &dir->findData)) {
98+
dir->result.d_name = dir->findData.cFileName;
99+
return &dir->result;
100+
}
101+
102+
return NULL;
103+
}

windows/msvc/dirent.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// dirent.h implementation for msvc
28+
29+
// for ino_t
30+
#include <sys/types.h>
31+
32+
// opaque DIR structure
33+
typedef struct DIR DIR;
34+
35+
// the dirent structure
36+
// d_ino is always 0 - if ever needed use GetFileInformationByHandle
37+
typedef struct dirent {
38+
ino_t d_ino;
39+
char *d_name;
40+
} dirent;
41+
42+
DIR *opendir(const char *name);
43+
int closedir(DIR *dir);
44+
struct dirent *readdir(DIR *dir);

0 commit comments

Comments
 (0)