Code

fdbfb70a627ccea462e683a56616f2613522daae
[git.git] / compat / msvc.c
1 #include "../git-compat-util.h"
2 #include "win32.h"
3 #include <conio.h>
4 #include "../strbuf.h"
6 DIR *opendir(const char *name)
7 {
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;
16         }
18         /* check if it's a directory */
19         if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
20                 errno = ENOTDIR;
21                 return NULL;
22         }
24         /* check that the pattern won't be too long for FindFirstFileA */
25         len = strlen(name);
26         if (is_dir_sep(name[len - 1]))
27                 len--;
28         if (len + 2 >= MAX_PATH) {
29                 errno = ENAMETOOLONG;
30                 return NULL;
31         }
33         p = malloc(sizeof(DIR) + len + 2);
34         if (!p)
35                 return NULL;
37         memset(p, 0, sizeof(DIR) + len + 2);
38         strcpy(p->dd_name, name);
39         p->dd_name[len] = '/';
40         p->dd_name[len+1] = '*';
42         p->dd_handle = (long)INVALID_HANDLE_VALUE;
43         return p;
44 }
45 int closedir(DIR *dir)
46 {
47         if (!dir) {
48                 errno = EBADF;
49                 return -1;
50         }
52         if (dir->dd_handle != (long)INVALID_HANDLE_VALUE)
53                 FindClose((HANDLE)dir->dd_handle);
54         free(dir);
55         return 0;
56 }
58 #include "mingw.c"