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