blob: 95bceccf4be48050a7a199ed4cd57867984f6b5e [file] [log] [blame]
Jan Kundrátce0606e2021-12-09 17:42:43 +01001#ifndef _UNISTD_H
2#define _UNISTD_H 1
3
Jan Kundrátc9989482021-12-14 18:05:26 +01004/* headers are broken on Windows, which means that some of them simply *have* to come first */
5# include <winsock2.h>
6# include <ws2tcpip.h>
7
Jan Kundrátce0606e2021-12-09 17:42:43 +01008/* This is intended as a drop-in replacement for unistd.h on Windows.
9 * Please add functionality as neeeded.
10 * https://stackoverflow.com/a/826027/1202830
11 */
12
13#include <stdlib.h>
14#include <io.h>
15#include <process.h> /* for getpid() and the exec..() family */
16#include <direct.h> /* for _getcwd() and _chdir() */
17
18#define srandom srand
19#define random rand
20
21/* Values for the second argument to access.
22 These may be OR'd together. */
23#define R_OK 4 /* Test for read permission. */
24#define W_OK 2 /* Test for write permission. */
Jan Kundráte31e4f52021-12-09 22:31:18 +010025#define X_OK 0 /* jkt: unsupported on Windows, so we don't really care */
Jan Kundrátce0606e2021-12-09 17:42:43 +010026#define F_OK 0 /* Test for existence. */
27
28#define access _access
29#define dup2 _dup2
30#define execve _execve
31#define ftruncate _chsize
32#define unlink _unlink
33#define fileno _fileno
34#define getcwd _getcwd
35#define chdir _chdir
36#define isatty _isatty
37#define lseek _lseek
Jan Kundrát8d3229b2021-12-10 16:14:13 +010038#define fsync _commit
Jan Kundrát6e251762021-12-10 16:29:19 +010039#define timegm _mkgmtime
Jan Kundrátce0606e2021-12-09 17:42:43 +010040/* read, write, and close are NOT being #defined here, because while there are file handle specific versions for Windows, they probably don't work for sockets. You need to look at your app and consider whether to call e.g. closesocket(). */
41
42#ifdef _WIN64
43#define ssize_t __int64
44#else
45#define ssize_t long
46#endif
47
48#define STDIN_FILENO 0
49#define STDOUT_FILENO 1
50#define STDERR_FILENO 2
51/* should be in some equivalent to <sys/types.h> */
52typedef __int8 int8_t;
53typedef __int16 int16_t;
54typedef __int32 int32_t;
55typedef __int64 int64_t;
56typedef unsigned __int8 uint8_t;
57typedef unsigned __int16 uint16_t;
58typedef unsigned __int32 uint32_t;
59typedef unsigned __int64 uint64_t;
60
Jan Kundrát054cad32021-12-09 23:34:59 +010061#include <windows.h>
62#ifndef PATH_MAX
63#define PATH_MAX MAX_PATH
64#endif
65#ifndef _POSIX_PATH_MAX
66#define _POSIX_PATH_MAX 256
67#endif
68
Jan Kundrát100c5722021-12-10 16:03:58 +010069#ifndef S_ISREG
70# define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
71#endif
72#ifndef S_ISDIR
73# define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
74#endif
75#ifndef S_IRUSR
76# define S_IRUSR _S_IREAD
77#endif
78#ifndef S_IWUSR
79# define S_IWUSR _S_IWRITE
80#endif
81
Jan Kundrátce0606e2021-12-09 17:42:43 +010082#endif /* unistd.h */