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