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