1 | // Copyright 2020 The Libc Authors. All rights reserved.
|
---|
2 | // Use of this source code is governed by a BSD-style
|
---|
3 | // license that can be found in the LICENSE file.
|
---|
4 |
|
---|
5 | package libc // import "modernc.org/libc"
|
---|
6 |
|
---|
7 | import (
|
---|
8 | crand "crypto/rand"
|
---|
9 | "encoding/hex"
|
---|
10 | "fmt"
|
---|
11 | "io"
|
---|
12 | "os"
|
---|
13 | "os/exec"
|
---|
14 | gosignal "os/signal"
|
---|
15 | "path/filepath"
|
---|
16 | "runtime"
|
---|
17 | "strings"
|
---|
18 | "syscall"
|
---|
19 | gotime "time"
|
---|
20 | "unicode"
|
---|
21 | "unsafe"
|
---|
22 |
|
---|
23 | guuid "github.com/google/uuid"
|
---|
24 | "golang.org/x/sys/unix"
|
---|
25 | "modernc.org/libc/errno"
|
---|
26 | "modernc.org/libc/fcntl"
|
---|
27 | "modernc.org/libc/fts"
|
---|
28 | gonetdb "modernc.org/libc/honnef.co/go/netdb"
|
---|
29 | "modernc.org/libc/langinfo"
|
---|
30 | "modernc.org/libc/limits"
|
---|
31 | "modernc.org/libc/netdb"
|
---|
32 | "modernc.org/libc/netinet/in"
|
---|
33 | "modernc.org/libc/signal"
|
---|
34 | "modernc.org/libc/stdio"
|
---|
35 | "modernc.org/libc/sys/socket"
|
---|
36 | "modernc.org/libc/sys/stat"
|
---|
37 | "modernc.org/libc/sys/types"
|
---|
38 | "modernc.org/libc/termios"
|
---|
39 | "modernc.org/libc/time"
|
---|
40 | "modernc.org/libc/unistd"
|
---|
41 | "modernc.org/libc/uuid/uuid"
|
---|
42 | "modernc.org/libc/wctype"
|
---|
43 | )
|
---|
44 |
|
---|
45 | const (
|
---|
46 | maxPathLen = 1024
|
---|
47 | )
|
---|
48 |
|
---|
49 | // var (
|
---|
50 | // in6_addr_any in.In6_addr
|
---|
51 | // )
|
---|
52 |
|
---|
53 | type (
|
---|
54 | long = types.User_long_t
|
---|
55 | ulong = types.User_ulong_t
|
---|
56 | )
|
---|
57 |
|
---|
58 | // // Keep these outside of the var block otherwise go generate will miss them.
|
---|
59 | var X__stderrp = Xstdout
|
---|
60 | var X__stdinp = Xstdin
|
---|
61 | var X__stdoutp = Xstdout
|
---|
62 |
|
---|
63 | // user@darwin-m1:~/tmp$ cat main.c
|
---|
64 | //
|
---|
65 | // #include <xlocale.h>
|
---|
66 | // #include <stdio.h>
|
---|
67 | //
|
---|
68 | // int main() {
|
---|
69 | // printf("%i\n", ___mb_cur_max());
|
---|
70 | // return 0;
|
---|
71 | // }
|
---|
72 | //
|
---|
73 | // user@darwin-m1:~/tmp$ gcc main.c && ./a.out
|
---|
74 | // 1
|
---|
75 | // user@darwin-m1:~/tmp$
|
---|
76 | var X__mb_cur_max int32 = 1
|
---|
77 |
|
---|
78 | var startTime = gotime.Now() // For clock(3)
|
---|
79 |
|
---|
80 | type file uintptr
|
---|
81 |
|
---|
82 | func (f file) fd() int32 { return int32((*stdio.FILE)(unsafe.Pointer(f)).F_file) }
|
---|
83 | func (f file) setFd(fd int32) { (*stdio.FILE)(unsafe.Pointer(f)).F_file = int16(fd) }
|
---|
84 |
|
---|
85 | func (f file) err() bool {
|
---|
86 | return (*stdio.FILE)(unsafe.Pointer(f)).F_flags&1 != 0
|
---|
87 | }
|
---|
88 |
|
---|
89 | func (f file) setErr() {
|
---|
90 | (*stdio.FILE)(unsafe.Pointer(f)).F_flags |= 1
|
---|
91 | }
|
---|
92 |
|
---|
93 | func (f file) close(t *TLS) int32 {
|
---|
94 | r := Xclose(t, f.fd())
|
---|
95 | Xfree(t, uintptr(f))
|
---|
96 | if r < 0 {
|
---|
97 | return stdio.EOF
|
---|
98 | }
|
---|
99 |
|
---|
100 | return 0
|
---|
101 | }
|
---|
102 |
|
---|
103 | func newFile(t *TLS, fd int32) uintptr {
|
---|
104 | p := Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(stdio.FILE{})))
|
---|
105 | if p == 0 {
|
---|
106 | return 0
|
---|
107 | }
|
---|
108 |
|
---|
109 | file(p).setFd(fd)
|
---|
110 | return p
|
---|
111 | }
|
---|
112 |
|
---|
113 | func fwrite(fd int32, b []byte) (int, error) {
|
---|
114 | if fd == unistd.STDOUT_FILENO {
|
---|
115 | return write(b)
|
---|
116 | }
|
---|
117 |
|
---|
118 | if dmesgs {
|
---|
119 | dmesg("%v: fd %v: %s", origin(1), fd, hex.Dump(b))
|
---|
120 | }
|
---|
121 | return unix.Write(int(fd), b)
|
---|
122 | }
|
---|
123 |
|
---|
124 | func X__inline_isnand(t *TLS, x float64) int32 { return Xisnan(t, x) }
|
---|
125 | func X__inline_isnanf(t *TLS, x float32) int32 { return Xisnanf(t, x) }
|
---|
126 | func X__inline_isnanl(t *TLS, x float64) int32 { return Xisnan(t, x) }
|
---|
127 |
|
---|
128 | // int fprintf(FILE *stream, const char *format, ...);
|
---|
129 | func Xfprintf(t *TLS, stream, format, args uintptr) int32 {
|
---|
130 | n, _ := fwrite(int32((*stdio.FILE)(unsafe.Pointer(stream)).F_file), printf(format, args))
|
---|
131 | return int32(n)
|
---|
132 | }
|
---|
133 |
|
---|
134 | // int usleep(useconds_t usec);
|
---|
135 | func Xusleep(t *TLS, usec types.Useconds_t) int32 {
|
---|
136 | gotime.Sleep(gotime.Microsecond * gotime.Duration(usec))
|
---|
137 | return 0
|
---|
138 | }
|
---|
139 |
|
---|
140 | // int futimes(int fd, const struct timeval tv[2]);
|
---|
141 | func Xfutimes(t *TLS, fd int32, tv uintptr) int32 {
|
---|
142 | var a []unix.Timeval
|
---|
143 | if tv != 0 {
|
---|
144 | a = make([]unix.Timeval, 2)
|
---|
145 | a[0] = *(*unix.Timeval)(unsafe.Pointer(tv))
|
---|
146 | a[1] = *(*unix.Timeval)(unsafe.Pointer(tv + unsafe.Sizeof(unix.Timeval{})))
|
---|
147 | }
|
---|
148 | if err := unix.Futimes(int(fd), a); err != nil {
|
---|
149 | if dmesgs {
|
---|
150 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
151 | }
|
---|
152 | t.setErrno(err)
|
---|
153 | return -1
|
---|
154 | }
|
---|
155 |
|
---|
156 | if dmesgs {
|
---|
157 | dmesg("%v: ok", origin(1))
|
---|
158 | }
|
---|
159 | return 0
|
---|
160 | }
|
---|
161 |
|
---|
162 | // void srandomdev(void);
|
---|
163 | func Xsrandomdev(t *TLS) {
|
---|
164 | panic(todo(""))
|
---|
165 | }
|
---|
166 |
|
---|
167 | // int gethostuuid(uuid_t id, const struct timespec *wait);
|
---|
168 | func Xgethostuuid(t *TLS, id uintptr, wait uintptr) int32 {
|
---|
169 | if _, _, err := unix.Syscall(unix.SYS_GETHOSTUUID, id, wait, 0); err != 0 { // Cannot avoid the syscall here.
|
---|
170 | if dmesgs {
|
---|
171 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
172 | }
|
---|
173 | t.setErrno(err)
|
---|
174 | return -1
|
---|
175 | }
|
---|
176 |
|
---|
177 | if dmesgs {
|
---|
178 | dmesg("%v: ok", origin(1))
|
---|
179 | }
|
---|
180 | return 0
|
---|
181 | }
|
---|
182 |
|
---|
183 | // int flock(int fd, int operation);
|
---|
184 | func Xflock(t *TLS, fd, operation int32) int32 {
|
---|
185 | if err := unix.Flock(int(fd), int(operation)); err != nil {
|
---|
186 | if dmesgs {
|
---|
187 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
188 | }
|
---|
189 | t.setErrno(err)
|
---|
190 | return -1
|
---|
191 | }
|
---|
192 |
|
---|
193 | if dmesgs {
|
---|
194 | dmesg("%v: ok", origin(1))
|
---|
195 | }
|
---|
196 | return 0
|
---|
197 | }
|
---|
198 |
|
---|
199 | // int fsctl(const char *,unsigned long,void*,unsigned int);
|
---|
200 | func Xfsctl(t *TLS, path uintptr, request ulong, data uintptr, options uint32) int32 {
|
---|
201 | panic(todo(""))
|
---|
202 | // if _, _, err := unix.Syscall6(unix.SYS_FSCTL, path, uintptr(request), data, uintptr(options), 0, 0); err != 0 {
|
---|
203 | // t.setErrno(err)
|
---|
204 | // return -1
|
---|
205 | // }
|
---|
206 |
|
---|
207 | // return 0
|
---|
208 | }
|
---|
209 |
|
---|
210 | // int * __error(void);
|
---|
211 | func X__error(t *TLS) uintptr {
|
---|
212 | return t.errnop
|
---|
213 | }
|
---|
214 |
|
---|
215 | // int isspace(int c);
|
---|
216 | func Xisspace(t *TLS, c int32) int32 {
|
---|
217 | return __isspace(t, c)
|
---|
218 | }
|
---|
219 |
|
---|
220 | // void __assert_rtn(const char *, const char *, int, const char *)
|
---|
221 | func X__assert_rtn(t *TLS, function, file uintptr, line int32, assertion uintptr) {
|
---|
222 | panic(todo(""))
|
---|
223 | // fmt.Fprintf(os.Stderr, "assertion failure: %s:%d.%s: %s\n", GoString(file), line, GoString(function), GoString(assertion))
|
---|
224 | // os.Stderr.Sync()
|
---|
225 | // Xexit(t, 1)
|
---|
226 | }
|
---|
227 |
|
---|
228 | // int getrusage(int who, struct rusage *usage);
|
---|
229 | func Xgetrusage(t *TLS, who int32, usage uintptr) int32 {
|
---|
230 | panic(todo(""))
|
---|
231 | // if _, _, err := unix.Syscall(unix.SYS_GETRUSAGE, uintptr(who), usage, 0); err != 0 {
|
---|
232 | // t.setErrno(err)
|
---|
233 | // return -1
|
---|
234 | // }
|
---|
235 |
|
---|
236 | // return 0
|
---|
237 | }
|
---|
238 |
|
---|
239 | // int fgetc(FILE *stream);
|
---|
240 | func Xfgetc(t *TLS, stream uintptr) int32 {
|
---|
241 | fd := int((*stdio.FILE)(unsafe.Pointer(stream)).F_file)
|
---|
242 | var buf [1]byte
|
---|
243 | if n, _ := unix.Read(fd, buf[:]); n != 0 {
|
---|
244 | return int32(buf[0])
|
---|
245 | }
|
---|
246 |
|
---|
247 | return stdio.EOF
|
---|
248 | }
|
---|
249 |
|
---|
250 | // int lstat(const char *pathname, struct stat *statbuf);
|
---|
251 | func Xlstat(t *TLS, pathname, statbuf uintptr) int32 {
|
---|
252 | return Xlstat64(t, pathname, statbuf)
|
---|
253 | }
|
---|
254 |
|
---|
255 | // int stat(const char *pathname, struct stat *statbuf);
|
---|
256 | func Xstat(t *TLS, pathname, statbuf uintptr) int32 {
|
---|
257 | return Xstat64(t, pathname, statbuf)
|
---|
258 | }
|
---|
259 |
|
---|
260 | // int chdir(const char *path);
|
---|
261 | func Xchdir(t *TLS, path uintptr) int32 {
|
---|
262 | if err := unix.Chdir(GoString(path)); err != nil {
|
---|
263 | if dmesgs {
|
---|
264 | dmesg("%v: %q: %v FAIL", origin(1), GoString(path), err)
|
---|
265 | }
|
---|
266 | t.setErrno(err)
|
---|
267 | return -1
|
---|
268 | }
|
---|
269 |
|
---|
270 | if dmesgs {
|
---|
271 | dmesg("%v: %q: ok", origin(1), GoString(path))
|
---|
272 | }
|
---|
273 | return 0
|
---|
274 | }
|
---|
275 |
|
---|
276 | var localtime time.Tm
|
---|
277 |
|
---|
278 | // struct tm *localtime(const time_t *timep);
|
---|
279 | func Xlocaltime(_ *TLS, timep uintptr) uintptr {
|
---|
280 | loc := gotime.Local
|
---|
281 | if r := getenv(Environ(), "TZ"); r != 0 {
|
---|
282 | zone, off := parseZone(GoString(r))
|
---|
283 | loc = gotime.FixedZone(zone, -off)
|
---|
284 | }
|
---|
285 | ut := *(*time.Time_t)(unsafe.Pointer(timep))
|
---|
286 | t := gotime.Unix(int64(ut), 0).In(loc)
|
---|
287 | localtime.Ftm_sec = int32(t.Second())
|
---|
288 | localtime.Ftm_min = int32(t.Minute())
|
---|
289 | localtime.Ftm_hour = int32(t.Hour())
|
---|
290 | localtime.Ftm_mday = int32(t.Day())
|
---|
291 | localtime.Ftm_mon = int32(t.Month() - 1)
|
---|
292 | localtime.Ftm_year = int32(t.Year() - 1900)
|
---|
293 | localtime.Ftm_wday = int32(t.Weekday())
|
---|
294 | localtime.Ftm_yday = int32(t.YearDay())
|
---|
295 | localtime.Ftm_isdst = Bool32(isTimeDST(t))
|
---|
296 | return uintptr(unsafe.Pointer(&localtime))
|
---|
297 | }
|
---|
298 |
|
---|
299 | // struct tm *localtime_r(const time_t *timep, struct tm *result);
|
---|
300 | func Xlocaltime_r(_ *TLS, timep, result uintptr) uintptr {
|
---|
301 | loc := gotime.Local
|
---|
302 | if r := getenv(Environ(), "TZ"); r != 0 {
|
---|
303 | zone, off := parseZone(GoString(r))
|
---|
304 | loc = gotime.FixedZone(zone, -off)
|
---|
305 | }
|
---|
306 | ut := *(*time_t)(unsafe.Pointer(timep))
|
---|
307 | t := gotime.Unix(int64(ut), 0).In(loc)
|
---|
308 | (*time.Tm)(unsafe.Pointer(result)).Ftm_sec = int32(t.Second())
|
---|
309 | (*time.Tm)(unsafe.Pointer(result)).Ftm_min = int32(t.Minute())
|
---|
310 | (*time.Tm)(unsafe.Pointer(result)).Ftm_hour = int32(t.Hour())
|
---|
311 | (*time.Tm)(unsafe.Pointer(result)).Ftm_mday = int32(t.Day())
|
---|
312 | (*time.Tm)(unsafe.Pointer(result)).Ftm_mon = int32(t.Month() - 1)
|
---|
313 | (*time.Tm)(unsafe.Pointer(result)).Ftm_year = int32(t.Year() - 1900)
|
---|
314 | (*time.Tm)(unsafe.Pointer(result)).Ftm_wday = int32(t.Weekday())
|
---|
315 | (*time.Tm)(unsafe.Pointer(result)).Ftm_yday = int32(t.YearDay())
|
---|
316 | (*time.Tm)(unsafe.Pointer(result)).Ftm_isdst = Bool32(isTimeDST(t))
|
---|
317 | return result
|
---|
318 | }
|
---|
319 |
|
---|
320 | // int open(const char *pathname, int flags, ...);
|
---|
321 | func Xopen(t *TLS, pathname uintptr, flags int32, args uintptr) int32 {
|
---|
322 | var mode types.Mode_t
|
---|
323 | if args != 0 {
|
---|
324 | mode = (types.Mode_t)(VaUint32(&args))
|
---|
325 | }
|
---|
326 | fd, err := unix.Open(GoString(pathname), int(flags), uint32(mode))
|
---|
327 | if err != nil {
|
---|
328 | if dmesgs {
|
---|
329 | dmesg("%v: %q %#x %#o: %v FAIL", origin(1), GoString(pathname), flags, mode, err)
|
---|
330 | }
|
---|
331 | t.setErrno(err)
|
---|
332 | return -1
|
---|
333 | }
|
---|
334 |
|
---|
335 | if dmesgs {
|
---|
336 | dmesg("%v: %q flags %#x mode %#o: fd %v", origin(1), GoString(pathname), flags, mode, fd)
|
---|
337 | }
|
---|
338 | return int32(fd)
|
---|
339 | }
|
---|
340 |
|
---|
341 | // off_t lseek(int fd, off_t offset, int whence);
|
---|
342 | func Xlseek(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
|
---|
343 | return types.Off_t(Xlseek64(t, fd, offset, whence))
|
---|
344 | }
|
---|
345 |
|
---|
346 | func whenceStr(whence int32) string {
|
---|
347 | switch whence {
|
---|
348 | case fcntl.SEEK_CUR:
|
---|
349 | return "SEEK_CUR"
|
---|
350 | case fcntl.SEEK_END:
|
---|
351 | return "SEEK_END"
|
---|
352 | case fcntl.SEEK_SET:
|
---|
353 | return "SEEK_SET"
|
---|
354 | default:
|
---|
355 | return fmt.Sprintf("whence(%d)", whence)
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | var fsyncStatbuf stat.Stat
|
---|
360 |
|
---|
361 | // int fsync(int fd);
|
---|
362 | func Xfsync(t *TLS, fd int32) int32 {
|
---|
363 | if noFsync {
|
---|
364 | // Simulate -DSQLITE_NO_SYNC for sqlite3 testfixture, see function full_sync in sqlite3.c
|
---|
365 | return Xfstat(t, fd, uintptr(unsafe.Pointer(&fsyncStatbuf)))
|
---|
366 | }
|
---|
367 |
|
---|
368 | if err := unix.Fsync(int(fd)); err != nil {
|
---|
369 | if dmesgs {
|
---|
370 | dmesg("%v: %v: %v FAIL", origin(1), fd, err)
|
---|
371 | }
|
---|
372 | t.setErrno(err)
|
---|
373 | return -1
|
---|
374 | }
|
---|
375 |
|
---|
376 | if dmesgs {
|
---|
377 | dmesg("%v: %d: ok", origin(1), fd)
|
---|
378 | }
|
---|
379 | return 0
|
---|
380 | }
|
---|
381 |
|
---|
382 | // long sysconf(int name);
|
---|
383 | func Xsysconf(t *TLS, name int32) long {
|
---|
384 | switch name {
|
---|
385 | case unistd.X_SC_PAGESIZE:
|
---|
386 | return long(unix.Getpagesize())
|
---|
387 | case unistd.X_SC_NPROCESSORS_ONLN:
|
---|
388 | return long(runtime.NumCPU())
|
---|
389 | }
|
---|
390 |
|
---|
391 | panic(todo(""))
|
---|
392 | }
|
---|
393 |
|
---|
394 | // int close(int fd);
|
---|
395 | func Xclose(t *TLS, fd int32) int32 {
|
---|
396 | if err := unix.Close(int(fd)); err != nil {
|
---|
397 | if dmesgs {
|
---|
398 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
399 | }
|
---|
400 | t.setErrno(err)
|
---|
401 | return -1
|
---|
402 | }
|
---|
403 |
|
---|
404 | if dmesgs {
|
---|
405 | dmesg("%v: %d: ok", origin(1), fd)
|
---|
406 | }
|
---|
407 | return 0
|
---|
408 | }
|
---|
409 |
|
---|
410 | // char *getcwd(char *buf, size_t size);
|
---|
411 | func Xgetcwd(t *TLS, buf uintptr, size types.Size_t) uintptr {
|
---|
412 | if _, err := unix.Getcwd((*RawMem)(unsafe.Pointer(buf))[:size:size]); err != nil {
|
---|
413 | if dmesgs {
|
---|
414 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
415 | }
|
---|
416 | t.setErrno(err)
|
---|
417 | return 0
|
---|
418 | }
|
---|
419 |
|
---|
420 | if dmesgs {
|
---|
421 | dmesg("%v: ok", origin(1))
|
---|
422 | }
|
---|
423 | return buf
|
---|
424 | }
|
---|
425 |
|
---|
426 | // int fstat(int fd, struct stat *statbuf);
|
---|
427 | func Xfstat(t *TLS, fd int32, statbuf uintptr) int32 {
|
---|
428 | return Xfstat64(t, fd, statbuf)
|
---|
429 | }
|
---|
430 |
|
---|
431 | // int ftruncate(int fd, off_t length);
|
---|
432 | func Xftruncate(t *TLS, fd int32, length types.Off_t) int32 {
|
---|
433 | if err := unix.Ftruncate(int(fd), int64(length)); err != nil {
|
---|
434 | if dmesgs {
|
---|
435 | dmesg("%v: fd %d: %v FAIL", origin(1), fd, err)
|
---|
436 | }
|
---|
437 | t.setErrno(err)
|
---|
438 | return -1
|
---|
439 | }
|
---|
440 |
|
---|
441 | if dmesgs {
|
---|
442 | dmesg("%v: %d %#x: ok", origin(1), fd, length)
|
---|
443 | }
|
---|
444 | return 0
|
---|
445 | }
|
---|
446 |
|
---|
447 | // int fcntl(int fd, int cmd, ... /* arg */ );
|
---|
448 | func Xfcntl(t *TLS, fd, cmd int32, args uintptr) int32 {
|
---|
449 | return Xfcntl64(t, fd, cmd, args)
|
---|
450 | }
|
---|
451 |
|
---|
452 | // ssize_t read(int fd, void *buf, size_t count);
|
---|
453 | func Xread(t *TLS, fd int32, buf uintptr, count types.Size_t) types.Ssize_t {
|
---|
454 | var n int
|
---|
455 | var err error
|
---|
456 | switch {
|
---|
457 | case count == 0:
|
---|
458 | n, err = unix.Read(int(fd), nil)
|
---|
459 | default:
|
---|
460 | n, err = unix.Read(int(fd), (*RawMem)(unsafe.Pointer(buf))[:count:count])
|
---|
461 | if dmesgs && err == nil {
|
---|
462 | dmesg("%v: fd %v, count %#x, n %#x\n%s", origin(1), fd, count, n, hex.Dump((*RawMem)(unsafe.Pointer(buf))[:n:n]))
|
---|
463 | }
|
---|
464 | }
|
---|
465 | if err != nil {
|
---|
466 | if dmesgs {
|
---|
467 | dmesg("%v: fd %v, %v FAIL", origin(1), fd, err)
|
---|
468 | }
|
---|
469 | t.setErrno(err)
|
---|
470 | return -1
|
---|
471 | }
|
---|
472 |
|
---|
473 | if dmesgs {
|
---|
474 | dmesg("%v: ok", origin(1))
|
---|
475 | }
|
---|
476 | return types.Ssize_t(n)
|
---|
477 | }
|
---|
478 |
|
---|
479 | // ssize_t write(int fd, const void *buf, size_t count);
|
---|
480 | func Xwrite(t *TLS, fd int32, buf uintptr, count types.Size_t) types.Ssize_t {
|
---|
481 | var n int
|
---|
482 | var err error
|
---|
483 | switch {
|
---|
484 | case count == 0:
|
---|
485 | n, err = unix.Write(int(fd), nil)
|
---|
486 | default:
|
---|
487 | n, err = unix.Write(int(fd), (*RawMem)(unsafe.Pointer(buf))[:count:count])
|
---|
488 | if dmesgs {
|
---|
489 | dmesg("%v: fd %v, count %#x\n%s", origin(1), fd, count, hex.Dump((*RawMem)(unsafe.Pointer(buf))[:count:count]))
|
---|
490 | }
|
---|
491 | }
|
---|
492 | if err != nil {
|
---|
493 | if dmesgs {
|
---|
494 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
495 | }
|
---|
496 | t.setErrno(err)
|
---|
497 | return -1
|
---|
498 | }
|
---|
499 |
|
---|
500 | if dmesgs {
|
---|
501 | dmesg("%v: ok", origin(1))
|
---|
502 | }
|
---|
503 | return types.Ssize_t(n)
|
---|
504 | }
|
---|
505 |
|
---|
506 | // int fchmod(int fd, mode_t mode);
|
---|
507 | func Xfchmod(t *TLS, fd int32, mode types.Mode_t) int32 {
|
---|
508 | if err := unix.Fchmod(int(fd), uint32(mode)); err != nil {
|
---|
509 | if dmesgs {
|
---|
510 | dmesg("%v: %d %#o: %v FAIL", origin(1), fd, mode, err)
|
---|
511 | }
|
---|
512 | t.setErrno(err)
|
---|
513 | return -1
|
---|
514 | }
|
---|
515 |
|
---|
516 | if dmesgs {
|
---|
517 | dmesg("%v: %d %#o: ok", origin(1), fd, mode)
|
---|
518 | }
|
---|
519 | return 0
|
---|
520 | }
|
---|
521 |
|
---|
522 | // int fchown(int fd, uid_t owner, gid_t group);
|
---|
523 | func Xfchown(t *TLS, fd int32, owner types.Uid_t, group types.Gid_t) int32 {
|
---|
524 | if _, _, err := unix.Syscall(unix.SYS_FCHOWN, uintptr(fd), uintptr(owner), uintptr(group)); err != 0 {
|
---|
525 | t.setErrno(err)
|
---|
526 | return -1
|
---|
527 | }
|
---|
528 |
|
---|
529 | return 0
|
---|
530 | }
|
---|
531 |
|
---|
532 | // uid_t geteuid(void);
|
---|
533 | func Xgeteuid(t *TLS) types.Uid_t {
|
---|
534 | r := types.Uid_t(unix.Geteuid())
|
---|
535 | if dmesgs {
|
---|
536 | dmesg("%v: %v", origin(1), r)
|
---|
537 | }
|
---|
538 | return r
|
---|
539 | }
|
---|
540 |
|
---|
541 | // void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
|
---|
542 | func Xmmap(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
|
---|
543 | // Cannot avoid the syscall here, addr sometimes matter.
|
---|
544 | data, _, err := unix.Syscall6(unix.SYS_MMAP, addr, uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset))
|
---|
545 | if err != 0 {
|
---|
546 | if dmesgs {
|
---|
547 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
548 | }
|
---|
549 | t.setErrno(err)
|
---|
550 | return ^uintptr(0) // (void*)-1
|
---|
551 | }
|
---|
552 |
|
---|
553 | if dmesgs {
|
---|
554 | dmesg("%v: %#x", origin(1), data)
|
---|
555 | }
|
---|
556 | return data
|
---|
557 | }
|
---|
558 |
|
---|
559 | // int munmap(void *addr, size_t length);
|
---|
560 | func Xmunmap(t *TLS, addr uintptr, length types.Size_t) int32 {
|
---|
561 | if _, _, err := unix.Syscall(unix.SYS_MUNMAP, addr, uintptr(length), 0); err != 0 { // Cannot avoid the syscall here, must pair with mmap.
|
---|
562 | if dmesgs {
|
---|
563 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
564 | }
|
---|
565 | t.setErrno(err)
|
---|
566 | return -1
|
---|
567 | }
|
---|
568 |
|
---|
569 | return 0
|
---|
570 | }
|
---|
571 |
|
---|
572 | // int gettimeofday(struct timeval *tv, struct timezone *tz);
|
---|
573 | func Xgettimeofday(t *TLS, tv, tz uintptr) int32 {
|
---|
574 | if tz != 0 {
|
---|
575 | panic(todo(""))
|
---|
576 | }
|
---|
577 |
|
---|
578 | var tvs unix.Timeval
|
---|
579 | err := unix.Gettimeofday(&tvs)
|
---|
580 | if err != nil {
|
---|
581 | if dmesgs {
|
---|
582 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
583 | }
|
---|
584 | t.setErrno(err)
|
---|
585 | return -1
|
---|
586 | }
|
---|
587 |
|
---|
588 | *(*unix.Timeval)(unsafe.Pointer(tv)) = tvs
|
---|
589 | return 0
|
---|
590 | }
|
---|
591 |
|
---|
592 | // int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);
|
---|
593 | func Xgetsockopt(t *TLS, sockfd, level, optname int32, optval, optlen uintptr) int32 {
|
---|
594 | if _, _, err := unix.Syscall6(unix.SYS_GETSOCKOPT, uintptr(sockfd), uintptr(level), uintptr(optname), optval, optlen, 0); err != 0 {
|
---|
595 | t.setErrno(err)
|
---|
596 | return -1
|
---|
597 | }
|
---|
598 |
|
---|
599 | return 0
|
---|
600 | }
|
---|
601 |
|
---|
602 | // int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
|
---|
603 | func Xsetsockopt(t *TLS, sockfd, level, optname int32, optval uintptr, optlen socket.Socklen_t) int32 {
|
---|
604 | if _, _, err := unix.Syscall6(unix.SYS_SETSOCKOPT, uintptr(sockfd), uintptr(level), uintptr(optname), optval, uintptr(optlen), 0); err != 0 {
|
---|
605 | t.setErrno(err)
|
---|
606 | return -1
|
---|
607 | }
|
---|
608 |
|
---|
609 | return 0
|
---|
610 | }
|
---|
611 |
|
---|
612 | // int ioctl(int fd, unsigned long request, ...);
|
---|
613 | func Xioctl(t *TLS, fd int32, request ulong, va uintptr) int32 {
|
---|
614 | var argp uintptr
|
---|
615 | if va != 0 {
|
---|
616 | argp = VaUintptr(&va)
|
---|
617 | }
|
---|
618 | n, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(request), argp)
|
---|
619 | if err != 0 {
|
---|
620 | t.setErrno(err)
|
---|
621 | return -1
|
---|
622 | }
|
---|
623 |
|
---|
624 | return int32(n)
|
---|
625 | }
|
---|
626 |
|
---|
627 | // int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
|
---|
628 | func Xgetsockname(t *TLS, sockfd int32, addr, addrlen uintptr) int32 {
|
---|
629 | if _, _, err := unix.Syscall(unix.SYS_GETSOCKNAME, uintptr(sockfd), addr, addrlen); err != 0 { // Cannot avoid the syscall here.
|
---|
630 | if dmesgs {
|
---|
631 | dmesg("%v: fd %v: %v FAIL", origin(1), sockfd, err)
|
---|
632 | }
|
---|
633 | t.setErrno(err)
|
---|
634 | return -1
|
---|
635 | }
|
---|
636 |
|
---|
637 | if dmesgs {
|
---|
638 | dmesg("%v: fd %v: ok", origin(1), sockfd)
|
---|
639 | }
|
---|
640 | return 0
|
---|
641 | }
|
---|
642 |
|
---|
643 | // int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
---|
644 | func Xselect(t *TLS, nfds int32, readfds, writefds, exceptfds, timeout uintptr) int32 {
|
---|
645 | n, err := unix.Select(
|
---|
646 | int(nfds),
|
---|
647 | (*unix.FdSet)(unsafe.Pointer(readfds)),
|
---|
648 | (*unix.FdSet)(unsafe.Pointer(writefds)),
|
---|
649 | (*unix.FdSet)(unsafe.Pointer(exceptfds)),
|
---|
650 | (*unix.Timeval)(unsafe.Pointer(timeout)),
|
---|
651 | )
|
---|
652 | if err != nil {
|
---|
653 | if dmesgs {
|
---|
654 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
655 | }
|
---|
656 | t.setErrno(err)
|
---|
657 | return -1
|
---|
658 | }
|
---|
659 |
|
---|
660 | if dmesgs {
|
---|
661 | dmesg("%v: ok", origin(1))
|
---|
662 | }
|
---|
663 | return int32(n)
|
---|
664 | }
|
---|
665 |
|
---|
666 | // int mkfifo(const char *pathname, mode_t mode);
|
---|
667 | func Xmkfifo(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
|
---|
668 | if err := unix.Mkfifo(GoString(pathname), uint32(mode)); err != nil {
|
---|
669 | t.setErrno(err)
|
---|
670 | return -1
|
---|
671 | }
|
---|
672 |
|
---|
673 | return 0
|
---|
674 | }
|
---|
675 |
|
---|
676 | // mode_t umask(mode_t mask);
|
---|
677 | func Xumask(t *TLS, mask types.Mode_t) types.Mode_t {
|
---|
678 | return types.Mode_t(unix.Umask(int(mask)))
|
---|
679 | }
|
---|
680 |
|
---|
681 | // // int execvp(const char *file, char *const argv[]);
|
---|
682 | // func Xexecvp(t *TLS, file, argv uintptr) int32 {
|
---|
683 | // if _, _, err := unix.Syscall(unix.SYS_EXECVE, file, argv, Environ()); err != 0 {
|
---|
684 | // t.setErrno(err)
|
---|
685 | // return -1
|
---|
686 | // }
|
---|
687 | //
|
---|
688 | // return 0
|
---|
689 | // }
|
---|
690 |
|
---|
691 | // pid_t (pid_t pid, int *wstatus, int options);
|
---|
692 | func Xwaitpid(t *TLS, pid types.Pid_t, wstatus uintptr, optname int32) types.Pid_t {
|
---|
693 | n, err := unix.Wait4(int(pid), (*unix.WaitStatus)(unsafe.Pointer(wstatus)), int(optname), nil)
|
---|
694 | if err != nil {
|
---|
695 | if dmesgs {
|
---|
696 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
697 | }
|
---|
698 | t.setErrno(err)
|
---|
699 | return -1
|
---|
700 | }
|
---|
701 |
|
---|
702 | if dmesgs {
|
---|
703 | dmesg("%v: ok", origin(1))
|
---|
704 | }
|
---|
705 | return types.Pid_t(n)
|
---|
706 | }
|
---|
707 |
|
---|
708 | // int uname(struct utsname *buf);
|
---|
709 | func Xuname(t *TLS, buf uintptr) int32 {
|
---|
710 | if err := unix.Uname((*unix.Utsname)(unsafe.Pointer(buf))); err != nil {
|
---|
711 | if dmesgs {
|
---|
712 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
713 | }
|
---|
714 | t.setErrno(err)
|
---|
715 | return -1
|
---|
716 | }
|
---|
717 |
|
---|
718 | if dmesgs {
|
---|
719 | dmesg("%v: ok", origin(1))
|
---|
720 | }
|
---|
721 | return 0
|
---|
722 | }
|
---|
723 |
|
---|
724 | // ssize_t recv(int sockfd, void *buf, size_t len, int flags);
|
---|
725 | func Xrecv(t *TLS, sockfd int32, buf uintptr, len types.Size_t, flags int32) types.Ssize_t {
|
---|
726 | n, _, err := unix.Syscall6(unix.SYS_RECVFROM, uintptr(sockfd), buf, uintptr(len), uintptr(flags), 0, 0)
|
---|
727 | if err != 0 {
|
---|
728 | t.setErrno(err)
|
---|
729 | return -1
|
---|
730 | }
|
---|
731 |
|
---|
732 | return types.Ssize_t(n)
|
---|
733 | }
|
---|
734 |
|
---|
735 | // ssize_t send(int sockfd, const void *buf, size_t len, int flags);
|
---|
736 | func Xsend(t *TLS, sockfd int32, buf uintptr, len types.Size_t, flags int32) types.Ssize_t {
|
---|
737 | n, _, err := unix.Syscall6(unix.SYS_SENDTO, uintptr(sockfd), buf, uintptr(len), uintptr(flags), 0, 0)
|
---|
738 | if err != 0 {
|
---|
739 | t.setErrno(err)
|
---|
740 | return -1
|
---|
741 | }
|
---|
742 |
|
---|
743 | return types.Ssize_t(n)
|
---|
744 | }
|
---|
745 |
|
---|
746 | // int shutdown(int sockfd, int how);
|
---|
747 | func Xshutdown(t *TLS, sockfd, how int32) int32 {
|
---|
748 | if _, _, err := unix.Syscall(unix.SYS_SHUTDOWN, uintptr(sockfd), uintptr(how), 0); err != 0 {
|
---|
749 | t.setErrno(err)
|
---|
750 | return -1
|
---|
751 | }
|
---|
752 |
|
---|
753 | return 0
|
---|
754 | }
|
---|
755 |
|
---|
756 | // int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
|
---|
757 | func Xgetpeername(t *TLS, sockfd int32, addr uintptr, addrlen uintptr) int32 {
|
---|
758 | if _, _, err := unix.Syscall(unix.SYS_GETPEERNAME, uintptr(sockfd), addr, uintptr(addrlen)); err != 0 {
|
---|
759 | t.setErrno(err)
|
---|
760 | return -1
|
---|
761 | }
|
---|
762 |
|
---|
763 | return 0
|
---|
764 | }
|
---|
765 |
|
---|
766 | // int socket(int domain, int type, int protocol);
|
---|
767 | func Xsocket(t *TLS, domain, type1, protocol int32) int32 {
|
---|
768 | n, _, err := unix.Syscall(unix.SYS_SOCKET, uintptr(domain), uintptr(type1), uintptr(protocol))
|
---|
769 | if err != 0 {
|
---|
770 | t.setErrno(err)
|
---|
771 | return -1
|
---|
772 | }
|
---|
773 |
|
---|
774 | return int32(n)
|
---|
775 | }
|
---|
776 |
|
---|
777 | // int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
|
---|
778 | func Xbind(t *TLS, sockfd int32, addr uintptr, addrlen uint32) int32 {
|
---|
779 | n, _, err := unix.Syscall(unix.SYS_BIND, uintptr(sockfd), addr, uintptr(addrlen))
|
---|
780 | if err != 0 {
|
---|
781 | t.setErrno(err)
|
---|
782 | return -1
|
---|
783 | }
|
---|
784 |
|
---|
785 | return int32(n)
|
---|
786 | }
|
---|
787 |
|
---|
788 | // int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
|
---|
789 | func Xconnect(t *TLS, sockfd int32, addr uintptr, addrlen uint32) int32 {
|
---|
790 | if _, _, err := unix.Syscall(unix.SYS_CONNECT, uintptr(sockfd), addr, uintptr(addrlen)); err != 0 {
|
---|
791 | t.setErrno(err)
|
---|
792 | return -1
|
---|
793 | }
|
---|
794 |
|
---|
795 | return 0
|
---|
796 | }
|
---|
797 |
|
---|
798 | // int listen(int sockfd, int backlog);
|
---|
799 | func Xlisten(t *TLS, sockfd, backlog int32) int32 {
|
---|
800 | if _, _, err := unix.Syscall(unix.SYS_LISTEN, uintptr(sockfd), uintptr(backlog), 0); err != 0 {
|
---|
801 | t.setErrno(err)
|
---|
802 | return -1
|
---|
803 | }
|
---|
804 |
|
---|
805 | return 0
|
---|
806 | }
|
---|
807 |
|
---|
808 | // int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
|
---|
809 | func Xaccept(t *TLS, sockfd int32, addr uintptr, addrlen uintptr) int32 {
|
---|
810 | panic(todo(""))
|
---|
811 | // n, _, err := unix.Syscall6(unix.SYS_ACCEPT4, uintptr(sockfd), addr, uintptr(addrlen), 0, 0, 0)
|
---|
812 | // if err != 0 {
|
---|
813 | // t.setErrno(err)
|
---|
814 | // return -1
|
---|
815 | // }
|
---|
816 |
|
---|
817 | // return int32(n)
|
---|
818 | }
|
---|
819 |
|
---|
820 | // // int getrlimit(int resource, struct rlimit *rlim);
|
---|
821 | // func Xgetrlimit(t *TLS, resource int32, rlim uintptr) int32 {
|
---|
822 | // return Xgetrlimit64(t, resource, rlim)
|
---|
823 | // }
|
---|
824 | //
|
---|
825 | // // int setrlimit(int resource, const struct rlimit *rlim);
|
---|
826 | // func Xsetrlimit(t *TLS, resource int32, rlim uintptr) int32 {
|
---|
827 | // return Xsetrlimit64(t, resource, rlim)
|
---|
828 | // }
|
---|
829 | //
|
---|
830 | // // int setrlimit(int resource, const struct rlimit *rlim);
|
---|
831 | // func Xsetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
|
---|
832 | // if _, _, err := unix.Syscall(unix.SYS_SETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
|
---|
833 | // t.setErrno(err)
|
---|
834 | // return -1
|
---|
835 | // }
|
---|
836 | //
|
---|
837 | // return 0
|
---|
838 | // }
|
---|
839 |
|
---|
840 | // uid_t getuid(void);
|
---|
841 | func Xgetuid(t *TLS) types.Uid_t {
|
---|
842 | r := types.Uid_t(os.Getuid())
|
---|
843 | if dmesgs {
|
---|
844 | dmesg("%v: %v", origin(1), r)
|
---|
845 | }
|
---|
846 | return r
|
---|
847 | }
|
---|
848 |
|
---|
849 | // pid_t getpid(void);
|
---|
850 | func Xgetpid(t *TLS) int32 {
|
---|
851 | r := int32(os.Getpid())
|
---|
852 | if dmesgs {
|
---|
853 | dmesg("%v: %v", origin(1), r)
|
---|
854 | }
|
---|
855 | return r
|
---|
856 | }
|
---|
857 |
|
---|
858 | // int system(const char *command);
|
---|
859 | func Xsystem(t *TLS, command uintptr) int32 {
|
---|
860 | s := GoString(command)
|
---|
861 | if command == 0 {
|
---|
862 | panic(todo(""))
|
---|
863 | }
|
---|
864 |
|
---|
865 | cmd := exec.Command("sh", "-c", s)
|
---|
866 | cmd.Stdout = os.Stdout
|
---|
867 | cmd.Stderr = os.Stderr
|
---|
868 | err := cmd.Run()
|
---|
869 | if err != nil {
|
---|
870 | ps := err.(*exec.ExitError)
|
---|
871 | return int32(ps.ExitCode())
|
---|
872 | }
|
---|
873 |
|
---|
874 | return 0
|
---|
875 | }
|
---|
876 |
|
---|
877 | // int setvbuf(FILE *stream, char *buf, int mode, size_t size);
|
---|
878 | func Xsetvbuf(t *TLS, stream, buf uintptr, mode int32, size types.Size_t) int32 {
|
---|
879 | return 0 //TODO
|
---|
880 | }
|
---|
881 |
|
---|
882 | // int raise(int sig);
|
---|
883 | func Xraise(t *TLS, sig int32) int32 {
|
---|
884 | panic(todo(""))
|
---|
885 | }
|
---|
886 |
|
---|
887 | // // int backtrace(void **buffer, int size);
|
---|
888 | // func Xbacktrace(t *TLS, buf uintptr, size int32) int32 {
|
---|
889 | // panic(todo(""))
|
---|
890 | // }
|
---|
891 | //
|
---|
892 | // // void backtrace_symbols_fd(void *const *buffer, int size, int fd);
|
---|
893 | // func Xbacktrace_symbols_fd(t *TLS, buffer uintptr, size, fd int32) {
|
---|
894 | // panic(todo(""))
|
---|
895 | // }
|
---|
896 |
|
---|
897 | // int fileno(FILE *stream);
|
---|
898 | func Xfileno(t *TLS, stream uintptr) int32 {
|
---|
899 | if stream == 0 {
|
---|
900 | if dmesgs {
|
---|
901 | dmesg("%v: FAIL", origin(1))
|
---|
902 | }
|
---|
903 | t.setErrno(errno.EBADF)
|
---|
904 | return -1
|
---|
905 | }
|
---|
906 |
|
---|
907 | if fd := int32((*stdio.FILE)(unsafe.Pointer(stream)).F_file); fd >= 0 {
|
---|
908 | return fd
|
---|
909 | }
|
---|
910 |
|
---|
911 | if dmesgs {
|
---|
912 | dmesg("%v: FAIL", origin(1))
|
---|
913 | }
|
---|
914 | t.setErrno(errno.EBADF)
|
---|
915 | return -1
|
---|
916 | }
|
---|
917 |
|
---|
918 | func newFtsent(t *TLS, info int, path string, stat *unix.Stat_t, err syscall.Errno) (r *fts.FTSENT) {
|
---|
919 | var statp uintptr
|
---|
920 | if stat != nil {
|
---|
921 | statp = Xmalloc(t, types.Size_t(unsafe.Sizeof(unix.Stat_t{})))
|
---|
922 | if statp == 0 {
|
---|
923 | panic("OOM")
|
---|
924 | }
|
---|
925 |
|
---|
926 | *(*unix.Stat_t)(unsafe.Pointer(statp)) = *stat
|
---|
927 | }
|
---|
928 | csp, errx := CString(path)
|
---|
929 | if errx != nil {
|
---|
930 | panic("OOM")
|
---|
931 | }
|
---|
932 |
|
---|
933 | return &fts.FTSENT{
|
---|
934 | Ffts_info: uint16(info),
|
---|
935 | Ffts_path: csp,
|
---|
936 | Ffts_pathlen: uint16(len(path)),
|
---|
937 | Ffts_statp: statp,
|
---|
938 | Ffts_errno: int32(err),
|
---|
939 | }
|
---|
940 | }
|
---|
941 |
|
---|
942 | func newCFtsent(t *TLS, info int, path string, stat *unix.Stat_t, err syscall.Errno) uintptr {
|
---|
943 | p := Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(fts.FTSENT{})))
|
---|
944 | if p == 0 {
|
---|
945 | panic("OOM")
|
---|
946 | }
|
---|
947 |
|
---|
948 | *(*fts.FTSENT)(unsafe.Pointer(p)) = *newFtsent(t, info, path, stat, err)
|
---|
949 | return p
|
---|
950 | }
|
---|
951 |
|
---|
952 | func ftsentClose(t *TLS, p uintptr) {
|
---|
953 | Xfree(t, (*fts.FTSENT)(unsafe.Pointer(p)).Ffts_path)
|
---|
954 | Xfree(t, (*fts.FTSENT)(unsafe.Pointer(p)).Ffts_statp)
|
---|
955 | }
|
---|
956 |
|
---|
957 | type ftstream struct {
|
---|
958 | s []uintptr
|
---|
959 | x int
|
---|
960 | }
|
---|
961 |
|
---|
962 | func (f *ftstream) close(t *TLS) {
|
---|
963 | for _, p := range f.s {
|
---|
964 | ftsentClose(t, p)
|
---|
965 | Xfree(t, p)
|
---|
966 | }
|
---|
967 | *f = ftstream{}
|
---|
968 | }
|
---|
969 |
|
---|
970 | // FTS *fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **));
|
---|
971 | func Xfts_open(t *TLS, path_argv uintptr, options int32, compar uintptr) uintptr {
|
---|
972 | f := &ftstream{}
|
---|
973 |
|
---|
974 | var walk func(string)
|
---|
975 | walk = func(path string) {
|
---|
976 | var fi os.FileInfo
|
---|
977 | var err error
|
---|
978 | switch {
|
---|
979 | case options&fts.FTS_LOGICAL != 0:
|
---|
980 | fi, err = os.Stat(path)
|
---|
981 | case options&fts.FTS_PHYSICAL != 0:
|
---|
982 | fi, err = os.Lstat(path)
|
---|
983 | default:
|
---|
984 | panic(todo(""))
|
---|
985 | }
|
---|
986 |
|
---|
987 | if err != nil {
|
---|
988 | return
|
---|
989 | }
|
---|
990 |
|
---|
991 | var statp *unix.Stat_t
|
---|
992 | if options&fts.FTS_NOSTAT == 0 {
|
---|
993 | var stat unix.Stat_t
|
---|
994 | switch {
|
---|
995 | case options&fts.FTS_LOGICAL != 0:
|
---|
996 | if err := unix.Stat(path, &stat); err != nil {
|
---|
997 | panic(todo(""))
|
---|
998 | }
|
---|
999 | case options&fts.FTS_PHYSICAL != 0:
|
---|
1000 | if err := unix.Lstat(path, &stat); err != nil {
|
---|
1001 | panic(todo(""))
|
---|
1002 | }
|
---|
1003 | default:
|
---|
1004 | panic(todo(""))
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | statp = &stat
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | out:
|
---|
1011 | switch {
|
---|
1012 | case fi.IsDir():
|
---|
1013 | f.s = append(f.s, newCFtsent(t, fts.FTS_D, path, statp, 0))
|
---|
1014 | g, err := os.Open(path)
|
---|
1015 | switch x := err.(type) {
|
---|
1016 | case nil:
|
---|
1017 | // ok
|
---|
1018 | case *os.PathError:
|
---|
1019 | f.s = append(f.s, newCFtsent(t, fts.FTS_DNR, path, statp, errno.EACCES))
|
---|
1020 | break out
|
---|
1021 | default:
|
---|
1022 | panic(todo("%q: %v %T", path, x, x))
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | names, err := g.Readdirnames(-1)
|
---|
1026 | g.Close()
|
---|
1027 | if err != nil {
|
---|
1028 | panic(todo(""))
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | for _, name := range names {
|
---|
1032 | walk(path + "/" + name)
|
---|
1033 | if f == nil {
|
---|
1034 | break out
|
---|
1035 | }
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | f.s = append(f.s, newCFtsent(t, fts.FTS_DP, path, statp, 0))
|
---|
1039 | default:
|
---|
1040 | info := fts.FTS_F
|
---|
1041 | if fi.Mode()&os.ModeSymlink != 0 {
|
---|
1042 | info = fts.FTS_SL
|
---|
1043 | }
|
---|
1044 | switch {
|
---|
1045 | case statp != nil:
|
---|
1046 | f.s = append(f.s, newCFtsent(t, info, path, statp, 0))
|
---|
1047 | case options&fts.FTS_NOSTAT != 0:
|
---|
1048 | f.s = append(f.s, newCFtsent(t, fts.FTS_NSOK, path, nil, 0))
|
---|
1049 | default:
|
---|
1050 | panic(todo(""))
|
---|
1051 | }
|
---|
1052 | }
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | for {
|
---|
1056 | p := *(*uintptr)(unsafe.Pointer(path_argv))
|
---|
1057 | if p == 0 {
|
---|
1058 | if f == nil {
|
---|
1059 | return 0
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | if compar != 0 {
|
---|
1063 | panic(todo(""))
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | return addObject(f)
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | walk(GoString(p))
|
---|
1070 | path_argv += unsafe.Sizeof(uintptr(0))
|
---|
1071 | }
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | // FTSENT *fts_read(FTS *ftsp);
|
---|
1075 | func Xfts_read(t *TLS, ftsp uintptr) uintptr {
|
---|
1076 | f := getObject(ftsp).(*ftstream)
|
---|
1077 | if f.x == len(f.s) {
|
---|
1078 | if dmesgs {
|
---|
1079 | dmesg("%v: FAIL", origin(1))
|
---|
1080 | }
|
---|
1081 | t.setErrno(0)
|
---|
1082 | return 0
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | r := f.s[f.x]
|
---|
1086 | if e := (*fts.FTSENT)(unsafe.Pointer(r)).Ffts_errno; e != 0 {
|
---|
1087 | if dmesgs {
|
---|
1088 | dmesg("%v: FAIL", origin(1))
|
---|
1089 | }
|
---|
1090 | t.setErrno(e)
|
---|
1091 | }
|
---|
1092 | f.x++
|
---|
1093 | return r
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | // int fts_close(FTS *ftsp);
|
---|
1097 | func Xfts_close(t *TLS, ftsp uintptr) int32 {
|
---|
1098 | getObject(ftsp).(*ftstream).close(t)
|
---|
1099 | removeObject(ftsp)
|
---|
1100 | return 0
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | // void tzset (void);
|
---|
1104 | func Xtzset(t *TLS) {
|
---|
1105 | //TODO
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | // char *strerror(int errnum);
|
---|
1109 | func Xstrerror(t *TLS, errnum int32) uintptr {
|
---|
1110 | panic(todo(""))
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | // void *dlopen(const char *filename, int flags);
|
---|
1114 | func Xdlopen(t *TLS, filename uintptr, flags int32) uintptr {
|
---|
1115 | panic(todo(""))
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | // char *dlerror(void);
|
---|
1119 | func Xdlerror(t *TLS) uintptr {
|
---|
1120 | panic(todo(""))
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | // int dlclose(void *handle);
|
---|
1124 | func Xdlclose(t *TLS, handle uintptr) int32 {
|
---|
1125 | panic(todo(""))
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | // void *dlsym(void *handle, const char *symbol);
|
---|
1129 | func Xdlsym(t *TLS, handle, symbol uintptr) uintptr {
|
---|
1130 | panic(todo(""))
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | // void perror(const char *s);
|
---|
1134 | func Xperror(t *TLS, s uintptr) {
|
---|
1135 | panic(todo(""))
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | // int pclose(FILE *stream);
|
---|
1139 | func Xpclose(t *TLS, stream uintptr) int32 {
|
---|
1140 | panic(todo(""))
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | // var gai_strerrorBuf [100]byte
|
---|
1144 |
|
---|
1145 | // const char *gai_strerror(int errcode);
|
---|
1146 | func Xgai_strerror(t *TLS, errcode int32) uintptr {
|
---|
1147 | panic(todo(""))
|
---|
1148 | // copy(gai_strerrorBuf[:], fmt.Sprintf("gai error %d\x00", errcode))
|
---|
1149 | // return uintptr(unsafe.Pointer(&gai_strerrorBuf))
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | // int tcgetattr(int fd, struct termios *termios_p);
|
---|
1153 | func Xtcgetattr(t *TLS, fd int32, termios_p uintptr) int32 {
|
---|
1154 | panic(todo(""))
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | // int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
|
---|
1158 | func Xtcsetattr(t *TLS, fd, optional_actions int32, termios_p uintptr) int32 {
|
---|
1159 | panic(todo(""))
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | // speed_t cfgetospeed(const struct termios *termios_p);
|
---|
1163 | func Xcfgetospeed(t *TLS, termios_p uintptr) termios.Speed_t {
|
---|
1164 | panic(todo(""))
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | // int cfsetospeed(struct termios *termios_p, speed_t speed);
|
---|
1168 | // func Xcfsetospeed(t *TLS, termios_p uintptr, speed uint32) int32 {
|
---|
1169 | func Xcfsetospeed(...interface{}) int32 {
|
---|
1170 | panic(todo(""))
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | // int cfsetispeed(struct termios *termios_p, speed_t speed);
|
---|
1174 | // func Xcfsetispeed(t *TLS, termios_p uintptr, speed uint32) int32 {
|
---|
1175 | func Xcfsetispeed(...interface{}) int32 {
|
---|
1176 | panic(todo(""))
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | // pid_t fork(void);
|
---|
1180 | func Xfork(t *TLS) int32 {
|
---|
1181 | if dmesgs {
|
---|
1182 | dmesg("%v: FAIL", origin(1))
|
---|
1183 | }
|
---|
1184 | t.setErrno(errno.ENOSYS)
|
---|
1185 | return -1
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | var emptyStr = [1]byte{}
|
---|
1189 |
|
---|
1190 | // char *setlocale(int category, const char *locale);
|
---|
1191 | func Xsetlocale(t *TLS, category int32, locale uintptr) uintptr {
|
---|
1192 | return uintptr(unsafe.Pointer(&emptyStr)) //TODO
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | // char *nl_langinfo(nl_item item);
|
---|
1196 | func Xnl_langinfo(t *TLS, item langinfo.Nl_item) uintptr {
|
---|
1197 | return uintptr(unsafe.Pointer(&emptyStr)) //TODO
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | // FILE *popen(const char *command, const char *type);
|
---|
1201 | func Xpopen(t *TLS, command, type1 uintptr) uintptr {
|
---|
1202 | panic(todo(""))
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | // char *realpath(const char *path, char *resolved_path);
|
---|
1206 | func Xrealpath(t *TLS, path, resolved_path uintptr) uintptr {
|
---|
1207 | s, err := filepath.EvalSymlinks(GoString(path))
|
---|
1208 | if err != nil {
|
---|
1209 | if os.IsNotExist(err) {
|
---|
1210 | if dmesgs {
|
---|
1211 | dmesg("%v: %q: %v FAIL", origin(1), GoString(path), err)
|
---|
1212 | }
|
---|
1213 | t.setErrno(errno.ENOENT)
|
---|
1214 | return 0
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | panic(todo("", err))
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | if resolved_path == 0 {
|
---|
1221 | panic(todo(""))
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | if len(s) >= limits.PATH_MAX {
|
---|
1225 | s = s[:limits.PATH_MAX-1]
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | copy((*RawMem)(unsafe.Pointer(resolved_path))[:len(s):len(s)], s)
|
---|
1229 | (*RawMem)(unsafe.Pointer(resolved_path))[len(s)] = 0
|
---|
1230 | if dmesgs {
|
---|
1231 | dmesg("%v: %q: ok", origin(1), GoString(path))
|
---|
1232 | }
|
---|
1233 | return resolved_path
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | // struct tm *gmtime_r(const time_t *timep, struct tm *result);
|
---|
1237 | func Xgmtime_r(t *TLS, timep, result uintptr) uintptr {
|
---|
1238 | panic(todo(""))
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | // char *inet_ntoa(struct in_addr in);
|
---|
1242 | func Xinet_ntoa(t *TLS, in1 in.In_addr) uintptr {
|
---|
1243 | panic(todo(""))
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | func X__ccgo_in6addr_anyp(t *TLS) uintptr {
|
---|
1247 | panic(todo(""))
|
---|
1248 | // return uintptr(unsafe.Pointer(&in6_addr_any))
|
---|
1249 | }
|
---|
1250 |
|
---|
1251 | func Xabort(t *TLS) {
|
---|
1252 | if dmesgs {
|
---|
1253 | dmesg("%v:", origin(1))
|
---|
1254 | }
|
---|
1255 | p := Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(signal.Sigaction{})))
|
---|
1256 | if p == 0 {
|
---|
1257 | panic("OOM")
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | (*signal.Sigaction)(unsafe.Pointer(p)).F__sigaction_u.F__sa_handler = signal.SIG_DFL
|
---|
1261 | Xsigaction(t, signal.SIGABRT, p, 0)
|
---|
1262 | Xfree(t, p)
|
---|
1263 | unix.Kill(unix.Getpid(), syscall.Signal(signal.SIGABRT))
|
---|
1264 | panic(todo("unrechable"))
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | // int fflush(FILE *stream);
|
---|
1268 | func Xfflush(t *TLS, stream uintptr) int32 {
|
---|
1269 | return 0 //TODO
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | // size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
|
---|
1273 | func Xfread(t *TLS, ptr uintptr, size, nmemb types.Size_t, stream uintptr) types.Size_t {
|
---|
1274 | fd := uintptr(file(stream).fd())
|
---|
1275 | count := size * nmemb
|
---|
1276 | var n int
|
---|
1277 | var err error
|
---|
1278 | switch {
|
---|
1279 | case count == 0:
|
---|
1280 | n, err = unix.Read(int(fd), nil)
|
---|
1281 | default:
|
---|
1282 | n, err = unix.Read(int(fd), (*RawMem)(unsafe.Pointer(ptr))[:count:count])
|
---|
1283 | if dmesgs && err == nil {
|
---|
1284 | dmesg("%v: fd %v, n %#x\n%s", origin(1), fd, n, hex.Dump((*RawMem)(unsafe.Pointer(ptr))[:n:n]))
|
---|
1285 | }
|
---|
1286 | }
|
---|
1287 | if err != nil {
|
---|
1288 | if dmesgs {
|
---|
1289 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1290 | }
|
---|
1291 | file(stream).setErr()
|
---|
1292 | return types.Size_t(n) / size
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | if dmesgs {
|
---|
1296 | dmesg("%v: ok", origin(1))
|
---|
1297 | }
|
---|
1298 | return types.Size_t(n) / size
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | // size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
|
---|
1302 | func Xfwrite(t *TLS, ptr uintptr, size, nmemb types.Size_t, stream uintptr) types.Size_t {
|
---|
1303 | fd := uintptr(file(stream).fd())
|
---|
1304 | count := size * nmemb
|
---|
1305 | var n int
|
---|
1306 | var err error
|
---|
1307 | switch {
|
---|
1308 | case count == 0:
|
---|
1309 | n, err = unix.Write(int(fd), nil)
|
---|
1310 | default:
|
---|
1311 | n, err = unix.Write(int(fd), (*RawMem)(unsafe.Pointer(ptr))[:count:count])
|
---|
1312 | if dmesgs {
|
---|
1313 | dmesg("%v: fd %v, count %#x\n%s", origin(1), fd, count, hex.Dump((*RawMem)(unsafe.Pointer(ptr))[:count:count]))
|
---|
1314 | }
|
---|
1315 | }
|
---|
1316 | if err != nil {
|
---|
1317 | if dmesgs {
|
---|
1318 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1319 | }
|
---|
1320 | file(stream).setErr()
|
---|
1321 | return types.Size_t(n) / size
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | if dmesgs {
|
---|
1325 | dmesg("%v: ok", origin(1))
|
---|
1326 | }
|
---|
1327 | return types.Size_t(n) / size
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | // int fclose(FILE *stream);
|
---|
1331 | func Xfclose(t *TLS, stream uintptr) int32 {
|
---|
1332 | r := file(stream).close(t)
|
---|
1333 | if r != 0 {
|
---|
1334 | if dmesgs {
|
---|
1335 | dmesg("%v: %v FAIL", origin(1), r)
|
---|
1336 | }
|
---|
1337 | t.setErrno(r)
|
---|
1338 | return stdio.EOF
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | if dmesgs {
|
---|
1342 | dmesg("%v: ok", origin(1))
|
---|
1343 | }
|
---|
1344 | return 0
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | // int fputc(int c, FILE *stream);
|
---|
1348 | func Xfputc(t *TLS, c int32, stream uintptr) int32 {
|
---|
1349 | if _, err := fwrite(file(stream).fd(), []byte{byte(c)}); err != nil {
|
---|
1350 | return stdio.EOF
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | return int32(byte(c))
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | // int fseek(FILE *stream, long offset, int whence);
|
---|
1357 | func Xfseek(t *TLS, stream uintptr, offset long, whence int32) int32 {
|
---|
1358 | if n := Xlseek(t, int32(file(stream).fd()), types.Off_t(offset), whence); n < 0 {
|
---|
1359 | if dmesgs {
|
---|
1360 | dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), file(stream).fd(), offset, whenceStr(whence), n)
|
---|
1361 | }
|
---|
1362 | file(stream).setErr()
|
---|
1363 | return -1
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | if dmesgs {
|
---|
1367 | dmesg("%v: fd %v, off %#x, whence %v: ok", origin(1), file(stream).fd(), offset, whenceStr(whence))
|
---|
1368 | }
|
---|
1369 | return 0
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | // long ftell(FILE *stream);
|
---|
1373 | func Xftell(t *TLS, stream uintptr) long {
|
---|
1374 | n := Xlseek(t, file(stream).fd(), 0, stdio.SEEK_CUR)
|
---|
1375 | if n < 0 {
|
---|
1376 | file(stream).setErr()
|
---|
1377 | return -1
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | if dmesgs {
|
---|
1381 | dmesg("%v: fd %v, n %#x: ok %#x", origin(1), file(stream).fd(), n, long(n))
|
---|
1382 | }
|
---|
1383 | return long(n)
|
---|
1384 | }
|
---|
1385 |
|
---|
1386 | // int ferror(FILE *stream);
|
---|
1387 | func Xferror(t *TLS, stream uintptr) int32 {
|
---|
1388 | return Bool32(file(stream).err())
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | // int fputs(const char *s, FILE *stream);
|
---|
1392 | func Xfputs(t *TLS, s, stream uintptr) int32 {
|
---|
1393 | panic(todo(""))
|
---|
1394 | // if _, _, err := unix.Syscall(unix.SYS_WRITE, uintptr(file(stream).fd()), s, uintptr(Xstrlen(t, s))); err != 0 {
|
---|
1395 | // return -1
|
---|
1396 | // }
|
---|
1397 |
|
---|
1398 | // return 0
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | var getservbynameStaticResult netdb.Servent
|
---|
1402 |
|
---|
1403 | // struct servent *getservbyname(const char *name, const char *proto);
|
---|
1404 | func Xgetservbyname(t *TLS, name, proto uintptr) uintptr {
|
---|
1405 | var protoent *gonetdb.Protoent
|
---|
1406 | if proto != 0 {
|
---|
1407 | protoent = gonetdb.GetProtoByName(GoString(proto))
|
---|
1408 | }
|
---|
1409 | servent := gonetdb.GetServByName(GoString(name), protoent)
|
---|
1410 | if servent == nil {
|
---|
1411 | if dmesgs {
|
---|
1412 | dmesg("%q %q: nil (protoent %+v)", GoString(name), GoString(proto), protoent)
|
---|
1413 | }
|
---|
1414 | return 0
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | Xfree(t, (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_name)
|
---|
1418 | if v := (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_aliases; v != 0 {
|
---|
1419 | for {
|
---|
1420 | p := *(*uintptr)(unsafe.Pointer(v))
|
---|
1421 | if p == 0 {
|
---|
1422 | break
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | Xfree(t, p)
|
---|
1426 | v += unsafe.Sizeof(uintptr(0))
|
---|
1427 | }
|
---|
1428 | Xfree(t, v)
|
---|
1429 | }
|
---|
1430 | Xfree(t, (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_proto)
|
---|
1431 | cname, err := CString(servent.Name)
|
---|
1432 | if err != nil {
|
---|
1433 | getservbynameStaticResult = netdb.Servent{}
|
---|
1434 | return 0
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | var protoname uintptr
|
---|
1438 | if protoent != nil {
|
---|
1439 | if protoname, err = CString(protoent.Name); err != nil {
|
---|
1440 | Xfree(t, cname)
|
---|
1441 | getservbynameStaticResult = netdb.Servent{}
|
---|
1442 | return 0
|
---|
1443 | }
|
---|
1444 | }
|
---|
1445 | var a []uintptr
|
---|
1446 | for _, v := range servent.Aliases {
|
---|
1447 | cs, err := CString(v)
|
---|
1448 | if err != nil {
|
---|
1449 | for _, v := range a {
|
---|
1450 | Xfree(t, v)
|
---|
1451 | }
|
---|
1452 | return 0
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | a = append(a, cs)
|
---|
1456 | }
|
---|
1457 | v := Xcalloc(t, types.Size_t(len(a)+1), types.Size_t(unsafe.Sizeof(uintptr(0))))
|
---|
1458 | if v == 0 {
|
---|
1459 | Xfree(t, cname)
|
---|
1460 | Xfree(t, protoname)
|
---|
1461 | for _, v := range a {
|
---|
1462 | Xfree(t, v)
|
---|
1463 | }
|
---|
1464 | getservbynameStaticResult = netdb.Servent{}
|
---|
1465 | return 0
|
---|
1466 | }
|
---|
1467 | for _, p := range a {
|
---|
1468 | *(*uintptr)(unsafe.Pointer(v)) = p
|
---|
1469 | v += unsafe.Sizeof(uintptr(0))
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 | getservbynameStaticResult = netdb.Servent{
|
---|
1473 | Fs_name: cname,
|
---|
1474 | Fs_aliases: v,
|
---|
1475 | Fs_port: int32(servent.Port),
|
---|
1476 | Fs_proto: protoname,
|
---|
1477 | }
|
---|
1478 | return uintptr(unsafe.Pointer(&getservbynameStaticResult))
|
---|
1479 | }
|
---|
1480 |
|
---|
1481 | // //TODO- func Xreaddir64(t *TLS, dir uintptr) uintptr {
|
---|
1482 | // //TODO- return Xreaddir(t, dir)
|
---|
1483 | // //TODO- }
|
---|
1484 | //
|
---|
1485 | // func __syscall(r, _ uintptr, errno syscall.Errno) long {
|
---|
1486 | // if errno != 0 {
|
---|
1487 | // return long(-errno)
|
---|
1488 | // }
|
---|
1489 | //
|
---|
1490 | // return long(r)
|
---|
1491 | // }
|
---|
1492 |
|
---|
1493 | func fcntlCmdStr(cmd int32) string {
|
---|
1494 | switch cmd {
|
---|
1495 | case fcntl.F_GETOWN:
|
---|
1496 | return "F_GETOWN"
|
---|
1497 | case fcntl.F_SETLK:
|
---|
1498 | return "F_SETLK"
|
---|
1499 | case fcntl.F_GETLK:
|
---|
1500 | return "F_GETLK"
|
---|
1501 | case fcntl.F_SETFD:
|
---|
1502 | return "F_SETFD"
|
---|
1503 | case fcntl.F_GETFD:
|
---|
1504 | return "F_GETFD"
|
---|
1505 | default:
|
---|
1506 | return fmt.Sprintf("cmd(%d)", cmd)
|
---|
1507 | }
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | // // struct __float2 { float __sinval; float __cosval; };
|
---|
1511 | // // struct __double2 { double __sinval; double __cosval; };
|
---|
1512 | // //
|
---|
1513 | // // extern struct __float2 __sincosf_stret(float);
|
---|
1514 | // // extern struct __double2 __sincos_stret(double);
|
---|
1515 | // // extern struct __float2 __sincospif_stret(float);
|
---|
1516 | // // extern struct __double2 __sincospi_stret(double);
|
---|
1517 | //
|
---|
1518 | // type X__float2 struct{ F__sinval, F__cosval float32 }
|
---|
1519 | // type X__double2 struct{ F__sinval, F__cosval float32 }
|
---|
1520 | //
|
---|
1521 | // func X__sincosf_stret(*TLS, float32) X__float2 {
|
---|
1522 | // panic(todo(""))
|
---|
1523 | // }
|
---|
1524 | //
|
---|
1525 | // func X__sincos_stret(*TLS, float64) X__double2 {
|
---|
1526 | // panic(todo(""))
|
---|
1527 | // }
|
---|
1528 | //
|
---|
1529 | // func X__sincospif_stret(*TLS, float32) X__float2 {
|
---|
1530 | // panic(todo(""))
|
---|
1531 | // }
|
---|
1532 | //
|
---|
1533 | // func X__sincospi_stret(*TLS, float64) X__double2 {
|
---|
1534 | // panic(todo(""))
|
---|
1535 | // }
|
---|
1536 |
|
---|
1537 | // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
|
---|
1538 | func Xpread(t *TLS, fd int32, buf uintptr, count types.Size_t, offset types.Off_t) types.Ssize_t {
|
---|
1539 | var n int
|
---|
1540 | var err error
|
---|
1541 | switch {
|
---|
1542 | case count == 0:
|
---|
1543 | n, err = unix.Pread(int(fd), nil, int64(offset))
|
---|
1544 | default:
|
---|
1545 | n, err = unix.Pread(int(fd), (*RawMem)(unsafe.Pointer(buf))[:count:count], int64(offset))
|
---|
1546 | if dmesgs && err == nil {
|
---|
1547 | dmesg("%v: fd %v, off %#x, count %#x, n %#x\n%s", origin(1), fd, offset, count, n, hex.Dump((*RawMem)(unsafe.Pointer(buf))[:n:n]))
|
---|
1548 | }
|
---|
1549 | }
|
---|
1550 | if err != nil {
|
---|
1551 | if dmesgs {
|
---|
1552 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1553 | }
|
---|
1554 | t.setErrno(err)
|
---|
1555 | return -1
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | if dmesgs {
|
---|
1559 | dmesg("%v: ok", origin(1))
|
---|
1560 | }
|
---|
1561 | return types.Ssize_t(n)
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 | // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
|
---|
1565 | func Xpwrite(t *TLS, fd int32, buf uintptr, count types.Size_t, offset types.Off_t) types.Ssize_t {
|
---|
1566 | var n int
|
---|
1567 | var err error
|
---|
1568 | switch {
|
---|
1569 | case count == 0:
|
---|
1570 | n, err = unix.Pwrite(int(fd), nil, int64(offset))
|
---|
1571 | default:
|
---|
1572 | n, err = unix.Pwrite(int(fd), (*RawMem)(unsafe.Pointer(buf))[:count:count], int64(offset))
|
---|
1573 | if dmesgs {
|
---|
1574 | dmesg("%v: fd %v, off %#x, count %#x\n%s", origin(1), fd, offset, count, hex.Dump((*RawMem)(unsafe.Pointer(buf))[:count:count]))
|
---|
1575 | }
|
---|
1576 | }
|
---|
1577 | if err != nil {
|
---|
1578 | if dmesgs {
|
---|
1579 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1580 | }
|
---|
1581 | t.setErrno(err)
|
---|
1582 | return -1
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | if dmesgs {
|
---|
1586 | dmesg("%v: ok", origin(1))
|
---|
1587 | }
|
---|
1588 | return types.Ssize_t(n)
|
---|
1589 | }
|
---|
1590 |
|
---|
1591 | // char***_NSGetEnviron()
|
---|
1592 | func X_NSGetEnviron(t *TLS) uintptr {
|
---|
1593 | return EnvironP()
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 | // int chflags(const char *path, u_int flags);
|
---|
1597 | func Xchflags(t *TLS, path uintptr, flags uint32) int32 {
|
---|
1598 | if err := unix.Chflags(GoString(path), int(flags)); err != nil {
|
---|
1599 | if dmesgs {
|
---|
1600 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1601 | }
|
---|
1602 | t.setErrno(err)
|
---|
1603 | return -1
|
---|
1604 | }
|
---|
1605 |
|
---|
1606 | if dmesgs {
|
---|
1607 | dmesg("%v: ok", origin(1))
|
---|
1608 | }
|
---|
1609 | return 0
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | // int rmdir(const char *pathname);
|
---|
1613 | func Xrmdir(t *TLS, pathname uintptr) int32 {
|
---|
1614 | if err := unix.Rmdir(GoString(pathname)); err != nil {
|
---|
1615 | if dmesgs {
|
---|
1616 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1617 | }
|
---|
1618 | t.setErrno(err)
|
---|
1619 | return -1
|
---|
1620 | }
|
---|
1621 |
|
---|
1622 | if dmesgs {
|
---|
1623 | dmesg("%v: ok", origin(1))
|
---|
1624 | }
|
---|
1625 | return 0
|
---|
1626 | }
|
---|
1627 |
|
---|
1628 | // uint64_t mach_absolute_time(void);
|
---|
1629 | func Xmach_absolute_time(t *TLS) uint64 {
|
---|
1630 | return uint64(gotime.Now().UnixNano())
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | // See https://developer.apple.com/library/archive/qa/qa1398/_index.html
|
---|
1634 | type machTimebaseInfo = struct {
|
---|
1635 | Fnumer uint32
|
---|
1636 | Fdenom uint32
|
---|
1637 | } /* mach_time.h:36:1 */
|
---|
1638 |
|
---|
1639 | // kern_return_t mach_timebase_info(mach_timebase_info_t info);
|
---|
1640 | func Xmach_timebase_info(t *TLS, info uintptr) int32 {
|
---|
1641 | *(*machTimebaseInfo)(unsafe.Pointer(info)) = machTimebaseInfo{Fnumer: 1, Fdenom: 1}
|
---|
1642 | return 0
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | // int getattrlist(const char* path, struct attrlist * attrList, void * attrBuf, size_t attrBufSize, unsigned long options);
|
---|
1646 | func Xgetattrlist(t *TLS, path, attrList, attrBuf uintptr, attrBufSize types.Size_t, options uint32) int32 {
|
---|
1647 | if _, _, err := unix.Syscall6(unix.SYS_GETATTRLIST, path, attrList, attrBuf, uintptr(attrBufSize), uintptr(options), 0); err != 0 { // Cannot avoid the syscall here.
|
---|
1648 | if dmesgs {
|
---|
1649 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1650 | }
|
---|
1651 | t.setErrno(err)
|
---|
1652 | return -1
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 | if dmesgs {
|
---|
1656 | dmesg("%v: ok", origin(1))
|
---|
1657 | }
|
---|
1658 | return 0
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | // int setattrlist(const char* path, struct attrlist * attrList, void * attrBuf, size_t attrBufSize, unsigned long options);
|
---|
1662 | func Xsetattrlist(t *TLS, path, attrList, attrBuf uintptr, attrBufSize types.Size_t, options uint32) int32 {
|
---|
1663 | if _, _, err := unix.Syscall6(unix.SYS_SETATTRLIST, path, attrList, attrBuf, uintptr(attrBufSize), uintptr(options), 0); err != 0 { // Cannot avoid the syscall here.
|
---|
1664 | if dmesgs {
|
---|
1665 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1666 | }
|
---|
1667 | t.setErrno(err)
|
---|
1668 | return -1
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | if dmesgs {
|
---|
1672 | dmesg("%v: ok", origin(1))
|
---|
1673 | }
|
---|
1674 | return 0
|
---|
1675 | }
|
---|
1676 |
|
---|
1677 | // int copyfile(const char *from, const char *to, copyfile_state_t state, copyfile_flags_t flags);
|
---|
1678 | func Xcopyfile(...interface{}) int32 {
|
---|
1679 | panic(todo(""))
|
---|
1680 | }
|
---|
1681 |
|
---|
1682 | // int truncate(const char *path, off_t length);
|
---|
1683 | func Xtruncate(...interface{}) int32 {
|
---|
1684 | panic(todo(""))
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | type darwinDir struct {
|
---|
1688 | buf [4096]byte
|
---|
1689 | fd int
|
---|
1690 | h int
|
---|
1691 | l int
|
---|
1692 |
|
---|
1693 | eof bool
|
---|
1694 | }
|
---|
1695 |
|
---|
1696 | // DIR *opendir(const char *name);
|
---|
1697 | func Xopendir(t *TLS, name uintptr) uintptr {
|
---|
1698 | p := Xmalloc(t, uint64(unsafe.Sizeof(darwinDir{})))
|
---|
1699 | if p == 0 {
|
---|
1700 | panic("OOM")
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 | fd := int(Xopen(t, name, fcntl.O_RDONLY|fcntl.O_DIRECTORY|fcntl.O_CLOEXEC, 0))
|
---|
1704 | if fd < 0 {
|
---|
1705 | if dmesgs {
|
---|
1706 | dmesg("%v: FAIL %v", origin(1), (*darwinDir)(unsafe.Pointer(p)).fd)
|
---|
1707 | }
|
---|
1708 | Xfree(t, p)
|
---|
1709 | // trc("==== opendir: %#x", 0)
|
---|
1710 | return 0
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | if dmesgs {
|
---|
1714 | dmesg("%v: ok", origin(1))
|
---|
1715 | }
|
---|
1716 | (*darwinDir)(unsafe.Pointer(p)).fd = fd
|
---|
1717 | (*darwinDir)(unsafe.Pointer(p)).h = 0
|
---|
1718 | (*darwinDir)(unsafe.Pointer(p)).l = 0
|
---|
1719 | (*darwinDir)(unsafe.Pointer(p)).eof = false
|
---|
1720 | // trc("==== opendir: %#x", p)
|
---|
1721 | return p
|
---|
1722 | }
|
---|
1723 |
|
---|
1724 | // struct dirent *readdir(DIR *dirp);
|
---|
1725 | func Xreaddir(t *TLS, dir uintptr) uintptr {
|
---|
1726 | if (*darwinDir)(unsafe.Pointer(dir)).eof {
|
---|
1727 | return 0
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | // trc(".... readdir %#x: l %v, h %v", dir, (*darwinDir)(unsafe.Pointer(dir)).l, (*darwinDir)(unsafe.Pointer(dir)).h)
|
---|
1731 | if (*darwinDir)(unsafe.Pointer(dir)).l == (*darwinDir)(unsafe.Pointer(dir)).h {
|
---|
1732 | n, err := unix.Getdirentries((*darwinDir)(unsafe.Pointer(dir)).fd, (*darwinDir)(unsafe.Pointer(dir)).buf[:], nil)
|
---|
1733 | // trc("must read: %v %v", n, err)
|
---|
1734 | if n == 0 {
|
---|
1735 | if err != nil && err != io.EOF {
|
---|
1736 | if dmesgs {
|
---|
1737 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1738 | }
|
---|
1739 | t.setErrno(err)
|
---|
1740 | }
|
---|
1741 | (*darwinDir)(unsafe.Pointer(dir)).eof = true
|
---|
1742 | return 0
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | (*darwinDir)(unsafe.Pointer(dir)).l = 0
|
---|
1746 | (*darwinDir)(unsafe.Pointer(dir)).h = n
|
---|
1747 | // trc("new l %v, h %v", (*darwinDir)(unsafe.Pointer(dir)).l, (*darwinDir)(unsafe.Pointer(dir)).h)
|
---|
1748 | }
|
---|
1749 | de := dir + unsafe.Offsetof(darwinDir{}.buf) + uintptr((*darwinDir)(unsafe.Pointer(dir)).l)
|
---|
1750 | // trc("dir %#x de %#x", dir, de)
|
---|
1751 | (*darwinDir)(unsafe.Pointer(dir)).l += int((*unix.Dirent)(unsafe.Pointer(de)).Reclen)
|
---|
1752 | // trc("final l %v, h %v", (*darwinDir)(unsafe.Pointer(dir)).l, (*darwinDir)(unsafe.Pointer(dir)).h)
|
---|
1753 | return de
|
---|
1754 | }
|
---|
1755 |
|
---|
1756 | func Xclosedir(t *TLS, dir uintptr) int32 {
|
---|
1757 | // trc("---- closedir: %#x", dir)
|
---|
1758 | r := Xclose(t, int32((*darwinDir)(unsafe.Pointer(dir)).fd))
|
---|
1759 | Xfree(t, dir)
|
---|
1760 | return r
|
---|
1761 | }
|
---|
1762 |
|
---|
1763 | // int pipe(int pipefd[2]);
|
---|
1764 | func Xpipe(t *TLS, pipefd uintptr) int32 {
|
---|
1765 | var a [2]int
|
---|
1766 | if err := syscall.Pipe(a[:]); err != nil {
|
---|
1767 | if dmesgs {
|
---|
1768 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1769 | }
|
---|
1770 | t.setErrno(err)
|
---|
1771 | return -1
|
---|
1772 | }
|
---|
1773 |
|
---|
1774 | *(*[2]int32)(unsafe.Pointer(pipefd)) = [2]int32{int32(a[0]), int32(a[1])}
|
---|
1775 | if dmesgs {
|
---|
1776 | dmesg("%v: %v ok", origin(1), a)
|
---|
1777 | }
|
---|
1778 | return 0
|
---|
1779 | }
|
---|
1780 |
|
---|
1781 | // int __isoc99_sscanf(const char *str, const char *format, ...);
|
---|
1782 | func X__isoc99_sscanf(t *TLS, str, format, va uintptr) int32 {
|
---|
1783 | r := scanf(strings.NewReader(GoString(str)), format, va)
|
---|
1784 | // if dmesgs {
|
---|
1785 | // dmesg("%v: %q %q: %d", origin(1), GoString(str), GoString(format), r)
|
---|
1786 | // }
|
---|
1787 | return r
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 | // int sscanf(const char *str, const char *format, ...);
|
---|
1791 | func Xsscanf(t *TLS, str, format, va uintptr) int32 {
|
---|
1792 | r := scanf(strings.NewReader(GoString(str)), format, va)
|
---|
1793 | // if dmesgs {
|
---|
1794 | // dmesg("%v: %q %q: %d", origin(1), GoString(str), GoString(format), r)
|
---|
1795 | // }
|
---|
1796 | return r
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
|
---|
1800 | func Xposix_fadvise(t *TLS, fd int32, offset, len types.Off_t, advice int32) int32 {
|
---|
1801 | panic(todo(""))
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | // clock_t clock(void);
|
---|
1805 | func Xclock(t *TLS) time.Clock_t {
|
---|
1806 | return time.Clock_t(gotime.Since(startTime) * gotime.Duration(time.CLOCKS_PER_SEC) / gotime.Second)
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | // int iswspace(wint_t wc);
|
---|
1810 | func Xiswspace(t *TLS, wc wctype.Wint_t) int32 {
|
---|
1811 | return Bool32(unicode.IsSpace(rune(wc)))
|
---|
1812 | }
|
---|
1813 |
|
---|
1814 | // int iswalnum(wint_t wc);
|
---|
1815 | func Xiswalnum(t *TLS, wc wctype.Wint_t) int32 {
|
---|
1816 | return Bool32(unicode.IsLetter(rune(wc)) || unicode.IsNumber(rune(wc)))
|
---|
1817 | }
|
---|
1818 |
|
---|
1819 | // void arc4random_buf(void *buf, size_t nbytes);
|
---|
1820 | func Xarc4random_buf(t *TLS, buf uintptr, buflen size_t) {
|
---|
1821 | if _, err := crand.Read((*RawMem)(unsafe.Pointer(buf))[:buflen]); err != nil {
|
---|
1822 | panic(todo(""))
|
---|
1823 | }
|
---|
1824 | }
|
---|
1825 |
|
---|
1826 | type darwin_mutexattr_t struct {
|
---|
1827 | sig int64
|
---|
1828 | x [8]byte
|
---|
1829 | }
|
---|
1830 |
|
---|
1831 | type darwin_mutex_t struct {
|
---|
1832 | sig int64
|
---|
1833 | x [65]byte
|
---|
1834 | }
|
---|
1835 |
|
---|
1836 | func X__ccgo_pthreadMutexattrGettype(tls *TLS, a uintptr) int32 {
|
---|
1837 | return (int32((*darwin_mutexattr_t)(unsafe.Pointer(a)).x[4] >> 2 & 3))
|
---|
1838 | }
|
---|
1839 |
|
---|
1840 | func X__ccgo_getMutexType(tls *TLS, m uintptr) int32 {
|
---|
1841 | return (int32((*darwin_mutex_t)(unsafe.Pointer(m)).x[4] >> 2 & 3))
|
---|
1842 | }
|
---|
1843 |
|
---|
1844 | func X__ccgo_pthreadAttrGetDetachState(tls *TLS, a uintptr) int32 {
|
---|
1845 | panic(todo(""))
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | func Xpthread_attr_getdetachstate(tls *TLS, a uintptr, state uintptr) int32 {
|
---|
1849 | panic(todo(""))
|
---|
1850 | }
|
---|
1851 |
|
---|
1852 | func Xpthread_attr_setdetachstate(tls *TLS, a uintptr, state int32) int32 {
|
---|
1853 | panic(todo(""))
|
---|
1854 | }
|
---|
1855 |
|
---|
1856 | func Xpthread_mutexattr_destroy(tls *TLS, a uintptr) int32 {
|
---|
1857 | return 0
|
---|
1858 | }
|
---|
1859 |
|
---|
1860 | func Xpthread_mutexattr_init(tls *TLS, a uintptr) int32 {
|
---|
1861 | *(*darwin_mutexattr_t)(unsafe.Pointer(a)) = darwin_mutexattr_t{}
|
---|
1862 | return 0
|
---|
1863 | }
|
---|
1864 |
|
---|
1865 | func Xpthread_mutexattr_settype(tls *TLS, a uintptr, type1 int32) int32 {
|
---|
1866 | if uint32(type1) > uint32(2) {
|
---|
1867 | return errno.EINVAL
|
---|
1868 | }
|
---|
1869 | (*darwin_mutexattr_t)(unsafe.Pointer(a)).x[4] = byte(type1 << 2)
|
---|
1870 | return 0
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 | // ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
|
---|
1874 | func Xwritev(t *TLS, fd int32, iov uintptr, iovcnt int32) types.Ssize_t {
|
---|
1875 | // if dmesgs {
|
---|
1876 | // dmesg("%v: fd %v iov %#x iovcnt %v", origin(1), fd, iov, iovcnt)
|
---|
1877 | // }
|
---|
1878 | r, _, err := unix.Syscall(unix.SYS_WRITEV, uintptr(fd), iov, uintptr(iovcnt))
|
---|
1879 | if err != 0 {
|
---|
1880 | if dmesgs {
|
---|
1881 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1882 | }
|
---|
1883 | t.setErrno(err)
|
---|
1884 | return -1
|
---|
1885 | }
|
---|
1886 |
|
---|
1887 | return types.Ssize_t(r)
|
---|
1888 | }
|
---|
1889 |
|
---|
1890 | // int pause(void);
|
---|
1891 | func Xpause(t *TLS) int32 {
|
---|
1892 | c := make(chan os.Signal)
|
---|
1893 | gosignal.Notify(c,
|
---|
1894 | syscall.SIGABRT,
|
---|
1895 | syscall.SIGALRM,
|
---|
1896 | syscall.SIGBUS,
|
---|
1897 | syscall.SIGCHLD,
|
---|
1898 | syscall.SIGCONT,
|
---|
1899 | syscall.SIGFPE,
|
---|
1900 | syscall.SIGHUP,
|
---|
1901 | syscall.SIGILL,
|
---|
1902 | // syscall.SIGINT,
|
---|
1903 | syscall.SIGIO,
|
---|
1904 | syscall.SIGIOT,
|
---|
1905 | syscall.SIGKILL,
|
---|
1906 | syscall.SIGPIPE,
|
---|
1907 | syscall.SIGPROF,
|
---|
1908 | syscall.SIGQUIT,
|
---|
1909 | syscall.SIGSEGV,
|
---|
1910 | syscall.SIGSTOP,
|
---|
1911 | syscall.SIGSYS,
|
---|
1912 | syscall.SIGTERM,
|
---|
1913 | syscall.SIGTRAP,
|
---|
1914 | syscall.SIGTSTP,
|
---|
1915 | syscall.SIGTTIN,
|
---|
1916 | syscall.SIGTTOU,
|
---|
1917 | syscall.SIGURG,
|
---|
1918 | syscall.SIGUSR1,
|
---|
1919 | syscall.SIGUSR2,
|
---|
1920 | syscall.SIGVTALRM,
|
---|
1921 | syscall.SIGWINCH,
|
---|
1922 | syscall.SIGXCPU,
|
---|
1923 | syscall.SIGXFSZ,
|
---|
1924 | )
|
---|
1925 | switch <-c {
|
---|
1926 | case syscall.SIGINT:
|
---|
1927 | panic(todo(""))
|
---|
1928 | default:
|
---|
1929 | t.setErrno(errno.EINTR)
|
---|
1930 | return -1
|
---|
1931 | }
|
---|
1932 | }
|
---|
1933 |
|
---|
1934 | // #define __DARWIN_FD_SETSIZE 1024
|
---|
1935 | // #define __DARWIN_NFDBITS (sizeof(__int32_t) * __DARWIN_NBBY) /* bits per mask */
|
---|
1936 | // #define __DARWIN_NBBY 8 /* bits in a byte */
|
---|
1937 | // #define __DARWIN_howmany(x, y) ((((x) % (y)) == 0) ? ((x) / (y)) : (((x) / (y)) + 1)) /* # y's == x bits? */
|
---|
1938 |
|
---|
1939 | // typedef struct fd_set {
|
---|
1940 | // __int32_t fds_bits[__DARWIN_howmany(__DARWIN_FD_SETSIZE, __DARWIN_NFDBITS)];
|
---|
1941 | // } fd_set;
|
---|
1942 |
|
---|
1943 | // __darwin_fd_set(int _fd, struct fd_set *const _p)
|
---|
1944 | //
|
---|
1945 | // {
|
---|
1946 | // (_p->fds_bits[(unsigned long)_fd / __DARWIN_NFDBITS] |= ((__int32_t)(((unsigned long)1) << ((unsigned long)_fd % __DARWIN_NFDBITS))));
|
---|
1947 | // }
|
---|
1948 | func X__darwin_fd_set(tls *TLS, _fd int32, _p uintptr) int32 { /* main.c:12:1: */
|
---|
1949 | *(*int32)(unsafe.Pointer(_p + uintptr(uint64(_fd)/(uint64(unsafe.Sizeof(int32(0)))*uint64(8)))*4)) |= int32(uint64(uint64(1)) << (uint64(_fd) % (uint64(unsafe.Sizeof(int32(0))) * uint64(8))))
|
---|
1950 | return int32(0)
|
---|
1951 | }
|
---|
1952 |
|
---|
1953 | // __darwin_fd_isset(int _fd, const struct fd_set *_p)
|
---|
1954 | //
|
---|
1955 | // {
|
---|
1956 | // return _p->fds_bits[(unsigned long)_fd / __DARWIN_NFDBITS] & ((__int32_t)(((unsigned long)1) << ((unsigned long)_fd % __DARWIN_NFDBITS)));
|
---|
1957 | // }
|
---|
1958 | func X__darwin_fd_isset(tls *TLS, _fd int32, _p uintptr) int32 { /* main.c:17:1: */
|
---|
1959 | return *(*int32)(unsafe.Pointer(_p + uintptr(uint64(_fd)/(uint64(unsafe.Sizeof(int32(0)))*uint64(8)))*4)) & int32(uint64(uint64(1))<<(uint64(_fd)%(uint64(unsafe.Sizeof(int32(0)))*uint64(8))))
|
---|
1960 | }
|
---|
1961 |
|
---|
1962 | // __darwin_fd_clr(int _fd, struct fd_set *const _p)
|
---|
1963 | //
|
---|
1964 | // {
|
---|
1965 | // (_p->fds_bits[(unsigned long)_fd / __DARWIN_NFDBITS] &= ~((__int32_t)(((unsigned long)1) << ((unsigned long)_fd % __DARWIN_NFDBITS))));
|
---|
1966 | // }
|
---|
1967 | func X__darwin_fd_clr(tls *TLS, _fd int32, _p uintptr) int32 { /* main.c:22:1: */
|
---|
1968 | *(*int32)(unsafe.Pointer(_p + uintptr(uint64(_fd)/(uint64(unsafe.Sizeof(int32(0)))*uint64(8)))*4)) &= ^int32(uint64(uint64(1)) << (uint64(_fd) % (uint64(unsafe.Sizeof(int32(0))) * uint64(8))))
|
---|
1969 | return int32(0)
|
---|
1970 | }
|
---|
1971 |
|
---|
1972 | // int ungetc(int c, FILE *stream);
|
---|
1973 | func Xungetc(t *TLS, c int32, stream uintptr) int32 {
|
---|
1974 | panic(todo(""))
|
---|
1975 | }
|
---|
1976 |
|
---|
1977 | // int issetugid(void);
|
---|
1978 | func Xissetugid(t *TLS) int32 {
|
---|
1979 | panic(todo(""))
|
---|
1980 | }
|
---|
1981 |
|
---|
1982 | var progname uintptr
|
---|
1983 |
|
---|
1984 | // const char *getprogname(void);
|
---|
1985 | func Xgetprogname(t *TLS) uintptr {
|
---|
1986 | if progname != 0 {
|
---|
1987 | return progname
|
---|
1988 | }
|
---|
1989 |
|
---|
1990 | var err error
|
---|
1991 | progname, err = CString(filepath.Base(os.Args[0]))
|
---|
1992 | if err != nil {
|
---|
1993 | t.setErrno(err)
|
---|
1994 | return 0
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | return progname
|
---|
1998 | }
|
---|
1999 |
|
---|
2000 | // void uuid_copy(uuid_t dst, uuid_t src);
|
---|
2001 | func Xuuid_copy(t *TLS, dst, src uintptr) {
|
---|
2002 | *(*uuid.Uuid_t)(unsafe.Pointer(dst)) = *(*uuid.Uuid_t)(unsafe.Pointer(src))
|
---|
2003 | }
|
---|
2004 |
|
---|
2005 | // int uuid_parse( char *in, uuid_t uu);
|
---|
2006 | func Xuuid_parse(t *TLS, in uintptr, uu uintptr) int32 {
|
---|
2007 | r, err := guuid.Parse(GoString(in))
|
---|
2008 | if err != nil {
|
---|
2009 | return -1
|
---|
2010 | }
|
---|
2011 |
|
---|
2012 | copy((*RawMem)(unsafe.Pointer(uu))[:unsafe.Sizeof(uuid.Uuid_t{})], r[:])
|
---|
2013 | return 0
|
---|
2014 | }
|
---|
2015 |
|
---|
2016 | // struct __float2 { float __sinval; float __cosval; };
|
---|
2017 |
|
---|
2018 | // struct __float2 __sincosf_stret(float);
|
---|
2019 | func X__sincosf_stret(t *TLS, f float32) struct{ F__sinval, F__cosval float32 } {
|
---|
2020 | panic(todo(""))
|
---|
2021 | }
|
---|
2022 |
|
---|
2023 | // struct __double2 { double __sinval; double __cosval; };
|
---|
2024 |
|
---|
2025 | // struct __double2 __sincos_stret(double);
|
---|
2026 | func X__sincos_stret(t *TLS, f float64) struct{ F__sinval, F__cosval float64 } {
|
---|
2027 | panic(todo(""))
|
---|
2028 | }
|
---|
2029 |
|
---|
2030 | // struct __float2 __sincospif_stret(float);
|
---|
2031 | func X__sincospif_stret(t *TLS, f float32) struct{ F__sinval, F__cosval float32 } {
|
---|
2032 | panic(todo(""))
|
---|
2033 | }
|
---|
2034 |
|
---|
2035 | // struct _double2 __sincospi_stret(double);
|
---|
2036 | func X__sincospi_stret(t *TLS, f float64) struct{ F__sinval, F__cosval float64 } {
|
---|
2037 | panic(todo(""))
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 | // int __srget(FILE *);
|
---|
2041 | func X__srget(t *TLS, f uintptr) int32 {
|
---|
2042 | panic(todo(""))
|
---|
2043 | }
|
---|
2044 |
|
---|
2045 | // int __svfscanf(FILE *, const char *, va_list) __scanflike(2, 0);
|
---|
2046 | func X__svfscanf(t *TLS, f uintptr, p, q uintptr) int32 {
|
---|
2047 | panic(todo(""))
|
---|
2048 | }
|
---|
2049 |
|
---|
2050 | // int __swbuf(int, FILE *);
|
---|
2051 | func X__swbuf(t *TLS, i int32, f uintptr) int32 {
|
---|
2052 | panic(todo(""))
|
---|
2053 | }
|
---|