1 | // Copyright 2020 The Libc Authors. All rights reserved.
|
---|
2 | // Use of this source code is governed by a BSD-style
|
---|
3 | // license that can be found in the LICENSE file.
|
---|
4 |
|
---|
5 | package libc // import "modernc.org/libc"
|
---|
6 |
|
---|
7 | import (
|
---|
8 | "fmt"
|
---|
9 | "io"
|
---|
10 | "os"
|
---|
11 | "os/exec"
|
---|
12 | "path/filepath"
|
---|
13 | "runtime"
|
---|
14 | "runtime/debug"
|
---|
15 | "strings"
|
---|
16 | "syscall"
|
---|
17 | gotime "time"
|
---|
18 | "unsafe"
|
---|
19 |
|
---|
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/stdio"
|
---|
31 | "modernc.org/libc/sys/socket"
|
---|
32 | "modernc.org/libc/sys/stat"
|
---|
33 | "modernc.org/libc/sys/types"
|
---|
34 | "modernc.org/libc/termios"
|
---|
35 | "modernc.org/libc/time"
|
---|
36 | "modernc.org/libc/unistd"
|
---|
37 | "modernc.org/libc/uuid"
|
---|
38 | )
|
---|
39 |
|
---|
40 | var (
|
---|
41 | in6_addr_any in.In6_addr
|
---|
42 | )
|
---|
43 |
|
---|
44 | // // Keep these outside of the var block otherwise go generate will miss them.
|
---|
45 | var X__stderrp = Xstdout
|
---|
46 | var X__stdinp = Xstdin
|
---|
47 | var X__stdoutp = Xstdout
|
---|
48 | var X__sF [3]stdio.FILE
|
---|
49 | var X_tolower_tab_ = Xmalloc(nil, 2*65537)
|
---|
50 | var X_toupper_tab_ = Xmalloc(nil, 2*65537)
|
---|
51 |
|
---|
52 | func init() {
|
---|
53 | for c := rune(0); c < 0xffff; c++ {
|
---|
54 | y := c
|
---|
55 | s := strings.ToLower(string(c))
|
---|
56 | a := []rune(s)
|
---|
57 | if len(a) != 0 {
|
---|
58 | y = a[0]
|
---|
59 | }
|
---|
60 | (*[65536]uint16)(unsafe.Pointer(X_tolower_tab_))[c+1] = uint16(y)
|
---|
61 | y = c
|
---|
62 | s = strings.ToUpper(string(c))
|
---|
63 | a = []rune(s)
|
---|
64 | if len(a) != 0 {
|
---|
65 | y = a[0]
|
---|
66 | }
|
---|
67 | (*[65536]uint16)(unsafe.Pointer(X_toupper_tab_))[c+1] = uint16(y)
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | // include/stdio.h:486:extern int __isthreaded;
|
---|
72 | var X__isthreaded int32
|
---|
73 |
|
---|
74 | // lib/libc/locale/mblocal.h:62: int __mb_sb_limit;
|
---|
75 | var X__mb_sb_limit int32 = 128 // UTF-8
|
---|
76 |
|
---|
77 | // include/runetype.h:94:extern _Thread_local const _RuneLocale *_ThreadRuneLocale;
|
---|
78 | var X_ThreadRuneLocale uintptr //TODO initialize and implement _Thread_local semantics.
|
---|
79 |
|
---|
80 | // include/xlocale/_ctype.h:54:_RuneLocale *__runes_for_locale(locale_t, int*);
|
---|
81 | func X__runes_for_locale(t *TLS, l locale_t, p uintptr) uintptr {
|
---|
82 | panic(todo(""))
|
---|
83 | }
|
---|
84 |
|
---|
85 | type file uintptr
|
---|
86 |
|
---|
87 | func (f file) fd() int32 { return int32((*stdio.FILE)(unsafe.Pointer(f)).F_file) }
|
---|
88 | func (f file) setFd(fd int32) { (*stdio.FILE)(unsafe.Pointer(f)).F_file = int16(fd) }
|
---|
89 |
|
---|
90 | func (f file) err() bool {
|
---|
91 | return (*stdio.FILE)(unsafe.Pointer(f)).F_flags&1 != 0
|
---|
92 | }
|
---|
93 |
|
---|
94 | func (f file) setErr() {
|
---|
95 | (*stdio.FILE)(unsafe.Pointer(f)).F_flags |= 1
|
---|
96 | }
|
---|
97 |
|
---|
98 | func (f file) close(t *TLS) int32 {
|
---|
99 | fd := f.fd()
|
---|
100 | r := Xclose(t, fd)
|
---|
101 | switch fd {
|
---|
102 | case unistd.STDIN_FILENO, unistd.STDOUT_FILENO, unistd.STDERR_FILENO:
|
---|
103 | X__sF[fd] = stdio.FILE{}
|
---|
104 | default:
|
---|
105 | Xfree(t, uintptr(f))
|
---|
106 | }
|
---|
107 | if r < 0 {
|
---|
108 | return stdio.EOF
|
---|
109 | }
|
---|
110 |
|
---|
111 | return 0
|
---|
112 | }
|
---|
113 |
|
---|
114 | func newFile(t *TLS, fd int32) uintptr {
|
---|
115 | var p uintptr
|
---|
116 | switch fd {
|
---|
117 | case unistd.STDIN_FILENO:
|
---|
118 | p = uintptr(unsafe.Pointer(&X__sF[0]))
|
---|
119 | case unistd.STDOUT_FILENO:
|
---|
120 | p = uintptr(unsafe.Pointer(&X__sF[1]))
|
---|
121 | case unistd.STDERR_FILENO:
|
---|
122 | p = uintptr(unsafe.Pointer(&X__sF[2]))
|
---|
123 | default:
|
---|
124 | if p = Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(stdio.FILE{}))); p == 0 {
|
---|
125 | return 0
|
---|
126 | }
|
---|
127 | }
|
---|
128 | file(p).setFd(fd)
|
---|
129 | return p
|
---|
130 | }
|
---|
131 |
|
---|
132 | func fwrite(fd int32, b []byte) (int, error) {
|
---|
133 | if fd == unistd.STDOUT_FILENO {
|
---|
134 | return write(b)
|
---|
135 | }
|
---|
136 |
|
---|
137 | // if dmesgs {
|
---|
138 | // dmesg("%v: fd %v: %s", origin(1), fd, b)
|
---|
139 | // }
|
---|
140 | return unix.Write(int(fd), b) //TODO use Xwrite
|
---|
141 | }
|
---|
142 |
|
---|
143 | // unsigned long ___runetype(__ct_rune_t) __pure;
|
---|
144 | func X___runetype(t *TLS, x int32) ulong {
|
---|
145 | panic(todo(""))
|
---|
146 | }
|
---|
147 |
|
---|
148 | // int fprintf(FILE *stream, const char *format, ...);
|
---|
149 | func Xfprintf(t *TLS, stream, format, args uintptr) int32 {
|
---|
150 | n, _ := fwrite(int32((*stdio.FILE)(unsafe.Pointer(stream)).F_file), printf(format, args))
|
---|
151 | return int32(n)
|
---|
152 | }
|
---|
153 |
|
---|
154 | // int usleep(useconds_t usec);
|
---|
155 | func Xusleep(t *TLS, usec uint32) int32 {
|
---|
156 | gotime.Sleep(gotime.Microsecond * gotime.Duration(usec))
|
---|
157 | return 0
|
---|
158 | }
|
---|
159 |
|
---|
160 | // int getrusage(int who, struct rusage *usage);
|
---|
161 | func Xgetrusage(t *TLS, who int32, usage uintptr) int32 {
|
---|
162 | if _, _, err := unix.Syscall(unix.SYS_GETRUSAGE, uintptr(who), usage, 0); err != 0 {
|
---|
163 | t.setErrno(err)
|
---|
164 | return -1
|
---|
165 | }
|
---|
166 |
|
---|
167 | return 0
|
---|
168 | }
|
---|
169 |
|
---|
170 | // int fgetc(FILE *stream);
|
---|
171 | func Xfgetc(t *TLS, stream uintptr) int32 {
|
---|
172 | fd := int((*stdio.FILE)(unsafe.Pointer(stream)).F_file)
|
---|
173 | var buf [1]byte
|
---|
174 | if n, _ := unix.Read(fd, buf[:]); n != 0 {
|
---|
175 | return int32(buf[0])
|
---|
176 | }
|
---|
177 |
|
---|
178 | return stdio.EOF
|
---|
179 | }
|
---|
180 |
|
---|
181 | // int lstat(const char *pathname, struct stat *statbuf);
|
---|
182 | func Xlstat(t *TLS, pathname, statbuf uintptr) int32 {
|
---|
183 | return Xlstat64(t, pathname, statbuf)
|
---|
184 | }
|
---|
185 |
|
---|
186 | // int stat(const char *pathname, struct stat *statbuf);
|
---|
187 | func Xstat(t *TLS, pathname, statbuf uintptr) int32 {
|
---|
188 | return Xstat64(t, pathname, statbuf)
|
---|
189 | }
|
---|
190 |
|
---|
191 | // int chdir(const char *path);
|
---|
192 | func Xchdir(t *TLS, path uintptr) int32 {
|
---|
193 | if _, _, err := unix.Syscall(unix.SYS_CHDIR, path, 0, 0); err != 0 {
|
---|
194 | t.setErrno(err)
|
---|
195 | return -1
|
---|
196 | }
|
---|
197 |
|
---|
198 | // if dmesgs {
|
---|
199 | // dmesg("%v: %q: ok", origin(1), GoString(path))
|
---|
200 | // }
|
---|
201 | return 0
|
---|
202 | }
|
---|
203 |
|
---|
204 | var localtime time.Tm
|
---|
205 |
|
---|
206 | // struct tm *localtime(const time_t *timep);
|
---|
207 | func Xlocaltime(_ *TLS, timep uintptr) uintptr {
|
---|
208 | loc := gotime.Local
|
---|
209 | if r := getenv(Environ(), "TZ"); r != 0 {
|
---|
210 | zone, off := parseZone(GoString(r))
|
---|
211 | loc = gotime.FixedZone(zone, -off)
|
---|
212 | }
|
---|
213 | ut := *(*time.Time_t)(unsafe.Pointer(timep))
|
---|
214 | t := gotime.Unix(int64(ut), 0).In(loc)
|
---|
215 | localtime.Ftm_sec = int32(t.Second())
|
---|
216 | localtime.Ftm_min = int32(t.Minute())
|
---|
217 | localtime.Ftm_hour = int32(t.Hour())
|
---|
218 | localtime.Ftm_mday = int32(t.Day())
|
---|
219 | localtime.Ftm_mon = int32(t.Month() - 1)
|
---|
220 | localtime.Ftm_year = int32(t.Year() - 1900)
|
---|
221 | localtime.Ftm_wday = int32(t.Weekday())
|
---|
222 | localtime.Ftm_yday = int32(t.YearDay())
|
---|
223 | localtime.Ftm_isdst = Bool32(isTimeDST(t))
|
---|
224 | return uintptr(unsafe.Pointer(&localtime))
|
---|
225 | }
|
---|
226 |
|
---|
227 | // struct tm *localtime_r(const time_t *timep, struct tm *result);
|
---|
228 | func Xlocaltime_r(_ *TLS, timep, result uintptr) uintptr {
|
---|
229 | loc := gotime.Local
|
---|
230 | if r := getenv(Environ(), "TZ"); r != 0 {
|
---|
231 | zone, off := parseZone(GoString(r))
|
---|
232 | loc = gotime.FixedZone(zone, -off)
|
---|
233 | }
|
---|
234 | ut := *(*time.Time_t)(unsafe.Pointer(timep))
|
---|
235 | t := gotime.Unix(int64(ut), 0).In(loc)
|
---|
236 | (*time.Tm)(unsafe.Pointer(result)).Ftm_sec = int32(t.Second())
|
---|
237 | (*time.Tm)(unsafe.Pointer(result)).Ftm_min = int32(t.Minute())
|
---|
238 | (*time.Tm)(unsafe.Pointer(result)).Ftm_hour = int32(t.Hour())
|
---|
239 | (*time.Tm)(unsafe.Pointer(result)).Ftm_mday = int32(t.Day())
|
---|
240 | (*time.Tm)(unsafe.Pointer(result)).Ftm_mon = int32(t.Month() - 1)
|
---|
241 | (*time.Tm)(unsafe.Pointer(result)).Ftm_year = int32(t.Year() - 1900)
|
---|
242 | (*time.Tm)(unsafe.Pointer(result)).Ftm_wday = int32(t.Weekday())
|
---|
243 | (*time.Tm)(unsafe.Pointer(result)).Ftm_yday = int32(t.YearDay())
|
---|
244 | (*time.Tm)(unsafe.Pointer(result)).Ftm_isdst = Bool32(isTimeDST(t))
|
---|
245 | return result
|
---|
246 | }
|
---|
247 |
|
---|
248 | // int open(const char *pathname, int flags, ...);
|
---|
249 | func Xopen(t *TLS, pathname uintptr, flags int32, args uintptr) int32 {
|
---|
250 | return Xopen64(t, pathname, flags, args)
|
---|
251 | }
|
---|
252 |
|
---|
253 | // int open(const char *pathname, int flags, ...);
|
---|
254 | func Xopen64(t *TLS, pathname uintptr, flags int32, args uintptr) int32 {
|
---|
255 | var mode types.Mode_t
|
---|
256 | if args != 0 {
|
---|
257 | mode = (types.Mode_t)(VaUint32(&args))
|
---|
258 | }
|
---|
259 | fdcwd := fcntl.AT_FDCWD
|
---|
260 | n, _, err := unix.Syscall6(unix.SYS_OPENAT, uintptr(fdcwd), pathname, uintptr(flags), uintptr(mode), 0, 0)
|
---|
261 | if err != 0 {
|
---|
262 | // if dmesgs {
|
---|
263 | // dmesg("%v: %q %#x: %v", origin(1), GoString(pathname), flags, err)
|
---|
264 | // }
|
---|
265 | t.setErrno(err)
|
---|
266 | return -1
|
---|
267 | }
|
---|
268 |
|
---|
269 | // if dmesgs {
|
---|
270 | // dmesg("%v: %q flags %#x mode %#o: fd %v", origin(1), GoString(pathname), flags, mode, n)
|
---|
271 | // }
|
---|
272 | return int32(n)
|
---|
273 | }
|
---|
274 |
|
---|
275 | // off_t lseek(int fd, off_t offset, int whence);
|
---|
276 | func Xlseek(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
|
---|
277 | return types.Off_t(Xlseek64(t, fd, offset, whence))
|
---|
278 | }
|
---|
279 |
|
---|
280 | func whenceStr(whence int32) string {
|
---|
281 | panic(todo(""))
|
---|
282 | }
|
---|
283 |
|
---|
284 | var fsyncStatbuf stat.Stat
|
---|
285 |
|
---|
286 | // int fsync(int fd);
|
---|
287 | func Xfsync(t *TLS, fd int32) int32 {
|
---|
288 | if noFsync {
|
---|
289 | // Simulate -DSQLITE_NO_SYNC for sqlite3 testfixture, see function full_sync in sqlite3.c
|
---|
290 | return Xfstat(t, fd, uintptr(unsafe.Pointer(&fsyncStatbuf)))
|
---|
291 | }
|
---|
292 |
|
---|
293 | if _, _, err := unix.Syscall(unix.SYS_FSYNC, uintptr(fd), 0, 0); err != 0 {
|
---|
294 | t.setErrno(err)
|
---|
295 | return -1
|
---|
296 | }
|
---|
297 |
|
---|
298 | // if dmesgs {
|
---|
299 | // dmesg("%v: %d: ok", origin(1), fd)
|
---|
300 | // }
|
---|
301 | return 0
|
---|
302 | }
|
---|
303 |
|
---|
304 | // long sysconf(int name);
|
---|
305 | func Xsysconf(t *TLS, name int32) long {
|
---|
306 | switch name {
|
---|
307 | case unistd.X_SC_PAGESIZE:
|
---|
308 | return long(unix.Getpagesize())
|
---|
309 | case unistd.X_SC_GETPW_R_SIZE_MAX:
|
---|
310 | return -1
|
---|
311 | case unistd.X_SC_GETGR_R_SIZE_MAX:
|
---|
312 | return -1
|
---|
313 | case unistd.X_SC_NPROCESSORS_ONLN:
|
---|
314 | return long(runtime.NumCPU())
|
---|
315 | }
|
---|
316 |
|
---|
317 | panic(todo("", name))
|
---|
318 | }
|
---|
319 |
|
---|
320 | // int close(int fd);
|
---|
321 | func Xclose(t *TLS, fd int32) int32 {
|
---|
322 | if _, _, err := unix.Syscall(unix.SYS_CLOSE, uintptr(fd), 0, 0); err != 0 {
|
---|
323 | t.setErrno(err)
|
---|
324 | return -1
|
---|
325 | }
|
---|
326 |
|
---|
327 | // if dmesgs {
|
---|
328 | // dmesg("%v: %d: ok", origin(1), fd)
|
---|
329 | // }
|
---|
330 | return 0
|
---|
331 | }
|
---|
332 |
|
---|
333 | // char *getcwd(char *buf, size_t size);
|
---|
334 | func Xgetcwd(t *TLS, buf uintptr, size types.Size_t) uintptr {
|
---|
335 | if _, err := unix.Getcwd((*RawMem)(unsafe.Pointer(buf))[:size:size]); err != nil {
|
---|
336 | if dmesgs {
|
---|
337 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
338 | }
|
---|
339 | t.setErrno(err)
|
---|
340 | return 0
|
---|
341 | }
|
---|
342 |
|
---|
343 | if dmesgs {
|
---|
344 | dmesg("%v: ok", origin(1))
|
---|
345 | }
|
---|
346 | return buf
|
---|
347 | }
|
---|
348 |
|
---|
349 | // int fstat(int fd, struct stat *statbuf);
|
---|
350 | func Xfstat(t *TLS, fd int32, statbuf uintptr) int32 {
|
---|
351 | return Xfstat64(t, fd, statbuf)
|
---|
352 | }
|
---|
353 |
|
---|
354 | // int ftruncate(int fd, off_t length);
|
---|
355 | func Xftruncate(t *TLS, fd int32, length types.Off_t) int32 {
|
---|
356 | if err := unix.Ftruncate(int(fd), int64(length)); err != nil {
|
---|
357 | if dmesgs {
|
---|
358 | dmesg("%v: fd %d: %v FAIL", origin(1), fd, err)
|
---|
359 | }
|
---|
360 | t.setErrno(err)
|
---|
361 | return -1
|
---|
362 | }
|
---|
363 |
|
---|
364 | if dmesgs {
|
---|
365 | dmesg("%v: %d %#x: ok", origin(1), fd, length)
|
---|
366 | }
|
---|
367 | return 0
|
---|
368 | }
|
---|
369 |
|
---|
370 | // int fcntl(int fd, int cmd, ... /* arg */ );
|
---|
371 | func Xfcntl(t *TLS, fd, cmd int32, args uintptr) int32 {
|
---|
372 | return Xfcntl64(t, fd, cmd, args)
|
---|
373 | }
|
---|
374 |
|
---|
375 | // ssize_t read(int fd, void *buf, size_t count);
|
---|
376 | func Xread(t *TLS, fd int32, buf uintptr, count types.Size_t) types.Ssize_t {
|
---|
377 | n, _, err := unix.Syscall(unix.SYS_READ, uintptr(fd), buf, uintptr(count))
|
---|
378 | if err != 0 {
|
---|
379 | t.setErrno(err)
|
---|
380 | return -1
|
---|
381 | }
|
---|
382 |
|
---|
383 | // if dmesgs {
|
---|
384 | // // dmesg("%v: %d %#x: %#x\n%s", origin(1), fd, count, n, hex.Dump(GoBytes(buf, int(n))))
|
---|
385 | // dmesg("%v: %d %#x: %#x", origin(1), fd, count, n)
|
---|
386 | // }
|
---|
387 | return types.Ssize_t(n)
|
---|
388 | }
|
---|
389 |
|
---|
390 | // ssize_t write(int fd, const void *buf, size_t count);
|
---|
391 | func Xwrite(t *TLS, fd int32, buf uintptr, count types.Size_t) types.Ssize_t {
|
---|
392 | const retry = 5
|
---|
393 | var err syscall.Errno
|
---|
394 | for i := 0; i < retry; i++ {
|
---|
395 | var n uintptr
|
---|
396 | switch n, _, err = unix.Syscall(unix.SYS_WRITE, uintptr(fd), buf, uintptr(count)); err {
|
---|
397 | case 0:
|
---|
398 | // if dmesgs {
|
---|
399 | // // dmesg("%v: %d %#x: %#x\n%s", origin(1), fd, count, n, hex.Dump(GoBytes(buf, int(n))))
|
---|
400 | // dmesg("%v: %d %#x: %#x", origin(1), fd, count, n)
|
---|
401 | // }
|
---|
402 | return types.Ssize_t(n)
|
---|
403 | case errno.EAGAIN:
|
---|
404 | // nop
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | // if dmesgs {
|
---|
409 | // dmesg("%v: fd %v, count %#x: %v", origin(1), fd, count, err)
|
---|
410 | // }
|
---|
411 | t.setErrno(err)
|
---|
412 | return -1
|
---|
413 | }
|
---|
414 |
|
---|
415 | // int fchmod(int fd, mode_t mode);
|
---|
416 | func Xfchmod(t *TLS, fd int32, mode types.Mode_t) int32 {
|
---|
417 | if _, _, err := unix.Syscall(unix.SYS_FCHMOD, uintptr(fd), uintptr(mode), 0); err != 0 {
|
---|
418 | t.setErrno(err)
|
---|
419 | return -1
|
---|
420 | }
|
---|
421 |
|
---|
422 | // if dmesgs {
|
---|
423 | // dmesg("%v: %d %#o: ok", origin(1), fd, mode)
|
---|
424 | // }
|
---|
425 | return 0
|
---|
426 | }
|
---|
427 |
|
---|
428 | // int fchown(int fd, uid_t owner, gid_t group);
|
---|
429 | func Xfchown(t *TLS, fd int32, owner types.Uid_t, group types.Gid_t) int32 {
|
---|
430 | if _, _, err := unix.Syscall(unix.SYS_FCHOWN, uintptr(fd), uintptr(owner), uintptr(group)); err != 0 {
|
---|
431 | t.setErrno(err)
|
---|
432 | return -1
|
---|
433 | }
|
---|
434 |
|
---|
435 | return 0
|
---|
436 | }
|
---|
437 |
|
---|
438 | // uid_t geteuid(void);
|
---|
439 | func Xgeteuid(t *TLS) types.Uid_t {
|
---|
440 | n, _, _ := unix.Syscall(unix.SYS_GETEUID, 0, 0, 0)
|
---|
441 | return types.Uid_t(n)
|
---|
442 | }
|
---|
443 |
|
---|
444 | // int munmap(void *addr, size_t length);
|
---|
445 | func Xmunmap(t *TLS, addr uintptr, length types.Size_t) int32 {
|
---|
446 | if _, _, err := unix.Syscall(unix.SYS_MUNMAP, addr, uintptr(length), 0); err != 0 {
|
---|
447 | t.setErrno(err)
|
---|
448 | return -1
|
---|
449 | }
|
---|
450 |
|
---|
451 | return 0
|
---|
452 | }
|
---|
453 |
|
---|
454 | // int gettimeofday(struct timeval *tv, struct timezone *tz);
|
---|
455 | func Xgettimeofday(t *TLS, tv, tz uintptr) int32 {
|
---|
456 | if tz != 0 {
|
---|
457 | panic(todo(""))
|
---|
458 | }
|
---|
459 |
|
---|
460 | var tvs unix.Timeval
|
---|
461 | err := unix.Gettimeofday(&tvs)
|
---|
462 | if err != nil {
|
---|
463 | t.setErrno(err)
|
---|
464 | return -1
|
---|
465 | }
|
---|
466 |
|
---|
467 | *(*unix.Timeval)(unsafe.Pointer(tv)) = tvs
|
---|
468 | return 0
|
---|
469 | }
|
---|
470 |
|
---|
471 | // int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);
|
---|
472 | func Xgetsockopt(t *TLS, sockfd, level, optname int32, optval, optlen uintptr) int32 {
|
---|
473 | if _, _, err := unix.Syscall6(unix.SYS_GETSOCKOPT, uintptr(sockfd), uintptr(level), uintptr(optname), optval, optlen, 0); err != 0 {
|
---|
474 | t.setErrno(err)
|
---|
475 | return -1
|
---|
476 | }
|
---|
477 |
|
---|
478 | return 0
|
---|
479 | }
|
---|
480 |
|
---|
481 | // int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
|
---|
482 | func Xsetsockopt(t *TLS, sockfd, level, optname int32, optval uintptr, optlen socket.Socklen_t) int32 {
|
---|
483 | if _, _, err := unix.Syscall6(unix.SYS_SETSOCKOPT, uintptr(sockfd), uintptr(level), uintptr(optname), optval, uintptr(optlen), 0); err != 0 {
|
---|
484 | t.setErrno(err)
|
---|
485 | return -1
|
---|
486 | }
|
---|
487 |
|
---|
488 | return 0
|
---|
489 | }
|
---|
490 |
|
---|
491 | // int ioctl(int fd, unsigned long request, ...);
|
---|
492 | func Xioctl(t *TLS, fd int32, request ulong, va uintptr) int32 {
|
---|
493 | var argp uintptr
|
---|
494 | if va != 0 {
|
---|
495 | argp = VaUintptr(&va)
|
---|
496 | }
|
---|
497 | n, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(request), argp)
|
---|
498 | if err != 0 {
|
---|
499 | t.setErrno(err)
|
---|
500 | return -1
|
---|
501 | }
|
---|
502 |
|
---|
503 | return int32(n)
|
---|
504 | }
|
---|
505 |
|
---|
506 | // int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
|
---|
507 | func Xgetsockname(t *TLS, sockfd int32, addr, addrlen uintptr) int32 {
|
---|
508 | if _, _, err := unix.Syscall(unix.SYS_GETSOCKNAME, uintptr(sockfd), addr, addrlen); err != 0 {
|
---|
509 | // if dmesgs {
|
---|
510 | // dmesg("%v: fd %v: %v", origin(1), sockfd, err)
|
---|
511 | // }
|
---|
512 | t.setErrno(err)
|
---|
513 | return -1
|
---|
514 | }
|
---|
515 |
|
---|
516 | return 0
|
---|
517 | }
|
---|
518 |
|
---|
519 | // int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
---|
520 | func Xselect(t *TLS, nfds int32, readfds, writefds, exceptfds, timeout uintptr) int32 {
|
---|
521 | n, err := unix.Select(
|
---|
522 | int(nfds),
|
---|
523 | (*unix.FdSet)(unsafe.Pointer(readfds)),
|
---|
524 | (*unix.FdSet)(unsafe.Pointer(writefds)),
|
---|
525 | (*unix.FdSet)(unsafe.Pointer(exceptfds)),
|
---|
526 | (*unix.Timeval)(unsafe.Pointer(timeout)),
|
---|
527 | )
|
---|
528 | if err != nil {
|
---|
529 | t.setErrno(err)
|
---|
530 | return -1
|
---|
531 | }
|
---|
532 |
|
---|
533 | return int32(n)
|
---|
534 | }
|
---|
535 |
|
---|
536 | // int mkfifo(const char *pathname, mode_t mode);
|
---|
537 | func Xmkfifo(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
|
---|
538 | panic(todo(""))
|
---|
539 | }
|
---|
540 |
|
---|
541 | // mode_t umask(mode_t mask);
|
---|
542 | func Xumask(t *TLS, mask types.Mode_t) types.Mode_t {
|
---|
543 | n, _, _ := unix.Syscall(unix.SYS_UMASK, uintptr(mask), 0, 0)
|
---|
544 | return types.Mode_t(n)
|
---|
545 | }
|
---|
546 |
|
---|
547 | // int execvp(const char *file, char *const argv[]);
|
---|
548 | func Xexecvp(t *TLS, file, argv uintptr) int32 {
|
---|
549 | if _, _, err := unix.Syscall(unix.SYS_EXECVE, file, argv, Environ()); err != 0 {
|
---|
550 | t.setErrno(err)
|
---|
551 | return -1
|
---|
552 | }
|
---|
553 |
|
---|
554 | return 0
|
---|
555 | }
|
---|
556 |
|
---|
557 | // pid_t waitpid(pid_t pid, int *wstatus, int options);
|
---|
558 | func Xwaitpid(t *TLS, pid types.Pid_t, wstatus uintptr, optname int32) types.Pid_t {
|
---|
559 | n, _, err := unix.Syscall6(unix.SYS_WAIT4, uintptr(pid), wstatus, uintptr(optname), 0, 0, 0)
|
---|
560 | if err != 0 {
|
---|
561 | t.setErrno(err)
|
---|
562 | return -1
|
---|
563 | }
|
---|
564 |
|
---|
565 | return types.Pid_t(n)
|
---|
566 | }
|
---|
567 |
|
---|
568 | // int uname(struct utsname *buf);
|
---|
569 | func Xuname(t *TLS, buf uintptr) int32 {
|
---|
570 | if err := unix.Uname((*unix.Utsname)(unsafe.Pointer(buf))); err != nil {
|
---|
571 | if dmesgs {
|
---|
572 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
573 | }
|
---|
574 | t.setErrno(err)
|
---|
575 | return -1
|
---|
576 | }
|
---|
577 |
|
---|
578 | if dmesgs {
|
---|
579 | dmesg("%v: ok", origin(1))
|
---|
580 | }
|
---|
581 | return 0
|
---|
582 | }
|
---|
583 |
|
---|
584 | // ssize_t recv(int sockfd, void *buf, size_t len, int flags);
|
---|
585 | func Xrecv(t *TLS, sockfd int32, buf uintptr, len types.Size_t, flags int32) types.Ssize_t {
|
---|
586 | n, _, err := unix.Syscall6(unix.SYS_RECVFROM, uintptr(sockfd), buf, uintptr(len), uintptr(flags), 0, 0)
|
---|
587 | if err != 0 {
|
---|
588 | t.setErrno(err)
|
---|
589 | return -1
|
---|
590 | }
|
---|
591 |
|
---|
592 | return types.Ssize_t(n)
|
---|
593 | }
|
---|
594 |
|
---|
595 | // ssize_t send(int sockfd, const void *buf, size_t len, int flags);
|
---|
596 | func Xsend(t *TLS, sockfd int32, buf uintptr, len types.Size_t, flags int32) types.Ssize_t {
|
---|
597 | n, _, err := unix.Syscall6(unix.SYS_SENDTO, uintptr(sockfd), buf, uintptr(len), uintptr(flags), 0, 0)
|
---|
598 | if err != 0 {
|
---|
599 | t.setErrno(err)
|
---|
600 | return -1
|
---|
601 | }
|
---|
602 |
|
---|
603 | return types.Ssize_t(n)
|
---|
604 | }
|
---|
605 |
|
---|
606 | // int shutdown(int sockfd, int how);
|
---|
607 | func Xshutdown(t *TLS, sockfd, how int32) int32 {
|
---|
608 | if _, _, err := unix.Syscall(unix.SYS_SHUTDOWN, uintptr(sockfd), uintptr(how), 0); err != 0 {
|
---|
609 | t.setErrno(err)
|
---|
610 | return -1
|
---|
611 | }
|
---|
612 |
|
---|
613 | return 0
|
---|
614 | }
|
---|
615 |
|
---|
616 | // int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
|
---|
617 | func Xgetpeername(t *TLS, sockfd int32, addr uintptr, addrlen uintptr) int32 {
|
---|
618 | if _, _, err := unix.Syscall(unix.SYS_GETPEERNAME, uintptr(sockfd), addr, uintptr(addrlen)); err != 0 {
|
---|
619 | t.setErrno(err)
|
---|
620 | return -1
|
---|
621 | }
|
---|
622 |
|
---|
623 | return 0
|
---|
624 | }
|
---|
625 |
|
---|
626 | // int socket(int domain, int type, int protocol);
|
---|
627 | func Xsocket(t *TLS, domain, type1, protocol int32) int32 {
|
---|
628 | n, _, err := unix.Syscall(unix.SYS_SOCKET, uintptr(domain), uintptr(type1), uintptr(protocol))
|
---|
629 | if err != 0 {
|
---|
630 | t.setErrno(err)
|
---|
631 | return -1
|
---|
632 | }
|
---|
633 |
|
---|
634 | return int32(n)
|
---|
635 | }
|
---|
636 |
|
---|
637 | // int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
|
---|
638 | func Xbind(t *TLS, sockfd int32, addr uintptr, addrlen uint32) int32 {
|
---|
639 | n, _, err := unix.Syscall(unix.SYS_BIND, uintptr(sockfd), addr, uintptr(addrlen))
|
---|
640 | if err != 0 {
|
---|
641 | t.setErrno(err)
|
---|
642 | return -1
|
---|
643 | }
|
---|
644 |
|
---|
645 | return int32(n)
|
---|
646 | }
|
---|
647 |
|
---|
648 | // int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
|
---|
649 | func Xconnect(t *TLS, sockfd int32, addr uintptr, addrlen uint32) int32 {
|
---|
650 | if _, _, err := unix.Syscall(unix.SYS_CONNECT, uintptr(sockfd), addr, uintptr(addrlen)); err != 0 {
|
---|
651 | t.setErrno(err)
|
---|
652 | return -1
|
---|
653 | }
|
---|
654 |
|
---|
655 | return 0
|
---|
656 | }
|
---|
657 |
|
---|
658 | // int listen(int sockfd, int backlog);
|
---|
659 | func Xlisten(t *TLS, sockfd, backlog int32) int32 {
|
---|
660 | if _, _, err := unix.Syscall(unix.SYS_LISTEN, uintptr(sockfd), uintptr(backlog), 0); err != 0 {
|
---|
661 | t.setErrno(err)
|
---|
662 | return -1
|
---|
663 | }
|
---|
664 |
|
---|
665 | return 0
|
---|
666 | }
|
---|
667 |
|
---|
668 | // int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
|
---|
669 | func Xaccept(t *TLS, sockfd int32, addr uintptr, addrlen uintptr) int32 {
|
---|
670 | panic(todo(""))
|
---|
671 | // n, _, err := unix.Syscall6(unix.SYS_ACCEPT4, uintptr(sockfd), addr, uintptr(addrlen), 0, 0, 0)
|
---|
672 | // if err != 0 {
|
---|
673 | // t.setErrno(err)
|
---|
674 | // return -1
|
---|
675 | // }
|
---|
676 |
|
---|
677 | // return int32(n)
|
---|
678 | }
|
---|
679 |
|
---|
680 | // int getrlimit(int resource, struct rlimit *rlim);
|
---|
681 | func Xgetrlimit(t *TLS, resource int32, rlim uintptr) int32 {
|
---|
682 | return Xgetrlimit64(t, resource, rlim)
|
---|
683 | }
|
---|
684 |
|
---|
685 | // int setrlimit(int resource, const struct rlimit *rlim);
|
---|
686 | func Xsetrlimit(t *TLS, resource int32, rlim uintptr) int32 {
|
---|
687 | return Xsetrlimit64(t, resource, rlim)
|
---|
688 | }
|
---|
689 |
|
---|
690 | // int setrlimit(int resource, const struct rlimit *rlim);
|
---|
691 | func Xsetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
|
---|
692 | if _, _, err := unix.Syscall(unix.SYS_SETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
|
---|
693 | t.setErrno(err)
|
---|
694 | return -1
|
---|
695 | }
|
---|
696 |
|
---|
697 | return 0
|
---|
698 | }
|
---|
699 |
|
---|
700 | // uid_t getuid(void);
|
---|
701 | func Xgetuid(t *TLS) types.Uid_t {
|
---|
702 | return types.Uid_t(os.Getuid())
|
---|
703 | }
|
---|
704 |
|
---|
705 | // pid_t getpid(void);
|
---|
706 | func Xgetpid(t *TLS) int32 {
|
---|
707 | return int32(os.Getpid())
|
---|
708 | }
|
---|
709 |
|
---|
710 | // int system(const char *command);
|
---|
711 | func Xsystem(t *TLS, command uintptr) int32 {
|
---|
712 | s := GoString(command)
|
---|
713 | if command == 0 {
|
---|
714 | panic(todo(""))
|
---|
715 | }
|
---|
716 |
|
---|
717 | cmd := exec.Command("sh", "-c", s)
|
---|
718 | cmd.Stdout = os.Stdout
|
---|
719 | cmd.Stderr = os.Stderr
|
---|
720 | err := cmd.Run()
|
---|
721 | if err != nil {
|
---|
722 | ps := err.(*exec.ExitError)
|
---|
723 | return int32(ps.ExitCode())
|
---|
724 | }
|
---|
725 |
|
---|
726 | return 0
|
---|
727 | }
|
---|
728 |
|
---|
729 | // int setvbuf(FILE *stream, char *buf, int mode, size_t size);
|
---|
730 | func Xsetvbuf(t *TLS, stream, buf uintptr, mode int32, size types.Size_t) int32 {
|
---|
731 | return 0 //TODO
|
---|
732 | }
|
---|
733 |
|
---|
734 | // int raise(int sig);
|
---|
735 | func Xraise(t *TLS, sig int32) int32 {
|
---|
736 | panic(todo(""))
|
---|
737 | }
|
---|
738 |
|
---|
739 | // int backtrace(void **buffer, int size);
|
---|
740 | func Xbacktrace(t *TLS, buf uintptr, size int32) int32 {
|
---|
741 | panic(todo(""))
|
---|
742 | }
|
---|
743 |
|
---|
744 | // void backtrace_symbols_fd(void *const *buffer, int size, int fd);
|
---|
745 | func Xbacktrace_symbols_fd(t *TLS, buffer uintptr, size, fd int32) {
|
---|
746 | panic(todo(""))
|
---|
747 | }
|
---|
748 |
|
---|
749 | // int fileno(FILE *stream);
|
---|
750 | func Xfileno(t *TLS, stream uintptr) int32 {
|
---|
751 | panic(todo(""))
|
---|
752 | }
|
---|
753 |
|
---|
754 | func newCFtsent(t *TLS, info int, path string, stat *unix.Stat_t, err syscall.Errno) uintptr {
|
---|
755 | p := Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(fts.FTSENT{})))
|
---|
756 | if p == 0 {
|
---|
757 | panic("OOM")
|
---|
758 | }
|
---|
759 |
|
---|
760 | *(*fts.FTSENT)(unsafe.Pointer(p)) = *newFtsent(t, info, path, stat, err)
|
---|
761 | return p
|
---|
762 | }
|
---|
763 |
|
---|
764 | func ftsentClose(t *TLS, p uintptr) {
|
---|
765 | Xfree(t, (*fts.FTSENT)(unsafe.Pointer(p)).Ffts_path)
|
---|
766 | Xfree(t, (*fts.FTSENT)(unsafe.Pointer(p)).Ffts_statp)
|
---|
767 | }
|
---|
768 |
|
---|
769 | type ftstream struct {
|
---|
770 | s []uintptr
|
---|
771 | x int
|
---|
772 | }
|
---|
773 |
|
---|
774 | func (f *ftstream) close(t *TLS) {
|
---|
775 | for _, p := range f.s {
|
---|
776 | ftsentClose(t, p)
|
---|
777 | Xfree(t, p)
|
---|
778 | }
|
---|
779 | *f = ftstream{}
|
---|
780 | }
|
---|
781 |
|
---|
782 | // FTS *fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **));
|
---|
783 | func Xfts_open(t *TLS, path_argv uintptr, options int32, compar uintptr) uintptr {
|
---|
784 | return Xfts64_open(t, path_argv, options, compar)
|
---|
785 | }
|
---|
786 |
|
---|
787 | // FTS *fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **));
|
---|
788 | func Xfts64_open(t *TLS, path_argv uintptr, options int32, compar uintptr) uintptr {
|
---|
789 | f := &ftstream{}
|
---|
790 |
|
---|
791 | var walk func(string)
|
---|
792 | walk = func(path string) {
|
---|
793 | var fi os.FileInfo
|
---|
794 | var err error
|
---|
795 | switch {
|
---|
796 | case options&fts.FTS_LOGICAL != 0:
|
---|
797 | fi, err = os.Stat(path)
|
---|
798 | case options&fts.FTS_PHYSICAL != 0:
|
---|
799 | fi, err = os.Lstat(path)
|
---|
800 | default:
|
---|
801 | panic(todo(""))
|
---|
802 | }
|
---|
803 |
|
---|
804 | if err != nil {
|
---|
805 | return
|
---|
806 | }
|
---|
807 |
|
---|
808 | var statp *unix.Stat_t
|
---|
809 | if options&fts.FTS_NOSTAT == 0 {
|
---|
810 | var stat unix.Stat_t
|
---|
811 | switch {
|
---|
812 | case options&fts.FTS_LOGICAL != 0:
|
---|
813 | if err := unix.Stat(path, &stat); err != nil {
|
---|
814 | panic(todo(""))
|
---|
815 | }
|
---|
816 | case options&fts.FTS_PHYSICAL != 0:
|
---|
817 | if err := unix.Lstat(path, &stat); err != nil {
|
---|
818 | panic(todo(""))
|
---|
819 | }
|
---|
820 | default:
|
---|
821 | panic(todo(""))
|
---|
822 | }
|
---|
823 |
|
---|
824 | statp = &stat
|
---|
825 | }
|
---|
826 |
|
---|
827 | out:
|
---|
828 | switch {
|
---|
829 | case fi.IsDir():
|
---|
830 | f.s = append(f.s, newCFtsent(t, fts.FTS_D, path, statp, 0))
|
---|
831 | g, err := os.Open(path)
|
---|
832 | switch x := err.(type) {
|
---|
833 | case nil:
|
---|
834 | // ok
|
---|
835 | case *os.PathError:
|
---|
836 | f.s = append(f.s, newCFtsent(t, fts.FTS_DNR, path, statp, errno.EACCES))
|
---|
837 | break out
|
---|
838 | default:
|
---|
839 | panic(todo("%q: %v %T", path, x, x))
|
---|
840 | }
|
---|
841 |
|
---|
842 | names, err := g.Readdirnames(-1)
|
---|
843 | g.Close()
|
---|
844 | if err != nil {
|
---|
845 | panic(todo(""))
|
---|
846 | }
|
---|
847 |
|
---|
848 | for _, name := range names {
|
---|
849 | walk(path + "/" + name)
|
---|
850 | if f == nil {
|
---|
851 | break out
|
---|
852 | }
|
---|
853 | }
|
---|
854 |
|
---|
855 | f.s = append(f.s, newCFtsent(t, fts.FTS_DP, path, statp, 0))
|
---|
856 | default:
|
---|
857 | info := fts.FTS_F
|
---|
858 | if fi.Mode()&os.ModeSymlink != 0 {
|
---|
859 | info = fts.FTS_SL
|
---|
860 | }
|
---|
861 | switch {
|
---|
862 | case statp != nil:
|
---|
863 | f.s = append(f.s, newCFtsent(t, info, path, statp, 0))
|
---|
864 | case options&fts.FTS_NOSTAT != 0:
|
---|
865 | f.s = append(f.s, newCFtsent(t, fts.FTS_NSOK, path, nil, 0))
|
---|
866 | default:
|
---|
867 | panic(todo(""))
|
---|
868 | }
|
---|
869 | }
|
---|
870 | }
|
---|
871 |
|
---|
872 | for {
|
---|
873 | p := *(*uintptr)(unsafe.Pointer(path_argv))
|
---|
874 | if p == 0 {
|
---|
875 | if f == nil {
|
---|
876 | return 0
|
---|
877 | }
|
---|
878 |
|
---|
879 | if compar != 0 {
|
---|
880 | panic(todo(""))
|
---|
881 | }
|
---|
882 |
|
---|
883 | return addObject(f)
|
---|
884 | }
|
---|
885 |
|
---|
886 | walk(GoString(p))
|
---|
887 | path_argv += unsafe.Sizeof(uintptr(0))
|
---|
888 | }
|
---|
889 | }
|
---|
890 |
|
---|
891 | // FTSENT *fts_read(FTS *ftsp);
|
---|
892 | func Xfts_read(t *TLS, ftsp uintptr) uintptr {
|
---|
893 | return Xfts64_read(t, ftsp)
|
---|
894 | }
|
---|
895 |
|
---|
896 | // FTSENT *fts_read(FTS *ftsp);
|
---|
897 | func Xfts64_read(t *TLS, ftsp uintptr) uintptr {
|
---|
898 | f := getObject(ftsp).(*ftstream)
|
---|
899 | if f.x == len(f.s) {
|
---|
900 | t.setErrno(0)
|
---|
901 | return 0
|
---|
902 | }
|
---|
903 |
|
---|
904 | r := f.s[f.x]
|
---|
905 | if e := (*fts.FTSENT)(unsafe.Pointer(r)).Ffts_errno; e != 0 {
|
---|
906 | t.setErrno(e)
|
---|
907 | }
|
---|
908 | f.x++
|
---|
909 | return r
|
---|
910 | }
|
---|
911 |
|
---|
912 | // int fts_close(FTS *ftsp);
|
---|
913 | func Xfts_close(t *TLS, ftsp uintptr) int32 {
|
---|
914 | return Xfts64_close(t, ftsp)
|
---|
915 | }
|
---|
916 |
|
---|
917 | // int fts_close(FTS *ftsp);
|
---|
918 | func Xfts64_close(t *TLS, ftsp uintptr) int32 {
|
---|
919 | getObject(ftsp).(*ftstream).close(t)
|
---|
920 | removeObject(ftsp)
|
---|
921 | return 0
|
---|
922 | }
|
---|
923 |
|
---|
924 | // void tzset (void);
|
---|
925 | func Xtzset(t *TLS) {
|
---|
926 | //TODO
|
---|
927 | }
|
---|
928 |
|
---|
929 | var strerrorBuf [100]byte
|
---|
930 |
|
---|
931 | // char *strerror(int errnum);
|
---|
932 | func Xstrerror(t *TLS, errnum int32) uintptr {
|
---|
933 | if dmesgs {
|
---|
934 | dmesg("%v: %v\n%s", origin(1), errnum, debug.Stack())
|
---|
935 | }
|
---|
936 | copy(strerrorBuf[:], fmt.Sprintf("strerror(%d)\x00", errnum))
|
---|
937 | return uintptr(unsafe.Pointer(&strerrorBuf[0]))
|
---|
938 | }
|
---|
939 |
|
---|
940 | // void *dlopen(const char *filename, int flags);
|
---|
941 | func Xdlopen(t *TLS, filename uintptr, flags int32) uintptr {
|
---|
942 | panic(todo(""))
|
---|
943 | }
|
---|
944 |
|
---|
945 | // char *dlerror(void);
|
---|
946 | func Xdlerror(t *TLS) uintptr {
|
---|
947 | panic(todo(""))
|
---|
948 | }
|
---|
949 |
|
---|
950 | // int dlclose(void *handle);
|
---|
951 | func Xdlclose(t *TLS, handle uintptr) int32 {
|
---|
952 | panic(todo(""))
|
---|
953 | }
|
---|
954 |
|
---|
955 | // void *dlsym(void *handle, const char *symbol);
|
---|
956 | func Xdlsym(t *TLS, handle, symbol uintptr) uintptr {
|
---|
957 | panic(todo(""))
|
---|
958 | }
|
---|
959 |
|
---|
960 | // void perror(const char *s);
|
---|
961 | func Xperror(t *TLS, s uintptr) {
|
---|
962 | panic(todo(""))
|
---|
963 | }
|
---|
964 |
|
---|
965 | // int pclose(FILE *stream);
|
---|
966 | func Xpclose(t *TLS, stream uintptr) int32 {
|
---|
967 | panic(todo(""))
|
---|
968 | }
|
---|
969 |
|
---|
970 | var gai_strerrorBuf [100]byte
|
---|
971 |
|
---|
972 | // const char *gai_strerror(int errcode);
|
---|
973 | func Xgai_strerror(t *TLS, errcode int32) uintptr {
|
---|
974 | copy(gai_strerrorBuf[:], fmt.Sprintf("gai error %d\x00", errcode))
|
---|
975 | return uintptr(unsafe.Pointer(&gai_strerrorBuf))
|
---|
976 | }
|
---|
977 |
|
---|
978 | // int tcgetattr(int fd, struct termios *termios_p);
|
---|
979 | func Xtcgetattr(t *TLS, fd int32, termios_p uintptr) int32 {
|
---|
980 | panic(todo(""))
|
---|
981 | }
|
---|
982 |
|
---|
983 | // int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
|
---|
984 | func Xtcsetattr(t *TLS, fd, optional_actions int32, termios_p uintptr) int32 {
|
---|
985 | panic(todo(""))
|
---|
986 | }
|
---|
987 |
|
---|
988 | // speed_t cfgetospeed(const struct termios *termios_p);
|
---|
989 | func Xcfgetospeed(t *TLS, termios_p uintptr) termios.Speed_t {
|
---|
990 | panic(todo(""))
|
---|
991 | }
|
---|
992 |
|
---|
993 | // int cfsetospeed(struct termios *termios_p, speed_t speed);
|
---|
994 | func Xcfsetospeed(t *TLS, termios_p uintptr, speed uint32) int32 {
|
---|
995 | panic(todo(""))
|
---|
996 | }
|
---|
997 |
|
---|
998 | // int cfsetispeed(struct termios *termios_p, speed_t speed);
|
---|
999 | func Xcfsetispeed(t *TLS, termios_p uintptr, speed uint32) int32 {
|
---|
1000 | panic(todo(""))
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | // pid_t fork(void);
|
---|
1004 | func Xfork(t *TLS) int32 {
|
---|
1005 | t.setErrno(errno.ENOSYS)
|
---|
1006 | return -1
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | var emptyStr = [1]byte{}
|
---|
1010 |
|
---|
1011 | // char *setlocale(int category, const char *locale);
|
---|
1012 | func Xsetlocale(t *TLS, category int32, locale uintptr) uintptr {
|
---|
1013 | return uintptr(unsafe.Pointer(&emptyStr)) //TODO
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | // char *nl_langinfo(nl_item item);
|
---|
1017 | func Xnl_langinfo(t *TLS, item langinfo.Nl_item) uintptr {
|
---|
1018 | return uintptr(unsafe.Pointer(&emptyStr)) //TODO
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | // FILE *popen(const char *command, const char *type);
|
---|
1022 | func Xpopen(t *TLS, command, type1 uintptr) uintptr {
|
---|
1023 | panic(todo(""))
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | // char *realpath(const char *path, char *resolved_path);
|
---|
1027 | func Xrealpath(t *TLS, path, resolved_path uintptr) uintptr {
|
---|
1028 | s, err := filepath.EvalSymlinks(GoString(path))
|
---|
1029 | if err != nil {
|
---|
1030 | if os.IsNotExist(err) {
|
---|
1031 | // if dmesgs {
|
---|
1032 | // dmesg("%v: %q: %v", origin(1), GoString(path), err)
|
---|
1033 | // }
|
---|
1034 | t.setErrno(errno.ENOENT)
|
---|
1035 | return 0
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | panic(todo("", err))
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | if resolved_path == 0 {
|
---|
1042 | panic(todo(""))
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | if len(s) >= limits.PATH_MAX {
|
---|
1046 | s = s[:limits.PATH_MAX-1]
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | copy((*RawMem)(unsafe.Pointer(resolved_path))[:len(s):len(s)], s)
|
---|
1050 | (*RawMem)(unsafe.Pointer(resolved_path))[len(s)] = 0
|
---|
1051 | return resolved_path
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | // struct tm *gmtime_r(const time_t *timep, struct tm *result);
|
---|
1055 | func Xgmtime_r(t *TLS, timep, result uintptr) uintptr {
|
---|
1056 | panic(todo(""))
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | // char *inet_ntoa(struct in_addr in);
|
---|
1060 | func Xinet_ntoa(t *TLS, in1 in.In_addr) uintptr {
|
---|
1061 | panic(todo(""))
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | func X__ccgo_in6addr_anyp(t *TLS) uintptr {
|
---|
1065 | return uintptr(unsafe.Pointer(&in6_addr_any))
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | func Xabort(t *TLS) {
|
---|
1069 | panic(todo("")) //TODO
|
---|
1070 | // if dmesgs {
|
---|
1071 | // dmesg("%v:", origin(1))
|
---|
1072 | // }
|
---|
1073 | // p := Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(signal.Sigaction{})))
|
---|
1074 | // if p == 0 {
|
---|
1075 | // panic("OOM")
|
---|
1076 | // }
|
---|
1077 |
|
---|
1078 | // (*signal.Sigaction)(unsafe.Pointer(p)).F__sigaction_u.F__sa_handler = signal.SIG_DFL
|
---|
1079 | // Xsigaction(t, signal.SIGABRT, p, 0)
|
---|
1080 | // Xfree(t, p)
|
---|
1081 | // unix.Kill(unix.Getpid(), syscall.Signal(signal.SIGABRT))
|
---|
1082 | // panic(todo("unrechable"))
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | // int fflush(FILE *stream);
|
---|
1086 | func Xfflush(t *TLS, stream uintptr) int32 {
|
---|
1087 | return 0 //TODO
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | // size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
|
---|
1091 | func Xfread(t *TLS, ptr uintptr, size, nmemb types.Size_t, stream uintptr) types.Size_t {
|
---|
1092 | m, _, err := unix.Syscall(unix.SYS_READ, uintptr(file(stream).fd()), ptr, uintptr(size*nmemb))
|
---|
1093 | if err != 0 {
|
---|
1094 | file(stream).setErr()
|
---|
1095 | return 0
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | // if dmesgs {
|
---|
1099 | // // dmesg("%v: %d %#x x %#x: %#x\n%s", origin(1), file(stream).fd(), size, nmemb, types.Size_t(m)/size, hex.Dump(GoBytes(ptr, int(m))))
|
---|
1100 | // dmesg("%v: %d %#x x %#x: %#x", origin(1), file(stream).fd(), size, nmemb, types.Size_t(m)/size)
|
---|
1101 | // }
|
---|
1102 | return types.Size_t(m) / size
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | // size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
|
---|
1106 | func Xfwrite(t *TLS, ptr uintptr, size, nmemb types.Size_t, stream uintptr) types.Size_t {
|
---|
1107 | m, _, err := unix.Syscall(unix.SYS_WRITE, uintptr(file(stream).fd()), ptr, uintptr(size*nmemb))
|
---|
1108 | if err != 0 {
|
---|
1109 | file(stream).setErr()
|
---|
1110 | return 0
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | // if dmesgs {
|
---|
1114 | // // dmesg("%v: %d %#x x %#x: %#x\n%s", origin(1), file(stream).fd(), size, nmemb, types.Size_t(m)/size, hex.Dump(GoBytes(ptr, int(m))))
|
---|
1115 | // dmesg("%v: %d %#x x %#x: %#x", origin(1), file(stream).fd(), size, nmemb, types.Size_t(m)/size)
|
---|
1116 | // }
|
---|
1117 | return types.Size_t(m) / size
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | // int fclose(FILE *stream);
|
---|
1121 | func Xfclose(t *TLS, stream uintptr) int32 {
|
---|
1122 | return file(stream).close(t)
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | // int fputc(int c, FILE *stream);
|
---|
1126 | func Xfputc(t *TLS, c int32, stream uintptr) int32 {
|
---|
1127 | if _, err := fwrite(file(stream).fd(), []byte{byte(c)}); err != nil {
|
---|
1128 | return stdio.EOF
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | return int32(byte(c))
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | // int fseek(FILE *stream, long offset, int whence);
|
---|
1135 | func Xfseek(t *TLS, stream uintptr, offset long, whence int32) int32 {
|
---|
1136 | if n := Xlseek(t, int32(file(stream).fd()), types.Off_t(offset), whence); n < 0 {
|
---|
1137 | // if dmesgs {
|
---|
1138 | // dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), file(stream).fd(), offset, whenceStr(whence), n)
|
---|
1139 | // }
|
---|
1140 | file(stream).setErr()
|
---|
1141 | return -1
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | // if dmesgs {
|
---|
1145 | // dmesg("%v: fd %v, off %#x, whence %v: ok", origin(1), file(stream).fd(), offset, whenceStr(whence))
|
---|
1146 | // }
|
---|
1147 | return 0
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | // long ftell(FILE *stream);
|
---|
1151 | func Xftell(t *TLS, stream uintptr) long {
|
---|
1152 | n := Xlseek(t, file(stream).fd(), 0, stdio.SEEK_CUR)
|
---|
1153 | if n < 0 {
|
---|
1154 | file(stream).setErr()
|
---|
1155 | return -1
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | // if dmesgs {
|
---|
1159 | // dmesg("%v: fd %v, n %#x: ok %#x", origin(1), file(stream).fd(), n, long(n))
|
---|
1160 | // }
|
---|
1161 | return long(n)
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | // int ferror(FILE *stream);
|
---|
1165 | func Xferror(t *TLS, stream uintptr) int32 {
|
---|
1166 | return Bool32(file(stream).err())
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | // int ungetc(int c, FILE *stream);
|
---|
1170 | func Xungetc(t *TLS, c int32, stream uintptr) int32 {
|
---|
1171 | panic(todo(""))
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | // int fscanf(FILE *stream, const char *format, ...);
|
---|
1175 | func Xfscanf(t *TLS, stream, format, va uintptr) int32 {
|
---|
1176 | panic(todo(""))
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | // int fputs(const char *s, FILE *stream);
|
---|
1180 | func Xfputs(t *TLS, s, stream uintptr) int32 {
|
---|
1181 | if _, _, err := unix.Syscall(unix.SYS_WRITE, uintptr(file(stream).fd()), s, uintptr(Xstrlen(t, s))); err != 0 {
|
---|
1182 | return -1
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | return 0
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | var getservbynameStaticResult netdb.Servent
|
---|
1189 |
|
---|
1190 | // struct servent *getservbyname(const char *name, const char *proto);
|
---|
1191 | func Xgetservbyname(t *TLS, name, proto uintptr) uintptr {
|
---|
1192 | var protoent *gonetdb.Protoent
|
---|
1193 | if proto != 0 {
|
---|
1194 | protoent = gonetdb.GetProtoByName(GoString(proto))
|
---|
1195 | }
|
---|
1196 | servent := gonetdb.GetServByName(GoString(name), protoent)
|
---|
1197 | if servent == nil {
|
---|
1198 | // if dmesgs {
|
---|
1199 | // dmesg("%q %q: nil (protoent %+v)", GoString(name), GoString(proto), protoent)
|
---|
1200 | // }
|
---|
1201 | return 0
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | Xfree(t, (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_name)
|
---|
1205 | if v := (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_aliases; v != 0 {
|
---|
1206 | for {
|
---|
1207 | p := *(*uintptr)(unsafe.Pointer(v))
|
---|
1208 | if p == 0 {
|
---|
1209 | break
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | Xfree(t, p)
|
---|
1213 | v += unsafe.Sizeof(uintptr(0))
|
---|
1214 | }
|
---|
1215 | Xfree(t, v)
|
---|
1216 | }
|
---|
1217 | Xfree(t, (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_proto)
|
---|
1218 | cname, err := CString(servent.Name)
|
---|
1219 | if err != nil {
|
---|
1220 | getservbynameStaticResult = netdb.Servent{}
|
---|
1221 | return 0
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | var protoname uintptr
|
---|
1225 | if protoent != nil {
|
---|
1226 | if protoname, err = CString(protoent.Name); err != nil {
|
---|
1227 | Xfree(t, cname)
|
---|
1228 | getservbynameStaticResult = netdb.Servent{}
|
---|
1229 | return 0
|
---|
1230 | }
|
---|
1231 | }
|
---|
1232 | var a []uintptr
|
---|
1233 | for _, v := range servent.Aliases {
|
---|
1234 | cs, err := CString(v)
|
---|
1235 | if err != nil {
|
---|
1236 | for _, v := range a {
|
---|
1237 | Xfree(t, v)
|
---|
1238 | }
|
---|
1239 | return 0
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | a = append(a, cs)
|
---|
1243 | }
|
---|
1244 | v := Xcalloc(t, types.Size_t(len(a)+1), types.Size_t(unsafe.Sizeof(uintptr(0))))
|
---|
1245 | if v == 0 {
|
---|
1246 | Xfree(t, cname)
|
---|
1247 | Xfree(t, protoname)
|
---|
1248 | for _, v := range a {
|
---|
1249 | Xfree(t, v)
|
---|
1250 | }
|
---|
1251 | getservbynameStaticResult = netdb.Servent{}
|
---|
1252 | return 0
|
---|
1253 | }
|
---|
1254 | for _, p := range a {
|
---|
1255 | *(*uintptr)(unsafe.Pointer(v)) = p
|
---|
1256 | v += unsafe.Sizeof(uintptr(0))
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | getservbynameStaticResult = netdb.Servent{
|
---|
1260 | Fs_name: cname,
|
---|
1261 | Fs_aliases: v,
|
---|
1262 | Fs_port: int32(servent.Port),
|
---|
1263 | Fs_proto: protoname,
|
---|
1264 | }
|
---|
1265 | return uintptr(unsafe.Pointer(&getservbynameStaticResult))
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | func Xreaddir64(t *TLS, dir uintptr) uintptr {
|
---|
1269 | return Xreaddir(t, dir)
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | func __syscall(r, _ uintptr, errno syscall.Errno) long {
|
---|
1273 | if errno != 0 {
|
---|
1274 | return long(-errno)
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 | return long(r)
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | func X__syscall1(t *TLS, trap, p1 long) long {
|
---|
1281 | return __syscall(unix.Syscall(uintptr(trap), uintptr(p1), 0, 0))
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | func X__syscall3(t *TLS, trap, p1, p2, p3 long) long {
|
---|
1285 | return __syscall(unix.Syscall(uintptr(trap), uintptr(p1), uintptr(p2), uintptr(p3)))
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | func X__syscall4(t *TLS, trap, p1, p2, p3, p4 long) long {
|
---|
1289 | return __syscall(unix.Syscall6(uintptr(trap), uintptr(p1), uintptr(p2), uintptr(p3), uintptr(p4), 0, 0))
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | func fcntlCmdStr(cmd int32) string {
|
---|
1293 | switch cmd {
|
---|
1294 | case fcntl.F_GETOWN:
|
---|
1295 | return "F_GETOWN"
|
---|
1296 | case fcntl.F_SETLK:
|
---|
1297 | return "F_SETLK"
|
---|
1298 | case fcntl.F_GETLK:
|
---|
1299 | return "F_GETLK"
|
---|
1300 | case fcntl.F_SETFD:
|
---|
1301 | return "F_SETFD"
|
---|
1302 | case fcntl.F_GETFD:
|
---|
1303 | return "F_GETFD"
|
---|
1304 | default:
|
---|
1305 | return fmt.Sprintf("cmd(%d)", cmd)
|
---|
1306 | }
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | // int setenv(const char *name, const char *value, int overwrite);
|
---|
1310 | func Xsetenv(t *TLS, name, value uintptr, overwrite int32) int32 {
|
---|
1311 | panic(todo(""))
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | // int unsetenv(const char *name);
|
---|
1315 | func Xunsetenv(t *TLS, name uintptr) int32 {
|
---|
1316 | panic(todo(""))
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | // int pause(void);
|
---|
1320 | func Xpause(t *TLS) int32 {
|
---|
1321 | panic(todo(""))
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | // ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
|
---|
1325 | func Xwritev(t *TLS, fd int32, iov uintptr, iovcnt int32) types.Ssize_t {
|
---|
1326 | panic(todo(""))
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | // int __isoc99_sscanf(const char *str, const char *format, ...);
|
---|
1330 | func X__isoc99_sscanf(t *TLS, str, format, va uintptr) int32 {
|
---|
1331 | r := Xsscanf(t, str, format, va)
|
---|
1332 | // if dmesgs {
|
---|
1333 | // dmesg("%v: %q %q: %d", origin(1), GoString(str), GoString(format), r)
|
---|
1334 | // }
|
---|
1335 | return r
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | // void __assert(const char * func, const char * file, int line, const char *expr) __dead2;
|
---|
1339 | func X__assert(t *TLS, fn, file uintptr, line int32, expr uintptr) {
|
---|
1340 | X__assert_fail(t, expr, file, uint32(line), fn)
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 | func X__assert13(t *TLS, file uintptr, line int32, fn, msg uintptr) {
|
---|
1344 | X__assert_fail(t, msg, file, uint32(line), fn)
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | // include/stdio.h:456:int __swbuf(int, FILE *);
|
---|
1348 | func X__swbuf(t *TLS, n int32, file uintptr) int32 {
|
---|
1349 | return Xfputc(t, n, file) //TODO improve performance, use a real buffer.
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | // int rmdir(const char *pathname);
|
---|
1353 | func Xrmdir(t *TLS, pathname uintptr) int32 {
|
---|
1354 | if err := unix.Rmdir(GoString(pathname)); err != nil {
|
---|
1355 | if dmesgs {
|
---|
1356 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1357 | }
|
---|
1358 | t.setErrno(err)
|
---|
1359 | return -1
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | if dmesgs {
|
---|
1363 | dmesg("%v: ok", origin(1))
|
---|
1364 | }
|
---|
1365 | return 0
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | // struct dirent *readdir(DIR *dirp);
|
---|
1369 | func Xreaddir(t *TLS, dir uintptr) uintptr {
|
---|
1370 | if (*darwinDir)(unsafe.Pointer(dir)).eof {
|
---|
1371 | return 0
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | if (*darwinDir)(unsafe.Pointer(dir)).l == (*darwinDir)(unsafe.Pointer(dir)).h {
|
---|
1375 | n, err := unix.Getdirentries((*darwinDir)(unsafe.Pointer(dir)).fd, (*darwinDir)(unsafe.Pointer(dir)).buf[:], nil)
|
---|
1376 | // trc("must read: %v %v", n, err)
|
---|
1377 | if n == 0 {
|
---|
1378 | if err != nil && err != io.EOF {
|
---|
1379 | if dmesgs {
|
---|
1380 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1381 | }
|
---|
1382 | t.setErrno(err)
|
---|
1383 | }
|
---|
1384 | (*darwinDir)(unsafe.Pointer(dir)).eof = true
|
---|
1385 | return 0
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | (*darwinDir)(unsafe.Pointer(dir)).l = 0
|
---|
1389 | (*darwinDir)(unsafe.Pointer(dir)).h = n
|
---|
1390 | // trc("new l %v, h %v", (*darwinDir)(unsafe.Pointer(dir)).l, (*darwinDir)(unsafe.Pointer(dir)).h)
|
---|
1391 | }
|
---|
1392 | de := dir + unsafe.Offsetof(darwinDir{}.buf) + uintptr((*darwinDir)(unsafe.Pointer(dir)).l)
|
---|
1393 | (*darwinDir)(unsafe.Pointer(dir)).l += int((*unix.Dirent)(unsafe.Pointer(de)).Reclen)
|
---|
1394 | return de
|
---|
1395 | }
|
---|
1396 |
|
---|
1397 | type darwinDir struct {
|
---|
1398 | buf [4096]byte
|
---|
1399 | fd int
|
---|
1400 | h int
|
---|
1401 | l int
|
---|
1402 |
|
---|
1403 | eof bool
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 | // int sscanf(const char *str, const char *format, ...);
|
---|
1407 | func Xsscanf(t *TLS, str, format, va uintptr) int32 {
|
---|
1408 | r := scanf(strings.NewReader(GoString(str)), format, va)
|
---|
1409 | // if dmesgs {
|
---|
1410 | // dmesg("%v: %q %q: %d", origin(1), GoString(str), GoString(format), r)
|
---|
1411 | // }
|
---|
1412 | return r
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | // int * __error(void);
|
---|
1416 | func X__error(t *TLS) uintptr {
|
---|
1417 | return t.errnop
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 | func Xclosedir(t *TLS, dir uintptr) int32 {
|
---|
1421 | r := Xclose(t, int32((*darwinDir)(unsafe.Pointer(dir)).fd))
|
---|
1422 | Xfree(t, dir)
|
---|
1423 | return r
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | // int __xuname(int namesize, void *namebuf)
|
---|
1427 | func X__xuname(t *TLS, namesize int32, namebuf uintptr) int32 {
|
---|
1428 | return Xuname(t, namebuf)
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | // int chflags(const char *path, u_int flags);
|
---|
1432 | func Xchflags(t *TLS, path uintptr, flags uint32) int32 {
|
---|
1433 | if err := unix.Chflags(GoString(path), int(flags)); err != nil {
|
---|
1434 | if dmesgs {
|
---|
1435 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1436 | }
|
---|
1437 | t.setErrno(err)
|
---|
1438 | return -1
|
---|
1439 | }
|
---|
1440 |
|
---|
1441 | if dmesgs {
|
---|
1442 | dmesg("%v: ok", origin(1))
|
---|
1443 | }
|
---|
1444 | return 0
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | // int pipe(int pipefd[2]);
|
---|
1448 | func Xpipe(t *TLS, pipefd uintptr) int32 {
|
---|
1449 | var a [2]int
|
---|
1450 | if err := syscall.Pipe(a[:]); err != nil {
|
---|
1451 | if dmesgs {
|
---|
1452 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1453 | }
|
---|
1454 | t.setErrno(err)
|
---|
1455 | return -1
|
---|
1456 | }
|
---|
1457 |
|
---|
1458 | *(*[2]int32)(unsafe.Pointer(pipefd)) = [2]int32{int32(a[0]), int32(a[1])}
|
---|
1459 | if dmesgs {
|
---|
1460 | dmesg("%v: %v ok", origin(1), a)
|
---|
1461 | }
|
---|
1462 | return 0
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 | // char *inet_ntoa(struct in_addr in);
|
---|
1466 | func X__inet_ntoa(t *TLS, in1 in.In_addr) uintptr {
|
---|
1467 | panic(todo(""))
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | func Xmmap(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
|
---|
1471 | // Cannot avoid the syscall here, addr sometimes matter.
|
---|
1472 | data, _, err := unix.Syscall6(unix.SYS_MMAP, addr, uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset))
|
---|
1473 | if err != 0 {
|
---|
1474 | if dmesgs {
|
---|
1475 | dmesg("%v: %v FAIL", origin(1), err)
|
---|
1476 | }
|
---|
1477 | t.setErrno(err)
|
---|
1478 | return ^uintptr(0) // (void*)-1
|
---|
1479 | }
|
---|
1480 |
|
---|
1481 | if dmesgs {
|
---|
1482 | dmesg("%v: %#x", origin(1), data)
|
---|
1483 | }
|
---|
1484 | return data
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | func X__errno(t *TLS) uintptr {
|
---|
1488 | return X__errno_location(t)
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | func X__ccgo_pthreadMutexattrGettype(tls *TLS, a uintptr) int32 { /* pthread_attr_get.c:93:5: */
|
---|
1492 | return (int32((*pthread_mutexattr_t)(unsafe.Pointer(a)).F__attr & uint32(3)))
|
---|
1493 | }
|
---|
1494 |
|
---|
1495 | func X__ccgo_getMutexType(tls *TLS, m uintptr) int32 { /* pthread_mutex_lock.c:3:5: */
|
---|
1496 | return (*(*int32)(unsafe.Pointer((m /* &.__u */ /* &.__i */))) & 15)
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | func X__ccgo_pthreadAttrGetDetachState(tls *TLS, a uintptr) int32 { /* pthread_attr_get.c:3:5: */
|
---|
1500 | return *(*int32)(unsafe.Pointer((a /* &.__u */ /* &.__i */) + 6*4))
|
---|
1501 | }
|
---|
1502 |
|
---|
1503 | func Xpthread_attr_getdetachstate(tls *TLS, a uintptr, state uintptr) int32 { /* pthread_attr_get.c:7:5: */
|
---|
1504 | *(*int32)(unsafe.Pointer(state)) = *(*int32)(unsafe.Pointer((a /* &.__u */ /* &.__i */) + 6*4))
|
---|
1505 | return 0
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | func Xpthread_attr_setdetachstate(tls *TLS, a uintptr, state int32) int32 { /* pthread_attr_setdetachstate.c:3:5: */
|
---|
1509 | if uint32(state) > 1 {
|
---|
1510 | return 22
|
---|
1511 | }
|
---|
1512 | *(*int32)(unsafe.Pointer((a /* &.__u */ /* &.__i */) + 6*4)) = state
|
---|
1513 | return 0
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | func Xpthread_mutexattr_destroy(tls *TLS, a uintptr) int32 { /* pthread_mutexattr_destroy.c:3:5: */
|
---|
1517 | return 0
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | func Xpthread_mutexattr_init(tls *TLS, a uintptr) int32 { /* pthread_mutexattr_init.c:3:5: */
|
---|
1521 | *(*pthread_mutexattr_t)(unsafe.Pointer(a)) = pthread_mutexattr_t{}
|
---|
1522 | return 0
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | func Xpthread_mutexattr_settype(tls *TLS, a uintptr, type1 int32) int32 { /* pthread_mutexattr_settype.c:3:5: */
|
---|
1526 | if uint32(type1) > uint32(2) {
|
---|
1527 | return 22
|
---|
1528 | }
|
---|
1529 | (*pthread_mutexattr_t)(unsafe.Pointer(a)).F__attr = (((*pthread_mutexattr_t)(unsafe.Pointer(a)).F__attr & Uint32FromInt32(CplInt32(3))) | uint32(type1))
|
---|
1530 | return 0
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | // int uuid_parse( char *in, uuid_t uu);
|
---|
1534 | func Xuuid_parse(t *TLS, in uintptr, uu uintptr) int32 {
|
---|
1535 | r, err := guuid.Parse(GoString(in))
|
---|
1536 | if err != nil {
|
---|
1537 | return -1
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | copy((*RawMem)(unsafe.Pointer(uu))[:unsafe.Sizeof(uuid.Uuid_t{})], r[:])
|
---|
1541 | return 0
|
---|
1542 | }
|
---|
1543 |
|
---|
1544 | func X__srget(t *TLS, stream uintptr) int32 { return Xgetc(t, stream) }
|
---|
1545 |
|
---|
1546 | // void __assert2(const char *, int, const char *, const char *);
|
---|
1547 | // __assert2(__FILE__, __LINE__, __func__, #e))
|
---|
1548 | func X__assert2(t *TLS, file uintptr, line int32, fn, expr uintptr) {
|
---|
1549 | X__assert_fail(t, expr, file, uint32(line), fn)
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 | // int getpagesize(void);
|
---|
1553 | func Xgetpagesize(t *TLS) int32 {
|
---|
1554 | return int32(unix.Getpagesize())
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | const PTHREAD_MUTEX_DEFAULT = 0
|
---|
1558 |
|
---|
1559 | // The pthread_mutex_init() function shall initialize the mutex referenced by
|
---|
1560 | // mutex with attributes specified by attr. If attr is NULL, the default mutex
|
---|
1561 | // attributes are used; the effect shall be the same as passing the address of
|
---|
1562 | // a default mutex attributes object. Upon successful initialization, the state
|
---|
1563 | // of the mutex becomes initialized and unlocked.
|
---|
1564 | //
|
---|
1565 | // If successful, the pthread_mutex_destroy() and pthread_mutex_init()
|
---|
1566 | // functions shall return zero; otherwise, an error number shall be returned to
|
---|
1567 | // indicate the error.
|
---|
1568 | //
|
---|
1569 | // int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
|
---|
1570 | func Xpthread_mutex_init(t *TLS, pMutex, pAttr uintptr) int32 {
|
---|
1571 | typ := PTHREAD_MUTEX_DEFAULT
|
---|
1572 | if pAttr != 0 {
|
---|
1573 | typ = int(X__ccgo_pthreadMutexattrGettype(t, pAttr))
|
---|
1574 | }
|
---|
1575 | mutexesMu.Lock()
|
---|
1576 |
|
---|
1577 | defer mutexesMu.Unlock()
|
---|
1578 |
|
---|
1579 | mutexes[pMutex] = newMutex(typ)
|
---|
1580 | return 0
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 | // uint16_t __builtin_bswap16 (uint32_t x)
|
---|
1584 | func Xbswap16(t *TLS, x uint16) uint16 {
|
---|
1585 | return X__builtin_bswap16(t, x)
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | // uint32_t __builtin_bswap32 (uint32_t x)
|
---|
1589 | func Xbswap32(t *TLS, x uint32) uint32 {
|
---|
1590 | return X__builtin_bswap32(t, x)
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 | // uint64_t __builtin_bswap64 (uint64_t x)
|
---|
1594 | func Xbswap64(t *TLS, x uint64) uint64 {
|
---|
1595 | return X__builtin_bswap64(t, x)
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | func X__builtin_isblank(t *TLS, _c int32) int32 { return Xisblank(t, _c) }
|
---|