ref: d3ed26c37dedef85e3b193724facde2ee24acb64
dir: /libstd/sys-osx.myr/
use "types.use" pkg std = type scno = int64 /* syscall */ type fdopt = int64 /* fd options */ type fd = int64 /* fd */ type mprot = int64 /* memory protection */ type mopt = int64 /* memory mapping options */ type socktype = int64 /* socket type */ type sockproto = int64 /* socket protocol */ type sockfam = uint8 /* socket family */ type timespec = struct sec : uint64 nsec : uint32 ;; type timeval = struct sec : uint64 usec : uint32 ;; type timezone = struct minwest : int32 /* of greenwich */ dsttime : int32 /* nonzero if DST applies */ ;; type clock = union `Clockrealtime `Clockmonotonic ;; type statbuf = struct dev : int32 mode : uint16 nlink : uint32 ino : uint64 /* 32/64? which do I use? */ uid : uint32 gid : uint32 rdev : int32 atime : timespec atimens : timespec mtime : timespec mtimens : timespec ctime : timespec ctimens : timespec btime : timespec btimens : timespec size : off blocks : uint blocksz : uint flags : uint32 gen : uint32 lspare : int32 qspare0 : int64 qspare1 : int64 ;; type rusage = struct utime : timeval /* user time */ stime : timeval /* system time */ _opaque : uint64[14] /* padding (darwin-specific data) */ ;; type utsname = struct system : byte[256] node : byte[256] release : byte[256] version : byte[256] machine : byte[256] ;; type sockaddr = struct len : byte fam : sockfam data : byte[14] /* what is the *actual* length? */ ;; type sockaddr_in = struct len : byte fam : sockfam port : uint16 addr : byte[4] zero : byte[8] ;; type sockaddr_storage = struct len : byte fam : sockfam __pad1 : byte[6] __align : uint64 __pad2 : byte[112] ;; /* open options */ const Ordonly : fdopt = 0x0 const Owronly : fdopt = 0x1 const Ordwr : fdopt = 0x2 const Ondelay : fdopt = 0x4 const Oappend : fdopt = 0x8 const Ocreat : fdopt = 0x200 const Onofollow : fdopt = 0x100 const Otrunc : fdopt = 0x400 /* mmap protection */ const Mprotnone : mprot = 0x0 const Mprotrd : mprot = 0x1 const Mprotwr : mprot = 0x2 const Mprotexec : mprot = 0x4 const Mprotrw : mprot = 0x3 /* mmap options */ const Mshared : mopt = 0x1 const Mpriv : mopt = 0x2 const Mfixed : mopt = 0x10 const Mfile : mopt = 0x0 const Manon : mopt = 0x1000 /* Only on Linux const M32bit : mopt = 0x40 */ /* socket families. INCOMPLETE. */ const Afunspec : sockfam = 0 const Afunix : sockfam = 1 const Afinet : sockfam = 2 const Afinet6 : sockfam = 30 /* socket types. */ const Sockstream : socktype = 1 const Sockdgram : socktype = 2 const Sockraw : socktype = 3 const Sockrdm : socktype = 4 const Sockseqpacket : socktype = 5 /* return value for a failed mapping */ const Mapbad : byte# = -1 castto(byte#) /* syscalls. note, creat() implemented as open(path, Creat|Trunc|Wronly) */ const Syssyscall : scno = 0x2000000 const Sysexit : scno = 0x2000001 const Sysfork : scno = 0x2000002 const Sysread : scno = 0x2000003 const Syswrite : scno = 0x2000004 const Sysopen : scno = 0x2000005 const Sysclose : scno = 0x2000006 const Syswait4 : scno = 0x2000007 const Syslink : scno = 0x2000009 const Sysunlink : scno = 0x200000a const Syschdir : scno = 0x200000c const Sysfchdir : scno = 0x200000d const Sysmknod : scno = 0x200000e const Syschmod : scno = 0x200000f const Syschown : scno = 0x2000010 const Sysgetfsstat : scno = 0x2000012 const Sysgetpid : scno = 0x2000014 const Syssetuid : scno = 0x2000017 const Sysgetuid : scno = 0x2000018 const Sysgeteuid : scno = 0x2000019 const Sysptrace : scno = 0x200001a const Sysrecvmsg : scno = 0x200001b const Syssendmsg : scno = 0x200001c const Sysrecvfrom : scno = 0x200001d const Sysaccept : scno = 0x200001e const Sysgetpeername : scno = 0x200001f const Sysgetsockname : scno = 0x2000020 const Sysaccess : scno = 0x2000021 const Syschflags : scno = 0x2000022 const Sysfchflags : scno = 0x2000023 const Syssync : scno = 0x2000024 const Syskill : scno = 0x2000025 const Sysgetppid : scno = 0x2000027 const Sysdup : scno = 0x2000029 const Syspipe : scno = 0x200002a const Sysgetegid : scno = 0x200002b const Sysprofil : scno = 0x200002c const Syssigaction : scno = 0x200002e const Sysgetgid : scno = 0x200002f const Syssigprocmask : scno = 0x2000030 const Sysgetlogin : scno = 0x2000031 const Syssetlogin : scno = 0x2000032 const Sysacct : scno = 0x2000033 const Syssigpending : scno = 0x2000034 const Syssigaltstack : scno = 0x2000035 const Sysioctl : scno = 0x2000036 const Sysreboot : scno = 0x2000037 const Sysrevoke : scno = 0x2000038 const Syssymlink : scno = 0x2000039 const Sysreadlink : scno = 0x200003a const Sysexecve : scno = 0x200003b const Sysumask : scno = 0x200003c const Syschroot : scno = 0x200003d const Sysmsync : scno = 0x2000041 const Sysvfork : scno = 0x2000042 const Sysmunmap : scno = 0x2000049 const Sysmprotect : scno = 0x200004a const Sysmadvise : scno = 0x200004b const Sysmincore : scno = 0x200004e const Sysgetgroups : scno = 0x200004f const Syssetgroups : scno = 0x2000050 const Sysgetpgrp : scno = 0x2000051 const Syssetpgid : scno = 0x2000052 const Syssetitimer : scno = 0x2000053 const Sysswapon : scno = 0x2000055 const Sysgetitimer : scno = 0x2000056 const Sysgetdtablesize : scno = 0x2000059 const Sysdup2 : scno = 0x200005a const Sysfcntl : scno = 0x200005c const Sysselect : scno = 0x200005d const Sysfsync : scno = 0x200005f const Syssetpriority : scno = 0x2000060 const Syssocket : scno = 0x2000061 const Sysconnect : scno = 0x2000062 const Sysgetpriority : scno = 0x2000064 const Sysbind : scno = 0x2000068 const Syssetsockopt : scno = 0x2000069 const Syslisten : scno = 0x200006a const Syssigsuspend : scno = 0x200006f const Sysgettimeofday : scno = 0x2000074 const Sysgetrusage : scno = 0x2000075 const Sysgetsockopt : scno = 0x2000076 const Sysreadv : scno = 0x2000078 const Syswritev : scno = 0x2000079 const Syssettimeofday : scno = 0x200007a const Sysfchown : scno = 0x200007b const Sysfchmod : scno = 0x200007c const Syssetreuid : scno = 0x200007e const Syssetregid : scno = 0x200007f const Sysrename : scno = 0x2000080 const Sysflock : scno = 0x2000083 const Sysmkfifo : scno = 0x2000084 const Syssendto : scno = 0x2000085 const Sysshutdown : scno = 0x2000086 const Syssocketpair : scno = 0x2000087 const Sysmkdir : scno = 0x2000088 const Sysrmdir : scno = 0x2000089 const Sysutimes : scno = 0x200008a const Sysfutimes : scno = 0x200008b const Sysadjtime : scno = 0x200008c const Sysgethostuuid : scno = 0x200008e const Syssetsid : scno = 0x2000093 const Sysgetpgid : scno = 0x2000097 const Syssetprivexec : scno = 0x2000098 const Syspread : scno = 0x2000099 const Syspwrite : scno = 0x200009a const Sysnfssvc : scno = 0x200009b const Sysstatfs : scno = 0x200009d const Sysfstatfs : scno = 0x200009e const Sysunmount : scno = 0x200009f const Sysgetfh : scno = 0x20000a1 const Sysquotactl : scno = 0x20000a5 const Sysmount : scno = 0x20000a7 const Syscsops : scno = 0x20000a9 const Syswaitid : scno = 0x20000ad const Sysadd_profil : scno = 0x20000b0 const Syskdebug_trace : scno = 0x20000b4 const Syssetgid : scno = 0x20000b5 const Syssetegid : scno = 0x20000b6 const Sysseteuid : scno = 0x20000b7 const Syssigreturn : scno = 0x20000b8 const Syschud : scno = 0x20000b9 const Sysfdatasync : scno = 0x20000bb const Sysstat : scno = 0x20000bc const Sysfstat : scno = 0x20000bd const Syslstat : scno = 0x20000be const Syspathconf : scno = 0x20000bf const Sysfpathconf : scno = 0x20000c0 const Sysgetrlimit : scno = 0x20000c2 const Syssetrlimit : scno = 0x20000c3 const Sysgetdirentries : scno = 0x20000c4 const Sysmmap : scno = 0x20000c5 const Syslseek : scno = 0x20000c7 const Systruncate : scno = 0x20000c8 const Sysftruncate : scno = 0x20000c9 const Sys__sysctl : scno = 0x20000ca const Sysmlock : scno = 0x20000cb const Sysmunlock : scno = 0x20000cc const Sysundelete : scno = 0x20000cd const SysATsocket : scno = 0x20000ce const SysATgetmsg : scno = 0x20000cf const SysATputmsg : scno = 0x20000d0 const SysATPsndreq : scno = 0x20000d1 const SysATPsndrsp : scno = 0x20000d2 const SysATPgetreq : scno = 0x20000d3 const SysATPgetrsp : scno = 0x20000d4 const Sysmkcomplex : scno = 0x20000d8 const Sysstatv : scno = 0x20000d9 const Syslstatv : scno = 0x20000da const Sysfstatv : scno = 0x20000db const Sysgetattrlist : scno = 0x20000dc const Syssetattrlist : scno = 0x20000dd const Sysgetdirentriesattr : scno = 0x20000de const Sysexchangedata : scno = 0x20000df const Syssearchfs : scno = 0x20000e1 const Sysdelete : scno = 0x20000e2 const Syscopyfile : scno = 0x20000e3 const Sysfgetattrlist : scno = 0x20000e4 const Sysfsetattrlist : scno = 0x20000e5 const Syspoll : scno = 0x20000e6 const Syswatchevent : scno = 0x20000e7 const Syswaitevent : scno = 0x20000e8 const Sysmodwatch : scno = 0x20000e9 const Sysgetxattr : scno = 0x20000ea const Sysfgetxattr : scno = 0x20000eb const Syssetxattr : scno = 0x20000ec const Sysfsetxattr : scno = 0x20000ed const Sysremovexattr : scno = 0x20000ee const Sysfremovexattr : scno = 0x20000ef const Syslistxattr : scno = 0x20000f0 const Sysflistxattr : scno = 0x20000f1 const Sysfsctl : scno = 0x20000f2 const Sysinitgroups : scno = 0x20000f3 const Sysposix_spawn : scno = 0x20000f4 const Sysffsctl : scno = 0x20000f5 const Sysnfsclnt : scno = 0x20000f7 const Sysfhopen : scno = 0x20000f8 const Sysminherit : scno = 0x20000fa const Syssemsys : scno = 0x20000fb const Sysmsgsys : scno = 0x20000fc const Sysshmsys : scno = 0x20000fd const Syssemctl : scno = 0x20000fe const Syssemget : scno = 0x20000ff const Syssemop : scno = 0x2000100 const Sysmsgctl : scno = 0x2000102 const Sysmsgget : scno = 0x2000103 const Sysmsgsnd : scno = 0x2000104 const Sysmsgrcv : scno = 0x2000105 const Sysshmat : scno = 0x2000106 const Sysshmctl : scno = 0x2000107 const Sysshmdt : scno = 0x2000108 const Sysshmget : scno = 0x2000109 const Sysshm_open : scno = 0x200010a const Sysshm_unlink : scno = 0x200010b const Syssem_open : scno = 0x200010c const Syssem_close : scno = 0x200010d const Syssem_unlink : scno = 0x200010e const Syssem_wait : scno = 0x200010f const Syssem_trywait : scno = 0x2000110 const Syssem_post : scno = 0x2000111 const Syssem_getvalue : scno = 0x2000112 const Syssem_init : scno = 0x2000113 const Syssem_destroy : scno = 0x2000114 const Sysopen_extended : scno = 0x2000115 const Sysumask_extended : scno = 0x2000116 const Sysstat_extended : scno = 0x2000117 const Syslstat_extended : scno = 0x2000118 const Sysfstat_extended : scno = 0x2000119 const Syschmod_extended : scno = 0x200011a const Sysfchmod_extended : scno = 0x200011b const Sysaccess_extended : scno = 0x200011c const Syssettid : scno = 0x200011d const Sysgettid : scno = 0x200011e const Syssetsgroups : scno = 0x200011f const Sysgetsgroups : scno = 0x2000120 const Syssetwgroups : scno = 0x2000121 const Sysgetwgroups : scno = 0x2000122 const Sysmkfifo_extended : scno = 0x2000123 const Sysmkdir_extended : scno = 0x2000124 const Sysidentitysvc : scno = 0x2000125 const Sysshared_region_check_np : scno = 0x2000126 const Sysshared_region_map_np : scno = 0x2000127 const Sysvm_pressure_monitor : scno = 0x2000128 const Syspsynch_rw_longrdlock : scno = 0x2000129 const Syspsynch_rw_yieldwrlock : scno = 0x200012a const Syspsynch_rw_downgrade : scno = 0x200012b const Syspsynch_rw_upgrade : scno = 0x200012c const Syspsynch_mutexwait : scno = 0x200012d const Syspsynch_mutexdrop : scno = 0x200012e const Syspsynch_cvbroad : scno = 0x200012f const Syspsynch_cvsignal : scno = 0x2000130 const Syspsynch_cvwait : scno = 0x2000131 const Syspsynch_rw_rdlock : scno = 0x2000132 const Syspsynch_rw_wrlock : scno = 0x2000133 const Syspsynch_rw_unlock : scno = 0x2000134 const Syspsynch_rw_unlock2 : scno = 0x2000135 const Sysgetsid : scno = 0x2000136 const Syssettid_with_pid : scno = 0x2000137 const Sysaio_fsync : scno = 0x2000139 const Sysaio_return : scno = 0x200013a const Sysaio_suspend : scno = 0x200013b const Sysaio_cancel : scno = 0x200013c const Sysaio_error : scno = 0x200013d const Sysaio_read : scno = 0x200013e const Sysaio_write : scno = 0x200013f const Syslio_listio : scno = 0x2000140 const Sysiopolicysys : scno = 0x2000142 const Sysmlockall : scno = 0x2000144 const Sysmunlockall : scno = 0x2000145 const Sysissetugid : scno = 0x2000147 const Sys__pthread_kill : scno = 0x2000148 const Sys__pthread_sigmask : scno = 0x2000149 const Sys__sigwait : scno = 0x200014a const Sys__disable_threadsignal : scno = 0x200014b const Sys__pthread_markcancel : scno = 0x200014c const Sys__pthread_canceled : scno = 0x200014d const Sys__semwait_signal : scno = 0x200014e const Sysproc_info : scno = 0x2000150 const Syssendfile : scno = 0x2000151 const Sysstat64 : scno = 0x2000152 const Sysfstat64 : scno = 0x2000153 const Syslstat64 : scno = 0x2000154 const Sysstat64_extended : scno = 0x2000155 const Syslstat64_extended : scno = 0x2000156 const Sysfstat64_extended : scno = 0x2000157 const Sysgetdirentries64 : scno = 0x2000158 const Sysstatfs64 : scno = 0x2000159 const Sysfstatfs64 : scno = 0x200015a const Sysgetfsstat64 : scno = 0x200015b const Sys__pthread_chdir : scno = 0x200015c const Sys__pthread_fchdir : scno = 0x200015d const Sysaudit : scno = 0x200015e const Sysauditon : scno = 0x200015f const Sysgetauid : scno = 0x2000161 const Syssetauid : scno = 0x2000162 const Sysgetaudit : scno = 0x2000163 const Syssetaudit : scno = 0x2000164 const Sysgetaudit_addr : scno = 0x2000165 const Syssetaudit_addr : scno = 0x2000166 const Sysauditctl : scno = 0x2000167 const Sysbsdthread_create : scno = 0x2000168 const Sysbsdthread_terminate : scno = 0x2000169 const Syskqueue : scno = 0x200016a const Syskevent : scno = 0x200016b const Syslchown : scno = 0x200016c const Sysstack_snapshot : scno = 0x200016d const Sysbsdthread_register : scno = 0x200016e const Sysworkq_open : scno = 0x200016f const Sysworkq_kernreturn : scno = 0x2000170 const Syskevent64 : scno = 0x2000171 const Sys__old_semwait_signal : scno = 0x2000172 const Sys__old_semwait_signal_nocancel : scno = 0x2000173 const Systhread_selfid : scno = 0x2000174 const Sys__mac_execve : scno = 0x200017c const Sys__mac_syscall : scno = 0x200017d const Sys__mac_get_file : scno = 0x200017e const Sys__mac_set_file : scno = 0x200017f const Sys__mac_get_link : scno = 0x2000180 const Sys__mac_set_link : scno = 0x2000181 const Sys__mac_get_proc : scno = 0x2000182 const Sys__mac_set_proc : scno = 0x2000183 const Sys__mac_get_fd : scno = 0x2000184 const Sys__mac_set_fd : scno = 0x2000185 const Sys__mac_get_pid : scno = 0x2000186 const Sys__mac_get_lcid : scno = 0x2000187 const Sys__mac_get_lctx : scno = 0x2000188 const Sys__mac_set_lctx : scno = 0x2000189 const Syssetlcid : scno = 0x200018a const Sysgetlcid : scno = 0x200018b const Sysread_nocancel : scno = 0x200018c const Syswrite_nocancel : scno = 0x200018d const Sysopen_nocancel : scno = 0x200018e const Sysclose_nocancel : scno = 0x200018f const Syswait4_nocancel : scno = 0x2000190 const Sysrecvmsg_nocancel : scno = 0x2000191 const Syssendmsg_nocancel : scno = 0x2000192 const Sysrecvfrom_nocancel : scno = 0x2000193 const Sysaccept_nocancel : scno = 0x2000194 const Sysmsync_nocancel : scno = 0x2000195 const Sysfcntl_nocancel : scno = 0x2000196 const Sysselect_nocancel : scno = 0x2000197 const Sysfsync_nocancel : scno = 0x2000198 const Sysconnect_nocancel : scno = 0x2000199 const Syssigsuspend_nocancel : scno = 0x200019a const Sysreadv_nocancel : scno = 0x200019b const Syswritev_nocancel : scno = 0x200019c const Syssendto_nocancel : scno = 0x200019d const Syspread_nocancel : scno = 0x200019e const Syspwrite_nocancel : scno = 0x200019f const Syswaitid_nocancel : scno = 0x20001a0 const Syspoll_nocancel : scno = 0x20001a1 const Sysmsgsnd_nocancel : scno = 0x20001a2 const Sysmsgrcv_nocancel : scno = 0x20001a3 const Syssem_wait_nocancel : scno = 0x20001a4 const Sysaio_suspend_nocancel : scno = 0x20001a5 const Sys__sigwait_nocancel : scno = 0x20001a6 const Sys__semwait_signal_nocancel : scno = 0x20001a7 const Sys__mac_mount : scno = 0x20001a8 const Sys__mac_get_mount : scno = 0x20001a9 const Sys__mac_getfsstat : scno = 0x20001aa const Sysfsgetpath : scno = 0x20001ab const Sysaudit_session_self : scno = 0x20001ac const Sysaudit_session_join : scno = 0x20001ad const Syspid_suspend : scno = 0x20001ae const Syspid_resume : scno = 0x20001af const Sysfileport_makeport : scno = 0x20001b0 const Sysfileport_makefd : scno = 0x20001b1 extern const syscall : (sc:scno, args:... -> int64) /* process control */ const exit : (status:int -> void) const getpid : ( -> int64) const kill : (pid:int64, sig:int64 -> int64) const fork : (-> int64) const wait4 : (pid:int64, loc:int32#, opt : int64, usage:rusage# -> int64) const waitpid : (pid:int64, loc:int32#, opt : int64 -> int64) const execv : (cmd : byte[:], args : byte[:][:] -> int64) const execve : (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64) /* fd manipulation */ const open : (path:byte[:], opts:fdopt, mode:int64 -> fd) const close : (fd:fd -> int64) const creat : (path:byte[:], mode:int64 -> fd) const read : (fd:fd, buf:byte[:] -> size) const write : (fd:fd, buf:byte[:] -> size) const lseek : (fd:fd, off:uint64, whence:int64 -> int64) const stat : (path:byte[:], sb:statbuf# -> int64) const fstat : (fd:fd, sb:statbuf# -> int64) const mkdir : (path : byte[:], mode : int64 -> int64) /* networking */ const socket : (dom : sockfam, stype : socktype, proto : sockproto -> fd) const connect : (sock : fd, addr : sockaddr#, len : size -> int) const accept : (sock : fd, addr : sockaddr#, len : size# -> fd) const listen : (sock : fd, backlog : int -> int) const bind : (sock : fd, addr : sockaddr#, len : size -> int) /* memory mapping */ const munmap : (addr:byte#, len:size -> int64) const mmap : (addr:byte#, len:size, prot:mprot, flags:mopt, fd:fd, off:off -> byte#) /* time */ const gettimeofday : (tv : timeval#, tz : timezone# -> int) const settimeofday : (tv : timeval#, tz : timezone# -> int) /* faked with gettimeofday */ const clock_getres : (clk : clock, ts : timespec# -> int) const clock_gettime : (clk : clock, ts : timespec# -> int) const clock_settime : (clk : clock, ts : timespec# -> int) /* system information */ const uname : (buf : utsname# -> int) const sysctl : (mib : int[:], old : byte[:]#, new : byte[:] -> int) ;; extern const __osx_fork : (->int64) extern const cstring : (str : byte[:] -> byte#) extern const alloca : (sz : size -> byte#) extern const __cenvp : byte## /* process control */ const exit = {status; syscall(Sysexit, status castto(int64))} const getpid = {; -> syscall(Sysgetpid, 1)} const kill = {pid, sig; -> syscall(Syskill, pid, sig)} const fork = {; -> __osx_fork()} const wait4 = {pid, loc, opt, usage; -> syscall(Syswait4, pid, loc, opt, usage)} const waitpid = {pid, loc, opt; -> wait4(pid, loc, opt, 0 castto(rusage#)) } const execv = {cmd, args var p, cargs, i /* doesn't just call execve() for efficiency's sake. */ p = alloca((args.len + 1)*sizeof(byte#)) cargs = (p castto(byte##))[:args.len] for i = 0; i < args.len; i++ cargs[i] = cstring(args[i]) ;; cargs[args.len] = 0 castto(byte#) -> syscall(Sysexecve, cstring(cmd), p, __cenvp) } const execve = {cmd, args, env var cargs, cenv, i var p /* copy the args */ p = alloca((args.len + 1)*sizeof(byte#)) cargs = (p castto(byte##))[:args.len] for i = 0; i < args.len; i++ cargs[i] = cstring(args[i]) ;; cargs[args.len] = 0 castto(byte#) /* copy the env. of course we fucking have to duplicate this code everywhere, since we want to stack allocate... */ p = alloca((env.len + 1)*sizeof(byte#)) cenv = (p castto(byte##))[:env.len] for i = 0; i < env.len; i++ cenv[i] = cstring(env[i]) ;; cenv[env.len] = 0 castto(byte#) -> syscall(Sysexecve, cstring(cmd), p, cenv) } /* fd manipulation */ const open = {path, opts, mode; -> syscall(Sysopen, cstring(path), opts, mode) castto(fd)} const close = {fd; -> syscall(Sysclose, fd)} const creat = {path, mode; -> open(path, Ocreat | Otrunc | Owronly, mode) castto(fd)} const read = {fd, buf; -> syscall(Sysread, fd, buf castto(char#), buf.len castto(size)) castto(size)} const write = {fd, buf; -> syscall(Syswrite, fd, buf castto(char#), buf.len castto(size)) castto(size)} const lseek = {fd, off, whence; -> syscall(Syslseek, fd, off, whence)} const stat = {path, sb; -> syscall(Sysstat, cstring(path), sb)} const fstat = {fd, sb; -> syscall(Sysfstat, fd, sb)} const mkdir = {path, mode; -> syscall(Sysmkdir, cstring(path), mode) castto(int64)} /* networking */ const socket = {dom, stype, proto; -> syscall(Syssocket, dom castto(int64), stype, proto) castto(fd) } const connect = {sock, addr, len; -> syscall(Sysconnect, sock, addr, len) castto(int)} const accept = {sock, addr, len; -> syscall(Sysaccept, sock, addr, len) castto(fd)} const listen = {sock, backlog; -> syscall(Syslisten, sock, backlog castto(int64)) castto(int)} const bind = {sock, addr, len; -> syscall(Sysbind, sock, addr, len) castto(int)} /* memory management */ const munmap = {addr, len; -> syscall(Sysmunmap, addr, len)} const mmap = {addr, len, prot, flags, fd, off; -> syscall(Sysmmap, addr, len, prot, flags, fd, off) castto(byte#)} /* time */ const gettimeofday = {tv, tz; -> syscall(Sysgettimeofday, tv, tz) castto(int)} const settimeofday = {tv, tz; -> syscall(Syssettimeofday, tv, tz) castto(int)} /* faked with gettimeofday */ const clock_getres = {clk, ts ts.sec = 0 ts.nsec = 1000*10 /* 10ms is reasonable resolution */ -> 0 } const clock_gettime = {clk, ts var tv var ret ret = gettimeofday(&tv, 0 castto(timezone#)) ts.sec = tv.sec ts.nsec = tv.usec * 1000 -> ret } const clock_settime = {clk, ts var tv tv.sec = ts.sec tv.usec = ts.nsec / 1000 -> settimeofday(&tv, 0 castto(timezone#)) } /* system information */ const uname = {buf; buf.system[0] = 'D' castto(byte) buf.system[1] = 'a' castto(byte) buf.system[2] = 'r' castto(byte) buf.system[3] = 'w' castto(byte) buf.system[4] = 'i' castto(byte) buf.system[5] = 'n' castto(byte) buf.system[6] = 0 -> 0 /* FIXME: THIS IS BROKEN. Miscompiled? DEBUG IT. var mib : int[2] var ret var sys var nod var rel var ver var mach ret = 0 mib[0] = 1 /* CTL_KERN */ mib[1] = 1 /* KERN_OSTYPE */ sys = buf.system[:] if sysctl(mib[:], &sys, [][:]) < 0 ret = -1 ;; std.put("%s\n", sys) mib[0] = 1 /* CTL_KERN */ mib[1] = 10 /* KERN_HOSTNAME */ nod = buf.node[:] if sysctl(mib[:], &nod, [][:]) < 0 ret = -1 ;; mib[0] = 1 /* CTL_KERN */ mib[1] = 2 /* KERN_OSRELEASE */ rel = buf.release[:] if sysctl(mib[:], &rel, [][:]) < 0 ret = -1 ;; mib[0] = 1 /* CTL_KERN */ mib[1] = 4 /* KERN_VERSION */ ver = buf.version[:] if sysctl(mib[:], &ver, [][:]) < 0 ret = -1 ;; mib[0] = 6 /* CTL_HW */ mib[1] = 1 /* HW_MACHINE */ mach = buf.machine[:] if sysctl(mib[:], &mach, [][:]) < 0 ret = -1 ;; -> ret */ } const sysctl = {mib, old, new var mibp var mibsz var o var oldp var oldsz var newp var newsz var ret mibp = mib castto(byte#) mibsz = mib.len castto(uint64) o = old# oldp = o castto(byte#) oldsz = o.len castto(uint64) newp = new castto(byte#) newsz = new castto(uint64) ret = syscall(Sys__sysctl, mibp, mibsz, oldp, &oldsz, newp, newsz) castto(int) old# = o[:oldsz] -> ret }