ddat interop with dat js cable interop with cabal js ddat's frontend = dat-thang cable fe = cable-thang dillo support based on cel's dillo-dat bootstrap node xplatform support Signed-off-by: divo <418@cryspace.space>
54 lines
1.4 KiB
C
54 lines
1.4 KiB
C
#ifndef DATGPT_WIN_DIRENT_H
|
|
#define DATGPT_WIN_DIRENT_H
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#define NAME_MAX MAX_PATH
|
|
typedef struct dirent { char d_name[MAX_PATH]; } struct_dirent_t;
|
|
typedef struct dat_win_dir {
|
|
HANDLE handle;
|
|
WIN32_FIND_DATAA data;
|
|
int first;
|
|
struct dirent entry;
|
|
} DIR;
|
|
static DIR *dat_opendir(const char *path)
|
|
{
|
|
DIR *dir = (DIR *)calloc(1, sizeof(*dir));
|
|
char pattern[MAX_PATH * 2];
|
|
int n;
|
|
if (!dir) return NULL;
|
|
n = snprintf(pattern, sizeof(pattern), "%s\\*", path);
|
|
if (n < 0 || (size_t)n >= sizeof(pattern)) { free(dir); errno = ENAMETOOLONG; return NULL; }
|
|
dir->handle = FindFirstFileA(pattern, &dir->data);
|
|
if (dir->handle == INVALID_HANDLE_VALUE) { free(dir); errno = ENOENT; return NULL; }
|
|
dir->first = 1;
|
|
return dir;
|
|
}
|
|
static struct dirent *dat_readdir(DIR *dir)
|
|
{
|
|
if (!dir) return NULL;
|
|
if (dir->first) dir->first = 0;
|
|
else if (!FindNextFileA(dir->handle, &dir->data)) return NULL;
|
|
strncpy(dir->entry.d_name, dir->data.cFileName, sizeof(dir->entry.d_name) - 1);
|
|
dir->entry.d_name[sizeof(dir->entry.d_name) - 1] = 0;
|
|
return &dir->entry;
|
|
}
|
|
static int dat_closedir(DIR *dir)
|
|
{
|
|
int rc;
|
|
if (!dir) { errno = EINVAL; return -1; }
|
|
rc = FindClose(dir->handle) ? 0 : -1;
|
|
free(dir);
|
|
return rc;
|
|
}
|
|
#define opendir dat_opendir
|
|
#define readdir dat_readdir
|
|
#define closedir dat_closedir
|
|
#else
|
|
#include <dirent.h>
|
|
#endif
|
|
#endif
|