

//
// flags for 'open'
//

alias O_RDONLY   = octal(#"00");
alias O_WRONLY   = octal(#"01");
alias O_RDWR     = octal(#"02");
alias O_CREAT    = octal(#"0100");
alias O_EXCL     = octal(#"0200");
alias O_NOCTTY   = octal(#"0400");
alias O_TRUNC    = octal(#"01000");
alias O_APPEND   = octal(#"02000");
alias O_NONBLOCK = octal(#"04000");
alias O_NDELAY   = O_NONBLOCK;
alias O_SYNC     = octal(#"010000");
alias O_FSYNC    = O_SYNC;
alias O_ASYNC    = octal(#"020000");



//
// 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_SHARED    = 1;
alias MAP_PRIVATE   = 2;
alias MAP_32BIT     = 64;
alias MAP_ANONYMOUS = 32;
alias MAP_FILE      = 0;
alias MAP_FIXED     = 16;
alias MAP_GROWSDOWN = 256;
alias MAP_LOCKED    = 8192;
alias MAP_NONBLOCK  = 65536;
alias MAP_NORESERVE = 16384;
alias MAP_POPULATE  = 32768;
alias MAP_STACK     = 131072;

alias MAP_FAILED = RawPointer(-1);



//
// flags for msync
//

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



//
// 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;



//
// 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 = 1;

alias SO_REUSEADDR = 2;


//
// signals
//

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


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

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

alias WCOREFLAG = 0x80;

WEXITSTATUS(x) = bitshr(bitand(Int(x), 0xff00), 8);
WSTOPSIG(x) = WEXITSTATUS(x);
WIFCONTINUED(x) = x == 0xffff;
WIFSTOPPED(x) = bitand(Int(x), 0xff) == 0x7f;
WIFEXITED(x) = WTERMSIG(x) == 0;
WIFSIGNALED(x) = bitshr((bitand(Int(x), 0x7f) + 1), 1) > 0;
WTERMSIG(x) = bitand(Int(x), 0x7f);
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);

//
// clock ids for clock_gettime() etc.
//

alias CLOCK_REALTIME            = 0;
alias CLOCK_MONOTONIC           = 1;
alias CLOCK_PROCESS_CPUTIME_ID  = 2;
alias CLOCK_THREAD_CPUTIME_ID   = 3;
alias CLOCK_MONOTONIC_RAW       = 4;
