libc_darwin.go 50 KB

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