Jan Kundrát | 049729d | 2021-12-09 17:42:43 +0100 | [diff] [blame] | 1 | #ifndef _UNISTD_H |
| 2 | #define _UNISTD_H 1 |
| 3 | |
| 4 | /* This is intended as a drop-in replacement for unistd.h on Windows. |
| 5 | * Please add functionality as neeeded. |
| 6 | * https://stackoverflow.com/a/826027/1202830 |
| 7 | */ |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <io.h> |
| 11 | #include <process.h> /* for getpid() and the exec..() family */ |
| 12 | #include <direct.h> /* for _getcwd() and _chdir() */ |
| 13 | |
| 14 | #define srandom srand |
| 15 | #define random rand |
| 16 | |
| 17 | /* Values for the second argument to access. |
| 18 | These may be OR'd together. */ |
| 19 | #define R_OK 4 /* Test for read permission. */ |
| 20 | #define W_OK 2 /* Test for write permission. */ |
Jan Kundrát | 4bcc96f | 2021-12-09 22:31:18 +0100 | [diff] [blame] | 21 | #define X_OK 0 /* jkt: unsupported on Windows, so we don't really care */ |
Jan Kundrát | 049729d | 2021-12-09 17:42:43 +0100 | [diff] [blame] | 22 | #define F_OK 0 /* Test for existence. */ |
| 23 | |
| 24 | #define access _access |
| 25 | #define dup2 _dup2 |
| 26 | #define execve _execve |
| 27 | #define ftruncate _chsize |
| 28 | #define unlink _unlink |
| 29 | #define fileno _fileno |
| 30 | #define getcwd _getcwd |
| 31 | #define chdir _chdir |
| 32 | #define isatty _isatty |
| 33 | #define lseek _lseek |
Jan Kundrát | 32b5a88 | 2021-12-10 16:14:13 +0100 | [diff] [blame] | 34 | #define fsync _commit |
Jan Kundrát | 1d050e5 | 2021-12-10 16:29:19 +0100 | [diff] [blame] | 35 | #define timegm _mkgmtime |
Jan Kundrát | 049729d | 2021-12-09 17:42:43 +0100 | [diff] [blame] | 36 | /* 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(). */ |
| 37 | |
| 38 | #ifdef _WIN64 |
| 39 | #define ssize_t __int64 |
| 40 | #else |
| 41 | #define ssize_t long |
| 42 | #endif |
| 43 | |
| 44 | #define STDIN_FILENO 0 |
| 45 | #define STDOUT_FILENO 1 |
| 46 | #define STDERR_FILENO 2 |
| 47 | /* should be in some equivalent to <sys/types.h> */ |
| 48 | typedef __int8 int8_t; |
| 49 | typedef __int16 int16_t; |
| 50 | typedef __int32 int32_t; |
| 51 | typedef __int64 int64_t; |
| 52 | typedef unsigned __int8 uint8_t; |
| 53 | typedef unsigned __int16 uint16_t; |
| 54 | typedef unsigned __int32 uint32_t; |
| 55 | typedef unsigned __int64 uint64_t; |
| 56 | |
Jan Kundrát | e3d3355 | 2021-12-09 23:34:59 +0100 | [diff] [blame] | 57 | #include <windows.h> |
| 58 | #ifndef PATH_MAX |
| 59 | #define PATH_MAX MAX_PATH |
| 60 | #endif |
| 61 | #ifndef _POSIX_PATH_MAX |
| 62 | #define _POSIX_PATH_MAX 256 |
| 63 | #endif |
| 64 | |
Jan Kundrát | 73aa07a | 2021-12-10 16:03:58 +0100 | [diff] [blame] | 65 | #ifndef S_ISREG |
| 66 | # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG) |
| 67 | #endif |
| 68 | #ifndef S_ISDIR |
| 69 | # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR) |
| 70 | #endif |
| 71 | #ifndef S_IRUSR |
| 72 | # define S_IRUSR _S_IREAD |
| 73 | #endif |
| 74 | #ifndef S_IWUSR |
| 75 | # define S_IWUSR _S_IWRITE |
| 76 | #endif |
| 77 | |
Jan Kundrát | 049729d | 2021-12-09 17:42:43 +0100 | [diff] [blame] | 78 | #endif /* unistd.h */ |