libc_windows_386.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. package libc // import "modernc.org/libc"
  5. import (
  6. "os"
  7. "strings"
  8. "syscall"
  9. "unsafe"
  10. "modernc.org/libc/errno"
  11. "modernc.org/libc/sys/stat"
  12. "modernc.org/libc/sys/types"
  13. )
  14. // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
  15. func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 {
  16. panic(todo(""))
  17. // // musl/arch/x32/ksigaction.h
  18. // //
  19. // // struct k_sigaction {
  20. // // void (*handler)(int);
  21. // // unsigned long flags;
  22. // // void (*restorer)(void);
  23. // // unsigned mask[2];
  24. // // };
  25. // type k_sigaction struct {
  26. // handler uintptr
  27. // flags ulong
  28. // restorer uintptr
  29. // mask [2]uint32
  30. // }
  31. // var kact, koldact uintptr
  32. // if act != 0 {
  33. // kact = t.Alloc(int(unsafe.Sizeof(k_sigaction{})))
  34. // defer Xfree(t, kact)
  35. // *(*k_sigaction)(unsafe.Pointer(kact)) = k_sigaction{
  36. // handler: (*signal.Sigaction)(unsafe.Pointer(act)).F__sigaction_handler.Fsa_handler,
  37. // flags: ulong((*signal.Sigaction)(unsafe.Pointer(act)).Fsa_flags),
  38. // restorer: (*signal.Sigaction)(unsafe.Pointer(act)).Fsa_restorer,
  39. // }
  40. // Xmemcpy(t, kact+unsafe.Offsetof(k_sigaction{}.mask), act+unsafe.Offsetof(signal.Sigaction{}.Fsa_mask), types.Size_t(unsafe.Sizeof(k_sigaction{}.mask)))
  41. // }
  42. // if oldact != 0 {
  43. // panic(todo(""))
  44. // }
  45. // if _, _, err := unix.Syscall6(unix.SYS_RT_SIGACTION, uintptr(signal.SIGABRT), kact, koldact, unsafe.Sizeof(k_sigaction{}.mask), 0, 0); err != 0 {
  46. // t.setErrno(err)
  47. // return -1
  48. // }
  49. // if oldact != 0 {
  50. // panic(todo(""))
  51. // }
  52. // return 0
  53. }
  54. // int fcntl(int fd, int cmd, ... /* arg */ );
  55. func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) int32 {
  56. panic(todo(""))
  57. // var arg uintptr
  58. // if args != 0 {
  59. // arg = *(*uintptr)(unsafe.Pointer(args))
  60. // }
  61. // n, _, err := unix.Syscall(unix.SYS_FCNTL64, uintptr(fd), uintptr(cmd), arg)
  62. // if err != 0 {
  63. // if dmesgs {
  64. // dmesg("%v: fd %v cmd %v", origin(1), fcntlCmdStr(fd), cmd)
  65. // }
  66. // t.setErrno(err)
  67. // return -1
  68. // }
  69. //
  70. // if dmesgs {
  71. // dmesg("%v: %d %s %#x: %d", origin(1), fd, fcntlCmdStr(cmd), arg, n)
  72. // }
  73. // return int32(n)
  74. }
  75. // int lstat(const char *pathname, struct stat *statbuf);
  76. func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 {
  77. panic(todo(""))
  78. // if _, _, err := unix.Syscall(unix.SYS_LSTAT64, pathname, statbuf, 0); err != 0 {
  79. // if dmesgs {
  80. // dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
  81. // }
  82. // t.setErrno(err)
  83. // return -1
  84. // }
  85. //
  86. // if dmesgs {
  87. // dmesg("%v: %q: ok", origin(1), GoString(pathname))
  88. // }
  89. // return 0
  90. }
  91. // int stat(const char *pathname, struct stat *statbuf);
  92. func Xstat64(t *TLS, pathname, statbuf uintptr) int32 {
  93. panic(todo(""))
  94. // if _, _, err := unix.Syscall(unix.SYS_STAT64, pathname, statbuf, 0); err != 0 {
  95. // if dmesgs {
  96. // dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
  97. // }
  98. // t.setErrno(err)
  99. // return -1
  100. // }
  101. //
  102. // if dmesgs {
  103. // dmesg("%v: %q: ok", origin(1), GoString(pathname))
  104. // }
  105. // return 0
  106. }
  107. // int fstat(int fd, struct stat *statbuf);
  108. func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 {
  109. panic(todo(""))
  110. // if _, _, err := unix.Syscall(unix.SYS_FSTAT64, uintptr(fd), statbuf, 0); err != 0 {
  111. // if dmesgs {
  112. // dmesg("%v: fd %d: %v", origin(1), fd, err)
  113. // }
  114. // t.setErrno(err)
  115. // return -1
  116. // }
  117. //
  118. // if dmesgs {
  119. // dmesg("%v: %d, size %#x: ok\n%+v", origin(1), fd, (*stat.Stat)(unsafe.Pointer(statbuf)).Fst_size, (*stat.Stat)(unsafe.Pointer(statbuf)))
  120. // }
  121. // return 0
  122. }
  123. // void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, ... /* void *new_address */);
  124. func Xmremap(t *TLS, old_address uintptr, old_size, new_size types.Size_t, flags int32, args uintptr) uintptr {
  125. panic(todo(""))
  126. // var arg uintptr
  127. // if args != 0 {
  128. // arg = *(*uintptr)(unsafe.Pointer(args))
  129. // }
  130. // data, _, err := unix.Syscall6(unix.SYS_MREMAP, old_address, uintptr(old_size), uintptr(new_size), uintptr(flags), arg, 0)
  131. // if err != 0 {
  132. // if dmesgs {
  133. // dmesg("%v: %v", origin(1), err)
  134. // }
  135. // t.setErrno(err)
  136. // return ^uintptr(0) // (void*)-1
  137. // }
  138. //
  139. // if dmesgs {
  140. // dmesg("%v: %#x", origin(1), data)
  141. // }
  142. // return data
  143. }
  144. func Xmmap(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
  145. return Xmmap64(t, addr, length, prot, flags, fd, offset)
  146. }
  147. // void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
  148. func Xmmap64(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
  149. panic(todo(""))
  150. // data, _, err := unix.Syscall6(unix.SYS_MMAP2, addr, uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset>>12))
  151. // if err != 0 {
  152. // if dmesgs {
  153. // dmesg("%v: %v", origin(1), err)
  154. // }
  155. // t.setErrno(err)
  156. // return ^uintptr(0) // (void*)-1
  157. // }
  158. //
  159. // if dmesgs {
  160. // dmesg("%v: %#x", origin(1), data)
  161. // }
  162. // return data
  163. }
  164. // int ftruncate(int fd, off_t length);
  165. func Xftruncate64(t *TLS, fd int32, length types.Off_t) int32 {
  166. panic(todo(""))
  167. // if _, _, err := unix.Syscall(unix.SYS_FTRUNCATE64, uintptr(fd), uintptr(length), uintptr(length>>32)); err != 0 {
  168. // if dmesgs {
  169. // dmesg("%v: fd %d: %v", origin(1), fd, err)
  170. // }
  171. // t.setErrno(err)
  172. // return -1
  173. // }
  174. //
  175. // if dmesgs {
  176. // dmesg("%v: %d %#x: ok", origin(1), fd, length)
  177. // }
  178. // return 0
  179. }
  180. // off64_t lseek64(int fd, off64_t offset, int whence);
  181. func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
  182. panic(todo(""))
  183. // bp := t.Alloc(int(unsafe.Sizeof(types.X__loff_t(0))))
  184. // defer t.Free(int(unsafe.Sizeof(types.X__loff_t(0))))
  185. // if _, _, err := unix.Syscall6(unix.SYS__LLSEEK, uintptr(fd), uintptr(offset>>32), uintptr(offset), bp, uintptr(whence), 0); err != 0 {
  186. // if dmesgs {
  187. // dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), fd, offset, whenceStr(whence), err)
  188. // }
  189. // t.setErrno(err)
  190. // return -1
  191. // }
  192. //
  193. // if dmesgs {
  194. // dmesg("%v: fd %v, off %#x, whence %v: %#x", origin(1), fd, offset, whenceStr(whence), *(*types.Off_t)(unsafe.Pointer(bp)))
  195. // }
  196. // return *(*types.Off_t)(unsafe.Pointer(bp))
  197. }
  198. // int utime(const char *filename, const struct utimbuf *times);
  199. func Xutime(t *TLS, filename, times uintptr) int32 {
  200. panic(todo(""))
  201. // if _, _, err := unix.Syscall(unix.SYS_UTIME, filename, times, 0); err != 0 {
  202. // t.setErrno(err)
  203. // return -1
  204. // }
  205. //
  206. // return 0
  207. }
  208. // unsigned int alarm(unsigned int seconds);
  209. func Xalarm(t *TLS, seconds uint32) uint32 {
  210. panic(todo(""))
  211. // n, _, err := unix.Syscall(unix.SYS_ALARM, uintptr(seconds), 0, 0)
  212. // if err != 0 {
  213. // panic(todo(""))
  214. // }
  215. //
  216. // return uint32(n)
  217. }
  218. // int getrlimit(int resource, struct rlimit *rlim);
  219. func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
  220. panic(todo(""))
  221. // if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
  222. // t.setErrno(err)
  223. // return -1
  224. // }
  225. //
  226. // return 0
  227. }
  228. // time_t time(time_t *tloc);
  229. func Xtime(t *TLS, tloc uintptr) types.Time_t {
  230. panic(todo(""))
  231. // n, _, err := unix.Syscall(unix.SYS_TIME, tloc, 0, 0)
  232. // if err != 0 {
  233. // t.setErrno(err)
  234. // return types.Time_t(-1)
  235. // }
  236. //
  237. // if tloc != 0 {
  238. // *(*types.Time_t)(unsafe.Pointer(tloc)) = types.Time_t(n)
  239. // }
  240. // return types.Time_t(n)
  241. }
  242. // int mkdir(const char *path, mode_t mode);
  243. func Xmkdir(t *TLS, path uintptr, mode types.Mode_t) int32 {
  244. panic(todo(""))
  245. // if _, _, err := unix.Syscall(unix.SYS_MKDIR, path, uintptr(mode), 0); err != 0 {
  246. // t.setErrno(err)
  247. // return -1
  248. // }
  249. //
  250. // if dmesgs {
  251. // dmesg("%v: %q: ok", origin(1), GoString(path))
  252. // }
  253. // return 0
  254. }
  255. // int symlink(const char *target, const char *linkpath);
  256. func Xsymlink(t *TLS, target, linkpath uintptr) int32 {
  257. panic(todo(""))
  258. // if _, _, err := unix.Syscall(unix.SYS_SYMLINK, target, linkpath, 0); err != 0 {
  259. // t.setErrno(err)
  260. // return -1
  261. // }
  262. //
  263. // if dmesgs {
  264. // dmesg("%v: %q %q: ok", origin(1), GoString(target), GoString(linkpath))
  265. // }
  266. // return 0
  267. }
  268. // int utimes(const char *filename, const struct timeval times[2]);
  269. func Xutimes(t *TLS, filename, times uintptr) int32 {
  270. panic(todo(""))
  271. // if _, _, err := unix.Syscall(unix.SYS_UTIMES, filename, times, 0); err != 0 {
  272. // t.setErrno(err)
  273. // return -1
  274. // }
  275. //
  276. // if dmesgs {
  277. // dmesg("%v: %q: ok", origin(1), GoString(filename))
  278. // }
  279. // return 0
  280. }
  281. // int unlink(const char *pathname);
  282. func Xunlink(t *TLS, pathname uintptr) int32 {
  283. err := syscall.DeleteFile((*uint16)(unsafe.Pointer(pathname)))
  284. if err != nil {
  285. t.setErrno(err)
  286. return -1
  287. }
  288. if dmesgs {
  289. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  290. }
  291. return 0
  292. }
  293. // int access(const char *pathname, int mode);
  294. func Xaccess(t *TLS, pathname uintptr, mode int32) int32 {
  295. panic(todo(""))
  296. // if _, _, err := unix.Syscall(unix.SYS_ACCESS, pathname, uintptr(mode), 0); err != 0 {
  297. // if dmesgs {
  298. // dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
  299. // }
  300. // t.setErrno(err)
  301. // return -1
  302. // }
  303. //
  304. // if dmesgs {
  305. // dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
  306. // }
  307. // return 0
  308. }
  309. // int rmdir(const char *pathname);
  310. func Xrmdir(t *TLS, pathname uintptr) int32 {
  311. panic(todo(""))
  312. // if _, _, err := unix.Syscall(unix.SYS_RMDIR, pathname, 0, 0); err != 0 {
  313. // t.setErrno(err)
  314. // return -1
  315. // }
  316. //
  317. // if dmesgs {
  318. // dmesg("%v: %q: ok", origin(1), GoString(pathname))
  319. // }
  320. // return 0
  321. }
  322. // int mknod(const char *pathname, mode_t mode, dev_t dev);
  323. func Xmknod(t *TLS, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 {
  324. panic(todo(""))
  325. // if _, _, err := unix.Syscall(unix.SYS_MKNOD, pathname, uintptr(mode), uintptr(dev)); err != 0 {
  326. // t.setErrno(err)
  327. // return -1
  328. // }
  329. //
  330. // return 0
  331. }
  332. // // int chown(const char *pathname, uid_t owner, gid_t group);
  333. // func Xchown(t *TLS, pathname uintptr, owner types.Uid_t, group types.Gid_t) int32 {
  334. // panic(todo(""))
  335. // // if _, _, err := unix.Syscall(unix.SYS_CHOWN, pathname, uintptr(owner), uintptr(group)); err != 0 {
  336. // // t.setErrno(err)
  337. // // return -1
  338. // // }
  339. // //
  340. // // return 0
  341. // }
  342. // int link(const char *oldpath, const char *newpath);
  343. func Xlink(t *TLS, oldpath, newpath uintptr) int32 {
  344. panic(todo(""))
  345. // if _, _, err := unix.Syscall(unix.SYS_LINK, oldpath, newpath, 0); err != 0 {
  346. // t.setErrno(err)
  347. // return -1
  348. // }
  349. //
  350. // return 0
  351. }
  352. // int pipe(int pipefd[2]);
  353. func Xpipe(t *TLS, pipefd uintptr) int32 {
  354. panic(todo(""))
  355. // if _, _, err := unix.Syscall(unix.SYS_PIPE, pipefd, 0, 0); err != 0 {
  356. // t.setErrno(err)
  357. // return -1
  358. // }
  359. //
  360. // return 0
  361. }
  362. // int dup2(int oldfd, int newfd);
  363. func Xdup2(t *TLS, oldfd, newfd int32) int32 {
  364. panic(todo(""))
  365. // n, _, err := unix.Syscall(unix.SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
  366. // if err != 0 {
  367. // t.setErrno(err)
  368. // return -1
  369. // }
  370. //
  371. // return int32(n)
  372. }
  373. // ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
  374. func Xreadlink(t *TLS, path, buf uintptr, bufsize types.Size_t) types.Ssize_t {
  375. panic(todo(""))
  376. // n, _, err := unix.Syscall(unix.SYS_READLINK, path, buf, uintptr(bufsize))
  377. // if err != 0 {
  378. // t.setErrno(err)
  379. // return -1
  380. // }
  381. //
  382. // return types.Ssize_t(n)
  383. }
  384. // FILE *fopen64(const char *pathname, const char *mode);
  385. func Xfopen64(t *TLS, pathname, mode uintptr) uintptr {
  386. m := strings.ReplaceAll(GoString(mode), "b", "")
  387. var flags int
  388. switch m {
  389. case "r":
  390. flags = os.O_RDONLY
  391. case "r+":
  392. flags = os.O_RDWR
  393. case "w":
  394. flags = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
  395. case "w+":
  396. flags = os.O_RDWR | os.O_CREATE | os.O_TRUNC
  397. case "a":
  398. flags = os.O_WRONLY | os.O_CREATE | os.O_APPEND
  399. case "a+":
  400. flags = os.O_RDWR | os.O_CREATE | os.O_APPEND
  401. default:
  402. panic(m)
  403. }
  404. //TODO- flags |= fcntl.O_LARGEFILE
  405. h, err := syscall.Open(GoString(pathname), int(flags), uint32(0666))
  406. if err != nil {
  407. t.setErrno(err)
  408. return 0
  409. }
  410. p, _ := wrapFdHandle(h)
  411. if p != 0 {
  412. return p
  413. }
  414. _ = syscall.Close(h)
  415. t.setErrno(errno.ENOMEM)
  416. return 0
  417. }
  418. func Xrecv(t *TLS, sockfd uint32, buf uintptr, len, flags int32) int32 {
  419. panic(todo(""))
  420. }
  421. func Xsend(t *TLS, sockfd uint32, buf uintptr, len, flags int32) int32 {
  422. panic(todo(""))
  423. }
  424. func Xshutdown(t *TLS, sockfd uint32, how int32) int32 {
  425. panic(todo(""))
  426. // if _, _, err := unix.Syscall(unix.SYS_SHUTDOWN, uintptr(sockfd), uintptr(how), 0); err != 0 {
  427. // t.setErrno(err)
  428. // return -1
  429. // }
  430. //
  431. // return 0
  432. }
  433. func Xgetpeername(t *TLS, sockfd uint32, addr uintptr, addrlen uintptr) int32 {
  434. panic(todo(""))
  435. }
  436. func Xgetsockname(t *TLS, sockfd uint32, addr, addrlen uintptr) int32 {
  437. panic(todo(""))
  438. }
  439. func Xsocket(t *TLS, domain, type1, protocol int32) uint32 {
  440. panic(todo(""))
  441. }
  442. func Xbind(t *TLS, sockfd uint32, addr uintptr, addrlen int32) int32 {
  443. panic(todo(""))
  444. }
  445. func Xconnect(t *TLS, sockfd uint32, addr uintptr, addrlen int32) int32 {
  446. panic(todo(""))
  447. }
  448. func Xlisten(t *TLS, sockfd uint32, backlog int32) int32 {
  449. panic(todo(""))
  450. }
  451. func Xaccept(t *TLS, sockfd uint32, addr uintptr, addrlen uintptr) uint32 {
  452. panic(todo(""))
  453. }
  454. // struct tm *_localtime32( const __time32_t *sourceTime );
  455. func X_localtime32(t *TLS, sourceTime uintptr) uintptr {
  456. panic(todo(""))
  457. }
  458. // struct tm *_gmtime32( const __time32_t *sourceTime );
  459. func X_gmtime32(t *TLS, sourceTime uintptr) uintptr {
  460. panic(todo(""))
  461. }
  462. // LONG SetWindowLongW(
  463. //
  464. // HWND hWnd,
  465. // int nIndex,
  466. // LONG dwNewLong
  467. //
  468. // );
  469. func XSetWindowLongW(t *TLS, hwnd uintptr, nIndex int32, dwNewLong long) long {
  470. panic(todo(""))
  471. }
  472. // LONG GetWindowLongW(
  473. //
  474. // HWND hWnd,
  475. // int nIndex
  476. //
  477. // );
  478. func XGetWindowLongW(t *TLS, hwnd uintptr, nIndex int32) long {
  479. panic(todo(""))
  480. }
  481. // LRESULT LRESULT DefWindowProcW(
  482. //
  483. // HWND hWnd,
  484. // UINT Msg,
  485. // WPARAM wParam,
  486. // LPARAM lParam
  487. //
  488. // );
  489. func XDefWindowProcW(t *TLS, _ ...interface{}) int32 {
  490. panic(todo(""))
  491. }
  492. func XSendMessageTimeoutW(t *TLS, _ ...interface{}) int32 {
  493. panic(todo(""))
  494. }
  495. // int _fstat(
  496. //
  497. // int fd,
  498. // struct __stat *buffer
  499. //
  500. // );
  501. func X_fstat(t *TLS, fd int32, buffer uintptr) int32 {
  502. f, ok := fdToFile(fd)
  503. if !ok {
  504. t.setErrno(EBADF)
  505. return -1
  506. }
  507. var d syscall.ByHandleFileInformation
  508. err := syscall.GetFileInformationByHandle(f.Handle, &d)
  509. if err != nil {
  510. t.setErrno(EBADF)
  511. return -1
  512. }
  513. var bStat32 = (*stat.X_stat32)(unsafe.Pointer(buffer))
  514. var accessTime = int64(d.LastAccessTime.HighDateTime)<<32 + int64(d.LastAccessTime.LowDateTime)
  515. bStat32.Fst_atime = int32(WindowsTickToUnixSeconds(accessTime))
  516. var modTime = int64(d.LastWriteTime.HighDateTime)<<32 + int64(d.LastWriteTime.LowDateTime)
  517. bStat32.Fst_mtime = int32(WindowsTickToUnixSeconds(modTime))
  518. var crTime = int64(d.CreationTime.HighDateTime)<<32 + int64(d.CreationTime.LowDateTime)
  519. bStat32.Fst_ctime = int32(WindowsTickToUnixSeconds(crTime))
  520. var fSz = int64(d.FileSizeHigh)<<32 + int64(d.FileSizeLow)
  521. bStat32.Fst_size = int32(fSz)
  522. bStat32.Fst_mode = WindowsAttrbiutesToStat(d.FileAttributes)
  523. return 0
  524. }