summaryrefslogtreecommitdiff
path: root/src/lib/evil/evil_dirent.c
blob: 6f6b86a7388ff84c8006417ddbece08f408ce6e9 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>

#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN

#include "evil_macro.h"
#include "evil_stdio.h"


struct DIR
{
   struct dirent   dirent;
   WIN32_FIND_DATA data;
   HANDLE          handle;
};


DIR *opendir(char const *name)
{
   DIR     *dir;
   char    *tmp;
   DWORD    attr;
   size_t   l;
#ifdef UNICODE
   wchar_t *wname;
   char    *d_name;
#endif

   /* valid name */
   if (!name || !*name)
     {
        errno = ENOENT;
        return NULL;
     }

#ifdef UNICODE
   wname = evil_char_to_wchar(name);
   if (!wname)
     {
        errno = ENOMEM;
        return NULL;
     }

   if((attr = GetFileAttributes(wname)) == 0xFFFFFFFF)
#else
   if((attr = GetFileAttributes(name)) == 0xFFFFFFFF)
#endif
     {
        errno = ENOENT;
        return NULL;
     }

#ifdef UNICODE
   free(wname);
#endif

   /* directory */
   if (!(attr & FILE_ATTRIBUTE_DIRECTORY))
     {
        errno = ENOTDIR;
        return NULL;
     }

   dir = (DIR *)malloc(sizeof(DIR));
   if (!dir)
     {
        errno = ENOMEM;
        return NULL;
     }

   l = strlen(name);
   tmp = (char *)malloc(sizeof(char) * l + 5);
   if (!tmp)
     {
        errno = ENOMEM;
        free(dir);
        return NULL;
     }

   memcpy(tmp, name, l);
   memcpy(tmp + l, "\\*.*", 5);

   EVIL_PATH_SEP_UNIX_TO_WIN32(tmp);

#ifdef UNICODE
   wname = evil_char_to_wchar(tmp);
   if (!wname)
     {
        errno = ENOMEM;
        free(tmp);
        free(dir);
        return NULL;
     }
   dir->handle = FindFirstFile(wname, &dir->data);
   free(wname);
#else
   dir->handle = FindFirstFile(tmp, &dir->data);
#endif

   free(tmp);

   if (dir->handle == INVALID_HANDLE_VALUE)
     {
        free(dir);
        return NULL;
     }

#ifdef UNICODE
   d_name = evil_wchar_to_char(dir->data.cFileName);
   strcpy(dir->dirent.d_name, d_name);
   free(d_name);
#else
   strcpy(dir->dirent.d_name, dir->data.cFileName);
#endif
   dir->dirent.d_mode = (int)dir->data.dwFileAttributes;

   if (dir->data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
     dir->dirent.d_type = DT_DIR;
   else
     dir->dirent.d_type = DT_UNKNOWN;

   return dir;
}

int closedir(DIR *dir)
{
   if (!dir)
     {
        errno = EBADF;
        return -1;
     }

   if (dir->handle != INVALID_HANDLE_VALUE)
     FindClose(dir->handle);
   free(dir);

   return 0;
}

struct dirent *readdir(DIR *dir)
{
#ifdef UNICODE
   char *d_name;
#endif

   if (!dir)
     {
        errno = EBADF;
        return NULL;
     }

   if (dir->handle == INVALID_HANDLE_VALUE)
     return NULL;

#ifdef UNICODE
     d_name = evil_wchar_to_char(dir->data.cFileName);
     strcpy(dir->dirent.d_name, d_name);
     free(d_name);
#else
     strcpy(dir->dirent.d_name, dir->data.cFileName);
#endif

   if (!FindNextFile(dir->handle, &dir->data))
     {
        FindClose(dir->handle);
        dir->handle = INVALID_HANDLE_VALUE;
     }

   return &dir->dirent;
}