import byteorder.*;


//
// flags for 'open'
//

alias O_RDONLY   = 0;
alias O_WRONLY   = 1;
alias O_RDWR     = 2;
alias O_NONBLOCK = 4;
alias O_APPEND   = 8;
alias O_CREAT    = 0x200;
alias O_TRUNC    = 0x400;
alias O_EXCL     = 0x800;
alias O_SHLOCK   = 0x10;
alias O_EXLOCK   = 0x20;
alias O_NOFOLLOW = 0x100;
alias O_SYMLINK  = 0x200000;
alias O_EVTONLY  = 0x8000;



//
// flags for 'chmod'/'stat'
//

alias S_IRWXU = octal(#"0700");
alias S_IRUSR = octal(#"0400");
alias S_IWUSR = octal(#"0200");
alias S_IXUSR = octal(#"0100");

alias S_IRWXG = octal(#"070");
alias S_IRGRP = octal(#"040");
alias S_IWGRP = octal(#"020");
alias S_IXGRP = octal(#"010");

alias S_IRWXO = octal(#"07");
alias S_IROTH = octal(#"04");
alias S_IWOTH = octal(#"02");
alias S_IXOTH = octal(#"01");

alias S_ISUID = octal(#"04000");
alias S_ISGID = octal(#"02000");
alias S_ISVTX = octal(#"01000");

alias S_IFMT   = octal(#"0170000");
alias S_IFIFO  = octal(#"0010000");
alias S_IFCHR  = octal(#"0020000");
alias S_IFDIR  = octal(#"0040000");
alias S_IFBLK  = octal(#"0060000");
alias S_IFREG  = octal(#"0100000");
alias S_IFLNK  = octal(#"0120000");
alias S_IFSOCK = octal(#"0140000");
alias S_IFWHT  = octal(#"0160000");


//
// flags for lseek
//

alias SEEK_SET = 0;
alias SEEK_CUR = 1;
alias SEEK_END = 2;



//
// flags for mmap
//

alias PROT_NONE  = 0;
alias PROT_READ  = 1;
alias PROT_WRITE = 2;
alias PROT_EXEC  = 4;

alias MAP_ANON         = 4096;
alias MAP_FILE         = 0;
alias MAP_FIXED        = 16;
alias MAP_HASSEMAPHORE = 512;
alias MAP_PRIVATE      = 2;
alias MAP_SHARED       = 1;
alias MAP_NOCACHE      = 1024;

alias MAP_FAILED = RawPointer(-1);



//
// flags for msync
//

alias MS_ASYNC      = 1;
alias MS_SYNC       = 16;
alias MS_INVALIDATE = 2;



//
// flags for dlsym
//

alias RTLD_LAZY = 0x1;
alias RTLD_NOW = 0x2;
alias RTLD_LOCAL = 0x4;
alias RTLD_GLOBAL = 0x8;
alias RTLD_NOLOAD = 0x10;
alias RTLD_NODELETE = 0x80;
alias RTLD_FIRST = 0x100;
alias RTLD_NEXT = RawPointer(-1);
alias RTLD_DEFAULT = RawPointer(-2);
alias RTLD_SELF = RawPointer(-3);
alias RTLD_MAIN_ONLY = RawPointer(-5);



//
// socket protocol families
//
alias AF_UNSPEC = 0uss;
alias AF_LOCAL = 1uss;
alias AF_INET = 2uss;
alias AF_INET6 = 30uss;



//
// socket protocol families
//
alias PF_UNSPEC = AF_UNSPEC;
alias PF_LOCAL = AF_LOCAL;
alias PF_INET = AF_INET;
alias PF_INET6 = AF_INET6;



//
// socket types
//
alias SOCK_STREAM = 1;
alias SOCK_DGRAM = 2;
alias SOCK_RAW = 3;



//
// IP protocols
//
alias IPPROTO_ICMP   =  1;
alias IPPROTO_IGMP   =  2;
alias IPPROTO_TCP    =  6;
alias IPPROTO_UDP    = 17;
alias IPPROTO_ICMPV6 = 58;



//
// special IPv4 addresses
//
alias INADDR_ANY       = 0x00_00_00_00u;
alias INADDR_BROADCAST = 0xFF_FF_FF_FFu;
alias INADDR_LOOPBACK  = 0x7F_00_00_01u;
alias INADDR_NONE      = 0xFF_FF_FF_FFu;
alias INADDR_UNSPEC_GROUP    = 0xE0_00_00_00u;
alias INADDR_ALLHOSTS_GROUP  = 0xE0_00_00_01u;
alias INADDR_ALLRTRS_GROUP   = 0xE0_00_00_02u;
alias INADDR_MAX_LOCAL_GROUP = 0xE0_00_00_FFu;



//
// getsockopt/setsockopt constants
//
alias SOL_SOCKET = 0xffff;

alias SO_REUSEADDR = 4;



//
// byte order macros
//
htons(x: UInt16) = hostToNetwork(x);
ntohs(x: UInt16) = networkToHost(x);

htonl(x: UInt32) = hostToNetwork(x);
ntohl(x: UInt32) = networkToHost(x);



//
// signals
//

alias SIGHUP    = 1;
alias SIGINT    = 2;
alias SIGQUIT   = 3;
alias SIGILL    = 4;
alias SIGTRAP   = 5;
alias SIGABRT   = 6;
alias SIGPOLL   = 7;
alias SIGIOT    = SIGABRT;
alias SIGEMT    = 7;
alias SIGFPE    = 8;
alias SIGKILL   = 9;
alias SIGBUS    = 10;
alias SIGSEGV   = 11;
alias SIGSYS    = 12;
alias SIGPIPE   = 13;
alias SIGALRM   = 14;
alias SIGTERM   = 15;
alias SIGURG    = 16;
alias SIGSTOP   = 17;
alias SIGTSTP   = 18;
alias SIGCONT   = 19;
alias SIGCHLD   = 20;
alias SIGTTIN   = 21;
alias SIGTTOU   = 22;
alias SIGIO     = 23;
alias SIGXCPU   = 24;
alias SIGXFSZ   = 25;
alias SIGVTALRM = 26;
alias SIGPROF   = 27;
alias SIGWINCH  = 28;
alias SIGINFO   = 29;
alias SIGUSR1   = 30;
alias SIGUSR2   = 31;

alias SIG_DFL   = CCodePointer[[Int],[]](0);
alias SIG_IGN   = CCodePointer[[Int],[]](1);
alias SIG_HOLD  = CCodePointer[[Int],[]](5);
alias SIG_ERR   = CCodePointer[[Int],[]](-1);

private alias _WSTOPPED = 0x7f;
private _WSTATUS(x) = bitand(Int(x), 0x7f);

alias WCOREFLAG = 0x80;

WEXITSTATUS(x) = bitand(bitshr(Int(x), 8), 0xff);
WSTOPSIG(x) = bitshr(Int(x), 8);
WIFCONTINUED(x) = _WSTATUS(x) == _WSTOPPED and WSTOPSIG(x) == SIGCONT;
WIFSTOPPED(x) = _WSTATUS(x) == _WSTOPPED and WSTOPSIG(x) != SIGCONT;
WIFEXITED(x) = _WSTATUS(x) == 0;
WIFSIGNALED(x) = _WSTATUS(x) != _WSTOPPED and _WSTATUS(x) != 0;
WTERMSIG(x) = _WSTATUS(x);
WCOREDUMP(x) = bitand(Int(x), WCOREFLAG) != 0;
W_EXITCODE(ret, sig) = bitor(bitshl(Int(ret), 8), Int(sig));
W_STOPCODE(sig) = bitor(bitshl(Int(sig), 8), _WSTOPPED);

