2016-12-19 23:46:28 +01:00
|
|
|
// win32_dirent.c
|
|
|
|
// directory entry functions in Windows platform like Ubix/Linux
|
|
|
|
// opendir(), readdir(), closedir().
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "inc\utf.h"
|
|
|
|
|
|
|
|
#include "inc\dirent.h"
|
|
|
|
#include "inc\libgen.h"
|
2016-12-22 06:17:14 +01:00
|
|
|
#include "misc_internal.h"
|
2016-12-19 23:46:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
struct DIR_ {
|
|
|
|
intptr_t hFile;
|
|
|
|
struct _wfinddata_t c_file;
|
|
|
|
int first;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Open a directory stream on NAME.
|
|
|
|
Return a DIR stream on the directory, or NULL if it could not be opened. */
|
|
|
|
DIR * opendir(const char *name)
|
|
|
|
{
|
|
|
|
struct _wfinddata_t c_file;
|
|
|
|
intptr_t hFile;
|
|
|
|
DIR *pdir;
|
2017-01-15 07:12:19 +01:00
|
|
|
wchar_t searchstr[PATH_MAX];
|
2016-12-19 23:46:28 +01:00
|
|
|
wchar_t* wname = NULL;
|
|
|
|
int needed;
|
|
|
|
|
2016-12-22 06:17:14 +01:00
|
|
|
if ((wname = utf8_to_utf16(sanitized_path(name))) == NULL) {
|
2016-12-19 23:46:28 +01:00
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add *.* for Windows _findfirst() search pattern
|
2017-01-15 07:12:19 +01:00
|
|
|
swprintf_s(searchstr, PATH_MAX, L"%s\\*.*", wname);
|
2016-12-19 23:46:28 +01:00
|
|
|
free(wname);
|
|
|
|
|
|
|
|
if ((hFile = _wfindfirst(searchstr, &c_file)) == -1L)
|
|
|
|
return NULL; /* errno is set by _wfindfirst */
|
|
|
|
else {
|
|
|
|
if ((pdir = malloc(sizeof(DIR))) == NULL) {
|
|
|
|
_findclose(hFile);
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(pdir, 0, sizeof(DIR));
|
|
|
|
pdir->hFile = hFile;
|
|
|
|
memcpy(&pdir->c_file, &c_file, sizeof(c_file));
|
|
|
|
pdir->first = 1;
|
|
|
|
|
|
|
|
return pdir ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the directory stream DIRP.
|
|
|
|
Return 0 if successful, -1 if not. */
|
|
|
|
int closedir(DIR *dirp)
|
|
|
|
{
|
|
|
|
if ( dirp && (dirp->hFile) ) {
|
|
|
|
_findclose( dirp->hFile );
|
|
|
|
dirp->hFile = 0;
|
|
|
|
free (dirp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read a directory entry from DIRP.
|
|
|
|
Return a pointer to a `struct dirent' describing the entry,
|
|
|
|
or NULL for EOF or error. The storage returned may be overwritten
|
|
|
|
by a later readdir call on the same DIR stream. */
|
|
|
|
struct dirent *readdir(void *avp)
|
|
|
|
{
|
2017-01-15 07:12:19 +01:00
|
|
|
static struct dirent pdirentry;
|
2016-12-19 23:46:28 +01:00
|
|
|
struct _wfinddata_t c_file;
|
|
|
|
DIR *dirp = (DIR *)avp;
|
|
|
|
char *tmp = NULL;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (dirp->first) {
|
|
|
|
memcpy(&c_file, &dirp->c_file, sizeof(c_file));
|
|
|
|
dirp->first = 0;
|
|
|
|
}
|
|
|
|
else if (_wfindnext(dirp->hFile, &c_file) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (wcscmp(c_file.name, L".") == 0 || wcscmp(c_file.name, L"..") == 0 )
|
|
|
|
continue;
|
2017-01-15 07:12:19 +01:00
|
|
|
|
|
|
|
if ((tmp = utf16_to_utf8(c_file.name)) == NULL) {
|
2016-12-19 23:46:28 +01:00
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-01-15 07:12:19 +01:00
|
|
|
strncpy(pdirentry.d_name, tmp, strlen(tmp) + 1);
|
2016-12-19 23:46:28 +01:00
|
|
|
free(tmp);
|
|
|
|
|
2017-01-15 07:12:19 +01:00
|
|
|
pdirentry.d_ino = 1; // a fictious one like UNIX to say it is nonzero
|
|
|
|
return &pdirentry ;
|
|
|
|
}
|
2016-12-19 23:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// return last part of a path. The last path being a filename.
|
|
|
|
char *basename(char *path)
|
|
|
|
{
|
|
|
|
char *pdest;
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
return ".";
|
|
|
|
pdest = strrchr(path, '/');
|
|
|
|
if (pdest)
|
|
|
|
return (pdest+1);
|
|
|
|
pdest = strrchr(path, '\\');
|
|
|
|
if (pdest)
|
|
|
|
return (pdest+1);
|
|
|
|
|
|
|
|
return path; // path does not have a slash
|
|
|
|
}
|