// POSIX compatibility support for posix. #pragma once // posix.h - POSIX type and constant definitions for posix.dll // Covers just what open/read/write/close/lseek/spawn/pthread/chdir need. #include #include #include // errno. #ifdef errno #undef errno #endif #ifdef EAGAIN #undef EAGAIN #endif #ifdef ENOSYS #undef ENOSYS #endif #ifdef EWOULDBLOCK #undef EWOULDBLOCK #endif #ifdef EINPROGRESS #undef EINPROGRESS #endif #ifdef ETIMEDOUT #undef ETIMEDOUT #endif #ifdef ECONNRESET #undef ECONNRESET #endif #ifdef ENOTSOCK #undef ENOTSOCK #endif extern int errno; // defined in posix_fd.c; single global in v1 #define EPERM 1 #define ENOENT 2 #define ESRCH 3 #define EINTR 4 #define EIO 5 #define EBADF 9 #define ENOMEM 12 #define EACCES 13 #define EEXIST 17 #define ENODEV 19 #define ENOTDIR 20 #define EINVAL 22 #define ESPIPE 29 #define EMFILE 24 #define ENOSPC 28 #define ERANGE 34 #define EAGAIN 35 #define EWOULDBLOCK EAGAIN #define ENOSYS 38 #define ENOTSOCK 88 #define EINPROGRESS 115 #define ETIMEDOUT 116 #define ECONNRESET 104 // open() flags. #define O_RDONLY 0x0000 #define O_WRONLY 0x0001 #define O_RDWR 0x0002 #define O_CREAT 0x0040 #define O_TRUNC 0x0200 #define O_APPEND 0x0400 // lseek() whence. #define SEEK_SET 0 #define SEEK_CUR 1 #define SEEK_END 2 // mmap() protection and mapping flags. #define PROT_NONE 0x0 #define PROT_READ 0x1 #define PROT_WRITE 0x2 #define PROT_EXEC 0x4 #define MAP_SHARED 0x01 #define MAP_PRIVATE 0x02 #define MAP_FAILED ((void*)(intptr_t)-1) #define MS_SYNC 0x04 #define MS_ASYNC 0x01 #define MS_INVALIDATE 0x02 // Scalar types. typedef int32_t pid_t; typedef int32_t off_t; typedef uint32_t mode_t; typedef int64_t ssize_t; __declspec(dllexport) void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset); __declspec(dllexport) int munmap(void* addr, size_t length); __declspec(dllexport) int msync(void* addr, size_t length, int flags); // Sockets. typedef uint16_t sa_family_t; typedef uint16_t in_port_t; typedef uint32_t in_addr_t; typedef uint32_t socklen_t; #define AF_INET 2 #define PF_INET AF_INET #define SOCK_STREAM 1 #define SOCK_DGRAM 2 #define SOCK_RAW 3 #define IPPROTO_ICMP 1 #define IPPROTO_TCP 6 #define IPPROTO_UDP 17 #define SHUT_RD 0 #define SHUT_WR 1 #define SHUT_RDWR 2 #define SOL_SOCKET 1 #define SO_RCVTIMEO 1 #define SO_SNDTIMEO 2 #define SO_NONBLOCK 3 #define INADDR_ANY ((in_addr_t)0x00000000u) #define INADDR_NONE ((in_addr_t)0xFFFFFFFFu) struct in_addr { in_addr_t s_addr; }; struct sockaddr { sa_family_t sa_family; char sa_data[14]; }; struct sockaddr_in { sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; }; struct hostent { char* h_name; char** h_aliases; int h_addrtype; int h_length; char** h_addr_list; }; #define h_addr h_addr_list[0] struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; socklen_t ai_addrlen; struct sockaddr* ai_addr; char* ai_canonname; struct addrinfo* ai_next; }; #define AI_PASSIVE 0x0001 struct pollfd { int fd; short events; short revents; }; #define POLLIN 0x0001 #define POLLOUT 0x0002 #define POLLERR 0x0004 #define POLLHUP 0x0008 #define POLLNVAL 0x0010 typedef unsigned long nfds_t; typedef struct timeval { long tv_sec; long tv_usec; } timeval; __declspec(dllexport) uint16_t htons(uint16_t x); __declspec(dllexport) uint16_t ntohs(uint16_t x); __declspec(dllexport) uint32_t htonl(uint32_t x); __declspec(dllexport) uint32_t ntohl(uint32_t x); __declspec(dllexport) in_addr_t inet_addr(const char* text); __declspec(dllexport) char* inet_ntoa(struct in_addr in); __declspec(dllexport) int socket(int domain, int type, int protocol); __declspec(dllexport) int bind(int fd, const struct sockaddr* addr, socklen_t addrlen); __declspec(dllexport) int listen(int fd, int backlog); __declspec(dllexport) int accept(int fd, struct sockaddr* addr, socklen_t* addrlen); __declspec(dllexport) int connect(int fd, const struct sockaddr* addr, socklen_t addrlen); __declspec(dllexport) ssize_t send(int fd, const void* buf, size_t len, int flags); __declspec(dllexport) ssize_t recv(int fd, void* buf, size_t len, int flags); __declspec(dllexport) ssize_t sendto(int fd, const void* buf, size_t len, int flags, const struct sockaddr* dest, socklen_t destlen); __declspec(dllexport) ssize_t recvfrom(int fd, void* buf, size_t len, int flags, struct sockaddr* src, socklen_t* srclen); __declspec(dllexport) int shutdown(int fd, int how); __declspec(dllexport) int setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen); __declspec(dllexport) int getsockopt(int fd, int level, int optname, void* optval, socklen_t* optlen); __declspec(dllexport) int poll(struct pollfd* fds, nfds_t nfds, int timeout); __declspec(dllexport) struct hostent* gethostbyname(const char* name); __declspec(dllexport) int getaddrinfo(const char* node, const char* service, const struct addrinfo* hints, struct addrinfo** res); __declspec(dllexport) void freeaddrinfo(struct addrinfo* res); // pthread types. typedef struct { uint32_t Handle; uint32_t Tid; } pthread_t; typedef struct { uint32_t Handle; } pthread_mutex_t; typedef void* pthread_attr_t; // unused in v1 typedef void* pthread_mutexattr_t; // posix_spawn types (file_actions and attrp both ignored in v1). typedef void* posix_spawn_file_actions_t; typedef void* posix_spawnattr_t; __declspec(dllexport) const char* posix_get_command_line(void); // dirent (used by posix_dir.c). #define NAME_MAX 259 typedef struct dirent { char d_name[NAME_MAX + 1]; unsigned char d_type; // DT_REG or DT_DIR } dirent; #define DT_UNKNOWN 0 #define DT_REG 8 #define DT_DIR 4 typedef struct DIR { uint32_t KHandle; // kernel DIR_OBJECT handle dirent Current; bool Valid; } DIR;