2008-11-01 10:07:41 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
|
|
|
#include <dirent.h>
|
2013-01-10 12:25:44 -08:00
|
|
|
#include <errno.h>
|
2008-11-01 10:07:41 -07:00
|
|
|
|
|
|
|
#include "Evil.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct DIR
|
|
|
|
{
|
|
|
|
struct dirent dirent;
|
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
HANDLE handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
DIR *opendir(char const *name)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
2013-03-18 15:40:03 -07:00
|
|
|
char *tmp;
|
2008-11-01 10:07:41 -07:00
|
|
|
DWORD attr;
|
2009-01-11 22:03:51 -08:00
|
|
|
size_t l;
|
2008-11-01 10:07:41 -07:00
|
|
|
#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
|
|
|
|
|
2009-01-19 09:51:17 -08:00
|
|
|
/* directory */
|
|
|
|
if (!(attr & FILE_ATTRIBUTE_DIRECTORY))
|
2008-11-01 10:07:41 -07:00
|
|
|
{
|
|
|
|
errno = ENOTDIR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-19 09:51:17 -08:00
|
|
|
dir = (DIR *)malloc(sizeof(DIR));
|
|
|
|
if (!dir)
|
2008-11-01 10:07:41 -07:00
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
l = strlen(name);
|
2013-03-18 15:40:03 -07:00
|
|
|
tmp = (char *)malloc(sizeof(char) * l + 5);
|
|
|
|
if (!tmp)
|
2008-11-01 10:07:41 -07:00
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-18 15:40:03 -07:00
|
|
|
memcpy(tmp, name, l);
|
|
|
|
memcpy(tmp + l, "\\*.*", 5);
|
2008-11-01 10:07:41 -07:00
|
|
|
|
2013-03-18 15:40:03 -07:00
|
|
|
EVIL_PATH_SEP_UNIX_TO_WIN32(tmp);
|
2008-11-01 10:07:41 -07:00
|
|
|
|
|
|
|
#ifdef UNICODE
|
2013-03-18 15:40:03 -07:00
|
|
|
wname = evil_char_to_wchar(tmp);
|
2008-11-01 10:07:41 -07:00
|
|
|
if (!wname)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
2013-03-18 15:40:03 -07:00
|
|
|
free(tmp);
|
2008-11-01 10:07:41 -07:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dir->handle = FindFirstFile(wname, &dir->data);
|
|
|
|
free(wname);
|
|
|
|
#else
|
2013-03-18 15:40:03 -07:00
|
|
|
dir->handle = FindFirstFile(tmp, &dir->data);
|
2008-11-01 10:07:41 -07:00
|
|
|
#endif
|
|
|
|
|
2013-03-18 15:40:03 -07:00
|
|
|
free(tmp);
|
2008-11-01 10:07:41 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-10-22 09:47:28 -07:00
|
|
|
if (dir->data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
dir->dirent.d_type = DT_DIR;
|
|
|
|
else
|
|
|
|
dir->dirent.d_type = DT_UNKNOWN;
|
|
|
|
|
2008-11-01 10:07:41 -07:00
|
|
|
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;
|
|
|
|
}
|