| 1 | (* The C library: what raz's own library calls in it, and what most |
| 2 | programs reach for; this file is macOS's, libSystem's, C.raz the |
| 3 | others'. A program that imports C uses C, as every program on a Mac |
| 4 | does. |
| 5 | |
| 6 | libSystem has every call of C.raz but those Linux's C library alone |
| 7 | has, which are procedures here that fail as C's calls do, -1 with |
| 8 | errno ENOSYS, as an unknown system call does: memfd_create, |
| 9 | clock_nanosleep, getrandom, accept4, close_range, sched_getaffinity |
| 10 | and pipe2 (Sys makes them of what libSystem has, in CAbi). errno is |
| 11 | __error here, and __errno_location gives it. stdout and stderr are |
| 12 | __stdoutp and __stderrp, put in variables of those names first. *) |
| 13 | module C; |
| 14 | import SYSTEM; |
| 15 | |
| 16 | extern proc write*(fd: I32; buf: ^U8; n: UINT): INT; |
| 17 | extern proc read*(fd: I32; buf: ^U8; n: UINT): INT; |
| 18 | extern proc open*(path: ^U8; flags: I32; ...): I32; |
| 19 | extern proc creat*(path: ^U8; mode: U32): I32; |
| 20 | extern proc close*(fd: I32): I32; |
| 21 | extern proc mmap*(addr: ^U8; n: UINT; prot, flags, fd: I32; off: I64): ^U8; |
| 22 | extern proc munmap*(addr: ^U8; n: UINT): I32; |
| 23 | extern proc mprotect*(addr: ^U8; n: UINT; prot: I32): I32; |
| 24 | extern proc isatty*(fd: I32): I32; |
| 25 | extern proc exit*(status: I32); |
| 26 | extern proc _exit*(status: I32); |
| 27 | extern proc __error: ^I32; |
| 28 | extern proc sched_yield*: I32; |
| 29 | extern proc pthread_create*(t: ^UINT; attr: ^U8; |
| 30 | start: proc(arg: ^U8): ^U8; arg: ^U8): I32; |
| 31 | extern proc pthread_join*(t: UINT; result: ^^U8): I32; |
| 32 | extern proc rename*(old, new: ^U8): I32; |
| 33 | extern proc unlink*(path: ^U8): I32; |
| 34 | extern proc mkdir*(path: ^U8; mode: U32): I32; |
| 35 | extern proc rmdir*(path: ^U8): I32; |
| 36 | extern proc chmod*(path: ^U8; mode: U32): I32; |
| 37 | extern proc chown*(path: ^U8; uid, gid: U32): I32; |
| 38 | extern proc mkfifo*(path: ^U8; mode: U32): I32; |
| 39 | extern proc utimensat*(dir: I32; path, times: ^U8; flags: I32): I32; |
| 40 | extern proc readlink*(path, buf: ^U8; n: UINT): INT; |
| 41 | extern proc lseek*(fd: I32; off: I64; whence: I32): I64; |
| 42 | extern proc opendir*(path: ^U8): ^U8; (* DIR * *) |
| 43 | extern proc readdir*(d: ^U8): ^U8; (* struct dirent * *) |
| 44 | extern proc closedir*(d: ^U8): I32; |
| 45 | extern proc tcgetattr*(fd: I32; t: ^U8): I32; |
| 46 | extern proc tcsetattr*(fd: I32; when: I32; t: ^U8): I32; |
| 47 | extern proc cfmakeraw*(t: ^U8); |
| 48 | extern proc ioctl*(fd: I32; request: UINT; ...): I32; |
| 49 | extern proc poll*(fds: ^U8; n: UINT; ms: I32): I32; |
| 50 | extern proc fcntl*(fd, cmd: I32; ...): I32; |
| 51 | extern proc socket*(domain, kind, protocol: I32): I32; |
| 52 | extern proc connect*(fd: I32; addr: ^U8; len: U32): I32; |
| 53 | extern proc send*(fd: I32; buf: ^U8; n: UINT; flags: I32): INT; |
| 54 | extern proc bind*(fd: I32; addr: ^U8; len: U32): I32; |
| 55 | extern proc listen*(fd, backlog: I32): I32; |
| 56 | extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32; |
| 57 | extern proc setsockopt*(fd, level, name: I32; value: ^U8; len: U32): I32; |
| 58 | extern proc getsockopt*(fd, level, name: I32; value: ^U8; len: ^U32): I32; |
| 59 | extern proc getpeername*(fd: I32; addr: ^U8; len: ^U32): I32; |
| 60 | extern proc shutdown*(fd, how: I32): I32; |
| 61 | extern proc socketpair*(domain, kind, protocol: I32; fds: ^I32): I32; |
| 62 | extern proc getsockname*(fd: I32; addr: ^U8; len: ^U32): I32; |
| 63 | extern proc recvfrom*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: ^U32): INT; |
| 64 | extern proc sendto*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: U32): INT; |
| 65 | extern proc sendmsg*(fd: I32; msg: ^U8; flags: I32): INT; |
| 66 | extern proc recvmsg*(fd: I32; msg: ^U8; flags: I32): INT; |
| 67 | extern proc ftruncate*(fd: I32; size: I64): I32; |
| 68 | extern proc inet_pton*(family: I32; src, dst: ^U8): I32; |
| 69 | extern proc clock_gettime*(clock: I32; t: ^U8): I32; |
| 70 | extern proc getenv*(name: ^U8): ^U8; |
| 71 | extern proc setenv*(name, value: ^U8; overwrite: I32): I32; |
| 72 | extern proc strerror*(err: I32): ^U8; |
| 73 | extern proc getcwd*(buf: ^U8; n: UINT): ^U8; |
| 74 | extern proc chdir*(path: ^U8): I32; |
| 75 | extern proc stat*(path, st: ^U8): I32; |
| 76 | extern proc fstat*(fd: I32; st: ^U8): I32; |
| 77 | extern proc fsync*(fd: I32): I32; |
| 78 | extern proc sync*; |
| 79 | extern proc flock*(fd, how: I32): I32; |
| 80 | extern proc lstat*(path, st: ^U8): I32; |
| 81 | extern proc statvfs*(path, st: ^U8): I32; |
| 82 | extern proc symlink*(target, path: ^U8): I32; |
| 83 | extern proc posix_openpt*(flags: I32): I32; |
| 84 | extern proc grantpt*(fd: I32): I32; |
| 85 | extern proc unlockpt*(fd: I32): I32; |
| 86 | extern proc ptsname*(fd: I32): ^U8; |
| 87 | extern proc fork*: I32; |
| 88 | extern proc setsid*: I32; |
| 89 | extern proc dup2*(fd, fd2: I32): I32; |
| 90 | extern proc dup*(fd: I32): I32; |
| 91 | extern proc execve*(path: ^U8; argv, envp: ^^U8): I32; |
| 92 | extern proc waitpid*(pid: I32; status: ^I32; options: I32): I32; |
| 93 | extern proc kill*(pid, signal: I32): I32; |
| 94 | extern proc getpid*: I32; |
| 95 | extern proc getuid*: U32; |
| 96 | extern proc geteuid*: U32; |
| 97 | extern proc gethostname*(name: ^U8; n: UINT): I32; |
| 98 | extern proc access*(path: ^U8; how: I32): I32; |
| 99 | extern proc umask*(mask: U32): U32; |
| 100 | extern proc getrusage*(who: I32; usage: ^U8): I32; |
| 101 | extern proc getrlimit*(which: I32; limit: ^U8): I32; |
| 102 | extern proc setrlimit*(which: I32; limit: ^U8): I32; |
| 103 | extern proc getpgrp*: I32; |
| 104 | extern proc setpgid*(pid, group: I32): I32; |
| 105 | extern proc tcsetpgrp*(fd, group: I32): I32; |
| 106 | extern proc tcgetpgrp*(fd: I32): I32; |
| 107 | extern proc sigaction*(sig: I32; act, old: ^U8): I32; |
| 108 | extern proc sigprocmask*(how: I32; mask, old: ^U8): I32; |
| 109 | extern proc printf*(format: ^U8; ...): I32; |
| 110 | extern proc fflush*(f: ^U8): I32; |
| 111 | extern var environ*: ^^U8; |
| 112 | extern var __stdoutp: ^U8; |
| 113 | extern var __stderrp: ^U8; |
| 114 | |
| 115 | var |
| 116 | stdout*: ^U8; (* FILE *: __stdoutp *) |
| 117 | stderr*: ^U8; (* __stderrp *) |
| 118 | |
| 119 | (* What a call Linux alone has does here, as each below: -1, errno |
| 120 | ENOSYS. Their parameters are named as in C.raz, as the interface |
| 121 | check wants, and dropped. *) |
| 122 | proc none: I32; |
| 123 | var e: ^I32; |
| 124 | begin |
| 125 | e := __error(); |
| 126 | e^ := 78; (* ENOSYS *) |
| 127 | return -1; |
| 128 | end; |
| 129 | |
| 130 | (* errno, this thread's *) |
| 131 | proc __errno_location*: ^I32; |
| 132 | begin |
| 133 | return __error(); |
| 134 | end; |
| 135 | |
| 136 | proc accept4*(fd: I32; addr: ^U8; len: ^U32; flags: I32): I32; |
| 137 | begin |
| 138 | _ := fd; _ := addr; _ := len; _ := flags; |
| 139 | return none(); |
| 140 | end; |
| 141 | |
| 142 | proc memfd_create*(name: ^U8; flags: U32): I32; |
| 143 | begin |
| 144 | _ := name; _ := flags; |
| 145 | return none(); |
| 146 | end; |
| 147 | |
| 148 | proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32; |
| 149 | begin |
| 150 | _ := clock; _ := flags; _ := t; _ := left; |
| 151 | return none(); |
| 152 | end; |
| 153 | |
| 154 | proc getrandom*(buf: ^U8; n: UINT; flags: U32): INT; |
| 155 | begin |
| 156 | _ := buf; _ := n; _ := flags; |
| 157 | return INT(none()); |
| 158 | end; |
| 159 | |
| 160 | proc close_range*(first, last: U32; flags: I32): I32; |
| 161 | begin |
| 162 | _ := first; _ := last; _ := flags; |
| 163 | return none(); |
| 164 | end; |
| 165 | |
| 166 | proc sched_getaffinity*(pid: I32; size: UINT; mask: ^U8): I32; |
| 167 | begin |
| 168 | _ := pid; _ := size; _ := mask; |
| 169 | return none(); |
| 170 | end; |
| 171 | |
| 172 | proc pipe2*(fds: ^I32; flags: I32): I32; |
| 173 | begin |
| 174 | _ := fds; _ := flags; |
| 175 | return none(); |
| 176 | end; |
| 177 | |
| 178 | begin |
| 179 | stdout := __stdoutp; |
| 180 | stderr := __stderrp; |
| 181 | end C. |