win32: dirent: handle errors
[git/mingw/j6t.git] / compat / msvc.c
blob199eb220f42032c1b50dd226dbea084cc10ccd38
1 #include "../git-compat-util.h"
2 #include "win32.h"
3 #include <conio.h>
4 #include "../strbuf.h"
6 DIR *opendir(const char *name)
8 DWORD attrs = GetFileAttributes(name);
9 int len;
10 DIR *p;
12 /* check for valid path */
13 if (attrs == INVALID_FILE_ATTRIBUTES) {
14 errno = ENOENT;
15 return NULL;
18 /* check if it's a directory */
19 if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
20 errno = ENOTDIR;
21 return NULL;
24 /* check that the pattern won't be too long for FindFirstFileA */
25 len = strlen(name);
26 if (len + 2 >= MAX_PATH) {
27 errno = ENAMETOOLONG;
28 return NULL;
31 p = malloc(sizeof(DIR) + len + 2);
32 if (!p)
33 return NULL;
35 memset(p, 0, sizeof(DIR) + len + 2);
36 strcpy(p->dd_name, name);
37 p->dd_name[len] = '/';
38 p->dd_name[len+1] = '*';
40 p->dd_handle = (long)INVALID_HANDLE_VALUE;
41 return p;
43 int closedir(DIR *dir)
45 if (!dir) {
46 errno = EBADF;
47 return -1;
50 if (dir->dd_handle != (long)INVALID_HANDLE_VALUE)
51 FindClose((HANDLE)dir->dd_handle);
52 free(dir);
53 return 0;
56 #include "mingw.c"