

//
// flags for 'open'
//

alias O_RDONLY   = 0x0000;
alias O_WRONLY   = 0x0001;
alias O_RDWR     = 0x0002;
alias O_CREAT    = 0x0200;
alias O_EXCL     = 0x0800;
alias O_NOCTTY   = 0x8000;
alias O_TRUNC    = 0x0400;
alias O_APPEND   = 0x0008;
alias O_NONBLOCK = 0x0004;
alias O_NDELAY   = O_NONBLOCK;
alias O_SYNC     = 0x0080;
alias O_FSYNC    = O_SYNC;
alias O_ASYNC    = 0x0040;



//
// flags for 'chmod'
//

alias S_IRWXU = octal(#"0000700");
alias S_IRUSR = octal(#"0000400");
alias S_IWUSR = octal(#"0000200");
alias S_IXUSR = octal(#"0000100");

alias S_IRWXG = octal(#"0000070");
alias S_IRGRP = octal(#"0000040");
alias S_IWGRP = octal(#"0000020");
alias S_IXGRP = octal(#"0000010");

alias S_IRWXO = octal(#"0000007");
alias S_IROTH = octal(#"0000004");
alias S_IWOTH = octal(#"0000002");
alias S_IXOTH = octal(#"0000001");

alias S_ISUID = octal(#"0004000");
alias S_ISGID = octal(#"0002000");
alias S_ISVTX = octal(#"0001000");



//
// 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         = 0x1000;
alias MAP_FILE         = 0x0000;
alias MAP_FIXED        = 0x0010;
alias MAP_HASSEMAPHORE = 0x0200;
alias MAP_PRIVATE      = 0x0002;
alias MAP_SHARED       = 0x0001;

alias MAP_FAILED = RawPointer(-1);



//
// flags for msync
//

alias MS_ASYNC      = 0x0001;
alias MS_SYNC       = 0x0000;
alias MS_INVALIDATE = 0x0002;



//
// flags for dlsym
//

alias RTLD_LAZY = 1;
alias RTLD_NOW = 2;
alias RTLD_LOCAL = 0;
alias RTLD_GLOBAL = 0x100;
alias RTLD_NOLOAD = 0x02000;
alias RTLD_NODELETE = 0x01000;
alias RTLD_NEXT = RawPointer(-1);
alias RTLD_DEFAULT = RawPointer(-2);
alias RTLD_SELF = RawPointer(-3);



//
// socket protocol families
//
alias AF_UNSPEC = UInt8(0);
alias AF_LOCAL = UInt8(1);
alias AF_INET = UInt8(2);
alias AF_INET6 = UInt8(28);



//
// 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 = 0x0004;


//
// signals
//

alias SIGHUP    = 1;
alias SIGINT    = 2;
alias SIGQUIT   = 3;
alias SIGILL    = 4;
alias SIGTRAP   = 5;
alias SIGABRT   = 6;
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 SIGTHR    = 32;
alias SIGLWP    = SIGTHR;

alias SIGRTMIN  = 65;
alias SIGRTMAX  = 126;

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

//
// status flags for wait() calls
//

private alias _WSTOPPED = octal(#"0177");
private _WSTATUS(x) = bitand(Int(x), octal(#"0177"));

alias WCOREFLAG = octal(#"0200");

WIFSTOPPED(x) = _WSTATUS(x) == _WSTOPPED;
WSTOPSIG(x) = bitshr(Int(x), 8);
WIFSIGNALED(x) = _WSTATUS(x) != _WSTOPPED and _WSTATUS(x) != 0;
WTERMSIG(x) = _WSTATUS(x);
WIFEXITED(x) = _WSTATUS(x) == 0;
WEXITSTATUS(x) = bitshr(Int(x), 8);
WIFCONTINUED(x) = x == 0x13;
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);
