libc_netbsd_amd64.go (17582B)
1 // Copyright 2021 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 "strings" 9 "syscall" 10 "unsafe" 11 12 "golang.org/x/sys/unix" 13 "modernc.org/libc/fcntl" 14 "modernc.org/libc/fts" 15 "modernc.org/libc/sys/types" 16 "modernc.org/libc/time" 17 "modernc.org/libc/utime" 18 ) 19 20 type ( 21 long = int64 22 ulong = uint64 23 ) 24 25 // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); 26 func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 { 27 panic(todo("")) 28 // if _, _, err := unix.Syscall(unix.SYS_SIGACTION, uintptr(signum), act, oldact); err != 0 { 29 // t.setErrno(err) 30 // return -1 31 // } 32 33 // return 0 34 } 35 36 // FILE *fopen64(const char *pathname, const char *mode); 37 func Xfopen64(t *TLS, pathname, mode uintptr) uintptr { 38 m := strings.ReplaceAll(GoString(mode), "b", "") 39 var flags int 40 switch m { 41 case "r": 42 flags = fcntl.O_RDONLY 43 case "r+": 44 flags = fcntl.O_RDWR 45 case "w": 46 flags = fcntl.O_WRONLY | fcntl.O_CREAT | fcntl.O_TRUNC 47 case "w+": 48 flags = fcntl.O_RDWR | fcntl.O_CREAT | fcntl.O_TRUNC 49 case "a": 50 flags = fcntl.O_WRONLY | fcntl.O_CREAT | fcntl.O_APPEND 51 case "a+": 52 flags = fcntl.O_RDWR | fcntl.O_CREAT | fcntl.O_APPEND 53 default: 54 panic(m) 55 } 56 fd, err := unix.Open(GoString(pathname), int(flags), 0666) 57 if err != nil { 58 if dmesgs { 59 dmesg("%v: %q %q: %v FAIL", origin(1), GoString(pathname), GoString(mode), err) 60 } 61 t.setErrno(err) 62 return 0 63 } 64 65 if dmesgs { 66 dmesg("%v: %q %q: fd %v", origin(1), GoString(pathname), GoString(mode), fd) 67 } 68 if p := newFile(t, int32(fd)); p != 0 { 69 return p 70 } 71 72 panic("OOM") 73 } 74 75 // int lstat(const char *pathname, struct stat *statbuf); 76 func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 { 77 if err := unix.Lstat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil { 78 if dmesgs { 79 dmesg("%v: %q: %v FAIL", origin(1), GoString(pathname), err) 80 } 81 t.setErrno(err) 82 return -1 83 } 84 85 if dmesgs { 86 dmesg("%v: %q: ok", origin(1), GoString(pathname)) 87 } 88 return 0 89 } 90 91 // int stat(const char *pathname, struct stat *statbuf); 92 func Xstat64(t *TLS, pathname, statbuf uintptr) int32 { 93 if err := unix.Stat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil { 94 if dmesgs { 95 dmesg("%v: %q: %v FAIL", origin(1), GoString(pathname), err) 96 } 97 t.setErrno(err) 98 return -1 99 } 100 101 if dmesgs { 102 dmesg("%v: %q: ok", origin(1), GoString(pathname)) 103 } 104 return 0 105 } 106 107 // int mkdir(const char *path, mode_t mode); 108 func Xmkdir(t *TLS, path uintptr, mode types.Mode_t) int32 { 109 if err := unix.Mkdir(GoString(path), uint32(mode)); err != nil { 110 if dmesgs { 111 dmesg("%v: %q: %v FAIL", origin(1), GoString(path), err) 112 } 113 t.setErrno(err) 114 return -1 115 } 116 117 if dmesgs { 118 dmesg("%v: %q: ok", origin(1), GoString(path)) 119 } 120 return 0 121 } 122 123 // int access(const char *pathname, int mode); 124 func Xaccess(t *TLS, pathname uintptr, mode int32) int32 { 125 if err := unix.Access(GoString(pathname), uint32(mode)); err != nil { 126 if dmesgs { 127 dmesg("%v: %q %#o: %v FAIL", origin(1), GoString(pathname), mode, err) 128 } 129 t.setErrno(err) 130 return -1 131 } 132 133 if dmesgs { 134 dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode) 135 } 136 return 0 137 } 138 139 // int unlink(const char *pathname); 140 func Xunlink(t *TLS, pathname uintptr) int32 { 141 if err := unix.Unlink(GoString(pathname)); err != nil { 142 if dmesgs { 143 dmesg("%v: %q: %v", origin(1), GoString(pathname), err) 144 } 145 t.setErrno(err) 146 return -1 147 } 148 149 if dmesgs { 150 dmesg("%v: ok", origin(1)) 151 } 152 return 0 153 } 154 155 // ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize); 156 func Xreadlink(t *TLS, path, buf uintptr, bufsize types.Size_t) types.Ssize_t { 157 var n int 158 var err error 159 switch { 160 case buf == 0 || bufsize == 0: 161 n, err = unix.Readlink(GoString(path), nil) 162 default: 163 n, err = unix.Readlink(GoString(path), (*RawMem)(unsafe.Pointer(buf))[:bufsize:bufsize]) 164 } 165 if err != nil { 166 if dmesgs { 167 dmesg("%v: %v FAIL", err) 168 } 169 t.setErrno(err) 170 return -1 171 } 172 173 if dmesgs { 174 dmesg("%v: ok") 175 } 176 return types.Ssize_t(n) 177 } 178 179 // int symlink(const char *target, const char *linkpath); 180 func Xsymlink(t *TLS, target, linkpath uintptr) int32 { 181 if err := unix.Symlink(GoString(target), GoString(linkpath)); err != nil { 182 if dmesgs { 183 dmesg("%v: %v FAIL", origin(1), err) 184 } 185 t.setErrno(err) 186 return -1 187 } 188 189 if dmesgs { 190 dmesg("%v: ok", origin(1)) 191 } 192 return 0 193 } 194 195 // int chmod(const char *pathname, mode_t mode) 196 func Xchmod(t *TLS, pathname uintptr, mode types.Mode_t) int32 { 197 if err := unix.Chmod(GoString(pathname), uint32(mode)); err != nil { 198 if dmesgs { 199 dmesg("%v: %q %#o: %v FAIL", origin(1), GoString(pathname), mode, err) 200 } 201 t.setErrno(err) 202 return -1 203 } 204 205 if dmesgs { 206 dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode) 207 } 208 return 0 209 } 210 211 // time_t time(time_t *tloc); 212 func Xtime(t *TLS, tloc uintptr) time.Time_t { 213 panic(todo("")) 214 // n := time.Now().UTC().Unix() 215 // if tloc != 0 { 216 // *(*types.Time_t)(unsafe.Pointer(tloc)) = types.Time_t(n) 217 // } 218 // return types.Time_t(n) 219 } 220 221 // int utimes(const char *filename, const struct timeval times[2]); 222 func Xutimes(t *TLS, filename, times uintptr) int32 { 223 var a []unix.Timeval 224 if times != 0 { 225 a = make([]unix.Timeval, 2) 226 a[0] = *(*unix.Timeval)(unsafe.Pointer(times)) 227 a[1] = *(*unix.Timeval)(unsafe.Pointer(times + unsafe.Sizeof(unix.Timeval{}))) 228 } 229 if err := unix.Utimes(GoString(filename), a); err != nil { 230 if dmesgs { 231 dmesg("%v: %v FAIL", origin(1), err) 232 } 233 t.setErrno(err) 234 return -1 235 } 236 237 if dmesgs { 238 dmesg("%v: ok", origin(1)) 239 } 240 return 0 241 } 242 243 // int fstat(int fd, struct stat *statbuf); 244 func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 { 245 if err := unix.Fstat(int(fd), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil { 246 if dmesgs { 247 dmesg("%v: fd %d: %v FAIL", origin(1), fd, err) 248 } 249 t.setErrno(err) 250 return -1 251 } 252 253 if dmesgs { 254 dmesg("%v: fd %d: ok", origin(1), fd) 255 } 256 return 0 257 } 258 259 // off64_t lseek64(int fd, off64_t offset, int whence); 260 func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t { 261 n, err := unix.Seek(int(fd), int64(offset), int(whence)) 262 if err != nil { 263 if dmesgs { 264 dmesg("%v: %v FAIL", origin(1), err) 265 } 266 t.setErrno(err) 267 return -1 268 } 269 270 if dmesgs { 271 dmesg("%v: ok", origin(1)) 272 } 273 return types.Off_t(n) 274 } 275 276 func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) int32 { 277 var arg uintptr 278 if args != 0 { 279 arg = *(*uintptr)(unsafe.Pointer(args)) 280 } 281 n, _, err := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), uintptr(cmd), arg) 282 if err != 0 { 283 if dmesgs { 284 dmesg("%v: fd %v cmd %v", origin(1), fcntlCmdStr(fd), cmd) 285 } 286 t.setErrno(err) 287 return -1 288 } 289 290 if dmesgs { 291 dmesg("%v: %d %s %#x: %d", origin(1), fd, fcntlCmdStr(cmd), arg, n) 292 } 293 return int32(n) 294 } 295 296 // int rename(const char *oldpath, const char *newpath); 297 func Xrename(t *TLS, oldpath, newpath uintptr) int32 { 298 if err := unix.Rename(GoString(oldpath), GoString(newpath)); err != nil { 299 if dmesgs { 300 dmesg("%v: %v FAIL", origin(1), err) 301 } 302 t.setErrno(err) 303 return -1 304 } 305 306 if dmesgs { 307 dmesg("%v: ok", origin(1)) 308 } 309 return 0 310 } 311 312 // int mknod(const char *pathname, mode_t mode, dev_t dev); 313 func Xmknod(t *TLS, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 { 314 panic(todo("")) 315 // if _, _, err := unix.Syscall(unix.SYS_MKNOD, pathname, uintptr(mode), uintptr(dev)); err != 0 { 316 // t.setErrno(err) 317 // return -1 318 // } 319 320 // return 0 321 } 322 323 // int utime(const char *filename, const struct utimbuf *times); 324 func Xutime(t *TLS, filename, times uintptr) int32 { 325 var a []unix.Timeval 326 if times != 0 { 327 a = make([]unix.Timeval, 2) 328 a[0].Sec = (*utime.Utimbuf)(unsafe.Pointer(times)).Factime 329 a[1].Sec = (*utime.Utimbuf)(unsafe.Pointer(times)).Fmodtime 330 } 331 if err := unix.Utimes(GoString(filename), a); err != nil { 332 if dmesgs { 333 dmesg("%v: %v FAIL", origin(1), err) 334 } 335 t.setErrno(err) 336 return -1 337 } 338 339 if dmesgs { 340 dmesg("%v: ok", origin(1)) 341 } 342 return 0 343 } 344 345 // int chown(const char *pathname, uid_t owner, gid_t group); 346 func Xchown(t *TLS, pathname uintptr, owner types.Uid_t, group types.Gid_t) int32 { 347 if _, _, err := unix.Syscall(unix.SYS_CHOWN, pathname, uintptr(owner), uintptr(group)); err != 0 { 348 t.setErrno(err) 349 return -1 350 } 351 352 return 0 353 } 354 355 // int link(const char *oldpath, const char *newpath); 356 func Xlink(t *TLS, oldpath, newpath uintptr) int32 { 357 panic(todo("")) 358 // if _, _, err := unix.Syscall(unix.SYS_LINK, oldpath, newpath, 0); err != 0 { 359 // t.setErrno(err) 360 // return -1 361 // } 362 363 // return 0 364 } 365 366 // int dup2(int oldfd, int newfd); 367 func Xdup2(t *TLS, oldfd, newfd int32) int32 { 368 panic(todo("")) 369 // n, _, err := unix.Syscall(unix.SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0) 370 // if err != 0 { 371 // t.setErrno(err) 372 // return -1 373 // } 374 375 // return int32(n) 376 } 377 378 // unsigned int alarm(unsigned int seconds); 379 func Xalarm(t *TLS, seconds uint32) uint32 { 380 panic(todo("")) 381 // n, _, err := unix.Syscall(unix.SYS_ALARM, uintptr(seconds), 0, 0) 382 // if err != 0 { 383 // panic(todo("")) 384 // } 385 386 // return uint32(n) 387 } 388 389 // int getnameinfo(const struct sockaddr * restrict sa, socklen_t salen, char * restrict host, socklen_t hostlen, char * restrict serv, socklen_t servlen, int flags); 390 func Xgetnameinfo(tls *TLS, sa1 uintptr, sl socklen_t, node uintptr, nodelen socklen_t, serv uintptr, servlen socklen_t, flags int32) int32 { /* getnameinfo.c:125:5: */ 391 panic(todo("")) 392 //TODO bp := tls.Alloc(347) 393 //TODO defer tls.Free(347) 394 395 //TODO // var ptr [78]int8 at bp, 78 396 397 //TODO // var buf [256]int8 at bp+78, 256 398 399 //TODO // var num [13]int8 at bp+334, 13 400 401 //TODO var af int32 = int32((*sockaddr)(unsafe.Pointer(sa1)).sa_family) 402 //TODO var a uintptr 403 //TODO var scopeid uint32 404 405 //TODO switch af { 406 //TODO case 2: 407 //TODO a = (sa1 + 4 /* &.sin_addr */) 408 //TODO if (uint64(sl) < uint64(unsafe.Sizeof(sockaddr_in{}))) { 409 //TODO return -6 410 //TODO } 411 //TODO mkptr4(tls, bp /* &ptr[0] */, a) 412 //TODO scopeid = uint32(0) 413 //TODO break 414 //TODO case 10: 415 //TODO a = (sa1 + 8 /* &.sin6_addr */) 416 //TODO if (uint64(sl) < uint64(unsafe.Sizeof(sockaddr_in6{}))) { 417 //TODO return -6 418 //TODO } 419 //TODO if Xmemcmp(tls, a, ts+88 /* "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff" */, uint64(12)) != 0 { 420 //TODO mkptr6(tls, bp /* &ptr[0] */, a) 421 //TODO } else { 422 //TODO mkptr4(tls, bp /* &ptr[0] */, (a + uintptr(12))) 423 //TODO } 424 //TODO scopeid = (*sockaddr_in6)(unsafe.Pointer(sa1)).sin6_scope_id 425 //TODO break 426 //TODO default: 427 //TODO return -6 428 //TODO } 429 430 //TODO if (node != 0) && (nodelen != 0) { 431 //TODO *(*int8)(unsafe.Pointer(bp + 78 /* &buf[0] */)) = int8(0) 432 //TODO if !((flags & 0x01) != 0) { 433 //TODO reverse_hosts(tls, bp+78 /* &buf[0] */, a, scopeid, af) 434 //TODO } 435 //TODO if !(int32(*(*int8)(unsafe.Pointer(bp + 78 /* buf */))) != 0) && !((flags & 0x01) != 0) { 436 //TODO Xabort(tls) //TODO- 437 //TODO // unsigned char query[18+PTR_MAX], reply[512]; 438 //TODO // int qlen = __res_mkquery(0, ptr, 1, RR_PTR, 439 //TODO // 0, 0, 0, query, sizeof query); 440 //TODO // query[3] = 0; /* don't need AD flag */ 441 //TODO // int rlen = __res_send(query, qlen, reply, sizeof reply); 442 //TODO // buf[0] = 0; 443 //TODO // if (rlen > 0) 444 //TODO // __dns_parse(reply, rlen, dns_parse_callback, buf); 445 //TODO } 446 //TODO if !(int32(*(*int8)(unsafe.Pointer(bp + 78 /* buf */))) != 0) { 447 //TODO if (flags & 0x08) != 0 { 448 //TODO return -2 449 //TODO } 450 //TODO Xinet_ntop(tls, af, a, bp+78 /* &buf[0] */, uint32(unsafe.Sizeof([256]int8{}))) 451 //TODO if scopeid != 0 { 452 //TODO Xabort(tls) //TODO- 453 //TODO // char *p = 0, tmp[IF_NAMESIZE+1]; 454 //TODO // if (!(flags & NI_NUMERICSCOPE) && 455 //TODO // (IN6_IS_ADDR_LINKLOCAL(a) || 456 //TODO // IN6_IS_ADDR_MC_LINKLOCAL(a))) 457 //TODO // p = if_indextoname(scopeid, tmp+1); 458 //TODO // if (!p) 459 //TODO // p = itoa(num, scopeid); 460 //TODO // *--p = '%'; 461 //TODO // strcat(buf, p); 462 //TODO } 463 //TODO } 464 //TODO if Xstrlen(tls, bp+78 /* &buf[0] */) >= size_t(nodelen) { 465 //TODO return -12 466 //TODO } 467 //TODO Xstrcpy(tls, node, bp+78 /* &buf[0] */) 468 //TODO } 469 470 //TODO if (serv != 0) && (servlen != 0) { 471 //TODO var p uintptr = bp + 78 /* buf */ 472 //TODO var port int32 = int32(Xntohs(tls, (*sockaddr_in)(unsafe.Pointer(sa1)).sin_port)) 473 //TODO *(*int8)(unsafe.Pointer(bp + 78 /* &buf[0] */)) = int8(0) 474 //TODO if !((flags & 0x02) != 0) { 475 //TODO reverse_services(tls, bp+78 /* &buf[0] */, port, (flags & 0x10)) 476 //TODO } 477 //TODO if !(int32(*(*int8)(unsafe.Pointer(p))) != 0) { 478 //TODO p = itoa(tls, bp+334 /* &num[0] */, uint32(port)) 479 //TODO } 480 //TODO if Xstrlen(tls, p) >= size_t(servlen) { 481 //TODO return -12 482 //TODO } 483 //TODO Xstrcpy(tls, serv, p) 484 //TODO } 485 486 //TODO return 0 487 } 488 489 func Xgethostbyaddr_r(tls *TLS, a uintptr, l socklen_t, af int32, h uintptr, buf uintptr, buflen size_t, res uintptr, err uintptr) int32 { /* gethostbyaddr_r.c:10:5: */ 490 panic(todo("")) 491 //TODO bp := tls.Alloc(28) 492 //TODO defer tls.Free(28) 493 494 //TODO //TODO union { 495 //TODO //TODO struct sockaddr_in sin; 496 //TODO //TODO struct sockaddr_in6 sin6; 497 //TODO //TODO } sa = { .sin.sin_family = af }; 498 //TODO *(*struct { 499 //TODO sin sockaddr_in 500 //TODO _ [12]byte 501 //TODO })(unsafe.Pointer(bp /* sa1 */)) = struct { 502 //TODO sin sockaddr_in 503 //TODO _ [12]byte 504 //TODO }{} //TODO- 505 //TODO (*sockaddr_in)(unsafe.Pointer(bp /* &sa1 */)).sin_family = sa_family_t(af) //TODO- 506 //TODO var sl socklen_t 507 //TODO if af == 10 { 508 //TODO sl = uint32(unsafe.Sizeof(sockaddr_in6{})) 509 //TODO } else { 510 //TODO sl = uint32(unsafe.Sizeof(sockaddr_in{})) 511 //TODO } 512 //TODO var i int32 513 514 //TODO *(*uintptr)(unsafe.Pointer(res)) = uintptr(0) 515 516 //TODO // Load address argument into sockaddr structure 517 //TODO if (af == 10) && (l == socklen_t(16)) { 518 //TODO Xmemcpy(tls, (bp /* &sa1 */ /* &.sin6 */ + 8 /* &.sin6_addr */), a, uint64(16)) 519 //TODO } else if (af == 2) && (l == socklen_t(4)) { 520 //TODO Xmemcpy(tls, (bp /* &sa1 */ /* &.sin */ + 4 /* &.sin_addr */), a, uint64(4)) 521 //TODO } else { 522 //TODO *(*int32)(unsafe.Pointer(err)) = 3 523 //TODO return 22 524 //TODO } 525 526 //TODO // Align buffer and check for space for pointers and ip address 527 //TODO i = (int32(uintptr_t(buf) & (uint64(unsafe.Sizeof(uintptr(0))) - uint64(1)))) 528 //TODO if !(i != 0) { 529 //TODO i = int32(unsafe.Sizeof(uintptr(0))) 530 //TODO } 531 //TODO if buflen <= (((uint64(5) * uint64(unsafe.Sizeof(uintptr(0)))) - uint64(i)) + uint64(l)) { 532 //TODO return 34 533 //TODO } 534 //TODO buf += (uintptr(uint64(unsafe.Sizeof(uintptr(0))) - uint64(i))) 535 //TODO buflen = buflen - (((uint64(5) * uint64(unsafe.Sizeof(uintptr(0)))) - uint64(i)) + uint64(l)) 536 537 //TODO (*hostent)(unsafe.Pointer(h)).h_addr_list = buf 538 //TODO buf += (uintptr(uint64(2) * uint64(unsafe.Sizeof(uintptr(0))))) 539 //TODO (*hostent)(unsafe.Pointer(h)).h_aliases = buf 540 //TODO buf += (uintptr(uint64(2) * uint64(unsafe.Sizeof(uintptr(0))))) 541 542 //TODO *(*uintptr)(unsafe.Pointer((*hostent)(unsafe.Pointer(h)).h_addr_list)) = buf 543 //TODO Xmemcpy(tls, *(*uintptr)(unsafe.Pointer((*hostent)(unsafe.Pointer(h)).h_addr_list)), a, uint64(l)) 544 //TODO buf += uintptr(l) 545 //TODO *(*uintptr)(unsafe.Pointer((*hostent)(unsafe.Pointer(h)).h_addr_list + 1*8)) = uintptr(0) 546 //TODO *(*uintptr)(unsafe.Pointer((*hostent)(unsafe.Pointer(h)).h_aliases)) = buf 547 //TODO *(*uintptr)(unsafe.Pointer((*hostent)(unsafe.Pointer(h)).h_aliases + 1*8)) = uintptr(0) 548 549 //TODO switch Xgetnameinfo(tls, bp /* &sa1 */, sl, buf, uint32(buflen), uintptr(0), uint32(0), 0) { 550 //TODO case -3: 551 //TODO *(*int32)(unsafe.Pointer(err)) = 2 552 //TODO return 11 553 //TODO case -12: 554 //TODO return 34 555 //TODO default: 556 //TODO fallthrough 557 //TODO case -10: 558 //TODO fallthrough 559 //TODO case -11: 560 //TODO fallthrough 561 //TODO case -4: 562 //TODO *(*int32)(unsafe.Pointer(err)) = 3 563 //TODO return *(*int32)(unsafe.Pointer(X___errno_location(tls))) 564 //TODO case 0: 565 //TODO break 566 //TODO } 567 568 //TODO (*hostent)(unsafe.Pointer(h)).h_addrtype = af 569 //TODO (*hostent)(unsafe.Pointer(h)).h_length = int32(l) 570 //TODO (*hostent)(unsafe.Pointer(h)).h_name = *(*uintptr)(unsafe.Pointer((*hostent)(unsafe.Pointer(h)).h_aliases)) 571 //TODO *(*uintptr)(unsafe.Pointer(res)) = h 572 //TODO return 0 573 } 574 575 // int getrlimit(int resource, struct rlimit *rlim); 576 func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 { 577 if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 { 578 t.setErrno(err) 579 return -1 580 } 581 582 return 0 583 } 584 585 func newFtsent(t *TLS, info int, path string, stat *unix.Stat_t, err syscall.Errno) (r *fts.FTSENT) { 586 var statp uintptr 587 if stat != nil { 588 statp = Xmalloc(t, types.Size_t(unsafe.Sizeof(unix.Stat_t{}))) 589 if statp == 0 { 590 panic("OOM") 591 } 592 593 *(*unix.Stat_t)(unsafe.Pointer(statp)) = *stat 594 } 595 csp, errx := CString(path) 596 if errx != nil { 597 panic("OOM") 598 } 599 600 return &fts.FTSENT{ 601 Ffts_info: uint16(info), 602 Ffts_path: csp, 603 Ffts_pathlen: uint32(len(path)), 604 Ffts_statp: statp, 605 Ffts_errno: int32(err), 606 } 607 } 608 609 // DIR *opendir(const char *name); 610 func Xopendir(t *TLS, name uintptr) uintptr { 611 p := Xmalloc(t, uint64(unsafe.Sizeof(darwinDir{}))) 612 if p == 0 { 613 panic("OOM") 614 } 615 616 fd := int(Xopen(t, name, fcntl.O_RDONLY|fcntl.O_DIRECTORY|fcntl.O_CLOEXEC, 0)) 617 if fd < 0 { 618 if dmesgs { 619 dmesg("%v: FAIL %v", origin(1), (*darwinDir)(unsafe.Pointer(p)).fd) 620 } 621 Xfree(t, p) 622 return 0 623 } 624 625 if dmesgs { 626 dmesg("%v: ok", origin(1)) 627 } 628 (*darwinDir)(unsafe.Pointer(p)).fd = fd 629 (*darwinDir)(unsafe.Pointer(p)).h = 0 630 (*darwinDir)(unsafe.Pointer(p)).l = 0 631 (*darwinDir)(unsafe.Pointer(p)).eof = false 632 return p 633 }