minimalc/sys.h

148 lines
5.2 KiB
C

#ifndef MINIMALSYS_H
#define MINIMALSYS_H
#include "int.h"
void exit(int8_t status);
#define STDIN 0
#define STDOUT 1
#define STDERR 2
intptr_t write(int32_t fd, const void* buf, intptr_t size);
intptr_t read(int32_t fd, const void* buf, intptr_t size);
#define O_RDONLY 00
#define O_WRONLY 01
#define O_RDWR 02
#define AT_FDCWD -100
#if defined(__mips__)
// linux/arch/mips/include/uapi/asm/fcntl.h
#define O_APPEND 0x0008
#define O_CREAT 0x0100 /* not fcntl */
#define O_TRUNC 0x0200 /* not fcntl */
#define O_EXCL 0x0400 /* not fcntl */
#define O_NOCTTY 0x0800 /* not fcntl */
#else
// linux/include/uapi/asm-generic/fcntl.h
#define O_CREAT 1<<6
#define O_EXCL 1<<7
#define O_NOCTTY 1<<8
#define O_TRUNC 1<<9
#define O_APPEND 1<<10
#endif
int32_t openat(int32_t fd, const char* filename, uint32_t flags, uint32_t mode);
#define open(...) openat(AT_FDCWD, __VA_ARGS__, 0)
#define openc(...) openat(AT_FDCWD, __VA_ARGS__)
int close(unsigned int fd);
int fsync(unsigned int fd);
// Socket syscalls
int socket(int domain, int type, int protocol);
int setsockopt(int sockfd, int level, int optname, const void* optval, socklen_t optlen);
int bind(int sockfd, const void* sockaddr, socklen_t addrlen);
int listen(int fd, int backlog);
int accept(int sockfd, void* addr, socklen_t* addrlen);
int connect(int sockfd, void* addr, socklen_t addrlen);
ssize_t sendto(int sockfd, const void* buf, size_t size, int flags, const void* sockaddr, socklen_t addrlen);
#define send(sockfd, buf, size, flags) sendto(sockfd, buf, size, flags, 0, 0)
ssize_t recvfrom(int sockfd, const void* buf, size_t size, int flags, const void* sockaddr, socklen_t addrlen);
#define recv(sockfd, buf, size, flags) recvfrom(sockfd, buf, size, flags, 0, 0)
// Socket Domain constants
typedef unsigned short sa_family_t;
/* Supported address families. */
#define AF_UNIX 1 /* Unix domain sockets */
#define AF_LOCAL 1 /* POSIX name for AF_UNIX */
#define AF_INET 2 /* Internet IP Protocol */
#define AF_AX25 3 /* Amateur Radio AX.25 */
#define AF_INET6 10 /* IP version 6 */
#define AF_ROSE 11 /* Amateur Radio X.25 PLP */
#define AF_PACKET 17 /* Packet family */
#define AF_CAN 29 /* Controller Area Network */
#define AF_BLUETOOTH 31 /* Bluetooth sockets */
// Socket type constants
#if defined(__mips__)
// File: include/asm-mips/socket.h
enum sock_type {
SOCK_DGRAM = 1,
SOCK_STREAM = 2,
SOCK_RAW = 3,
SOCK_RDM = 4,
SOCK_SEQPACKET = 5,
SOCK_DCCP = 6,
SOCK_PACKET = 10,
};
#define SOL_SOCKET 0xffff
#define SO_DEBUG 0x0001 /* Record debugging information. */
#define SO_REUSEADDR 0x0004 /* Allow reuse of local addresses. */
#define SO_TYPE 0x1008 /* Compatible name for SO_STYLE. */
#define SO_ERROR 0x1007 /* get error status and clear */
#define SO_DONTROUTE 0x0010 /* Don't do local routing. */
#define SO_BROADCAST 0x0020 /* Allow transmission of broadcast messages. */
#define SO_SNDBUF 0x1001 /* Send buffer size. */
#define SO_RCVBUF 0x1002 /* Receive buffer. */
#define SO_KEEPALIVE 0x0008 /* Keep connections alive and send SIGPIPE when they die. */
#define SO_OOBINLINE 0x0100 /* Receive out-of-band data in-band. */
#define SO_LINGER 0x0080 /* Block on close of a reliable socket to transmit pending data. */
#define SO_REUSEPORT 0x0200 /* Allow local address and port reuse. */
#define SO_RCVLOWAT 0x1004 /* receive low-water mark */
#define SO_SNDLOWAT 0x1003 /* send low-water mark */
#define SO_RCVTIMEO 0x1006 /* receive timeout */
#define SO_SNDTIMEO 0x1005 /* send timeout */
#else
enum sock_type {
SOCK_STREAM = 1,
SOCK_DGRAM = 2,
SOCK_RAW = 3,
SOCK_RDM = 4,
SOCK_SEQPACKET = 5,
SOCK_DCCP = 6,
SOCK_PACKET = 10,
};
#define SOL_SOCKET 1
#define SO_DEBUG 1
#define SO_REUSEADDR 2
#define SO_TYPE 3
#define SO_ERROR 4
#define SO_DONTROUTE 5
#define SO_BROADCAST 6
#define SO_SNDBUF 7
#define SO_RCVBUF 8
#define SO_KEEPALIVE 9
#define SO_OOBINLINE 10
#define SO_LINGER 13
#define SO_REUSEPORT 15
#define SO_RCVLOWAT 18
#define SO_SNDLOWAT 19
#define SO_RCVTIMEO 20
#define SO_SNDTIMEO 21
#endif
#define SO_STYLE SO_TYPE /* Synonym */
/* linux-specific, might as well be the same as on i386 */
#define SO_NO_CHECK 11
#define SO_PRIORITY 12
#define SO_BSDCOMPAT 14
#define SO_PASSCRED 17
#define SO_PEERCRED 18
// Time management
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
#define CLOCK_PROCESS_CPUTIME_ID 2
#define CLOCK_THREAD_CPUTIME_ID 3
#define TIMER_ABSTIME 0x01
int clock_settime(clockid_t clockid, const struct timespec *tp);
int clock_gettime(clockid_t clockid, struct timespec *tp);
int clock_getres(clockid_t clockid, struct timespec *res);
int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *t, struct timespec* remain);
#define nanosleep(t, remain) clock_nanosleep(CLOCK_MONOTONIC, 0, t, remain)
uint32_t fork();
void *memset(void* s, int c, unsigned long n);
#endif // !MINIMALSYS_H