Skip to content

Instantly share code, notes, and snippets.

@mu578
Last active October 28, 2023 01:35
Show Gist options
  • Save mu578/34aa398c6a63febc1bb8c22573f1b400 to your computer and use it in GitHub Desktop.
Save mu578/34aa398c6a63febc1bb8c22573f1b400 to your computer and use it in GitHub Desktop.
// unistd_msvc_min.h
# if defined(_WIN64)
typedef unsigned __int64 ino_t;
typedef __int64 ssize_t;
typedef __int64 suseconds_t;
typedef unsigned __int64 useconds_t;
# else
typedef unsigned __int32 ino_t;
typedef __int32 ssize_t;
typedef __int32 suseconds_t;
typedef unsigned __int32 useconds_t;
# endif
typedef __int32 pid_t;
typedef __int32 uid_t;
typedef __int32 gid_t;
#include <handleapi.h>
#include <synchapi.h>
#include <io.h>
#include <dirent.h>
#include <stddef.h>
#include <errno.h>
static __forceinline int access (const char * path, int amode) { return _access(path, amode); }
static __forceinline unsigned int alarm (unsigned int seconds) { (void)seconds; return 0U; }
static __forceinline useconds_t ualarm (useconds_t useconds, useconds_t interval) { (void)useconds; (void)interval; return 0U; }
static __forceinline int brk (void * addr) { (void)addr; return 0; }
static __forceinline void * sbrk (intptr_t incr) { (void)incr; _set_errno(ENOMEM); return (void *)-1; }
static __forceinline int chdir (const char * path) { return _chdir(path); }
static __forceinline int chroot (const char * path) { (void)path; return EACCES; }
static __forceinline int chown (const char * path, uid_t owner, gid_t group) { (void)path; (void)owner; (void)group; return EACCES; }
static __forceinline int close (int fildes) { return _close(fildes); }
// so on ...
static __forceinline
int usleep(useconds_t microseconds)
{
int ret = -1;
HANDLE tm;
LARGE_INTEGER ft;
ft.QuadPart = 10Ui64 * microseconds);
ft.QuadPart = -ft.QuadPart;
if ((tm = CreateWaitableTimer(NULL, TRUE, NULL))) {
if (SetWaitableTimer(tm, &ft, 0, NULL, NULL, FALSE)) {
if (WAIT_OBJECT_0 == WaitForSingleObject(tm, INFINITE)) {
ret = 0;
} else {
_set_errno(EINTR);
}
}
CloseHandle(tm);
}
return ret;
}
/* EOF */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment