raz / lib / C.darwin.raz

181 lines · 6.5 KiB · raw · history · last change 89524972

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