reach

ssh by name on the local network
git clone https://github.com/tortr-rs/reach
Log | Files | Refs | README | LICENSE

c.h (222437B)


      1 /* c.h: the whole libc of Chamesle Source Linux, in one file. x86-64 and i386 Linux.
      2  * this copy lives in rifle and has grown: directories, links, users and groups, the errno messages, a real
      3  * malloc, strftime, setenv. what rifle added is marked [rifle]. nothing the original had was taken out.
      4  * include it once (in one .c file) and you get everything: the start-up code, system calls, strings, memory,
      5  * files and printf. it builds with tcc (your programs) and gcc (vi and tcc themselves). */
      6 #ifndef C_H
      7 #define C_H
      8 
      9 /* ---- types ---- */
     10 typedef __SIZE_TYPE__ size_t;
     11 typedef __PTRDIFF_TYPE__ ssize_t;
     12 typedef __PTRDIFF_TYPE__ ptrdiff_t;
     13 typedef long intptr_t;
     14 typedef unsigned long uintptr_t;
     15 #ifdef __i386__
     16 typedef long long off_t;      /* files past 2G on i386 too */
     17 #else
     18 typedef long off_t;
     19 #endif
     20 typedef long time_t;
     21 typedef int pid_t;
     22 typedef int mode_t;
     23 typedef unsigned int uid_t;    /* [rifle] */
     24 typedef unsigned int gid_t;
     25 #ifdef __i386__
     26 typedef unsigned long long dev_t;
     27 typedef unsigned long long ino_t;
     28 #else
     29 typedef unsigned long dev_t;
     30 typedef unsigned long ino_t;
     31 #endif
     32 typedef unsigned long nlink_t;
     33 typedef long clock_t;
     34 typedef signed char int8_t;
     35 typedef unsigned char uint8_t;
     36 typedef short int16_t;
     37 typedef unsigned short uint16_t;
     38 typedef int int32_t;
     39 typedef unsigned int uint32_t;
     40 typedef long long int64_t;
     41 typedef unsigned long long uint64_t;
     42 typedef long long intmax_t;
     43 typedef unsigned long long uintmax_t;
     44 typedef int wchar_t_c; /* not wchar_t: tcc and gcc both predefine their own */
     45 #define NULL ((void *)0)
     46 #define offsetof(t, m) ((size_t)&((t *)0)->m)
     47 #define CHAR_BIT 8
     48 #define INT_MAX 2147483647
     49 #define INT_MIN (-INT_MAX - 1)
     50 #define UINT_MAX 4294967295U
     51 #ifdef __i386__
     52 #define LONG_MAX 2147483647L
     53 #define ULONG_MAX 4294967295UL
     54 #else
     55 #define LONG_MAX 9223372036854775807L
     56 #define ULONG_MAX 18446744073709551615UL
     57 #endif
     58 #define LONG_MIN (-LONG_MAX - 1)
     59 #define LLONG_MAX 9223372036854775807LL
     60 #define LLONG_MIN (-LLONG_MAX - 1)
     61 #define ULLONG_MAX 18446744073709551615ULL
     62 #define PATH_MAX 4096
     63 #define NAME_MAX 255
     64 #define SIZE_MAX ULONG_MAX
     65 #define SSIZE_MAX LONG_MAX
     66 #define SHRT_MAX 32767
     67 #define UINT32_MAX 4294967295U
     68 #define INT64_MAX LONG_MAX
     69 #define INT64_MIN LONG_MIN
     70 #define UINT64_MAX ULONG_MAX
     71 #ifndef __cplusplus
     72 typedef _Bool bool;
     73 #define true 1
     74 #define false 0
     75 #endif
     76 #define EXIT_SUCCESS 0
     77 #define EXIT_FAILURE 1
     78 
     79 typedef __builtin_va_list va_list;
     80 #define va_start(ap, last) __builtin_va_start(ap, last)
     81 #define va_arg(ap, t) __builtin_va_arg(ap, t)
     82 #define va_end(ap) __builtin_va_end(ap)
     83 #define va_copy(d, s) __builtin_va_copy(d, s)
     84 
     85 /* ---- system calls ----
     86  * sys6(n, a..f): the number goes in rax, the arguments in rdi rsi rdx r10 r8 r9. written in plain asm so it
     87  * builds the same under gcc and tcc. returns -errno on failure, like the kernel */
     88 #ifdef C_DECLS_ONLY
     89 extern long sys6(long n, long a, long b, long c, long d, long e, long f);
     90 #else
     91 long sys6(long n, long a, long b, long c, long d, long e, long f);
     92 #endif
     93 #ifdef __i386__
     94 /* i386: int 0x80, the number in eax, the arguments in ebx ecx edx esi edi ebp */
     95 #ifndef C_DECLS_ONLY
     96 __asm__(".text\n.global sys6\nsys6:\n push %ebx\n push %esi\n push %edi\n push %ebp\n"
     97         " mov 20(%esp),%eax\n mov 24(%esp),%ebx\n mov 28(%esp),%ecx\n mov 32(%esp),%edx\n"
     98         " mov 36(%esp),%esi\n mov 40(%esp),%edi\n mov 44(%esp),%ebp\n int $0x80\n"
     99         " pop %ebp\n pop %edi\n pop %esi\n pop %ebx\n ret\n");
    100 #endif
    101 #else
    102 #ifndef C_DECLS_ONLY
    103 __asm__(".text\n.global sys6\nsys6:\n"
    104         " mov %rdi,%rax\n mov %rsi,%rdi\n mov %rdx,%rsi\n mov %rcx,%rdx\n mov %r8,%r10\n mov %r9,%r8\n"
    105         " mov 8(%rsp),%r9\n syscall\n ret\n");
    106 #endif
    107 #endif
    108 #define sys0(n) sys6(n, 0, 0, 0, 0, 0, 0)
    109 #define sys1(n, a) sys6(n, (long)(a), 0, 0, 0, 0, 0)
    110 #define sys2(n, a, b) sys6(n, (long)(a), (long)(b), 0, 0, 0, 0)
    111 #define sys3(n, a, b, c) sys6(n, (long)(a), (long)(b), (long)(c), 0, 0, 0)
    112 
    113 #ifdef __i386__
    114 /* where i386 has an old call and a new one, the new one: stat64, mmap2, _llseek, the 32-bit uids */
    115 #define SYS_accept 364
    116 #define SYS_accept4 364
    117 #define SYS_access 33
    118 #define SYS_alarm 27
    119 #define SYS_arch_prctl 384
    120 #define SYS_bind 361
    121 #define SYS_brk 45
    122 #define SYS_chdir 12
    123 #define SYS_chmod 15
    124 #define SYS_chown 212
    125 #define SYS_chroot 61
    126 #define SYS_clock_getres 266
    127 #define SYS_clock_gettime 265
    128 #define SYS_clock_nanosleep 267
    129 #define SYS_clone 120
    130 #define SYS_close 6
    131 #define SYS_connect 362
    132 #define SYS_copy_file_range 377
    133 #define SYS_creat 8
    134 #define SYS_dup 41
    135 #define SYS_dup2 63
    136 #define SYS_dup3 330
    137 #define SYS_execve 11
    138 #define SYS_exit 1
    139 #define SYS_exit_group 252
    140 #define SYS_faccessat 307
    141 #define SYS_fchdir 133
    142 #define SYS_fchmod 94
    143 #define SYS_fchmodat 306
    144 #define SYS_fchmodat2 452
    145 #define SYS_fchown 207
    146 #define SYS_fchownat 298
    147 #define SYS_fcntl 221
    148 #define SYS_fdatasync 148
    149 #define SYS_flock 143
    150 #define SYS_fork 2
    151 #define SYS_fstat 197
    152 #define SYS_fstatfs 100
    153 #define SYS_fsync 118
    154 #define SYS_ftruncate 194
    155 #define SYS_futex 240
    156 #define SYS_getcwd 183
    157 #define SYS_getdents 141
    158 #define SYS_getdents64 220
    159 #define SYS_getegid 202
    160 #define SYS_geteuid 201
    161 #define SYS_getgid 200
    162 #define SYS_getgroups 205
    163 #define SYS_getitimer 105
    164 #define SYS_getpeername 368
    165 #define SYS_getpgid 132
    166 #define SYS_getpgrp 65
    167 #define SYS_getpid 20
    168 #define SYS_getppid 64
    169 #define SYS_getpriority 96
    170 #define SYS_getrandom 355
    171 #define SYS_getrlimit 191
    172 #define SYS_getrusage 77
    173 #define SYS_getsid 147
    174 #define SYS_getsockname 367
    175 #define SYS_getsockopt 365
    176 #define SYS_gettid 224
    177 #define SYS_gettimeofday 78
    178 #define SYS_getuid 199
    179 #define SYS_ioctl 54
    180 #define SYS_kill 37
    181 #define SYS_lchown 198
    182 #define SYS_link 9
    183 #define SYS_linkat 303
    184 #define SYS_listen 363
    185 #define SYS_lseek 140
    186 #define SYS_lstat 196
    187 #define SYS_madvise 219
    188 #define SYS_mkdir 39
    189 #define SYS_mkdirat 296
    190 #define SYS_mknod 14
    191 #define SYS_mknodat 297
    192 #define SYS_mmap 192
    193 #define SYS_mount 21
    194 #define SYS_mprotect 125
    195 #define SYS_mremap 163
    196 #define SYS_msync 144
    197 #define SYS_munmap 91
    198 #define SYS_nanosleep 162
    199 #define SYS_newfstatat 300
    200 #define SYS_open 5
    201 #define SYS_openat 295
    202 #define SYS_pause 29
    203 #define SYS_pipe 42
    204 #define SYS_pipe2 331
    205 #define SYS_poll 168
    206 #define SYS_ppoll 309
    207 #define SYS_prctl 172
    208 #define SYS_pread64 180
    209 #define SYS_pwrite64 181
    210 #define SYS_read 3
    211 #define SYS_readlink 85
    212 #define SYS_readlinkat 305
    213 #define SYS_readv 145
    214 #define SYS_recvfrom 371
    215 #define SYS_recvmsg 372
    216 #define SYS_rename 38
    217 #define SYS_renameat 302
    218 #define SYS_rmdir 40
    219 #define SYS_rt_sigaction 174
    220 #define SYS_rt_sigpending 176
    221 #define SYS_rt_sigprocmask 175
    222 #define SYS_rt_sigreturn 173
    223 #define SYS_rt_sigsuspend 179
    224 #define SYS_rt_sigtimedwait 177
    225 #define SYS_sched_getaffinity 242
    226 #define SYS_sched_yield 158
    227 #define SYS_select 142
    228 #define SYS_sendfile 239
    229 #define SYS_sendmsg 370
    230 #define SYS_sendto 369
    231 #define SYS_set_tid_address 258
    232 #define SYS_setgid 214
    233 #define SYS_setgroups 206
    234 #define SYS_sethostname 74
    235 #define SYS_setitimer 104
    236 #define SYS_setpgid 57
    237 #define SYS_setpriority 97
    238 #define SYS_setregid 204
    239 #define SYS_setresgid 210
    240 #define SYS_setresuid 208
    241 #define SYS_setreuid 203
    242 #define SYS_setrlimit 75
    243 #define SYS_setsid 66
    244 #define SYS_setsockopt 366
    245 #define SYS_setuid 213
    246 #define SYS_shutdown 373
    247 #define SYS_sigaltstack 186
    248 #define SYS_socket 359
    249 #define SYS_socketpair 360
    250 #define SYS_stat 195
    251 #define SYS_statfs 99
    252 #define SYS_symlink 83
    253 #define SYS_symlinkat 304
    254 #define SYS_sync 36
    255 #define SYS_syncfs 344
    256 #define SYS_sysinfo 116
    257 #define SYS_tgkill 270
    258 #define SYS_time 13
    259 #define SYS_times 43
    260 #define SYS_truncate 193
    261 #define SYS_umask 60
    262 #define SYS_umount2 52
    263 #define SYS_uname 122
    264 #define SYS_unlink 10
    265 #define SYS_unlinkat 301
    266 #define SYS_utime 30
    267 #define SYS_utimensat 320
    268 #define SYS_utimes 271
    269 #define SYS_vfork 190
    270 #define SYS_wait4 114
    271 #define SYS_write 4
    272 #define SYS_writev 146
    273 #define O_LARGEFILE 0100000
    274 #else
    275 #define O_LARGEFILE 0
    276 #endif
    277 
    278 #ifndef __i386__
    279 #define SYS_read 0
    280 #define SYS_write 1
    281 #define SYS_open 2
    282 #define SYS_close 3
    283 #define SYS_lseek 8
    284 #define SYS_mmap 9
    285 #define SYS_mprotect 10
    286 #define SYS_munmap 11
    287 #define SYS_ioctl 16
    288 #define SYS_fork 57
    289 #define SYS_execve 59
    290 #define SYS_exit 60
    291 #define SYS_wait4 61
    292 #define SYS_getcwd 79
    293 #define SYS_mkdir 83
    294 #define SYS_unlink 87
    295 #define SYS_mount 165
    296 #define SYS_time 201
    297 #define SYS_gettimeofday 96
    298 #define SYS_stat 4
    299 #define SYS_fstat 5
    300 #define SYS_brk 12
    301 #define SYS_rt_sigaction 13
    302 #define SYS_rt_sigreturn 15
    303 #define SYS_getpid 39
    304 #define SYS_chdir 80
    305 #define SYS_creat 85
    306 #define SYS_chmod 90
    307 #define SYS_pipe 22
    308 #define SYS_dup 32
    309 #define SYS_getuid 102
    310 #endif
    311 
    312 /* errno: the libc keeps the last error here. one program, one thread: a plain variable does it */
    313 #ifdef C_DECLS_ONLY
    314 extern int errno;
    315 #else
    316 int errno;
    317 #endif
    318 #define EPERM 1
    319 #define ENOENT 2
    320 #define ESRCH 3
    321 #define EINTR 4
    322 #define EIO 5
    323 #define ENXIO 6
    324 #define E2BIG 7
    325 #define ENOEXEC 8
    326 #define EBADF 9
    327 #define ECHILD 10
    328 #define EAGAIN 11
    329 #define ENOMEM 12
    330 #define EACCES 13
    331 #define EFAULT 14
    332 #define EBUSY 16
    333 #define EEXIST 17
    334 #define EXDEV 18
    335 #define ENODEV 19
    336 #define ENOTDIR 20
    337 #define EISDIR 21
    338 #define EINVAL 22
    339 #define ENFILE 23
    340 #define EMFILE 24
    341 #define ENOTTY 25
    342 #define ETXTBSY 26
    343 #define EFBIG 27
    344 #define ENOSPC 28
    345 #define ESPIPE 29
    346 #define EROFS 30
    347 #define EMLINK 31
    348 #define EPIPE 32
    349 #define EDOM 33
    350 #define ERANGE 34
    351 #define EDEADLK 35
    352 #define ENAMETOOLONG 36
    353 #define ENOSYS 38
    354 #define ENOTEMPTY 39
    355 #define ELOOP 40
    356 #define ENODATA 61
    357 #define EOVERFLOW 75
    358 #define EILSEQ 84
    359 #define EOPNOTSUPP 95
    360 #define ETIMEDOUT 110
    361 #define ESTALE 116
    362 #define EDQUOT 122
    363 #ifndef C_DECLS_ONLY
    364 static long fail(long r) { if (r < 0 && r > -4096) { errno = -r; return -1; } return r; }
    365 #endif
    366 
    367 #define O_RDONLY 0
    368 #define O_WRONLY 1
    369 #define O_RDWR 2
    370 #define O_CREAT 0100
    371 #define O_TRUNC 01000
    372 #define O_APPEND 02000
    373 #define O_EXCL 0200          /* [rifle] */
    374 #define O_NOCTTY 0400
    375 #define O_NONBLOCK 04000
    376 #define O_SYNC 04010000
    377 #define O_DIRECTORY 0200000
    378 #define O_NOFOLLOW 0400000
    379 #define O_CLOEXEC 02000000
    380 #define AT_FDCWD (-100)
    381 #define AT_SYMLINK_NOFOLLOW 0x100
    382 #define AT_REMOVEDIR 0x200
    383 #define SEEK_SET 0
    384 #define SEEK_CUR 1
    385 #define SEEK_END 2
    386 #define PROT_READ 1
    387 #define PROT_WRITE 2
    388 #define PROT_EXEC 4
    389 #define _SC_PAGESIZE 30
    390 
    391 ssize_t read(int fd, void *buf, size_t n)
    392 #ifdef C_DECLS_ONLY
    393 ;
    394 #else
    395 { return fail(sys3(SYS_read, fd, buf, n)); }
    396 #endif
    397 ssize_t write(int fd, const void *buf, size_t n)
    398 #ifdef C_DECLS_ONLY
    399 ;
    400 #else
    401 { return fail(sys3(SYS_write, fd, buf, n)); }
    402 #endif
    403 int open(const char *path, int flags, ...)
    404 #ifdef C_DECLS_ONLY
    405 ;
    406 #else
    407 {
    408     va_list ap; int mode;
    409     va_start(ap, flags); mode = va_arg(ap, int); va_end(ap);
    410     return fail(sys3(SYS_open, path, flags | O_LARGEFILE, flags & O_CREAT ? mode : 0));
    411 }
    412 #endif
    413 int close(int fd)
    414 #ifdef C_DECLS_ONLY
    415 ;
    416 #else
    417 { return fail(sys1(SYS_close, fd)); }
    418 #endif
    419 #ifdef __i386__
    420 /* i386 hands 64-bit offsets over in two halves, high first for _llseek */
    421 off_t lseek(int fd, off_t off, int whence)
    422 #ifdef C_DECLS_ONLY
    423 ;
    424 #else
    425 {
    426     off_t r;
    427     if (fail(sys6(SYS_lseek, fd, (long)(off >> 32), (long)off, (long)&r, whence, 0)) < 0) return -1;
    428     return r;
    429 }
    430 #endif
    431 #else
    432 off_t lseek(int fd, off_t off, int whence)
    433 #ifdef C_DECLS_ONLY
    434 ;
    435 #else
    436 { return fail(sys3(SYS_lseek, fd, off, whence)); }
    437 #endif
    438 #endif
    439 int ioctl(int fd, unsigned long req, void *arg)
    440 #ifdef C_DECLS_ONLY
    441 ;
    442 #else
    443 { return fail(sys3(SYS_ioctl, fd, req, arg)); }
    444 #endif
    445 int mprotect(void *p, size_t n, int prot)
    446 #ifdef C_DECLS_ONLY
    447 ;
    448 #else
    449 { return fail(sys3(SYS_mprotect, p, n, prot)); }
    450 #endif
    451 int unlink(const char *path)
    452 #ifdef C_DECLS_ONLY
    453 ;
    454 #else
    455 { return fail(sys1(SYS_unlink, path)); }
    456 #endif
    457 int mkdir(const char *path, mode_t mode)
    458 #ifdef C_DECLS_ONLY
    459 ;
    460 #else
    461 { return fail(sys2(SYS_mkdir, path, mode)); }
    462 #endif
    463 pid_t fork(void)
    464 #ifdef C_DECLS_ONLY
    465 ;
    466 #else
    467 { return fail(sys0(SYS_fork)); }
    468 #endif
    469 int execve(const char *path, char *const argv[], char *const envp[])
    470 #ifdef C_DECLS_ONLY
    471 ;
    472 #else
    473 { return fail(sys3(SYS_execve, path, argv, envp)); }
    474 #endif
    475 pid_t waitpid(pid_t pid, int *status, int options)
    476 #ifdef C_DECLS_ONLY
    477 ;
    478 #else
    479 { return fail(sys6(SYS_wait4, pid, (long)status, options, 0, 0, 0)); }
    480 #endif
    481 int mount(const char *src, const char *dst, const char *type, unsigned long flags, const void *data)
    482 #ifdef C_DECLS_ONLY
    483 ;
    484 #else
    485 {
    486     return fail(sys6(SYS_mount, (long)src, (long)dst, (long)type, flags, (long)data, 0));
    487 }
    488 #endif
    489 char *getcwd(char *buf, size_t n)
    490 #ifdef C_DECLS_ONLY
    491 ;
    492 #else
    493 { return fail(sys2(SYS_getcwd, buf, n)) < 0 ? NULL : buf; }
    494 #endif
    495 time_t time(time_t *t)
    496 #ifdef C_DECLS_ONLY
    497 ;
    498 #else
    499 { time_t now = sys1(SYS_time, 0); if (t) *t = now; return now; }
    500 #endif
    501 long sysconf(int name)
    502 #ifdef C_DECLS_ONLY
    503 ;
    504 #else
    505 { return name == _SC_PAGESIZE ? 4096 : -1; }
    506 #endif
    507 int creat(const char *path, mode_t mode)
    508 #ifdef C_DECLS_ONLY
    509 ;
    510 #else
    511 { return fail(sys2(SYS_creat, path, mode)); }
    512 #endif
    513 int chdir(const char *path)
    514 #ifdef C_DECLS_ONLY
    515 ;
    516 #else
    517 { return fail(sys1(SYS_chdir, path)); }
    518 #endif
    519 int chmod(const char *path, mode_t mode)
    520 #ifdef C_DECLS_ONLY
    521 ;
    522 #else
    523 { return fail(sys2(SYS_chmod, path, mode)); }
    524 #endif
    525 int dup(int fd)
    526 #ifdef C_DECLS_ONLY
    527 ;
    528 #else
    529 { return fail(sys1(SYS_dup, fd)); }
    530 #endif
    531 int pipe(int fd[2])
    532 #ifdef C_DECLS_ONLY
    533 ;
    534 #else
    535 { return fail(sys1(SYS_pipe, fd)); }
    536 #endif
    537 pid_t getpid(void)
    538 #ifdef C_DECLS_ONLY
    539 ;
    540 #else
    541 { return sys0(SYS_getpid); }
    542 #endif
    543 int getuid(void)
    544 #ifdef C_DECLS_ONLY
    545 ;
    546 #else
    547 { return sys0(SYS_getuid); }
    548 #endif
    549 pid_t wait(int *status)
    550 #ifdef C_DECLS_ONLY
    551 ;
    552 #else
    553 { return waitpid(-1, status, 0); }
    554 #endif
    555 
    556 /* the end of the data segment, the "break". brk(p) moves it to p, sbrk(n) moves it n bytes and returns where it was */
    557 #ifndef C_DECLS_ONLY
    558 static char *cur_brk;
    559 #endif
    560 int brk(void *p)
    561 #ifdef C_DECLS_ONLY
    562 ;
    563 #else
    564 {
    565     char *r = (char *)sys1(SYS_brk, p);
    566     cur_brk = r;
    567     if (r != p) { errno = ENOMEM; return -1; }
    568     return 0;
    569 }
    570 #endif
    571 void *sbrk(long n)
    572 #ifdef C_DECLS_ONLY
    573 ;
    574 #else
    575 {
    576     char *old;
    577     if (!cur_brk) cur_brk = (char *)sys1(SYS_brk, 0);
    578     old = cur_brk;
    579     if (n && brk(old + n) < 0) return (void *)-1;
    580     return old;
    581 }
    582 #endif
    583 
    584 /* ---- files' details ---- */
    585 #ifdef __i386__
    586 /* the kernel's stat64 */
    587 struct stat {
    588     unsigned long long st_dev;
    589     unsigned int pad0, st_ino32, st_mode, st_nlink, st_uid, st_gid;
    590     unsigned long long st_rdev;
    591     unsigned int pad1;
    592     long long st_size;
    593     unsigned long st_blksize;
    594     unsigned long long st_blocks;
    595     unsigned long st_atime, st_atime_nsec, st_mtime, st_mtime_nsec, st_ctime, st_ctime_nsec;
    596     unsigned long long st_ino;
    597 };
    598 #else
    599 struct stat {
    600     unsigned long st_dev, st_ino, st_nlink;
    601     unsigned int st_mode, st_uid, st_gid, pad0;
    602     unsigned long st_rdev;
    603     long st_size, st_blksize, st_blocks;
    604     unsigned long st_atime, st_atime_nsec, st_mtime, st_mtime_nsec, st_ctime, st_ctime_nsec;
    605     long unused[3];
    606 };
    607 #endif
    608 #define S_IFMT 0170000
    609 #define S_IFDIR 0040000
    610 #define S_IFCHR 0020000
    611 #define S_IFREG 0100000
    612 #define S_IFBLK 0060000     /* [rifle] */
    613 #define S_IFIFO 0010000
    614 #define S_IFLNK 0120000
    615 #define S_IFSOCK 0140000
    616 #define S_ISUID 04000
    617 #define S_ISGID 02000
    618 #define S_ISVTX 01000
    619 #define S_IRWXU 0700
    620 #define S_IRUSR 0400
    621 #define S_IWUSR 0200
    622 #define S_IXUSR 0100
    623 #define S_IRWXG 070
    624 #define S_IRGRP 040
    625 #define S_IWGRP 020
    626 #define S_IXGRP 010
    627 #define S_IRWXO 07
    628 #define S_IROTH 04
    629 #define S_IWOTH 02
    630 #define S_IXOTH 01
    631 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
    632 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
    633 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
    634 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
    635 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
    636 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
    637 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
    638 int stat(const char *path, struct stat *st)
    639 #ifdef C_DECLS_ONLY
    640 ;
    641 #else
    642 { return fail(sys2(SYS_stat, path, st)); }
    643 #endif
    644 int fstat(int fd, struct stat *st)
    645 #ifdef C_DECLS_ONLY
    646 ;
    647 #else
    648 { return fail(sys2(SYS_fstat, fd, st)); }
    649 #endif
    650 
    651 /* ---- the terminal ---- */
    652 struct termios { unsigned int c_iflag, c_oflag, c_cflag, c_lflag; unsigned char c_line, c_cc[19]; };
    653 #define TCGETS 0x5401
    654 #define TCSETSW 0x5403
    655 #define VINTR 0
    656 #define VQUIT 1
    657 #define VERASE 2
    658 #define VKILL 3
    659 #define VEOF 4
    660 #define VTIME 5
    661 #define VMIN 6
    662 #define ICRNL 0400
    663 #define IXON 02000
    664 #define OPOST 01
    665 #define ONLCR 04
    666 #define TAB3 014000
    667 #define ISIG 01
    668 #define ICANON 02
    669 #define ECHO 010
    670 #define IEXTEN 0100000
    671 int tcgetattr(int fd, struct termios *t)
    672 #ifdef C_DECLS_ONLY
    673 ;
    674 #else
    675 { return ioctl(fd, TCGETS, t); }
    676 #endif
    677 int tcsetattr(int fd, int when, struct termios *t)
    678 #ifdef C_DECLS_ONLY
    679 ;
    680 #else
    681 { (void)when; return ioctl(fd, TCSETSW, t); }
    682 #endif
    683 int isatty(int fd)
    684 #ifdef C_DECLS_ONLY
    685 ;
    686 #else
    687 { struct termios t; return tcgetattr(fd, &t) == 0; }
    688 #endif
    689 
    690 /* ---- signals ----
    691  * signal(n, f): f runs when signal n comes. SIG_DFL (0) puts back what the kernel does, SIG_IGN (1) ignores it.
    692  * the kernel needs a way back from f: restore_rt, which asks it to carry on where the signal hit */
    693 typedef void (*sighandler_t)(int);
    694 #define SIG_DFL ((sighandler_t)0)
    695 #define SIG_IGN ((sighandler_t)1)
    696 #define SIG_ERR ((sighandler_t)-1)
    697 #define SIGHUP 1
    698 #define SIGINT 2
    699 #define SIGQUIT 3
    700 #define SIGKILL 9
    701 #define SIGTERM 15
    702 #define SIGCHLD 17
    703 #ifdef C_DECLS_ONLY
    704 extern void c_restore_rt(void);
    705 #else
    706 void c_restore_rt(void);
    707 #endif
    708 #ifdef __i386__
    709 #ifndef C_DECLS_ONLY
    710 __asm__(".text\n.global c_restore_rt\nc_restore_rt:\n mov $173,%eax\n int $0x80\n");
    711 #endif
    712 #else
    713 #ifndef C_DECLS_ONLY
    714 __asm__(".text\n.global c_restore_rt\nc_restore_rt:\n mov $15,%rax\n syscall\n");
    715 #endif
    716 #endif
    717 struct c_sigaction { sighandler_t handler; unsigned long flags; void (*restorer)(void); unsigned long long mask; };
    718 sighandler_t signal(int n, sighandler_t f)
    719 #ifdef C_DECLS_ONLY
    720 ;
    721 #else
    722 {
    723     struct c_sigaction new = { f, 0x04000000 | 0x10000000 /* restorer, restart */, c_restore_rt, 0 }, old;
    724     if (fail(sys6(SYS_rt_sigaction, n, (long)&new, (long)&old, 8, 0, 0)) < 0) return SIG_ERR;
    725     return old.handler;
    726 }
    727 #endif
    728 __attribute__((noreturn)) void _exit(int code)
    729 #ifdef C_DECLS_ONLY
    730 ;
    731 #else
    732 { for (;;) sys1(SYS_exit, code); }
    733 #endif
    734 
    735 /* ---- more system calls [rifle] ---- */
    736 #ifndef __i386__
    737 #define SYS_lstat 6
    738 #define SYS_access 21
    739 #define SYS_mremap 25
    740 #define SYS_dup2 33
    741 #define SYS_nanosleep 35
    742 #define SYS_kill 62
    743 #define SYS_uname 63
    744 #define SYS_fcntl 72
    745 #define SYS_fsync 74
    746 #define SYS_fdatasync 75
    747 #define SYS_truncate 76
    748 #define SYS_ftruncate 77
    749 #define SYS_rename 82
    750 #define SYS_rmdir 84
    751 #define SYS_link 86
    752 #define SYS_symlink 88
    753 #define SYS_readlink 89
    754 #define SYS_fchmod 91
    755 #define SYS_chown 92
    756 #define SYS_fchown 93
    757 #define SYS_lchown 94
    758 #define SYS_umask 95
    759 #define SYS_getgid 104
    760 #define SYS_geteuid 107
    761 #define SYS_getegid 108
    762 #define SYS_getppid 110
    763 #define SYS_getgroups 115
    764 #define SYS_mknod 133
    765 #define SYS_sync 162
    766 #define SYS_sethostname 170
    767 #define SYS_sched_getaffinity 204
    768 #define SYS_getdents64 217
    769 #define SYS_clock_gettime 228
    770 #define SYS_utimensat 280
    771 #define SYS_openat 257
    772 #define SYS_mkdirat 258
    773 #define SYS_newfstatat 262
    774 #define SYS_unlinkat 263
    775 #define SYS_syncfs 306
    776 #define SYS_statfs 137
    777 #define SYS_fstatfs 138
    778 #define SYS_getrandom 318
    779 #define SYS_fchdir 81
    780 #define SYS_pread64 17
    781 #define SYS_pwrite64 18
    782 #define SYS_sysinfo 99
    783 #define SYS_chroot 161
    784 #define SYS_setuid 105
    785 #define SYS_setgid 106
    786 #define SYS_times 100
    787 #define SYS_getpgid 121
    788 #define SYS_setsid 112
    789 #define SYS_sendfile 40
    790 #define SYS_copy_file_range 326
    791 #endif
    792 #define F_GETFL 3
    793 #define F_SETFL 4
    794 #define F_GETFD 1
    795 #define F_SETFD 2
    796 #define FD_CLOEXEC 1
    797 #define R_OK 4
    798 #define W_OK 2
    799 #define X_OK 1
    800 #define F_OK 0
    801 #define UTIME_NOW ((1L << 30) - 1L)
    802 #define UTIME_OMIT ((1L << 30) - 2L)
    803 struct timespec { long tv_sec, tv_nsec; };
    804 struct utsname { char sysname[65], nodename[65], release[65], version[65], machine[65], domainname[65]; };
    805 struct statfs {
    806     long f_type, f_bsize;
    807     unsigned long f_blocks, f_bfree, f_bavail, f_files, f_ffree;
    808     int f_fsid[2];
    809     long f_namelen, f_frsize, f_flags, f_spare[4];
    810 };
    811 int lstat(const char *path, struct stat *st)
    812 #ifdef C_DECLS_ONLY
    813 ;
    814 #else
    815 { return fail(sys2(SYS_lstat, path, st)); }
    816 #endif
    817 int fstatat(int dirfd, const char *path, struct stat *st, int flags)
    818 #ifdef C_DECLS_ONLY
    819 ;
    820 #else
    821 { return fail(sys6(SYS_newfstatat, dirfd, (long)path, (long)st, flags, 0, 0)); }
    822 #endif
    823 int openat(int dirfd, const char *path, int flags, ...)
    824 #ifdef C_DECLS_ONLY
    825 ;
    826 #else
    827 {
    828     va_list ap; int mode;
    829     va_start(ap, flags); mode = va_arg(ap, int); va_end(ap);
    830     return fail(sys6(SYS_openat, dirfd, (long)path, flags | O_LARGEFILE, flags & O_CREAT ? mode : 0, 0, 0));
    831 }
    832 #endif
    833 int unlinkat(int dirfd, const char *path, int flags)
    834 #ifdef C_DECLS_ONLY
    835 ;
    836 #else
    837 { return fail(sys3(SYS_unlinkat, dirfd, path, flags)); }
    838 #endif
    839 int mkdirat(int dirfd, const char *path, mode_t mode)
    840 #ifdef C_DECLS_ONLY
    841 ;
    842 #else
    843 { return fail(sys3(SYS_mkdirat, dirfd, path, mode)); }
    844 #endif
    845 int rmdir(const char *path)
    846 #ifdef C_DECLS_ONLY
    847 ;
    848 #else
    849 { return fail(sys1(SYS_rmdir, path)); }
    850 #endif
    851 int rename(const char *a, const char *b)
    852 #ifdef C_DECLS_ONLY
    853 ;
    854 #else
    855 { return fail(sys2(SYS_rename, a, b)); }
    856 #endif
    857 int link(const char *a, const char *b)
    858 #ifdef C_DECLS_ONLY
    859 ;
    860 #else
    861 { return fail(sys2(SYS_link, a, b)); }
    862 #endif
    863 int symlink(const char *a, const char *b)
    864 #ifdef C_DECLS_ONLY
    865 ;
    866 #else
    867 { return fail(sys2(SYS_symlink, a, b)); }
    868 #endif
    869 ssize_t readlink(const char *path, char *buf, size_t n)
    870 #ifdef C_DECLS_ONLY
    871 ;
    872 #else
    873 { return fail(sys3(SYS_readlink, path, buf, n)); }
    874 #endif
    875 int fchmod(int fd, mode_t mode)
    876 #ifdef C_DECLS_ONLY
    877 ;
    878 #else
    879 { return fail(sys2(SYS_fchmod, fd, mode)); }
    880 #endif
    881 int chown(const char *path, uid_t u, gid_t g)
    882 #ifdef C_DECLS_ONLY
    883 ;
    884 #else
    885 { return fail(sys3(SYS_chown, path, u, g)); }
    886 #endif
    887 int lchown(const char *path, uid_t u, gid_t g)
    888 #ifdef C_DECLS_ONLY
    889 ;
    890 #else
    891 { return fail(sys3(SYS_lchown, path, u, g)); }
    892 #endif
    893 int fchown(int fd, uid_t u, gid_t g)
    894 #ifdef C_DECLS_ONLY
    895 ;
    896 #else
    897 { return fail(sys3(SYS_fchown, fd, u, g)); }
    898 #endif
    899 mode_t umask(mode_t m)
    900 #ifdef C_DECLS_ONLY
    901 ;
    902 #else
    903 { return sys1(SYS_umask, m); }
    904 #endif
    905 int access(const char *path, int mode)
    906 #ifdef C_DECLS_ONLY
    907 ;
    908 #else
    909 { return fail(sys2(SYS_access, path, mode)); }
    910 #endif
    911 #ifdef __i386__
    912 int truncate(const char *path, off_t n)
    913 #ifdef C_DECLS_ONLY
    914 ;
    915 #else
    916 { return fail(sys3(SYS_truncate, path, (long)n, (long)(n >> 32))); }
    917 #endif
    918 int ftruncate(int fd, off_t n)
    919 #ifdef C_DECLS_ONLY
    920 ;
    921 #else
    922 { return fail(sys3(SYS_ftruncate, fd, (long)n, (long)(n >> 32))); }
    923 #endif
    924 #else
    925 int truncate(const char *path, off_t n)
    926 #ifdef C_DECLS_ONLY
    927 ;
    928 #else
    929 { return fail(sys2(SYS_truncate, path, n)); }
    930 #endif
    931 int ftruncate(int fd, off_t n)
    932 #ifdef C_DECLS_ONLY
    933 ;
    934 #else
    935 { return fail(sys2(SYS_ftruncate, fd, n)); }
    936 #endif
    937 #endif
    938 int fsync(int fd)
    939 #ifdef C_DECLS_ONLY
    940 ;
    941 #else
    942 { return fail(sys1(SYS_fsync, fd)); }
    943 #endif
    944 int fdatasync(int fd)
    945 #ifdef C_DECLS_ONLY
    946 ;
    947 #else
    948 { return fail(sys1(SYS_fdatasync, fd)); }
    949 #endif
    950 void sync(void)
    951 #ifdef C_DECLS_ONLY
    952 ;
    953 #else
    954 { sys0(SYS_sync); }
    955 #endif
    956 int syncfs(int fd)
    957 #ifdef C_DECLS_ONLY
    958 ;
    959 #else
    960 { return fail(sys1(SYS_syncfs, fd)); }
    961 #endif
    962 int mknod(const char *path, mode_t mode, dev_t dev)
    963 #ifdef C_DECLS_ONLY
    964 ;
    965 #else
    966 { return fail(sys3(SYS_mknod, path, mode, dev)); }
    967 #endif
    968 int mkfifo(const char *path, mode_t mode)
    969 #ifdef C_DECLS_ONLY
    970 ;
    971 #else
    972 { return mknod(path, (mode & 07777) | S_IFIFO, 0); }
    973 #endif
    974 int dup2(int a, int b)
    975 #ifdef C_DECLS_ONLY
    976 ;
    977 #else
    978 { return fail(sys2(SYS_dup2, a, b)); }
    979 #endif
    980 int fcntl(int fd, int cmd, long arg)
    981 #ifdef C_DECLS_ONLY
    982 ;
    983 #else
    984 { return fail(sys3(SYS_fcntl, fd, cmd, arg)); }
    985 #endif
    986 int fchdir(int fd)
    987 #ifdef C_DECLS_ONLY
    988 ;
    989 #else
    990 { return fail(sys1(SYS_fchdir, fd)); }
    991 #endif
    992 int kill(pid_t pid, int sig)
    993 #ifdef C_DECLS_ONLY
    994 ;
    995 #else
    996 { return fail(sys2(SYS_kill, pid, sig)); }
    997 #endif
    998 int uname(struct utsname *u)
    999 #ifdef C_DECLS_ONLY
   1000 ;
   1001 #else
   1002 { return fail(sys1(SYS_uname, u)); }
   1003 #endif
   1004 int sethostname(const char *name, size_t n)
   1005 #ifdef C_DECLS_ONLY
   1006 ;
   1007 #else
   1008 { return fail(sys2(SYS_sethostname, name, n)); }
   1009 #endif
   1010 int nanosleep(const struct timespec *req, struct timespec *rem)
   1011 #ifdef C_DECLS_ONLY
   1012 ;
   1013 #else
   1014 { return fail(sys2(SYS_nanosleep, req, rem)); }
   1015 #endif
   1016 int clock_gettime(int clk, struct timespec *t)
   1017 #ifdef C_DECLS_ONLY
   1018 ;
   1019 #else
   1020 { return fail(sys2(SYS_clock_gettime, clk, t)); }
   1021 #endif
   1022 int utimensat(int dirfd, const char *path, const struct timespec t[2], int flags)
   1023 #ifdef C_DECLS_ONLY
   1024 ;
   1025 #else
   1026 { return fail(sys6(SYS_utimensat, dirfd, (long)path, (long)t, flags, 0, 0)); }
   1027 #endif
   1028 int statfs(const char *path, struct statfs *s)
   1029 #ifdef C_DECLS_ONLY
   1030 ;
   1031 #else
   1032 { return fail(sys2(SYS_statfs, path, s)); }
   1033 #endif
   1034 pid_t getppid(void)
   1035 #ifdef C_DECLS_ONLY
   1036 ;
   1037 #else
   1038 { return sys0(SYS_getppid); }
   1039 #endif
   1040 uid_t geteuid(void)
   1041 #ifdef C_DECLS_ONLY
   1042 ;
   1043 #else
   1044 { return sys0(SYS_geteuid); }
   1045 #endif
   1046 gid_t getgid(void)
   1047 #ifdef C_DECLS_ONLY
   1048 ;
   1049 #else
   1050 { return sys0(SYS_getgid); }
   1051 #endif
   1052 gid_t getegid(void)
   1053 #ifdef C_DECLS_ONLY
   1054 ;
   1055 #else
   1056 { return sys0(SYS_getegid); }
   1057 #endif
   1058 int getgroups(int n, gid_t *list)
   1059 #ifdef C_DECLS_ONLY
   1060 ;
   1061 #else
   1062 { return fail(sys2(SYS_getgroups, n, list)); }
   1063 #endif
   1064 int sched_getaffinity(pid_t pid, size_t n, void *mask)
   1065 #ifdef C_DECLS_ONLY
   1066 ;
   1067 #else
   1068 { return fail(sys3(SYS_sched_getaffinity, pid, n, mask)); }
   1069 #endif
   1070 ssize_t getrandom(void *buf, size_t n, unsigned flags)
   1071 #ifdef C_DECLS_ONLY
   1072 ;
   1073 #else
   1074 { return fail(sys3(SYS_getrandom, buf, n, flags)); }
   1075 #endif
   1076 #ifdef __i386__
   1077 ssize_t pread(int fd, void *buf, size_t n, off_t off)
   1078 #ifdef C_DECLS_ONLY
   1079 ;
   1080 #else
   1081 { return fail(sys6(SYS_pread64, fd, (long)buf, n, (long)off, (long)(off >> 32), 0)); }
   1082 #endif
   1083 ssize_t pwrite(int fd, const void *buf, size_t n, off_t off)
   1084 #ifdef C_DECLS_ONLY
   1085 ;
   1086 #else
   1087 { return fail(sys6(SYS_pwrite64, fd, (long)buf, n, (long)off, (long)(off >> 32), 0)); }
   1088 #endif
   1089 #else
   1090 ssize_t pread(int fd, void *buf, size_t n, off_t off)
   1091 #ifdef C_DECLS_ONLY
   1092 ;
   1093 #else
   1094 { return fail(sys6(SYS_pread64, fd, (long)buf, n, off, 0, 0)); }
   1095 #endif
   1096 ssize_t pwrite(int fd, const void *buf, size_t n, off_t off)
   1097 #ifdef C_DECLS_ONLY
   1098 ;
   1099 #else
   1100 { return fail(sys6(SYS_pwrite64, fd, (long)buf, n, off, 0, 0)); }
   1101 #endif
   1102 #endif
   1103 int setuid(uid_t u)
   1104 #ifdef C_DECLS_ONLY
   1105 ;
   1106 #else
   1107 { return fail(sys1(SYS_setuid, u)); }
   1108 #endif
   1109 int setgid(gid_t g)
   1110 #ifdef C_DECLS_ONLY
   1111 ;
   1112 #else
   1113 { return fail(sys1(SYS_setgid, g)); }
   1114 #endif
   1115 int chroot(const char *path)
   1116 #ifdef C_DECLS_ONLY
   1117 ;
   1118 #else
   1119 { return fail(sys1(SYS_chroot, path)); }
   1120 #endif
   1121 #define major(d) ((unsigned)((((d) >> 32) & 0xfffff000) | (((d) >> 8) & 0xfff)))
   1122 #define minor(d) ((unsigned)((((d) >> 12) & 0xffffff00) | ((d) & 0xff)))
   1123 #define makedev(ma, mi) ((dev_t)((((unsigned long long)(ma) & 0xfffff000ULL) << 32) | (((unsigned long long)(ma) & 0xfffULL) << 8) | \
   1124                                  (((unsigned long long)(mi) & 0xffffff00ULL) << 12) | ((unsigned long long)(mi) & 0xffULL)))
   1125 
   1126 /* ---- characters ---- */
   1127 int isdigit(int c)
   1128 #ifdef C_DECLS_ONLY
   1129 ;
   1130 #else
   1131 { return c >= '0' && c <= '9'; }
   1132 #endif
   1133 int isalpha(int c)
   1134 #ifdef C_DECLS_ONLY
   1135 ;
   1136 #else
   1137 { return (c | 32) >= 'a' && (c | 32) <= 'z'; }
   1138 #endif
   1139 int isalnum(int c)
   1140 #ifdef C_DECLS_ONLY
   1141 ;
   1142 #else
   1143 { return isalpha(c) || isdigit(c); }
   1144 #endif
   1145 int isspace(int c)
   1146 #ifdef C_DECLS_ONLY
   1147 ;
   1148 #else
   1149 { return c == ' ' || (c >= '\t' && c <= '\r'); }
   1150 #endif
   1151 int isupper(int c)
   1152 #ifdef C_DECLS_ONLY
   1153 ;
   1154 #else
   1155 { return c >= 'A' && c <= 'Z'; }
   1156 #endif
   1157 int islower(int c)
   1158 #ifdef C_DECLS_ONLY
   1159 ;
   1160 #else
   1161 { return c >= 'a' && c <= 'z'; }
   1162 #endif
   1163 int isxdigit(int c)
   1164 #ifdef C_DECLS_ONLY
   1165 ;
   1166 #else
   1167 { return isdigit(c) || ((c | 32) >= 'a' && (c | 32) <= 'f'); }
   1168 #endif
   1169 int isprint(int c)
   1170 #ifdef C_DECLS_ONLY
   1171 ;
   1172 #else
   1173 { return c >= 32 && c < 127; }
   1174 #endif
   1175 int ispunct(int c)
   1176 #ifdef C_DECLS_ONLY
   1177 ;
   1178 #else
   1179 { return isprint(c) && c != ' ' && !isalnum(c); }
   1180 #endif   /* [rifle] */
   1181 int toupper(int c)
   1182 #ifdef C_DECLS_ONLY
   1183 ;
   1184 #else
   1185 { return islower(c) ? c - 32 : c; }
   1186 #endif
   1187 int tolower(int c)
   1188 #ifdef C_DECLS_ONLY
   1189 ;
   1190 #else
   1191 { return isupper(c) ? c + 32 : c; }
   1192 #endif
   1193 
   1194 /* ---- strings ---- */
   1195 size_t strlen(const char *s)
   1196 #ifdef C_DECLS_ONLY
   1197 ;
   1198 #else
   1199 { size_t n = 0; while (s[n]) n++; return n; }
   1200 #endif
   1201 int strcmp(const char *a, const char *b)
   1202 #ifdef C_DECLS_ONLY
   1203 ;
   1204 #else
   1205 { while (*a && *a == *b) a++, b++; return (unsigned char)*a - (unsigned char)*b; }
   1206 #endif
   1207 int strncmp(const char *a, const char *b, size_t n)
   1208 #ifdef C_DECLS_ONLY
   1209 ;
   1210 #else
   1211 {
   1212     for (; n && *a && *a == *b; n--) a++, b++;
   1213     return n ? (unsigned char)*a - (unsigned char)*b : 0;
   1214 }
   1215 #endif
   1216 char *strcpy(char *d, const char *s)
   1217 #ifdef C_DECLS_ONLY
   1218 ;
   1219 #else
   1220 { char *r = d; while ((*d++ = *s++)); return r; }
   1221 #endif
   1222 char *strncpy(char *d, const char *s, size_t n)
   1223 #ifdef C_DECLS_ONLY
   1224 ;
   1225 #else
   1226 { size_t i = 0; for (; i < n && s[i]; i++) d[i] = s[i]; for (; i < n; i++) d[i] = 0; return d; }
   1227 #endif
   1228 char *strcat(char *d, const char *s)
   1229 #ifdef C_DECLS_ONLY
   1230 ;
   1231 #else
   1232 { strcpy(d + strlen(d), s); return d; }
   1233 #endif
   1234 char *strchr(const char *s, int c)
   1235 #ifdef C_DECLS_ONLY
   1236 ;
   1237 #else
   1238 { for (;; s++) { if (*s == (char)c) return (char *)s; if (!*s) return NULL; } }
   1239 #endif
   1240 char *strrchr(const char *s, int c)
   1241 #ifdef C_DECLS_ONLY
   1242 ;
   1243 #else
   1244 { const char *r = NULL; for (;; s++) { if (*s == (char)c) r = s; if (!*s) return (char *)r; } }
   1245 #endif
   1246 char *strstr(const char *h, const char *n)
   1247 #ifdef C_DECLS_ONLY
   1248 ;
   1249 #else
   1250 {
   1251     size_t l = strlen(n);
   1252     for (; *h; h++) if (!strncmp(h, n, l)) return (char *)h;
   1253     return l ? NULL : (char *)h;
   1254 }
   1255 #endif
   1256 char *strpbrk(const char *s, const char *set)
   1257 #ifdef C_DECLS_ONLY
   1258 ;
   1259 #else
   1260 { for (; *s; s++) if (strchr(set, *s)) return (char *)s; return NULL; }
   1261 #endif
   1262 void *memcpy(void *d, const void *s, size_t n)
   1263 #ifdef C_DECLS_ONLY
   1264 ;
   1265 #else
   1266 { char *a = d; const char *b = s; while (n--) *a++ = *b++; return d; }
   1267 #endif
   1268 void *memmove(void *d, const void *s, size_t n)
   1269 #ifdef C_DECLS_ONLY
   1270 ;
   1271 #else
   1272 {
   1273     char *a = d; const char *b = s;
   1274     if (a < b) while (n--) *a++ = *b++;
   1275     else while (n--) a[n] = b[n];
   1276     return d;
   1277 }
   1278 #endif
   1279 void *memset(void *d, int c, size_t n)
   1280 #ifdef C_DECLS_ONLY
   1281 ;
   1282 #else
   1283 { char *a = d; while (n--) *a++ = c; return d; }
   1284 #endif
   1285 int memcmp(const void *x, const void *y, size_t n)
   1286 #ifdef C_DECLS_ONLY
   1287 ;
   1288 #else
   1289 {
   1290     const unsigned char *a = x, *b = y;
   1291     for (; n; n--, a++, b++) if (*a != *b) return *a - *b;
   1292     return 0;
   1293 }
   1294 #endif
   1295 void *memchr(const void *s, int c, size_t n)
   1296 #ifdef C_DECLS_ONLY
   1297 ;
   1298 #else
   1299 { const unsigned char *p = s; for (; n; n--, p++) if (*p == (unsigned char)c) return (void *)p; return NULL; }
   1300 #endif
   1301 /* the messages are glibc's, so what rifle says matches what everyone's scripts expect [rifle] */
   1302 #ifndef C_DECLS_ONLY
   1303 static const struct { int e; const char *m; } c_errlist[] = {
   1304     { EPERM, "Operation not permitted" }, { ENOENT, "No such file or directory" }, { ESRCH, "No such process" },
   1305     { EINTR, "Interrupted system call" }, { EIO, "Input/output error" }, { ENXIO, "No such device or address" },
   1306     { E2BIG, "Argument list too long" }, { ENOEXEC, "Exec format error" }, { EBADF, "Bad file descriptor" },
   1307     { ECHILD, "No child processes" }, { EAGAIN, "Resource temporarily unavailable" }, { ENOMEM, "Cannot allocate memory" },
   1308     { EACCES, "Permission denied" }, { EFAULT, "Bad address" }, { EBUSY, "Device or resource busy" },
   1309     { EEXIST, "File exists" }, { EXDEV, "Invalid cross-device link" }, { ENODEV, "No such device" },
   1310     { ENOTDIR, "Not a directory" }, { EISDIR, "Is a directory" }, { EINVAL, "Invalid argument" },
   1311     { ENFILE, "Too many open files in system" }, { EMFILE, "Too many open files" },
   1312     { ENOTTY, "Inappropriate ioctl for device" }, { ETXTBSY, "Text file busy" }, { EFBIG, "File too large" },
   1313     { ENOSPC, "No space left on device" }, { ESPIPE, "Illegal seek" }, { EROFS, "Read-only file system" },
   1314     { EMLINK, "Too many links" }, { EPIPE, "Broken pipe" }, { EDOM, "Numerical argument out of domain" },
   1315     { ERANGE, "Numerical result out of range" }, { EDEADLK, "Resource deadlock avoided" },
   1316     { ENAMETOOLONG, "File name too long" }, { ENOSYS, "Function not implemented" },
   1317     { ENOTEMPTY, "Directory not empty" }, { ELOOP, "Too many levels of symbolic links" },
   1318     { ENODATA, "No data available" }, { EOVERFLOW, "Value too large for defined data type" },
   1319     { EILSEQ, "Invalid or incomplete multibyte or wide character" }, { EOPNOTSUPP, "Operation not supported" },
   1320     { ETIMEDOUT, "Connection timed out" }, { ESTALE, "Stale file handle" }, { EDQUOT, "Disk quota exceeded" },
   1321 };
   1322 #endif
   1323 #ifdef C_DECLS_ONLY
   1324 extern int snprintf(char *buf, size_t cap, const char *fmt, ...);
   1325 #else
   1326 int snprintf(char *buf, size_t cap, const char *fmt, ...);
   1327 #endif
   1328 char *strerror(int e)
   1329 #ifdef C_DECLS_ONLY
   1330 ;
   1331 #else
   1332 {
   1333     static char unknown[32];
   1334     unsigned i;
   1335     for (i = 0; i < sizeof c_errlist / sizeof c_errlist[0]; i++) if (c_errlist[i].e == e) return (char *)c_errlist[i].m;
   1336     snprintf(unknown, sizeof unknown, "Unknown error %d", e);
   1337     return unknown;
   1338 }
   1339 #endif
   1340 
   1341 /* ---- memory ----
   1342  * [rifle] small blocks (up to 2032 bytes) come from 256K chunks and go on a list for their size when freed, big
   1343  * blocks are their own mmap and grow with mremap. every block has 16 bytes in front of it holding its size. */
   1344 #define MALLOC_BIG 2048
   1345 #ifndef C_DECLS_ONLY
   1346 static void *malloc_lists[12];
   1347 #endif
   1348 #ifndef C_DECLS_ONLY
   1349 static char *malloc_cur, *malloc_end;
   1350 #endif
   1351 #ifndef C_DECLS_ONLY
   1352 static void *c_mmap(size_t len)
   1353 {
   1354     long p = sys6(SYS_mmap, 0, len, PROT_READ | PROT_WRITE, 0x22 /* private, anonymous */, -1, 0);
   1355     return (p < 0 && p > -4096) ? NULL : (void *)p;
   1356 }
   1357 #endif
   1358 void *malloc(size_t n)
   1359 #ifdef C_DECLS_ONLY
   1360 ;
   1361 #else
   1362 {
   1363     size_t need = n + 16;
   1364     if (need < n) { errno = ENOMEM; return NULL; }
   1365     if (need <= MALLOC_BIG) {
   1366         size_t sz = 32; int cl = 5;
   1367         while (sz < need) { sz <<= 1; cl++; }
   1368         char *b = malloc_lists[cl];
   1369         if (b) { malloc_lists[cl] = *(void **)(b + 16); return b + 16; }
   1370         if (malloc_cur + sz > malloc_end) {
   1371             malloc_cur = c_mmap(256 * 1024);
   1372             if (!malloc_cur) { malloc_end = NULL; errno = ENOMEM; return NULL; }
   1373             malloc_end = malloc_cur + 256 * 1024;
   1374         }
   1375         b = malloc_cur; malloc_cur += sz;
   1376         *(size_t *)b = sz;
   1377         return b + 16;
   1378     }
   1379     size_t len = (need + 4095) & ~4095UL;
   1380     if (len < need) { errno = ENOMEM; return NULL; }
   1381     char *p = c_mmap(len);
   1382     if (!p) { errno = ENOMEM; return NULL; }
   1383     *(size_t *)p = len;
   1384     return p + 16; /* 16: what a long double or an SSE value wants */
   1385 }
   1386 #endif
   1387 void free(void *p)
   1388 #ifdef C_DECLS_ONLY
   1389 ;
   1390 #else
   1391 {
   1392     if (!p) return;
   1393     char *h = (char *)p - 16;
   1394     size_t sz = *(size_t *)h;
   1395     if (sz <= MALLOC_BIG) {
   1396         int cl = 5; size_t c = 32;
   1397         while (c < sz) { c <<= 1; cl++; }
   1398         *(void **)p = malloc_lists[cl]; malloc_lists[cl] = h;
   1399     } else sys2(SYS_munmap, h, sz);
   1400 }
   1401 #endif
   1402 void *calloc(size_t n, size_t m)
   1403 #ifdef C_DECLS_ONLY
   1404 ;
   1405 #else
   1406 {
   1407     size_t total = n * m;
   1408     if (m && total / m != n) { errno = ENOMEM; return NULL; }
   1409     void *p = malloc(total);
   1410     if (p && total + 16 <= MALLOC_BIG) memset(p, 0, total); /* big blocks come from mmap, already zero */
   1411     return p;
   1412 }
   1413 #endif
   1414 void *realloc(void *p, size_t n)
   1415 #ifdef C_DECLS_ONLY
   1416 ;
   1417 #else
   1418 {
   1419     if (!p) return malloc(n);
   1420     char *h = (char *)p - 16;
   1421     size_t sz = *(size_t *)h, old = sz - 16;
   1422     if (n <= old) return p;
   1423     if (sz > MALLOC_BIG && n + 16 > n) {
   1424         size_t len = (n + 16 + 4095) & ~4095UL;
   1425         long q = sys6(SYS_mremap, (long)h, sz, len, 1 /* may move */, 0, 0);
   1426         if (q < 0 && q > -4096) { errno = ENOMEM; return NULL; }
   1427         *(size_t *)q = len;
   1428         return (char *)q + 16;
   1429     }
   1430     void *q = malloc(n);
   1431     if (q) { memcpy(q, p, old); free(p); }
   1432     return q;
   1433 }
   1434 #endif
   1435 size_t strspn(const char *s, const char *set)
   1436 #ifdef C_DECLS_ONLY
   1437 ;
   1438 #else
   1439 { size_t n = 0; while (s[n] && strchr(set, s[n])) n++; return n; }
   1440 #endif  /* [rifle] */
   1441 size_t strcspn(const char *s, const char *set)
   1442 #ifdef C_DECLS_ONLY
   1443 ;
   1444 #else
   1445 { size_t n = 0; while (s[n] && !strchr(set, s[n])) n++; return n; }
   1446 #endif
   1447 int strcasecmp(const char *a, const char *b)
   1448 #ifdef C_DECLS_ONLY
   1449 ;
   1450 #else
   1451 { while (*a && tolower(*a) == tolower(*b)) a++, b++; return tolower((unsigned char)*a) - tolower((unsigned char)*b); }
   1452 #endif
   1453 int strncasecmp(const char *a, const char *b, size_t n)
   1454 #ifdef C_DECLS_ONLY
   1455 ;
   1456 #else
   1457 {
   1458     for (; n && *a && tolower(*a) == tolower(*b); n--) a++, b++;
   1459     return n ? tolower((unsigned char)*a) - tolower((unsigned char)*b) : 0;
   1460 }
   1461 #endif
   1462 void *memrchr(const void *s, int c, size_t n)
   1463 #ifdef C_DECLS_ONLY
   1464 ;
   1465 #else
   1466 { const unsigned char *p = (const unsigned char *)s + n; while (n--) if (*--p == (unsigned char)c) return (void *)p; return NULL; }
   1467 #endif
   1468 char *strndup(const char *s, size_t n)
   1469 #ifdef C_DECLS_ONLY
   1470 ;
   1471 #else
   1472 { size_t l = 0; char *d; while (l < n && s[l]) l++; d = malloc(l + 1); if (!d) return NULL; memcpy(d, s, l); d[l] = 0; return d; }
   1473 #endif
   1474 char *stpcpy(char *d, const char *s)
   1475 #ifdef C_DECLS_ONLY
   1476 ;
   1477 #else
   1478 { while ((*d = *s++)) d++; return d; }
   1479 #endif
   1480 int abs(int x)
   1481 #ifdef C_DECLS_ONLY
   1482 ;
   1483 #else
   1484 { return x < 0 ? -x : x; }
   1485 #endif
   1486 long labs(long x)
   1487 #ifdef C_DECLS_ONLY
   1488 ;
   1489 #else
   1490 { return x < 0 ? -x : x; }
   1491 #endif
   1492 char *strdup(const char *s)
   1493 #ifdef C_DECLS_ONLY
   1494 ;
   1495 #else
   1496 { char *d = malloc(strlen(s) + 1); return d ? strcpy(d, s) : NULL; }
   1497 #endif
   1498 
   1499 /* ---- numbers from text ---- */
   1500 #ifndef C_DECLS_ONLY
   1501 static int digit_of(int c) { return isdigit(c) ? c - '0' : isalpha(c) ? (c | 32) - 'a' + 10 : 99; }
   1502 #endif
   1503 unsigned long long strtoull(const char *s, char **end, int base)
   1504 #ifdef C_DECLS_ONLY
   1505 ;
   1506 #else
   1507 {
   1508     const char *p = s; unsigned long long v = 0; int neg = 0, any = 0;
   1509     while (isspace(*p)) p++;
   1510     if (*p == '-' || *p == '+') neg = *p++ == '-';
   1511     if ((base == 0 || base == 16) && p[0] == '0' && (p[1] | 32) == 'x' && isxdigit(p[2])) { p += 2; base = 16; }
   1512     else if (base == 0) base = *p == '0' ? 8 : 10;
   1513     for (; digit_of(*p) < base; p++, any = 1) v = v * base + digit_of(*p);
   1514     if (end) *end = (char *)(any ? p : s);
   1515     return neg ? -v : v;
   1516 }
   1517 #endif
   1518 long long strtoll(const char *s, char **end, int base)
   1519 #ifdef C_DECLS_ONLY
   1520 ;
   1521 #else
   1522 { return (long long)strtoull(s, end, base); }
   1523 #endif
   1524 unsigned long strtoul(const char *s, char **end, int base)
   1525 #ifdef C_DECLS_ONLY
   1526 ;
   1527 #else
   1528 { return strtoull(s, end, base); }
   1529 #endif
   1530 long strtol(const char *s, char **end, int base)
   1531 #ifdef C_DECLS_ONLY
   1532 ;
   1533 #else
   1534 { return strtoull(s, end, base); }
   1535 #endif
   1536 int atoi(const char *s)
   1537 #ifdef C_DECLS_ONLY
   1538 ;
   1539 #else
   1540 { return strtol(s, NULL, 10); }
   1541 #endif
   1542 long atol(const char *s)
   1543 #ifdef C_DECLS_ONLY
   1544 ;
   1545 #else
   1546 { return strtol(s, NULL, 10); }
   1547 #endif
   1548 long long atoll(const char *s)
   1549 #ifdef C_DECLS_ONLY
   1550 ;
   1551 #else
   1552 { return strtoll(s, NULL, 10); }
   1553 #endif
   1554 
   1555 long double ldexpl(long double x, int e)
   1556 #ifdef C_DECLS_ONLY
   1557 ;
   1558 #else
   1559 {
   1560     while (e > 0) { x *= 2; e--; }
   1561     while (e < 0) { x /= 2; e++; }
   1562     return x;
   1563 }
   1564 #endif
   1565 double ldexp(double x, int e)
   1566 #ifdef C_DECLS_ONLY
   1567 ;
   1568 #else
   1569 { return ldexpl(x, e); }
   1570 #endif
   1571 /* decimal and hex floating point. good to the last digit or so, not correctly rounded in every case */
   1572 long double strtold(const char *s, char **end)
   1573 #ifdef C_DECLS_ONLY
   1574 ;
   1575 #else
   1576 {
   1577     const char *p = s; long double v = 0; int neg = 0, any = 0, ex = 0;
   1578     while (isspace(*p)) p++;
   1579     if (*p == '-' || *p == '+') neg = *p++ == '-';
   1580     if (p[0] == '0' && (p[1] | 32) == 'x') {
   1581         p += 2;
   1582         for (; isxdigit(*p); p++, any = 1) v = v * 16 + digit_of(*p);
   1583         if (*p == '.') for (p++; isxdigit(*p); p++, any = 1) { v = v * 16 + digit_of(*p); ex -= 4; }
   1584         if (any && (*p | 32) == 'p') { char *e2; long x = strtol(p + 1, &e2, 10); if (e2 != p + 1) { ex += x; p = e2; } }
   1585         v = ldexpl(v, ex);
   1586     } else {
   1587         for (; isdigit(*p); p++, any = 1) v = v * 10 + (*p - '0');
   1588         if (*p == '.') for (p++; isdigit(*p); p++, any = 1) { v = v * 10 + (*p - '0'); ex--; }
   1589         if (any && (*p | 32) == 'e') { char *e2; long x = strtol(p + 1, &e2, 10); if (e2 != p + 1) { ex += x; p = e2; } }
   1590         for (; ex > 0; ex--) v *= 10;
   1591         for (; ex < 0; ex++) v /= 10;
   1592     }
   1593     if (end) *end = (char *)(any ? p : s);
   1594     return neg ? -v : v;
   1595 }
   1596 #endif
   1597 double strtod(const char *s, char **end)
   1598 #ifdef C_DECLS_ONLY
   1599 ;
   1600 #else
   1601 { return strtold(s, end); }
   1602 #endif
   1603 float strtof(const char *s, char **end)
   1604 #ifdef C_DECLS_ONLY
   1605 ;
   1606 #else
   1607 { return strtold(s, end); }
   1608 #endif
   1609 double atof(const char *s)
   1610 #ifdef C_DECLS_ONLY
   1611 ;
   1612 #else
   1613 { return strtod(s, NULL); }
   1614 #endif
   1615 
   1616 /* ---- sorting ---- */
   1617 #ifndef C_DECLS_ONLY
   1618 static void swap_bytes(char *a, char *b, size_t n) { while (n--) { char t = *a; *a++ = *b; *b++ = t; } }
   1619 #endif
   1620 void qsort(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *))
   1621 #ifdef C_DECLS_ONLY
   1622 ;
   1623 #else
   1624 {
   1625     /* shell sort: short, no recursion, fine for the sizes a compiler sorts */
   1626     char *b = base; size_t gap, i, j;
   1627     for (gap = n / 2; gap; gap /= 2)
   1628         for (i = gap; i < n; i++)
   1629             for (j = i; j >= gap && cmp(b + (j - gap) * size, b + j * size) > 0; j -= gap)
   1630                 swap_bytes(b + (j - gap) * size, b + j * size, size);
   1631 }
   1632 #endif
   1633 
   1634 /* ---- jumps ---- */
   1635 typedef long jmp_buf[8];
   1636 #ifdef C_DECLS_ONLY
   1637 extern int setjmp(jmp_buf b);
   1638 #else
   1639 int setjmp(jmp_buf b);
   1640 #endif
   1641 #ifdef C_DECLS_ONLY
   1642 extern __attribute__((noreturn)) void longjmp(jmp_buf b, int v);
   1643 #else
   1644 __attribute__((noreturn)) void longjmp(jmp_buf b, int v);
   1645 #endif
   1646 #ifdef __i386__
   1647 #ifndef C_DECLS_ONLY
   1648 __asm__(".text\n.global setjmp\n.global _setjmp\nsetjmp:\n_setjmp:\n"
   1649         " mov 4(%esp),%eax\n mov %ebx,(%eax)\n mov %esi,4(%eax)\n mov %edi,8(%eax)\n mov %ebp,12(%eax)\n"
   1650         " lea 4(%esp),%ecx\n mov %ecx,16(%eax)\n mov (%esp),%ecx\n mov %ecx,20(%eax)\n xor %eax,%eax\n ret\n"
   1651         ".global longjmp\nlongjmp:\n"
   1652         " mov 4(%esp),%edx\n mov 8(%esp),%eax\n test %eax,%eax\n jnz 1f\n inc %eax\n1:\n"
   1653         " mov (%edx),%ebx\n mov 4(%edx),%esi\n mov 8(%edx),%edi\n mov 12(%edx),%ebp\n mov 16(%edx),%esp\n jmp *20(%edx)\n");
   1654 #endif
   1655 #else
   1656 #ifndef C_DECLS_ONLY
   1657 __asm__(".text\n.global setjmp\n.global _setjmp\nsetjmp:\n_setjmp:\n"
   1658         " mov %rbx,(%rdi)\n mov %rbp,8(%rdi)\n mov %r12,16(%rdi)\n mov %r13,24(%rdi)\n mov %r14,32(%rdi)\n"
   1659         " mov %r15,40(%rdi)\n lea 8(%rsp),%rdx\n mov %rdx,48(%rdi)\n mov (%rsp),%rdx\n mov %rdx,56(%rdi)\n"
   1660         " xor %eax,%eax\n ret\n"
   1661         ".global longjmp\nlongjmp:\n"
   1662         " mov %esi,%eax\n test %eax,%eax\n jnz 1f\n inc %eax\n1:\n"
   1663         " mov (%rdi),%rbx\n mov 8(%rdi),%rbp\n mov 16(%rdi),%r12\n mov 24(%rdi),%r13\n mov 32(%rdi),%r14\n"
   1664         " mov 40(%rdi),%r15\n mov 48(%rdi),%rsp\n jmp *56(%rdi)\n");
   1665 #endif
   1666 #endif
   1667 
   1668 /* ---- time ---- */
   1669 struct tm { int tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst; };
   1670 /* there are no time zones here: local time is UTC */
   1671 struct tm *localtime(const time_t *t)
   1672 #ifdef C_DECLS_ONLY
   1673 ;
   1674 #else
   1675 {
   1676     static struct tm r;
   1677     long s = *t, days = s / 86400, rem = s % 86400, y = 1970;
   1678     static const int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
   1679     r.tm_hour = rem / 3600; r.tm_min = rem / 60 % 60; r.tm_sec = rem % 60;
   1680     r.tm_wday = (days + 4) % 7;
   1681     for (;;) {
   1682         int len = (y % 4 == 0 && (y % 100 || y % 400 == 0)) ? 366 : 365;
   1683         if (days < len) break;
   1684         days -= len; y++;
   1685     }
   1686     r.tm_year = y - 1900; r.tm_yday = days;
   1687     for (r.tm_mon = 0; ; r.tm_mon++) {
   1688         int len = md[r.tm_mon] + (r.tm_mon == 1 && (y % 4 == 0 && (y % 100 || y % 400 == 0)));
   1689         if (days < len) break;
   1690         days -= len;
   1691     }
   1692     r.tm_mday = days + 1; r.tm_isdst = 0;
   1693     return &r;
   1694 }
   1695 #endif
   1696 struct tm *gmtime(const time_t *t)
   1697 #ifdef C_DECLS_ONLY
   1698 ;
   1699 #else
   1700 { return localtime(t); }
   1701 #endif
   1702 struct timeval { long tv_sec, tv_usec; };
   1703 int gettimeofday(struct timeval *tv, void *tz)
   1704 #ifdef C_DECLS_ONLY
   1705 ;
   1706 #else
   1707 { (void)tz; return fail(sys2(SYS_gettimeofday, tv, 0)); }
   1708 #endif
   1709 
   1710 /* ---- files with buffers ----
   1711  * a FILE reads or writes through one buffer. write: n bytes wait to go out. read: [pos, n) is what is left.
   1712  * ug is a pushed back character plus one, mode is 0 full, 1 line, 2 none buffered, 3 not asked yet */
   1713 typedef struct FILE { int fd, write, err, eof, n, pos, line, ug, mode, rd, wr, pid, append; struct FILE *next; char buf[4096]; } FILE;
   1714 typedef long fpos_t;
   1715 #define _IOFBF 0
   1716 #define _IOLBF 1
   1717 #define _IONBF 2
   1718 #ifndef C_DECLS_ONLY
   1719 static FILE files_std[3] = { { 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0 }, { 1, 1, 0, 0, 0, 0, 0, 0, 3, 0, 1 }, { 2, 1, 0, 0, 0, 0, 0, 0, 2, 0, 1 } };
   1720 #endif
   1721 #ifndef C_DECLS_ONLY
   1722 static FILE *c_files;
   1723 #endif
   1724 /*nosplit*/
   1725 #ifdef C_DECLS_ONLY
   1726 extern FILE *stdin, *stdout, *stderr;
   1727 #else
   1728 #ifdef C_DECLS_ONLY
   1729 extern FILE *stdin;
   1730 #else
   1731 FILE *stdin = &files_std[0], *stdout = &files_std[1], *stderr = &files_std[2];
   1732 #endif
   1733 #endif
   1734 #define EOF (-1)
   1735 #define BUFSIZ 4096
   1736 #define FILENAME_MAX 4096
   1737 #define FOPEN_MAX 16
   1738 #define L_tmpnam 20
   1739 #define TMP_MAX 238328
   1740 
   1741 #ifndef C_DECLS_ONLY
   1742 static int flush_one(FILE *f)
   1743 {
   1744     if (f->write && f->n) {
   1745         int off = 0;
   1746         while (off < f->n) { ssize_t w = write(f->fd, f->buf + off, f->n - off); if (w <= 0) { if (w < 0 && errno == EINTR) continue; f->err = 1; break; } off += w; }
   1747         f->n = 0;
   1748     }
   1749     return f->err ? EOF : 0;
   1750 }
   1751 #endif
   1752 int fflush(FILE *f)
   1753 #ifdef C_DECLS_ONLY
   1754 ;
   1755 #else
   1756 {
   1757     if (!f) {
   1758         FILE *p;
   1759         int r = 0;
   1760         for (p = c_files; p; p = p->next) if (flush_one(p)) r = EOF;
   1761         for (p = files_std; p < files_std + 3; p++) if (flush_one(p)) r = EOF;
   1762         return r;
   1763     }
   1764     if (!f->write && f->n > f->pos && f->rd && !f->wr) return 0;
   1765     return flush_one(f);
   1766 }
   1767 #endif
   1768 /* going from writing to reading, or back: whatever is buffered goes first, or the file position is put right */
   1769 #ifndef C_DECLS_ONLY
   1770 static int switch_to(FILE *f, int writing)
   1771 {
   1772     if (writing && !f->write) {
   1773         if (f->n > f->pos || f->ug) lseek(f->fd, -(long)(f->n - f->pos) - (f->ug ? 1 : 0), SEEK_CUR);
   1774         f->n = f->pos = 0; f->ug = 0; f->write = 1;
   1775     } else if (!writing && f->write) {
   1776         if (flush_one(f)) return -1;
   1777         f->n = f->pos = 0; f->write = 0;
   1778     }
   1779     return 0;
   1780 }
   1781 #endif
   1782 #ifndef C_DECLS_ONLY
   1783 static int open_flags(const char *mode)
   1784 {
   1785     int f = mode[0] == 'r' ? O_RDONLY : mode[0] == 'w' ? O_WRONLY | O_CREAT | O_TRUNC : O_WRONLY | O_CREAT | O_APPEND;
   1786     const char *m;
   1787     if (strchr(mode, '+')) f = (f & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
   1788     for (m = mode; *m; m++) { if (*m == 'x') f |= O_EXCL; else if (*m == 'e') f |= O_CLOEXEC; }
   1789     return f;
   1790 }
   1791 #endif
   1792 FILE *fdopen(int fd, const char *mode)
   1793 #ifdef C_DECLS_ONLY
   1794 ;
   1795 #else
   1796 {
   1797     FILE *f = calloc(1, sizeof *f);
   1798     if (!f) return NULL;
   1799     f->fd = fd; f->mode = 3;
   1800     f->rd = mode[0] == 'r' || strchr(mode, '+') != NULL;
   1801     f->wr = mode[0] != 'r' || strchr(mode, '+') != NULL;
   1802     f->write = !f->rd;
   1803     f->append = mode[0] == 'a';
   1804     f->next = c_files; c_files = f;
   1805     return f;
   1806 }
   1807 #endif
   1808 FILE *fopen(const char *path, const char *mode)
   1809 #ifdef C_DECLS_ONLY
   1810 ;
   1811 #else
   1812 {
   1813     int fd;
   1814     if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { errno = EINVAL; return NULL; }
   1815     fd = open(path, open_flags(mode), 0666);
   1816     return fd < 0 ? NULL : fdopen(fd, mode);
   1817 }
   1818 #endif
   1819 FILE *freopen(const char *path, const char *mode, FILE *f)
   1820 #ifdef C_DECLS_ONLY
   1821 ;
   1822 #else
   1823 {
   1824     int fd;
   1825     fflush(f);
   1826     if (!path) return f;
   1827     if ((fd = open(path, open_flags(mode), 0666)) < 0) return NULL;
   1828     dup2(fd, f->fd); close(fd);
   1829     f->rd = mode[0] == 'r' || strchr(mode, '+') != NULL;
   1830     f->wr = mode[0] != 'r' || strchr(mode, '+') != NULL;
   1831     f->write = !f->rd; f->n = f->pos = f->err = f->eof = f->ug = 0;
   1832     return f;
   1833 }
   1834 #endif
   1835 int fclose(FILE *f)
   1836 #ifdef C_DECLS_ONLY
   1837 ;
   1838 #else
   1839 {
   1840     int r = flush_one(f);
   1841     FILE **pp;
   1842     close(f->fd);
   1843     for (pp = &c_files; *pp; pp = &(*pp)->next) if (*pp == f) { *pp = f->next; break; }
   1844     if (f < files_std || f > files_std + 2) free(f);
   1845     return r;
   1846 }
   1847 #endif
   1848 int setvbuf(FILE *f, char *buf, int mode, size_t size)
   1849 #ifdef C_DECLS_ONLY
   1850 ;
   1851 #else
   1852 { (void)buf; (void)size; flush_one(f); f->mode = mode; return 0; }
   1853 #endif
   1854 void setbuf(FILE *f, char *buf)
   1855 #ifdef C_DECLS_ONLY
   1856 ;
   1857 #else
   1858 { setvbuf(f, buf, buf ? _IOFBF : _IONBF, BUFSIZ); }
   1859 #endif
   1860 void setbuffer(FILE *f, char *buf, size_t size)
   1861 #ifdef C_DECLS_ONLY
   1862 ;
   1863 #else
   1864 { setvbuf(f, buf, buf ? _IOFBF : _IONBF, size); }
   1865 #endif
   1866 void setlinebuf(FILE *f)
   1867 #ifdef C_DECLS_ONLY
   1868 ;
   1869 #else
   1870 { setvbuf(f, NULL, _IOLBF, 0); }
   1871 #endif
   1872 /* stdout is flushed at every newline when it's a terminal, and when it's a pipe or a file only when full, like
   1873  * everything else does. stderr always goes out at once [rifle] */
   1874 size_t fwrite(const void *p, size_t size, size_t n, FILE *f)
   1875 #ifdef C_DECLS_ONLY
   1876 ;
   1877 #else
   1878 {
   1879     const char *s = p; size_t len = size * n, left = len;
   1880     if (size && len / size != n) return 0;
   1881     if (!f->wr && f->rd) { f->err = 1; errno = EBADF; return 0; }
   1882     if (switch_to(f, 1)) return 0;
   1883     if (f->append) lseek(f->fd, 0, SEEK_END);
   1884     while (left) {
   1885         size_t room = BUFSIZ - f->n, c = left < room ? left : room;
   1886         memcpy(f->buf + f->n, s, c);
   1887         f->n += c; s += c; left -= c;
   1888         if (f->n == BUFSIZ) flush_one(f);
   1889     }
   1890     if (f->mode == 3) f->mode = f == stdout ? (isatty(1) ? _IOLBF : _IOFBF) : _IOFBF;
   1891     if (f->mode == _IONBF) flush_one(f);
   1892     else if (f->mode == _IOLBF && len && memchr(p, '\n', len)) flush_one(f);
   1893     return f->err ? 0 : n;
   1894 }
   1895 #endif
   1896 int fputc(int c, FILE *f)
   1897 #ifdef C_DECLS_ONLY
   1898 ;
   1899 #else
   1900 { unsigned char ch = c; return fwrite(&ch, 1, 1, f) ? ch : EOF; }
   1901 #endif
   1902 int putc(int c, FILE *f)
   1903 #ifdef C_DECLS_ONLY
   1904 ;
   1905 #else
   1906 { return fputc(c, f); }
   1907 #endif
   1908 int putchar(int c)
   1909 #ifdef C_DECLS_ONLY
   1910 ;
   1911 #else
   1912 { return fputc(c, stdout); }
   1913 #endif
   1914 int fputs(const char *s, FILE *f)
   1915 #ifdef C_DECLS_ONLY
   1916 ;
   1917 #else
   1918 { size_t l = strlen(s); return l && !fwrite(s, 1, l, f) ? EOF : 0; }
   1919 #endif
   1920 int puts(const char *s)
   1921 #ifdef C_DECLS_ONLY
   1922 ;
   1923 #else
   1924 { if (fputs(s, stdout) == EOF) return EOF; return putchar('\n') == EOF ? EOF : 0; }
   1925 #endif
   1926 #ifndef C_DECLS_ONLY
   1927 static int fill(FILE *f)
   1928 {
   1929     ssize_t r;
   1930     if (!f->rd && f->wr) { f->err = 1; errno = EBADF; return EOF; }
   1931     if (switch_to(f, 0)) return EOF;
   1932     if (f == stdin) fflush(stdout);
   1933     do r = read(f->fd, f->buf, BUFSIZ); while (r < 0 && errno == EINTR);
   1934     if (r <= 0) { if (r < 0) f->err = 1; else f->eof = 1; f->n = f->pos = 0; return EOF; }
   1935     f->n = r; f->pos = 0;
   1936     return 0;
   1937 }
   1938 #endif
   1939 int fgetc(FILE *f)
   1940 #ifdef C_DECLS_ONLY
   1941 ;
   1942 #else
   1943 {
   1944     if (f->ug) { int c = f->ug - 1; f->ug = 0; return c; }
   1945     if (f->write || f->pos == f->n) { if (fill(f) == EOF) return EOF; }
   1946     return (unsigned char)f->buf[f->pos++];
   1947 }
   1948 #endif
   1949 int ungetc(int c, FILE *f)
   1950 #ifdef C_DECLS_ONLY
   1951 ;
   1952 #else
   1953 {
   1954     if (c == EOF || f->ug) return EOF;
   1955     if (f->write && switch_to(f, 0)) return EOF;
   1956     f->ug = (unsigned char)c + 1; f->eof = 0;
   1957     return (unsigned char)c;
   1958 }
   1959 #endif
   1960 int getc(FILE *f)
   1961 #ifdef C_DECLS_ONLY
   1962 ;
   1963 #else
   1964 { return fgetc(f); }
   1965 #endif
   1966 int getchar(void)
   1967 #ifdef C_DECLS_ONLY
   1968 ;
   1969 #else
   1970 { return fgetc(stdin); }
   1971 #endif
   1972 size_t fread(void *p, size_t size, size_t n, FILE *f)
   1973 #ifdef C_DECLS_ONLY
   1974 ;
   1975 #else
   1976 {
   1977     char *d = p; size_t len = size * n, i = 0; int c;
   1978     if (size && len / size != n) return 0;
   1979     while (i < len) {
   1980         if (!f->ug && !f->write && f->pos < f->n) {
   1981             size_t c2 = f->n - f->pos; if (c2 > len - i) c2 = len - i;
   1982             memcpy(d + i, f->buf + f->pos, c2); f->pos += c2; i += c2;
   1983         } else if ((c = fgetc(f)) == EOF) break;
   1984         else d[i++] = c;
   1985     }
   1986     return size ? i / size : 0;
   1987 }
   1988 #endif
   1989 char *fgets(char *s, int n, FILE *f)
   1990 #ifdef C_DECLS_ONLY
   1991 ;
   1992 #else
   1993 {
   1994     int i = 0, c;
   1995     if (n < 1) return NULL;
   1996     while (i < n - 1 && (c = fgetc(f)) != EOF) { s[i++] = c; if (c == '\n') break; }
   1997     s[i] = 0;
   1998     return i ? s : NULL;
   1999 }
   2000 #endif
   2001 int feof(FILE *f)
   2002 #ifdef C_DECLS_ONLY
   2003 ;
   2004 #else
   2005 { return f->eof; }
   2006 #endif
   2007 int ferror(FILE *f)
   2008 #ifdef C_DECLS_ONLY
   2009 ;
   2010 #else
   2011 { return f->err; }
   2012 #endif
   2013 void clearerr(FILE *f)
   2014 #ifdef C_DECLS_ONLY
   2015 ;
   2016 #else
   2017 { f->err = f->eof = 0; }
   2018 #endif
   2019 int fileno(FILE *f)
   2020 #ifdef C_DECLS_ONLY
   2021 ;
   2022 #else
   2023 { return f->fd; }
   2024 #endif
   2025 int fseek(FILE *f, long off, int whence)
   2026 #ifdef C_DECLS_ONLY
   2027 ;
   2028 #else
   2029 {
   2030     if (f->write) { if (flush_one(f)) return -1; }
   2031     else if (whence == SEEK_CUR) off -= (f->n - f->pos) + (f->ug ? 1 : 0);
   2032     f->n = f->pos = 0; f->eof = 0; f->ug = 0;
   2033     return lseek(f->fd, off, whence) < 0 ? -1 : 0;
   2034 }
   2035 #endif
   2036 long ftell(FILE *f)
   2037 #ifdef C_DECLS_ONLY
   2038 ;
   2039 #else
   2040 {
   2041     long p = lseek(f->fd, 0, SEEK_CUR);
   2042     return p < 0 ? -1 : f->write ? p + f->n : p - (f->n - f->pos) - (f->ug ? 1 : 0);
   2043 }
   2044 #endif
   2045 int fseeko(FILE *f, off_t off, int whence)
   2046 #ifdef C_DECLS_ONLY
   2047 ;
   2048 #else
   2049 { return fseek(f, off, whence); }
   2050 #endif
   2051 off_t ftello(FILE *f)
   2052 #ifdef C_DECLS_ONLY
   2053 ;
   2054 #else
   2055 { return ftell(f); }
   2056 #endif
   2057 void rewind(FILE *f)
   2058 #ifdef C_DECLS_ONLY
   2059 ;
   2060 #else
   2061 { fseek(f, 0, SEEK_SET); f->err = 0; }
   2062 #endif
   2063 int fgetpos(FILE *f, fpos_t *p)
   2064 #ifdef C_DECLS_ONLY
   2065 ;
   2066 #else
   2067 { long v = ftell(f); if (v < 0) return -1; *p = v; return 0; }
   2068 #endif
   2069 int fsetpos(FILE *f, const fpos_t *p)
   2070 #ifdef C_DECLS_ONLY
   2071 ;
   2072 #else
   2073 { return fseek(f, *p, SEEK_SET); }
   2074 #endif
   2075 int remove(const char *path)
   2076 #ifdef C_DECLS_ONLY
   2077 ;
   2078 #else
   2079 { if (unlink(path) == 0) return 0; if (errno == EISDIR || errno == EPERM) return rmdir(path); return -1; }
   2080 #endif
   2081 void flockfile(FILE *f)
   2082 #ifdef C_DECLS_ONLY
   2083 ;
   2084 #else
   2085 { (void)f; }
   2086 #endif
   2087 void funlockfile(FILE *f)
   2088 #ifdef C_DECLS_ONLY
   2089 ;
   2090 #else
   2091 { (void)f; }
   2092 #endif
   2093 int ftrylockfile(FILE *f)
   2094 #ifdef C_DECLS_ONLY
   2095 ;
   2096 #else
   2097 { (void)f; return 0; }
   2098 #endif
   2099 int getc_unlocked(FILE *f)
   2100 #ifdef C_DECLS_ONLY
   2101 ;
   2102 #else
   2103 { return fgetc(f); }
   2104 #endif
   2105 int getchar_unlocked(void)
   2106 #ifdef C_DECLS_ONLY
   2107 ;
   2108 #else
   2109 { return fgetc(stdin); }
   2110 #endif
   2111 int putc_unlocked(int c, FILE *f)
   2112 #ifdef C_DECLS_ONLY
   2113 ;
   2114 #else
   2115 { return fputc(c, f); }
   2116 #endif
   2117 int putchar_unlocked(int c)
   2118 #ifdef C_DECLS_ONLY
   2119 ;
   2120 #else
   2121 { return fputc(c, stdout); }
   2122 #endif
   2123 size_t fread_unlocked(void *p, size_t s, size_t n, FILE *f)
   2124 #ifdef C_DECLS_ONLY
   2125 ;
   2126 #else
   2127 { return fread(p, s, n, f); }
   2128 #endif
   2129 size_t fwrite_unlocked(const void *p, size_t s, size_t n, FILE *f)
   2130 #ifdef C_DECLS_ONLY
   2131 ;
   2132 #else
   2133 { return fwrite(p, s, n, f); }
   2134 #endif
   2135 int fflush_unlocked(FILE *f)
   2136 #ifdef C_DECLS_ONLY
   2137 ;
   2138 #else
   2139 { return fflush(f); }
   2140 #endif
   2141 int feof_unlocked(FILE *f)
   2142 #ifdef C_DECLS_ONLY
   2143 ;
   2144 #else
   2145 { return f->eof; }
   2146 #endif
   2147 int ferror_unlocked(FILE *f)
   2148 #ifdef C_DECLS_ONLY
   2149 ;
   2150 #else
   2151 { return f->err; }
   2152 #endif
   2153 void perror(const char *s)
   2154 #ifdef C_DECLS_ONLY
   2155 ;
   2156 #else
   2157 { fflush(stdout); if (s && *s) { fputs(s, stderr); fputs(": ", stderr); } fputs(strerror(errno), stderr); fputc('\n', stderr); }
   2158 #endif
   2159 
   2160 /* ---- printf ----
   2161  * flags - + space # 0, width and precision (numbers or *), sizes hh h l ll z j t L,
   2162  * conversions d i u o x X c s p f F e E g G and %% */
   2163 struct out { char *buf; size_t cap, len; FILE *f; };
   2164 #ifndef C_DECLS_ONLY
   2165 static void out_c(struct out *o, char c)
   2166 {
   2167     if (o->f) fputc(c, o->f);
   2168     else if (o->len + 1 < o->cap) o->buf[o->len] = c;
   2169     o->len++;
   2170 }
   2171 #endif
   2172 #ifndef C_DECLS_ONLY
   2173 static void out_pad(struct out *o, char c, int n) { while (n-- > 0) out_c(o, c); }
   2174 #endif
   2175 #ifndef C_DECLS_ONLY
   2176 static void out_field(struct out *o, const char *pre, const char *s, int len, int width, int left, int zero)
   2177 {
   2178     int plen = strlen(pre), pad = width - plen - len;
   2179     if (!left && !zero) out_pad(o, ' ', pad);
   2180     while (*pre) out_c(o, *pre++);
   2181     if (!left && zero) out_pad(o, '0', pad);
   2182     while (len-- > 0) out_c(o, *s++);
   2183     if (left) out_pad(o, ' ', pad);
   2184 }
   2185 #endif
   2186 /* a long double as digits: fixed (f), exponent (e) or the shorter of both (g) */
   2187 #ifndef C_DECLS_ONLY
   2188 static int fmt_float(char *b, long double v, int prec, char conv, int alt)
   2189 {
   2190     int n = 0, e = 0, i, upper = conv == 'E' || conv == 'G' || conv == 'F';
   2191     char c = conv | 32;
   2192     if (v != v) { strcpy(b, upper ? "NAN" : "nan"); return 3; }
   2193     if (v > 1e4900L || v < -1e4900L) { strcpy(b, upper ? "INF" : "inf"); return 3; }
   2194     if (prec < 0) prec = 6;
   2195     if (v != 0) { long double a = v < 0 ? -v : v; while (a >= 10) { a /= 10; e++; } while (a < 1) { a *= 10; e--; } }
   2196     if (c == 'g') {
   2197         if (prec == 0) prec = 1;
   2198         c = (e < -4 || e >= prec) ? 'e' : 'f';
   2199         prec = c == 'e' ? prec - 1 : prec - 1 - e;
   2200         if (prec < 0) prec = 0;
   2201         if (!alt) alt = -1; /* -1: drop the trailing zeros */
   2202     }
   2203     long double a = v < 0 ? -v : v, r = 0.5L;
   2204     if (c == 'e') { if (v != 0) { for (i = 0; i < e; i++) a /= 10; for (i = 0; i > e; i--) a *= 10; } }
   2205     for (i = 0; i < prec; i++) r /= 10;
   2206     a += r;
   2207     if (c == 'e' && a >= 10) { a /= 10; e++; }
   2208     long double ip = 1; int idig = 1;
   2209     while (ip * 10 <= a) { ip *= 10; idig++; }
   2210     for (i = 0; i < idig; i++) { int d = (int)(a / ip); if (d > 9) d = 9; b[n++] = '0' + d; a -= d * ip; ip /= 10; }
   2211     if (prec > 0 || alt > 0) b[n++] = '.';
   2212     for (i = 0; i < prec; i++) { a *= 10; int d = (int)a; if (d > 9) d = 9; b[n++] = '0' + d; a -= d; }
   2213     if (alt < 0 && prec > 0) { while (b[n - 1] == '0') n--; if (b[n - 1] == '.') n--; }
   2214     if (c == 'e') {
   2215         b[n++] = upper ? 'E' : 'e'; b[n++] = e < 0 ? '-' : '+';
   2216         if (e < 0) e = -e;
   2217         if (e >= 100) b[n++] = '0' + e / 100;
   2218         b[n++] = '0' + e / 10 % 10; b[n++] = '0' + e % 10;
   2219     }
   2220     b[n] = 0;
   2221     return n;
   2222 }
   2223 #endif
   2224 #ifndef C_DECLS_ONLY
   2225 static int format(struct out *o, const char *fmt, va_list ap)
   2226 {
   2227     for (; *fmt; fmt++) {
   2228         if (*fmt != '%') { out_c(o, *fmt); continue; }
   2229         int left = 0, plus = 0, space = 0, alt = 0, zero = 0, width = 0, prec = -1, size = 0;
   2230         for (;; fmt++) {
   2231             if (fmt[1] == '-') left = 1; else if (fmt[1] == '+') plus = 1; else if (fmt[1] == ' ') space = 1;
   2232             else if (fmt[1] == '#') alt = 1; else if (fmt[1] == '0') zero = 1; else break;
   2233         }
   2234         fmt++;
   2235         if (*fmt == '*') { width = va_arg(ap, int); if (width < 0) { left = 1; width = -width; } fmt++; }
   2236         else while (isdigit(*fmt)) width = width * 10 + *fmt++ - '0';
   2237         if (*fmt == '.') {
   2238             fmt++; prec = 0;
   2239             if (*fmt == '*') { prec = va_arg(ap, int); fmt++; }
   2240             else while (isdigit(*fmt)) prec = prec * 10 + *fmt++ - '0';
   2241         }
   2242         for (;; fmt++) {
   2243             if (*fmt == 'h') size = size == -1 ? -2 : -1;
   2244             else if (*fmt == 'l') size++;
   2245             else if (*fmt == 'j') size = 2;
   2246             else if (*fmt == 'z' || *fmt == 't') size = sizeof(size_t) == 8 ? 2 : 0;
   2247             else if (*fmt == 'L') size = 3;
   2248             else break;
   2249         }
   2250         char b[128], *s = b; int len = 0; const char *pre = "";
   2251         switch (*fmt) {
   2252         case 'd': case 'i': {
   2253             long long v = size >= 2 ? va_arg(ap, long long) : size == 1 ? va_arg(ap, long) : va_arg(ap, int);
   2254             if (size == -1) v = (short)v; else if (size == -2) v = (signed char)v;
   2255             unsigned long long u = v < 0 ? -(unsigned long long)v : v;
   2256             pre = v < 0 ? "-" : plus ? "+" : space ? " " : "";
   2257             char *p = b + sizeof b; *--p = 0;
   2258             do *--p = '0' + u % 10; while (u /= 10);
   2259             if (prec == 0 && v == 0) p++;
   2260             while (prec > (int)(b + sizeof b - 1 - p)) *--p = '0';
   2261             if (prec >= 0) zero = 0;
   2262             out_field(o, pre, p, strlen(p), width, left, zero);
   2263             continue;
   2264         }
   2265         case 'u': case 'o': case 'x': case 'X': case 'p': {
   2266             unsigned long long u;
   2267             int base = *fmt == 'o' ? 8 : *fmt == 'u' ? 10 : 16;
   2268             const char *dig = *fmt == 'X' ? "0123456789ABCDEF" : "0123456789abcdef";
   2269             if (*fmt == 'p') { u = (uintptr_t)va_arg(ap, void *); alt = 1; }
   2270             else u = size >= 2 ? va_arg(ap, unsigned long long) : size == 1 ? va_arg(ap, unsigned long) : va_arg(ap, unsigned int);
   2271             if (size == -1) u = (unsigned short)u; else if (size == -2) u = (unsigned char)u;
   2272             if (alt && base == 16 && u) pre = *fmt == 'X' ? "0X" : "0x";
   2273             char *p = b + sizeof b; *--p = 0;
   2274             do *--p = dig[u % base]; while (u /= base);
   2275             if (prec == 0 && p[0] == '0' && !p[1] && *fmt != 'p') p++;
   2276             while (prec > (int)(b + sizeof b - 1 - p)) *--p = '0';
   2277             if (alt && base == 8 && *p != '0') *--p = '0';
   2278             if (prec >= 0) zero = 0;
   2279             out_field(o, pre, p, strlen(p), width, left, zero);
   2280             continue;
   2281         }
   2282         case 'c': b[0] = va_arg(ap, int); len = 1; break;
   2283         case 's': s = va_arg(ap, char *); if (!s) s = "(null)"; len = strlen(s); if (prec >= 0 && prec < len) len = prec; break;
   2284         case 'f': case 'F': case 'e': case 'E': case 'g': case 'G': {
   2285             long double v = size == 3 ? va_arg(ap, long double) : va_arg(ap, double);
   2286             pre = v < 0 ? "-" : plus ? "+" : space ? " " : "";
   2287             len = fmt_float(b, v, prec, *fmt, alt);
   2288             out_field(o, pre, b, len, width, left, zero);
   2289             continue;
   2290         }
   2291         case '%': b[0] = '%'; len = 1; break;
   2292         default: out_c(o, '%'); out_c(o, *fmt); continue;
   2293         }
   2294         out_field(o, "", s, len, width, left, 0);
   2295     }
   2296     return o->len;
   2297 }
   2298 #endif
   2299 int vsnprintf(char *buf, size_t cap, const char *fmt, va_list ap)
   2300 #ifdef C_DECLS_ONLY
   2301 ;
   2302 #else
   2303 {
   2304     struct out o = { buf, cap, 0, NULL };
   2305     int n = format(&o, fmt, ap);
   2306     if (cap) buf[o.len < cap ? o.len : cap - 1] = 0;
   2307     return n;
   2308 }
   2309 #endif
   2310 int vsprintf(char *buf, const char *fmt, va_list ap)
   2311 #ifdef C_DECLS_ONLY
   2312 ;
   2313 #else
   2314 { return vsnprintf(buf, (size_t)-1 / 2, fmt, ap); }
   2315 #endif
   2316 int vfprintf(FILE *f, const char *fmt, va_list ap)
   2317 #ifdef C_DECLS_ONLY
   2318 ;
   2319 #else
   2320 { struct out o = { NULL, 0, 0, f }; return format(&o, fmt, ap); }
   2321 #endif
   2322 int vprintf(const char *fmt, va_list ap)
   2323 #ifdef C_DECLS_ONLY
   2324 ;
   2325 #else
   2326 { return vfprintf(stdout, fmt, ap); }
   2327 #endif
   2328 int snprintf(char *buf, size_t cap, const char *fmt, ...)
   2329 #ifdef C_DECLS_ONLY
   2330 ;
   2331 #else
   2332 { va_list ap; va_start(ap, fmt); int n = vsnprintf(buf, cap, fmt, ap); va_end(ap); return n; }
   2333 #endif
   2334 int sprintf(char *buf, const char *fmt, ...)
   2335 #ifdef C_DECLS_ONLY
   2336 ;
   2337 #else
   2338 { va_list ap; va_start(ap, fmt); int n = vsprintf(buf, fmt, ap); va_end(ap); return n; }
   2339 #endif
   2340 int fprintf(FILE *f, const char *fmt, ...)
   2341 #ifdef C_DECLS_ONLY
   2342 ;
   2343 #else
   2344 { va_list ap; va_start(ap, fmt); int n = vfprintf(f, fmt, ap); va_end(ap); return n; }
   2345 #endif
   2346 int printf(const char *fmt, ...)
   2347 #ifdef C_DECLS_ONLY
   2348 ;
   2349 #else
   2350 { va_list ap; va_start(ap, fmt); int n = vfprintf(stdout, fmt, ap); va_end(ap); return n; }
   2351 #endif
   2352 
   2353 /* ---- the end of a program ---- */
   2354 #ifdef C_DECLS_ONLY
   2355 extern void c_run_exit(void);
   2356 #else
   2357 void c_run_exit(void);
   2358 #endif
   2359 __attribute__((noreturn)) void exit(int code)
   2360 #ifdef C_DECLS_ONLY
   2361 ;
   2362 #else
   2363 { c_run_exit(); _exit(code); }
   2364 #endif
   2365 __attribute__((noreturn)) void abort(void)
   2366 #ifdef C_DECLS_ONLY
   2367 ;
   2368 #else
   2369 { fputs("abort\n", stderr); _exit(134); }
   2370 #endif
   2371 #define assert(x) ((x) ? (void)0 : (fprintf(stderr, "%s:%d: assertion failed: %s\n", __FILE__, __LINE__, #x), abort()))
   2372 
   2373 /* ---- paths ---- */
   2374 /* the absolute path, with . and .. taken out (links aren't followed, there are none to follow here) */
   2375 char *realpath(const char *path, char *out)
   2376 #ifdef C_DECLS_ONLY
   2377 ;
   2378 #else
   2379 {
   2380     char tmp[PATH_MAX], *r = out ? out : malloc(PATH_MAX), *p, *seg;
   2381     size_t n = 0;
   2382     if (!r) return NULL;
   2383     if (path[0] == '/') strcpy(tmp, path);
   2384     else { if (!getcwd(tmp, sizeof tmp)) return NULL; strcat(tmp, "/"); strcat(tmp, path); }
   2385     r[0] = 0;
   2386     for (p = tmp; *p; ) {
   2387         while (*p == '/') p++;
   2388         seg = p;
   2389         while (*p && *p != '/') p++;
   2390         size_t l = p - seg;
   2391         if (l == 0 || (l == 1 && seg[0] == '.')) continue;
   2392         if (l == 2 && seg[0] == '.' && seg[1] == '.') { while (n && r[--n] != '/'); r[n] = 0; continue; }
   2393         r[n++] = '/'; memcpy(r + n, seg, l); n += l; r[n] = 0;
   2394     }
   2395     if (!n) strcpy(r, "/");
   2396     return r;
   2397 }
   2398 #endif
   2399 
   2400 /* ---- tcc's helper for va_arg ----
   2401  * under tcc every va_arg calls __va_arg(ap, kind, size, align): kind 0 = a general register, 1 = an SSE register,
   2402  * 2 = the stack. the layout is the x86-64 ABI's: 6 general registers (48 bytes), then 8 SSE ones of 16 */
   2403 #ifdef __TINYC__
   2404 void *__va_arg(__builtin_va_list ap, int kind, int size, int align)
   2405 #ifdef C_DECLS_ONLY
   2406 ;
   2407 #else
   2408 {
   2409     size = (size + 7) & ~7; align = (align + 7) & ~7;
   2410     if (kind == 0 && ap->gp_offset + size <= 48) { ap->gp_offset += size; return ap->reg_save_area + ap->gp_offset - size; }
   2411     if (kind == 1 && size <= 8 && ap->fp_offset < 176) { ap->fp_offset += 16; return ap->reg_save_area + ap->fp_offset - 16; }
   2412     ap->overflow_arg_area = (char *)(((uintptr_t)ap->overflow_arg_area + align - 1) & -(uintptr_t)align);
   2413     ap->overflow_arg_area += size;
   2414     return ap->overflow_arg_area - size;
   2415 }
   2416 #endif
   2417 #endif
   2418 
   2419 /* ---- the environment and running programs ---- */
   2420 #ifdef C_DECLS_ONLY
   2421 extern char **environ;
   2422 #else
   2423 char **environ;
   2424 #endif
   2425 char *getenv(const char *name)
   2426 #ifdef C_DECLS_ONLY
   2427 ;
   2428 #else
   2429 {
   2430     size_t l = strlen(name); char **e;
   2431     for (e = environ; e && *e; e++) if (!strncmp(*e, name, l) && (*e)[l] == '=') return *e + l + 1;
   2432     return NULL;
   2433 }
   2434 #endif
   2435 /* execl(path, arg0, arg1, ..., 0): execve with the arguments written out */
   2436 int execl(const char *path, const char *arg, ...)
   2437 #ifdef C_DECLS_ONLY
   2438 ;
   2439 #else
   2440 {
   2441     char *argv[64]; int n = 0; va_list ap;
   2442     argv[n++] = (char *)arg;
   2443     va_start(ap, arg);
   2444     while (n < 63 && (argv[n] = va_arg(ap, char *))) n++;
   2445     va_end(ap);
   2446     argv[n] = NULL;
   2447     return execve(path, argv, environ);
   2448 }
   2449 #endif
   2450 /* execve, looking through $PATH when the name has no / in it */
   2451 int execvp(const char *file, char *const argv[])
   2452 #ifdef C_DECLS_ONLY
   2453 ;
   2454 #else
   2455 {
   2456     char path[PATH_MAX]; const char *p = getenv("PATH"), *q;
   2457     if (strchr(file, '/')) return execve(file, argv, environ);
   2458     if (!p) p = "/bin";
   2459     for (; *p; p = *q ? q + 1 : q) {
   2460         q = strchr(p, ':'); if (!q) q = p + strlen(p);
   2461         snprintf(path, sizeof path, "%.*s/%s", (int)(q - p), p, file);
   2462         execve(path, argv, environ);
   2463     }
   2464     return -1;
   2465 }
   2466 #endif
   2467 
   2468 /* ---- directories [rifle] ----
   2469  * opendir/readdir over getdents64. readdir hands back a pointer into the buffer, good until the next call */
   2470 struct dirent { ino_t d_ino; long long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; };
   2471 #define DT_UNKNOWN 0
   2472 #define DT_FIFO 1
   2473 #define DT_CHR 2
   2474 #define DT_DIR 4
   2475 #define DT_BLK 6
   2476 #define DT_REG 8
   2477 #define DT_LNK 10
   2478 #define DT_SOCK 12
   2479 typedef struct DIR { int fd, pos, len; char buf[8192]; } DIR;
   2480 DIR *fdopendir(int fd)
   2481 #ifdef C_DECLS_ONLY
   2482 ;
   2483 #else
   2484 {
   2485     DIR *d = malloc(sizeof *d);
   2486     if (!d) return NULL;
   2487     d->fd = fd; d->pos = d->len = 0;
   2488     return d;
   2489 }
   2490 #endif
   2491 DIR *opendir(const char *path)
   2492 #ifdef C_DECLS_ONLY
   2493 ;
   2494 #else
   2495 {
   2496     int fd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
   2497     DIR *d;
   2498     if (fd < 0) return NULL;
   2499     if (!(d = fdopendir(fd))) { close(fd); return NULL; }
   2500     return d;
   2501 }
   2502 #endif
   2503 struct dirent *readdir(DIR *d)
   2504 #ifdef C_DECLS_ONLY
   2505 ;
   2506 #else
   2507 {
   2508     if (d->pos >= d->len) {
   2509         long r = sys3(SYS_getdents64, d->fd, d->buf, sizeof d->buf);
   2510         if (r <= 0) { if (r < 0) errno = -r; return NULL; }   /* errno stays alone at the end of the list */
   2511         d->len = r; d->pos = 0;
   2512     }
   2513     struct dirent *e = (struct dirent *)(d->buf + d->pos);
   2514     d->pos += e->d_reclen;
   2515     return e;
   2516 }
   2517 #endif
   2518 int closedir(DIR *d)
   2519 #ifdef C_DECLS_ONLY
   2520 ;
   2521 #else
   2522 { int r = close(d->fd); free(d); return r; }
   2523 #endif
   2524 int dirfd(DIR *d)
   2525 #ifdef C_DECLS_ONLY
   2526 ;
   2527 #else
   2528 { return d->fd; }
   2529 #endif
   2530 
   2531 /* ---- users and groups [rifle] ----
   2532  * read straight from /etc/passwd and /etc/group each time. no NSS, no caches: what's in the files is the answer */
   2533 struct passwd { char *pw_name, *pw_passwd; uid_t pw_uid; gid_t pw_gid; char *pw_gecos, *pw_dir, *pw_shell; };
   2534 struct group { char *gr_name, *gr_passwd; gid_t gr_gid; char **gr_mem; };
   2535 #ifndef C_DECLS_ONLY
   2536 static char *c_field(char **p)
   2537 {
   2538     char *r = *p, *q = strchr(*p, ':');
   2539     if (q) { *q = 0; *p = q + 1; } else *p += strlen(*p);
   2540     return r;
   2541 }
   2542 #endif
   2543 #ifndef C_DECLS_ONLY
   2544 static struct passwd *c_getpw(const char *name, long id)
   2545 {
   2546     static struct passwd pw; static char line[2048];
   2547     FILE *f = fopen("/etc/passwd", "r");
   2548     if (!f) return NULL;
   2549     while (fgets(line, sizeof line, f)) {
   2550         char *p = line, *nl = strchr(line, '\n'), *uid, *gid;
   2551         if (nl) *nl = 0;
   2552         if (!line[0] || line[0] == '#') continue;
   2553         pw.pw_name = c_field(&p); pw.pw_passwd = c_field(&p); uid = c_field(&p); gid = c_field(&p);
   2554         pw.pw_gecos = c_field(&p); pw.pw_dir = c_field(&p); pw.pw_shell = c_field(&p);
   2555         pw.pw_uid = strtoul(uid, NULL, 10); pw.pw_gid = strtoul(gid, NULL, 10);
   2556         if (name ? !strcmp(pw.pw_name, name) : (long)pw.pw_uid == id) { fclose(f); return &pw; }
   2557     }
   2558     fclose(f);
   2559     return NULL;
   2560 }
   2561 #endif
   2562 struct passwd *getpwnam(const char *name)
   2563 #ifdef C_DECLS_ONLY
   2564 ;
   2565 #else
   2566 { return c_getpw(name, 0); }
   2567 #endif
   2568 struct passwd *getpwuid(uid_t uid)
   2569 #ifdef C_DECLS_ONLY
   2570 ;
   2571 #else
   2572 { return c_getpw(NULL, uid); }
   2573 #endif
   2574 #ifndef C_DECLS_ONLY
   2575 static struct group *c_getgr(const char *name, long id)
   2576 {
   2577     static struct group gr; static char line[8192]; static char *mem[512];
   2578     FILE *f = fopen("/etc/group", "r");
   2579     if (!f) return NULL;
   2580     while (fgets(line, sizeof line, f)) {
   2581         char *p = line, *nl = strchr(line, '\n'), *gid, *list;
   2582         int n = 0;
   2583         if (nl) *nl = 0;
   2584         if (!line[0] || line[0] == '#') continue;
   2585         gr.gr_name = c_field(&p); gr.gr_passwd = c_field(&p); gid = c_field(&p); list = p;
   2586         gr.gr_gid = strtoul(gid, NULL, 10);
   2587         while (*list && n < 511) { mem[n++] = list; while (*list && *list != ',') list++; if (*list) *list++ = 0; }
   2588         mem[n] = NULL; gr.gr_mem = mem;
   2589         if (name ? !strcmp(gr.gr_name, name) : (long)gr.gr_gid == id) { fclose(f); return &gr; }
   2590     }
   2591     fclose(f);
   2592     return NULL;
   2593 }
   2594 #endif
   2595 struct group *getgrnam(const char *name)
   2596 #ifdef C_DECLS_ONLY
   2597 ;
   2598 #else
   2599 { return c_getgr(name, 0); }
   2600 #endif
   2601 struct group *getgrgid(gid_t gid)
   2602 #ifdef C_DECLS_ONLY
   2603 ;
   2604 #else
   2605 { return c_getgr(NULL, gid); }
   2606 #endif
   2607 
   2608 /* ---- time, written out [rifle] ----
   2609  * the days between 1970 and a date, counted the way everyone does it. TZ isn't read yet: the time is UTC */
   2610 #ifndef C_DECLS_ONLY
   2611 static long c_days(long y, int m, int d)   /* m 1..12 */
   2612 {
   2613     y -= m <= 2;
   2614     long era = (y >= 0 ? y : y - 399) / 400, yoe = y - era * 400;
   2615     long doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1, doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
   2616     return era * 146097 + doe - 719468;
   2617 }
   2618 #endif
   2619 time_t timegm(struct tm *t)
   2620 #ifdef C_DECLS_ONLY
   2621 ;
   2622 #else
   2623 {
   2624     long y = t->tm_year + 1900 + t->tm_mon / 12; int m = t->tm_mon % 12;
   2625     if (m < 0) { m += 12; y--; }
   2626     return c_days(y, m + 1, 1) * 86400L + (t->tm_mday - 1) * 86400L + t->tm_hour * 3600L + t->tm_min * 60L + t->tm_sec;
   2627 }
   2628 #endif
   2629 time_t mktime(struct tm *t)
   2630 #ifdef C_DECLS_ONLY
   2631 ;
   2632 #else
   2633 { return timegm(t); }
   2634 #endif
   2635 #ifndef C_DECLS_ONLY
   2636 static const char *c_wday[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
   2637 #endif
   2638 #ifndef C_DECLS_ONLY
   2639 static const char *c_month[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September",
   2640                                  "October", "November", "December" };
   2641 #endif
   2642 size_t strftime(char *buf, size_t cap, const char *fmt, const struct tm *t)
   2643 #ifdef C_DECLS_ONLY
   2644 ;
   2645 #else
   2646 {
   2647     size_t n = 0;
   2648     char tmp[64];
   2649     for (; *fmt; fmt++) {
   2650         const char *r = tmp; int pad = 0;
   2651         if (*fmt != '%') { if (n + 1 >= cap) return 0; buf[n++] = *fmt; continue; }
   2652         fmt++;
   2653         if (*fmt == '-' || *fmt == '_' || *fmt == '0') { pad = *fmt++; }   /* %-d no padding, %_d spaces */
   2654         tmp[0] = 0;
   2655         switch (*fmt) {
   2656         case 'a': r = c_wday[t->tm_wday % 7]; snprintf(tmp, 4, "%s", r); r = tmp; break;
   2657         case 'A': r = c_wday[t->tm_wday % 7]; break;
   2658         case 'b': case 'h': snprintf(tmp, 4, "%s", c_month[t->tm_mon % 12]); break;
   2659         case 'B': r = c_month[t->tm_mon % 12]; break;
   2660         case 'c': snprintf(tmp, sizeof tmp, "%.3s %.3s %2d %02d:%02d:%02d %d", c_wday[t->tm_wday % 7], c_month[t->tm_mon % 12],
   2661                            t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, t->tm_year + 1900); break;
   2662         case 'C': snprintf(tmp, sizeof tmp, "%02d", (t->tm_year + 1900) / 100); break;
   2663         case 'd': snprintf(tmp, sizeof tmp, pad == '-' ? "%d" : pad == '_' ? "%2d" : "%02d", t->tm_mday); break;
   2664         case 'D': snprintf(tmp, sizeof tmp, "%02d/%02d/%02d", t->tm_mon + 1, t->tm_mday, t->tm_year % 100); break;
   2665         case 'e': snprintf(tmp, sizeof tmp, pad == '-' ? "%d" : pad == '0' ? "%02d" : "%2d", t->tm_mday); break;
   2666         case 'F': snprintf(tmp, sizeof tmp, "%d-%02d-%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); break;
   2667         case 'H': snprintf(tmp, sizeof tmp, pad == '-' ? "%d" : pad == '_' ? "%2d" : "%02d", t->tm_hour); break;
   2668         case 'I': snprintf(tmp, sizeof tmp, pad == '-' ? "%d" : pad == '_' ? "%2d" : "%02d", t->tm_hour % 12 ? t->tm_hour % 12 : 12); break;
   2669         case 'j': snprintf(tmp, sizeof tmp, pad == '-' ? "%d" : "%03d", t->tm_yday + 1); break;
   2670         case 'k': snprintf(tmp, sizeof tmp, "%2d", t->tm_hour); break;
   2671         case 'l': snprintf(tmp, sizeof tmp, "%2d", t->tm_hour % 12 ? t->tm_hour % 12 : 12); break;
   2672         case 'm': snprintf(tmp, sizeof tmp, pad == '-' ? "%d" : pad == '_' ? "%2d" : "%02d", t->tm_mon + 1); break;
   2673         case 'M': snprintf(tmp, sizeof tmp, pad == '-' ? "%d" : pad == '_' ? "%2d" : "%02d", t->tm_min); break;
   2674         case 'n': r = "\n"; break;
   2675         case 'p': r = t->tm_hour < 12 ? "AM" : "PM"; break;
   2676         case 'P': r = t->tm_hour < 12 ? "am" : "pm"; break;
   2677         case 'r': snprintf(tmp, sizeof tmp, "%02d:%02d:%02d %s", t->tm_hour % 12 ? t->tm_hour % 12 : 12, t->tm_min, t->tm_sec, t->tm_hour < 12 ? "AM" : "PM"); break;
   2678         case 'R': snprintf(tmp, sizeof tmp, "%02d:%02d", t->tm_hour, t->tm_min); break;
   2679         case 's': { struct tm c = *t; snprintf(tmp, sizeof tmp, "%ld", (long)timegm(&c)); break; }
   2680         case 'S': snprintf(tmp, sizeof tmp, pad == '-' ? "%d" : pad == '_' ? "%2d" : "%02d", t->tm_sec); break;
   2681         case 't': r = "\t"; break;
   2682         case 'T': case 'X': snprintf(tmp, sizeof tmp, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec); break;
   2683         case 'u': snprintf(tmp, sizeof tmp, "%d", t->tm_wday ? t->tm_wday : 7); break;
   2684         case 'w': snprintf(tmp, sizeof tmp, "%d", t->tm_wday); break;
   2685         case 'x': snprintf(tmp, sizeof tmp, "%02d/%02d/%02d", t->tm_mon + 1, t->tm_mday, t->tm_year % 100); break;
   2686         case 'y': snprintf(tmp, sizeof tmp, "%02d", t->tm_year % 100); break;
   2687         case 'Y': snprintf(tmp, sizeof tmp, "%d", t->tm_year + 1900); break;
   2688         case 'z': r = "+0000"; break;
   2689         case 'Z': r = "UTC"; break;
   2690         case '%': r = "%"; break;
   2691         default: snprintf(tmp, sizeof tmp, "%%%c", *fmt); break;
   2692         }
   2693         size_t l = strlen(r);
   2694         if (n + l >= cap) return 0;
   2695         memcpy(buf + n, r, l); n += l;
   2696     }
   2697     if (n >= cap) return 0;
   2698     buf[n] = 0;
   2699     return n;
   2700 }
   2701 #endif
   2702 unsigned sleep(unsigned s)
   2703 #ifdef C_DECLS_ONLY
   2704 ;
   2705 #else
   2706 { struct timespec t = { s, 0 }; return nanosleep(&t, &t) < 0 ? t.tv_sec : 0; }
   2707 #endif
   2708 
   2709 /* ---- the environment, changed [rifle] ----
   2710  * the first change copies the list the kernel gave us onto the heap; after that it grows as needed */
   2711 #ifndef C_DECLS_ONLY
   2712 static int c_env_own;
   2713 #endif
   2714 #ifndef C_DECLS_ONLY
   2715 static int c_env_len(void) { int n = 0; while (environ && environ[n]) n++; return n; }
   2716 #endif
   2717 #ifndef C_DECLS_ONLY
   2718 static int c_env_copy(void)
   2719 {
   2720     if (!c_env_own) {
   2721         int n = c_env_len(), i;
   2722         char **e = malloc((n + 2) * sizeof *e);
   2723         if (!e) return -1;
   2724         for (i = 0; i < n; i++) e[i] = environ[i];
   2725         e[n] = NULL; environ = e; c_env_own = 1;
   2726     }
   2727     return 0;
   2728 }
   2729 #endif
   2730 int unsetenv(const char *name)
   2731 #ifdef C_DECLS_ONLY
   2732 ;
   2733 #else
   2734 {
   2735     size_t l = strlen(name); int i, j;
   2736     if (!l || strchr(name, '=')) { errno = EINVAL; return -1; }
   2737     if (c_env_copy()) return -1;
   2738     for (i = j = 0; environ[i]; i++)
   2739         if (!(!strncmp(environ[i], name, l) && environ[i][l] == '=')) environ[j++] = environ[i];
   2740     environ[j] = NULL;
   2741     return 0;
   2742 }
   2743 #endif
   2744 int putenv(char *entry)
   2745 #ifdef C_DECLS_ONLY
   2746 ;
   2747 #else
   2748 {
   2749     char *eq = strchr(entry, '='); size_t l; int i, n;
   2750     if (!eq || eq == entry) { errno = EINVAL; return -1; }
   2751     l = eq - entry;
   2752     if (c_env_copy()) return -1;
   2753     for (i = 0; environ[i]; i++)
   2754         if (!strncmp(environ[i], entry, l) && environ[i][l] == '=') { environ[i] = entry; return 0; }
   2755     n = i;
   2756     {
   2757         char **e = realloc(environ, (n + 2) * sizeof *e);
   2758         if (!e) return -1;
   2759         environ = e; e[n] = entry; e[n + 1] = NULL;
   2760     }
   2761     return 0;
   2762 }
   2763 #endif
   2764 int setenv(const char *name, const char *value, int overwrite)
   2765 #ifdef C_DECLS_ONLY
   2766 ;
   2767 #else
   2768 {
   2769     size_t l = strlen(name), v = strlen(value); char *e;
   2770     if (!l || strchr(name, '=')) { errno = EINVAL; return -1; }
   2771     if (!overwrite && getenv(name)) return 0;
   2772     if (!(e = malloc(l + v + 2))) return -1;
   2773     memcpy(e, name, l); e[l] = '='; memcpy(e + l + 1, value, v + 1);
   2774     return putenv(e);
   2775 }
   2776 #endif
   2777 int clearenv(void)
   2778 #ifdef C_DECLS_ONLY
   2779 ;
   2780 #else
   2781 { if (c_env_copy()) return -1; environ[0] = NULL; return 0; }
   2782 #endif
   2783 
   2784 #ifdef __i386__
   2785 /* i386 has no 64-bit divide: the compiler calls these, as it would call libgcc's */
   2786 unsigned long long __udivmoddi4(unsigned long long a, unsigned long long b, unsigned long long *rem)
   2787 #ifdef C_DECLS_ONLY
   2788 ;
   2789 #else
   2790 {
   2791     unsigned long long q = 0, r = 0;
   2792     int i;
   2793     if (!(a >> 32) && !(b >> 32)) {
   2794         q = (unsigned)a / (unsigned)b;
   2795         if (rem) *rem = (unsigned)a % (unsigned)b;
   2796         return q;
   2797     }
   2798     for (i = 63; i >= 0; i--) {
   2799         r = r << 1 | (a >> i & 1);
   2800         if (r >= b) { r -= b; q |= 1ULL << i; }
   2801     }
   2802     if (rem) *rem = r;
   2803     return q;
   2804 }
   2805 #endif
   2806 unsigned long long __udivdi3(unsigned long long a, unsigned long long b)
   2807 #ifdef C_DECLS_ONLY
   2808 ;
   2809 #else
   2810 { return __udivmoddi4(a, b, 0); }
   2811 #endif
   2812 unsigned long long __umoddi3(unsigned long long a, unsigned long long b)
   2813 #ifdef C_DECLS_ONLY
   2814 ;
   2815 #else
   2816 { unsigned long long r; __udivmoddi4(a, b, &r); return r; }
   2817 #endif
   2818 long long __divdi3(long long a, long long b)
   2819 #ifdef C_DECLS_ONLY
   2820 ;
   2821 #else
   2822 {
   2823     unsigned long long q = __udivmoddi4(a < 0 ? -(unsigned long long)a : a, b < 0 ? -(unsigned long long)b : b, 0);
   2824     return (a < 0) != (b < 0) ? -(long long)q : (long long)q;
   2825 }
   2826 #endif
   2827 long long __moddi3(long long a, long long b)
   2828 #ifdef C_DECLS_ONLY
   2829 ;
   2830 #else
   2831 {
   2832     unsigned long long r;
   2833     __udivmoddi4(a < 0 ? -(unsigned long long)a : a, b < 0 ? -(unsigned long long)b : b, &r);
   2834     return a < 0 ? -(long long)r : (long long)r;
   2835 }
   2836 #endif
   2837 #endif
   2838 
   2839 /* ---- start-up ----
   2840  * the kernel leaves argc, argv and the environment on the stack, then jumps to _start */
   2841 #ifdef C_DECLS_ONLY
   2842 extern int main();
   2843 #else
   2844 int main();
   2845 #endif /* no prototype: int main(void), main(argc, argv) and main(argc, argv, envp) all work */
   2846 void c_start(long *sp)
   2847 #ifdef C_DECLS_ONLY
   2848 ;
   2849 #else
   2850 {
   2851     int argc = (int)sp[0];
   2852     char **argv = (char **)(sp + 1);
   2853     environ = argv + argc + 1;
   2854     exit(main(argc, argv, environ));
   2855 }
   2856 #endif
   2857 #ifndef C_NO_START
   2858 #ifdef __i386__
   2859 #ifndef C_DECLS_ONLY
   2860 __asm__(".text\n.global _start\n_start:\n xor %ebp,%ebp\n mov %esp,%eax\n and $-16,%esp\n sub $12,%esp\n push %eax\n call c_start\n hlt\n");
   2861 #endif
   2862 #else
   2863 #ifndef C_DECLS_ONLY
   2864 __asm__(".text\n.global _start\n_start:\n xor %rbp,%rbp\n mov %rsp,%rdi\n and $-16,%rsp\n call c_start\n hlt\n");
   2865 #endif
   2866 #endif
   2867 #endif
   2868 
   2869 /* [rifle] priorities and getline */
   2870 int strcoll(const char *a, const char *b)
   2871 #ifdef C_DECLS_ONLY
   2872 ;
   2873 #else
   2874 { return strcmp(a, b); }
   2875 #endif
   2876 #ifndef __i386__
   2877 #define SYS_getpriority 140
   2878 #define SYS_setpriority 141
   2879 #endif
   2880 int getpriority(int which, int who)
   2881 #ifdef C_DECLS_ONLY
   2882 ;
   2883 #else
   2884 { return fail(sys2(SYS_getpriority, which, who)); }
   2885 #endif
   2886 int setpriority(int which, int who, int prio)
   2887 #ifdef C_DECLS_ONLY
   2888 ;
   2889 #else
   2890 { return fail(sys3(SYS_setpriority, which, who, prio)); }
   2891 #endif
   2892 ssize_t getline(char **lp, size_t *cap, FILE *f)
   2893 #ifdef C_DECLS_ONLY
   2894 ;
   2895 #else
   2896 {
   2897     size_t n = 0;
   2898     int c;
   2899     if (!*lp || !*cap) { *cap = 128; *lp = malloc(*cap); }
   2900     while ((c = getc(f)) != EOF) {
   2901         if (n + 2 > *cap) { *cap *= 2; *lp = realloc(*lp, *cap); }
   2902         (*lp)[n++] = c;
   2903         if (c == '\n') break;
   2904     }
   2905     if (!n) return -1;
   2906     (*lp)[n] = 0;
   2907     return n;
   2908 }
   2909 #endif
   2910 /* ---- what the rest of the additions lean on [chamesle] ---- */
   2911 #define sys4(n, a, b, c, d) sys6(n, (long)(a), (long)(b), (long)(c), (long)(d), 0, 0)
   2912 #define sys5(n, a, b, c, d, e) sys6(n, (long)(a), (long)(b), (long)(c), (long)(d), (long)(e), 0)
   2913 #ifndef ENOTBLK
   2914 #define ENOTBLK 15
   2915 #endif
   2916 #ifndef ENOMSG
   2917 #define ENOMSG 42
   2918 #endif
   2919 #ifndef EPROTO
   2920 #define EPROTO 71
   2921 #endif
   2922 #ifndef EBADMSG
   2923 #define EBADMSG 74
   2924 #endif
   2925 #ifndef ENOTSOCK
   2926 #define ENOTSOCK 88
   2927 #endif
   2928 #ifndef EDESTADDRREQ
   2929 #define EDESTADDRREQ 89
   2930 #endif
   2931 #ifndef EMSGSIZE
   2932 #define EMSGSIZE 90
   2933 #endif
   2934 #ifndef EPROTOTYPE
   2935 #define EPROTOTYPE 91
   2936 #endif
   2937 #ifndef ENOPROTOOPT
   2938 #define ENOPROTOOPT 92
   2939 #endif
   2940 #ifndef EPROTONOSUPPORT
   2941 #define EPROTONOSUPPORT 93
   2942 #endif
   2943 #ifndef EAFNOSUPPORT
   2944 #define EAFNOSUPPORT 97
   2945 #endif
   2946 #ifndef EADDRINUSE
   2947 #define EADDRINUSE 98
   2948 #endif
   2949 #ifndef EADDRNOTAVAIL
   2950 #define EADDRNOTAVAIL 99
   2951 #endif
   2952 #ifndef ENETDOWN
   2953 #define ENETDOWN 100
   2954 #endif
   2955 #ifndef ENETUNREACH
   2956 #define ENETUNREACH 101
   2957 #endif
   2958 #ifndef ECONNABORTED
   2959 #define ECONNABORTED 103
   2960 #endif
   2961 #ifndef ECONNRESET
   2962 #define ECONNRESET 104
   2963 #endif
   2964 #ifndef ENOBUFS
   2965 #define ENOBUFS 105
   2966 #endif
   2967 #ifndef EISCONN
   2968 #define EISCONN 106
   2969 #endif
   2970 #ifndef ENOTCONN
   2971 #define ENOTCONN 107
   2972 #endif
   2973 #ifndef ECONNREFUSED
   2974 #define ECONNREFUSED 111
   2975 #endif
   2976 #ifndef EHOSTUNREACH
   2977 #define EHOSTUNREACH 113
   2978 #endif
   2979 #ifndef EALREADY
   2980 #define EALREADY 114
   2981 #endif
   2982 #ifndef EINPROGRESS
   2983 #define EINPROGRESS 115
   2984 #endif
   2985 #ifndef ECANCELED
   2986 #define ECANCELED 125
   2987 #endif
   2988 #ifndef ENOTSUP
   2989 #define ENOTSUP 95
   2990 #endif
   2991 #ifndef EWOULDBLOCK
   2992 #define EWOULDBLOCK 11
   2993 #endif
   2994 #ifndef ENOLCK
   2995 #define ENOLCK 37
   2996 #endif
   2997 #ifndef EIDRM
   2998 #define EIDRM 43
   2999 #endif
   3000 #ifndef ENOLINK
   3001 #define ENOLINK 67
   3002 #endif
   3003 #ifndef ETIME
   3004 #define ETIME 62
   3005 #endif
   3006 #ifndef ENOSR
   3007 #define ENOSR 63
   3008 #endif
   3009 #ifndef ENOSTR
   3010 #define ENOSTR 60
   3011 #endif
   3012 #ifndef EMULTIHOP
   3013 #define EMULTIHOP 72
   3014 #endif
   3015 #ifndef EOWNERDEAD
   3016 #define EOWNERDEAD 130
   3017 #endif
   3018 #ifndef ENOTRECOVERABLE
   3019 #define ENOTRECOVERABLE 131
   3020 #endif
   3021 #ifndef ESHUTDOWN
   3022 #define ESHUTDOWN 108
   3023 #endif
   3024 #ifndef ETOOMANYREFS
   3025 #define ETOOMANYREFS 109
   3026 #endif
   3027 #ifndef EHOSTDOWN
   3028 #define EHOSTDOWN 112
   3029 #endif
   3030 /* ---- system call numbers [chamesle] ---- */
   3031 #ifndef SYS_read
   3032 #define SYS_read 0
   3033 #endif
   3034 #ifndef SYS_write
   3035 #define SYS_write 1
   3036 #endif
   3037 #ifndef SYS_open
   3038 #define SYS_open 2
   3039 #endif
   3040 #ifndef SYS_close
   3041 #define SYS_close 3
   3042 #endif
   3043 #ifndef SYS_stat
   3044 #define SYS_stat 4
   3045 #endif
   3046 #ifndef SYS_fstat
   3047 #define SYS_fstat 5
   3048 #endif
   3049 #ifndef SYS_lstat
   3050 #define SYS_lstat 6
   3051 #endif
   3052 #ifndef SYS_poll
   3053 #define SYS_poll 7
   3054 #endif
   3055 #ifndef SYS_lseek
   3056 #define SYS_lseek 8
   3057 #endif
   3058 #ifndef SYS_mmap
   3059 #define SYS_mmap 9
   3060 #endif
   3061 #ifndef SYS_mprotect
   3062 #define SYS_mprotect 10
   3063 #endif
   3064 #ifndef SYS_munmap
   3065 #define SYS_munmap 11
   3066 #endif
   3067 #ifndef SYS_brk
   3068 #define SYS_brk 12
   3069 #endif
   3070 #ifndef SYS_rt_sigaction
   3071 #define SYS_rt_sigaction 13
   3072 #endif
   3073 #ifndef SYS_rt_sigprocmask
   3074 #define SYS_rt_sigprocmask 14
   3075 #endif
   3076 #ifndef SYS_rt_sigreturn
   3077 #define SYS_rt_sigreturn 15
   3078 #endif
   3079 #ifndef SYS_ioctl
   3080 #define SYS_ioctl 16
   3081 #endif
   3082 #ifndef SYS_pread64
   3083 #define SYS_pread64 17
   3084 #endif
   3085 #ifndef SYS_pwrite64
   3086 #define SYS_pwrite64 18
   3087 #endif
   3088 #ifndef SYS_readv
   3089 #define SYS_readv 19
   3090 #endif
   3091 #ifndef SYS_writev
   3092 #define SYS_writev 20
   3093 #endif
   3094 #ifndef SYS_access
   3095 #define SYS_access 21
   3096 #endif
   3097 #ifndef SYS_pipe
   3098 #define SYS_pipe 22
   3099 #endif
   3100 #ifndef SYS_select
   3101 #define SYS_select 23
   3102 #endif
   3103 #ifndef SYS_sched_yield
   3104 #define SYS_sched_yield 24
   3105 #endif
   3106 #ifndef SYS_mremap
   3107 #define SYS_mremap 25
   3108 #endif
   3109 #ifndef SYS_msync
   3110 #define SYS_msync 26
   3111 #endif
   3112 #ifndef SYS_madvise
   3113 #define SYS_madvise 28
   3114 #endif
   3115 #ifndef SYS_dup
   3116 #define SYS_dup 32
   3117 #endif
   3118 #ifndef SYS_dup2
   3119 #define SYS_dup2 33
   3120 #endif
   3121 #ifndef SYS_pause
   3122 #define SYS_pause 34
   3123 #endif
   3124 #ifndef SYS_nanosleep
   3125 #define SYS_nanosleep 35
   3126 #endif
   3127 #ifndef SYS_getitimer
   3128 #define SYS_getitimer 36
   3129 #endif
   3130 #ifndef SYS_alarm
   3131 #define SYS_alarm 37
   3132 #endif
   3133 #ifndef SYS_setitimer
   3134 #define SYS_setitimer 38
   3135 #endif
   3136 #ifndef SYS_getpid
   3137 #define SYS_getpid 39
   3138 #endif
   3139 #ifndef SYS_sendfile
   3140 #define SYS_sendfile 40
   3141 #endif
   3142 #ifndef SYS_socket
   3143 #define SYS_socket 41
   3144 #endif
   3145 #ifndef SYS_connect
   3146 #define SYS_connect 42
   3147 #endif
   3148 #ifndef SYS_accept
   3149 #define SYS_accept 43
   3150 #endif
   3151 #ifndef SYS_sendto
   3152 #define SYS_sendto 44
   3153 #endif
   3154 #ifndef SYS_recvfrom
   3155 #define SYS_recvfrom 45
   3156 #endif
   3157 #ifndef SYS_sendmsg
   3158 #define SYS_sendmsg 46
   3159 #endif
   3160 #ifndef SYS_recvmsg
   3161 #define SYS_recvmsg 47
   3162 #endif
   3163 #ifndef SYS_shutdown
   3164 #define SYS_shutdown 48
   3165 #endif
   3166 #ifndef SYS_bind
   3167 #define SYS_bind 49
   3168 #endif
   3169 #ifndef SYS_listen
   3170 #define SYS_listen 50
   3171 #endif
   3172 #ifndef SYS_getsockname
   3173 #define SYS_getsockname 51
   3174 #endif
   3175 #ifndef SYS_getpeername
   3176 #define SYS_getpeername 52
   3177 #endif
   3178 #ifndef SYS_socketpair
   3179 #define SYS_socketpair 53
   3180 #endif
   3181 #ifndef SYS_setsockopt
   3182 #define SYS_setsockopt 54
   3183 #endif
   3184 #ifndef SYS_getsockopt
   3185 #define SYS_getsockopt 55
   3186 #endif
   3187 #ifndef SYS_clone
   3188 #define SYS_clone 56
   3189 #endif
   3190 #ifndef SYS_fork
   3191 #define SYS_fork 57
   3192 #endif
   3193 #ifndef SYS_vfork
   3194 #define SYS_vfork 58
   3195 #endif
   3196 #ifndef SYS_execve
   3197 #define SYS_execve 59
   3198 #endif
   3199 #ifndef SYS_exit
   3200 #define SYS_exit 60
   3201 #endif
   3202 #ifndef SYS_wait4
   3203 #define SYS_wait4 61
   3204 #endif
   3205 #ifndef SYS_kill
   3206 #define SYS_kill 62
   3207 #endif
   3208 #ifndef SYS_uname
   3209 #define SYS_uname 63
   3210 #endif
   3211 #ifndef SYS_fcntl
   3212 #define SYS_fcntl 72
   3213 #endif
   3214 #ifndef SYS_flock
   3215 #define SYS_flock 73
   3216 #endif
   3217 #ifndef SYS_fsync
   3218 #define SYS_fsync 74
   3219 #endif
   3220 #ifndef SYS_fdatasync
   3221 #define SYS_fdatasync 75
   3222 #endif
   3223 #ifndef SYS_truncate
   3224 #define SYS_truncate 76
   3225 #endif
   3226 #ifndef SYS_ftruncate
   3227 #define SYS_ftruncate 77
   3228 #endif
   3229 #ifndef SYS_getdents
   3230 #define SYS_getdents 78
   3231 #endif
   3232 #ifndef SYS_getcwd
   3233 #define SYS_getcwd 79
   3234 #endif
   3235 #ifndef SYS_chdir
   3236 #define SYS_chdir 80
   3237 #endif
   3238 #ifndef SYS_fchdir
   3239 #define SYS_fchdir 81
   3240 #endif
   3241 #ifndef SYS_rename
   3242 #define SYS_rename 82
   3243 #endif
   3244 #ifndef SYS_mkdir
   3245 #define SYS_mkdir 83
   3246 #endif
   3247 #ifndef SYS_rmdir
   3248 #define SYS_rmdir 84
   3249 #endif
   3250 #ifndef SYS_creat
   3251 #define SYS_creat 85
   3252 #endif
   3253 #ifndef SYS_link
   3254 #define SYS_link 86
   3255 #endif
   3256 #ifndef SYS_unlink
   3257 #define SYS_unlink 87
   3258 #endif
   3259 #ifndef SYS_symlink
   3260 #define SYS_symlink 88
   3261 #endif
   3262 #ifndef SYS_readlink
   3263 #define SYS_readlink 89
   3264 #endif
   3265 #ifndef SYS_chmod
   3266 #define SYS_chmod 90
   3267 #endif
   3268 #ifndef SYS_fchmod
   3269 #define SYS_fchmod 91
   3270 #endif
   3271 #ifndef SYS_chown
   3272 #define SYS_chown 92
   3273 #endif
   3274 #ifndef SYS_fchown
   3275 #define SYS_fchown 93
   3276 #endif
   3277 #ifndef SYS_lchown
   3278 #define SYS_lchown 94
   3279 #endif
   3280 #ifndef SYS_umask
   3281 #define SYS_umask 95
   3282 #endif
   3283 #ifndef SYS_gettimeofday
   3284 #define SYS_gettimeofday 96
   3285 #endif
   3286 #ifndef SYS_getrlimit
   3287 #define SYS_getrlimit 97
   3288 #endif
   3289 #ifndef SYS_getrusage
   3290 #define SYS_getrusage 98
   3291 #endif
   3292 #ifndef SYS_sysinfo
   3293 #define SYS_sysinfo 99
   3294 #endif
   3295 #ifndef SYS_times
   3296 #define SYS_times 100
   3297 #endif
   3298 #ifndef SYS_getuid
   3299 #define SYS_getuid 102
   3300 #endif
   3301 #ifndef SYS_getgid
   3302 #define SYS_getgid 104
   3303 #endif
   3304 #ifndef SYS_setuid
   3305 #define SYS_setuid 105
   3306 #endif
   3307 #ifndef SYS_setgid
   3308 #define SYS_setgid 106
   3309 #endif
   3310 #ifndef SYS_geteuid
   3311 #define SYS_geteuid 107
   3312 #endif
   3313 #ifndef SYS_getegid
   3314 #define SYS_getegid 108
   3315 #endif
   3316 #ifndef SYS_setpgid
   3317 #define SYS_setpgid 109
   3318 #endif
   3319 #ifndef SYS_getppid
   3320 #define SYS_getppid 110
   3321 #endif
   3322 #ifndef SYS_getpgrp
   3323 #define SYS_getpgrp 111
   3324 #endif
   3325 #ifndef SYS_setsid
   3326 #define SYS_setsid 112
   3327 #endif
   3328 #ifndef SYS_setreuid
   3329 #define SYS_setreuid 113
   3330 #endif
   3331 #ifndef SYS_setregid
   3332 #define SYS_setregid 114
   3333 #endif
   3334 #ifndef SYS_getgroups
   3335 #define SYS_getgroups 115
   3336 #endif
   3337 #ifndef SYS_setgroups
   3338 #define SYS_setgroups 116
   3339 #endif
   3340 #ifndef SYS_setresuid
   3341 #define SYS_setresuid 117
   3342 #endif
   3343 #ifndef SYS_setresgid
   3344 #define SYS_setresgid 119
   3345 #endif
   3346 #ifndef SYS_getpgid
   3347 #define SYS_getpgid 121
   3348 #endif
   3349 #ifndef SYS_getsid
   3350 #define SYS_getsid 124
   3351 #endif
   3352 #ifndef SYS_rt_sigpending
   3353 #define SYS_rt_sigpending 127
   3354 #endif
   3355 #ifndef SYS_rt_sigtimedwait
   3356 #define SYS_rt_sigtimedwait 128
   3357 #endif
   3358 #ifndef SYS_rt_sigsuspend
   3359 #define SYS_rt_sigsuspend 130
   3360 #endif
   3361 #ifndef SYS_sigaltstack
   3362 #define SYS_sigaltstack 131
   3363 #endif
   3364 #ifndef SYS_utime
   3365 #define SYS_utime 132
   3366 #endif
   3367 #ifndef SYS_mknod
   3368 #define SYS_mknod 133
   3369 #endif
   3370 #ifndef SYS_statfs
   3371 #define SYS_statfs 137
   3372 #endif
   3373 #ifndef SYS_fstatfs
   3374 #define SYS_fstatfs 138
   3375 #endif
   3376 #ifndef SYS_getpriority
   3377 #define SYS_getpriority 140
   3378 #endif
   3379 #ifndef SYS_setpriority
   3380 #define SYS_setpriority 141
   3381 #endif
   3382 #ifndef SYS_prctl
   3383 #define SYS_prctl 157
   3384 #endif
   3385 #ifndef SYS_arch_prctl
   3386 #define SYS_arch_prctl 158
   3387 #endif
   3388 #ifndef SYS_setrlimit
   3389 #define SYS_setrlimit 160
   3390 #endif
   3391 #ifndef SYS_chroot
   3392 #define SYS_chroot 161
   3393 #endif
   3394 #ifndef SYS_sync
   3395 #define SYS_sync 162
   3396 #endif
   3397 #ifndef SYS_mount
   3398 #define SYS_mount 165
   3399 #endif
   3400 #ifndef SYS_umount2
   3401 #define SYS_umount2 166
   3402 #endif
   3403 #ifndef SYS_gettid
   3404 #define SYS_gettid 186
   3405 #endif
   3406 #ifndef SYS_time
   3407 #define SYS_time 201
   3408 #endif
   3409 #ifndef SYS_futex
   3410 #define SYS_futex 202
   3411 #endif
   3412 #ifndef SYS_getdents64
   3413 #define SYS_getdents64 217
   3414 #endif
   3415 #ifndef SYS_set_tid_address
   3416 #define SYS_set_tid_address 218
   3417 #endif
   3418 #ifndef SYS_clock_gettime
   3419 #define SYS_clock_gettime 228
   3420 #endif
   3421 #ifndef SYS_clock_getres
   3422 #define SYS_clock_getres 229
   3423 #endif
   3424 #ifndef SYS_clock_nanosleep
   3425 #define SYS_clock_nanosleep 230
   3426 #endif
   3427 #ifndef SYS_exit_group
   3428 #define SYS_exit_group 231
   3429 #endif
   3430 #ifndef SYS_tgkill
   3431 #define SYS_tgkill 234
   3432 #endif
   3433 #ifndef SYS_utimes
   3434 #define SYS_utimes 235
   3435 #endif
   3436 #ifndef SYS_openat
   3437 #define SYS_openat 257
   3438 #endif
   3439 #ifndef SYS_mkdirat
   3440 #define SYS_mkdirat 258
   3441 #endif
   3442 #ifndef SYS_mknodat
   3443 #define SYS_mknodat 259
   3444 #endif
   3445 #ifndef SYS_fchownat
   3446 #define SYS_fchownat 260
   3447 #endif
   3448 #ifndef SYS_newfstatat
   3449 #define SYS_newfstatat 262
   3450 #endif
   3451 #ifndef SYS_unlinkat
   3452 #define SYS_unlinkat 263
   3453 #endif
   3454 #ifndef SYS_renameat
   3455 #define SYS_renameat 264
   3456 #endif
   3457 #ifndef SYS_linkat
   3458 #define SYS_linkat 265
   3459 #endif
   3460 #ifndef SYS_symlinkat
   3461 #define SYS_symlinkat 266
   3462 #endif
   3463 #ifndef SYS_readlinkat
   3464 #define SYS_readlinkat 267
   3465 #endif
   3466 #ifndef SYS_fchmodat
   3467 #define SYS_fchmodat 268
   3468 #endif
   3469 #ifndef SYS_faccessat
   3470 #define SYS_faccessat 269
   3471 #endif
   3472 #ifndef SYS_ppoll
   3473 #define SYS_ppoll 271
   3474 #endif
   3475 #ifndef SYS_utimensat
   3476 #define SYS_utimensat 280
   3477 #endif
   3478 #ifndef SYS_accept4
   3479 #define SYS_accept4 288
   3480 #endif
   3481 #ifndef SYS_dup3
   3482 #define SYS_dup3 292
   3483 #endif
   3484 #ifndef SYS_pipe2
   3485 #define SYS_pipe2 293
   3486 #endif
   3487 #ifndef SYS_getrandom
   3488 #define SYS_getrandom 318
   3489 #endif
   3490 #ifndef SYS_copy_file_range
   3491 #define SYS_copy_file_range 326
   3492 #endif
   3493 #ifndef SYS_setsid
   3494 #define SYS_setsid 112
   3495 #endif
   3496 #ifndef SYS_fchmodat2
   3497 #define SYS_fchmodat2 452
   3498 #endif
   3499 /* ---- more of unistd, fcntl, sys/...: the thin system calls [chamesle] ---- */
   3500 
   3501 #ifndef O_NONBLOCK
   3502 #define O_NONBLOCK 04000
   3503 #endif
   3504 #ifndef O_NOCTTY
   3505 #define O_NOCTTY 0400
   3506 #endif
   3507 #ifndef O_SYNC
   3508 #define O_SYNC 04010000
   3509 #endif
   3510 #ifndef O_DSYNC
   3511 #define O_DSYNC 010000
   3512 #endif
   3513 #ifndef O_NOFOLLOW
   3514 #define O_NOFOLLOW 0400000
   3515 #endif
   3516 #ifndef O_DIRECTORY
   3517 #define O_DIRECTORY 0200000
   3518 #endif
   3519 #ifndef O_NOATIME
   3520 #define O_NOATIME 01000000
   3521 #endif
   3522 #ifndef O_LARGEFILE
   3523 #define O_LARGEFILE 0
   3524 #endif
   3525 #ifndef O_ACCMODE
   3526 #define O_ACCMODE 3
   3527 #endif
   3528 #define O_NDELAY O_NONBLOCK
   3529 #define F_DUPFD 0
   3530 #define F_GETFD 1
   3531 #define F_SETFD 2
   3532 #define F_GETFL 3
   3533 #define F_SETFL 4
   3534 #define F_GETLK 5
   3535 #define F_SETLK 6
   3536 #define F_SETLKW 7
   3537 #define F_DUPFD_CLOEXEC 1030
   3538 #define FD_CLOEXEC 1
   3539 #define F_RDLCK 0
   3540 #define F_WRLCK 1
   3541 #define F_UNLCK 2
   3542 #define F_OK 0
   3543 #define X_OK 1
   3544 #define W_OK 2
   3545 #define R_OK 4
   3546 #define STDIN_FILENO 0
   3547 #define STDOUT_FILENO 1
   3548 #define STDERR_FILENO 2
   3549 #define LOCK_SH 1
   3550 #define LOCK_EX 2
   3551 #define LOCK_NB 4
   3552 #define LOCK_UN 8
   3553 #define AT_EACCESS 0x200
   3554 #define AT_SYMLINK_FOLLOW 0x400
   3555 #ifndef AT_REMOVEDIR
   3556 #define AT_REMOVEDIR 0x200
   3557 #endif
   3558 #ifndef AT_SYMLINK_NOFOLLOW
   3559 #define AT_SYMLINK_NOFOLLOW 0x100
   3560 #endif
   3561 #define _SC_ARG_MAX 0
   3562 #define _SC_CHILD_MAX 1
   3563 #define _SC_CLK_TCK 2
   3564 #define _SC_NGROUPS_MAX 3
   3565 #define _SC_OPEN_MAX 4
   3566 #define _SC_PAGESIZE 30
   3567 #define _SC_PAGE_SIZE 30
   3568 #define _SC_NPROCESSORS_CONF 83
   3569 #define _SC_NPROCESSORS_ONLN 84
   3570 #define _SC_PHYS_PAGES 85
   3571 #define _SC_GETPW_R_SIZE_MAX 70
   3572 #define _SC_GETGR_R_SIZE_MAX 69
   3573 #define _SC_HOST_NAME_MAX 180
   3574 #define _SC_LINE_MAX 43
   3575 #define _POSIX_VERSION 200809L
   3576 #define _POSIX_PATH_MAX 256
   3577 #define NGROUPS_MAX 65536
   3578 #define HOST_NAME_MAX 64
   3579 #define LOGIN_NAME_MAX 256
   3580 #define NAME_MAX 255
   3581 #define PIPE_BUF 4096
   3582 #define IOV_MAX 1024
   3583 #define SSIZE_MAX LONG_MAX
   3584 #define CLOCKS_PER_SEC 1000000
   3585 
   3586 struct iovec { void *iov_base; size_t iov_len; };
   3587 ssize_t readv(int fd, const struct iovec *v, int n)
   3588 #ifdef C_DECLS_ONLY
   3589 ;
   3590 #else
   3591 { return fail(sys3(SYS_readv, fd, v, n)); }
   3592 #endif
   3593 ssize_t writev(int fd, const struct iovec *v, int n)
   3594 #ifdef C_DECLS_ONLY
   3595 ;
   3596 #else
   3597 { return fail(sys3(SYS_writev, fd, v, n)); }
   3598 #endif
   3599 
   3600 int dup3(int a, int b, int flags)
   3601 #ifdef C_DECLS_ONLY
   3602 ;
   3603 #else
   3604 { return fail(sys3(SYS_dup3, a, b, flags)); }
   3605 #endif
   3606 int pipe2(int fd[2], int flags)
   3607 #ifdef C_DECLS_ONLY
   3608 ;
   3609 #else
   3610 { return fail(sys2(SYS_pipe2, fd, flags)); }
   3611 #endif
   3612 int flock(int fd, int op)
   3613 #ifdef C_DECLS_ONLY
   3614 ;
   3615 #else
   3616 { return fail(sys2(SYS_flock, fd, op)); }
   3617 #endif
   3618 pid_t vfork(void)
   3619 #ifdef C_DECLS_ONLY
   3620 ;
   3621 #else
   3622 { return fork(); }
   3623 #endif
   3624 pid_t getpgrp(void)
   3625 #ifdef C_DECLS_ONLY
   3626 ;
   3627 #else
   3628 { return sys0(SYS_getpgrp); }
   3629 #endif
   3630 pid_t getpgid(pid_t p)
   3631 #ifdef C_DECLS_ONLY
   3632 ;
   3633 #else
   3634 { return fail(sys1(SYS_getpgid, p)); }
   3635 #endif
   3636 int setpgid(pid_t p, pid_t g)
   3637 #ifdef C_DECLS_ONLY
   3638 ;
   3639 #else
   3640 { return fail(sys2(SYS_setpgid, p, g)); }
   3641 #endif
   3642 int setpgrp(void)
   3643 #ifdef C_DECLS_ONLY
   3644 ;
   3645 #else
   3646 { return setpgid(0, 0); }
   3647 #endif
   3648 pid_t getsid(pid_t p)
   3649 #ifdef C_DECLS_ONLY
   3650 ;
   3651 #else
   3652 { return fail(sys1(SYS_getsid, p)); }
   3653 #endif
   3654 pid_t setsid(void)
   3655 #ifdef C_DECLS_ONLY
   3656 ;
   3657 #else
   3658 { return fail(sys0(SYS_setsid)); }
   3659 #endif
   3660 int setreuid(uid_t r, uid_t e)
   3661 #ifdef C_DECLS_ONLY
   3662 ;
   3663 #else
   3664 { return fail(sys2(SYS_setreuid, r, e)); }
   3665 #endif
   3666 int setregid(gid_t r, gid_t e)
   3667 #ifdef C_DECLS_ONLY
   3668 ;
   3669 #else
   3670 { return fail(sys2(SYS_setregid, r, e)); }
   3671 #endif
   3672 int setresuid(uid_t r, uid_t e, uid_t s)
   3673 #ifdef C_DECLS_ONLY
   3674 ;
   3675 #else
   3676 { return fail(sys3(SYS_setresuid, r, e, s)); }
   3677 #endif
   3678 int setresgid(gid_t r, gid_t e, gid_t s)
   3679 #ifdef C_DECLS_ONLY
   3680 ;
   3681 #else
   3682 { return fail(sys3(SYS_setresgid, r, e, s)); }
   3683 #endif
   3684 int seteuid(uid_t e)
   3685 #ifdef C_DECLS_ONLY
   3686 ;
   3687 #else
   3688 { return setresuid(-1, e, -1); }
   3689 #endif
   3690 int setegid(gid_t e)
   3691 #ifdef C_DECLS_ONLY
   3692 ;
   3693 #else
   3694 { return setresgid(-1, e, -1); }
   3695 #endif
   3696 int setgroups(size_t n, const gid_t *l)
   3697 #ifdef C_DECLS_ONLY
   3698 ;
   3699 #else
   3700 { return fail(sys2(SYS_setgroups, n, l)); }
   3701 #endif
   3702 pid_t gettid(void)
   3703 #ifdef C_DECLS_ONLY
   3704 ;
   3705 #else
   3706 { return sys0(SYS_gettid); }
   3707 #endif
   3708 unsigned alarm(unsigned s)
   3709 #ifdef C_DECLS_ONLY
   3710 ;
   3711 #else
   3712 {
   3713     struct { long ti[2], tv[2]; } nw = { { 0, 0 }, { s, 0 } }, old = { { 0, 0 }, { 0, 0 } };
   3714     if (fail(sys3(SYS_setitimer, 0, &nw, &old)) < 0) return 0;
   3715     return old.tv[0] + (old.tv[1] ? 1 : 0);
   3716 }
   3717 #endif
   3718 int pause(void)
   3719 #ifdef C_DECLS_ONLY
   3720 ;
   3721 #else
   3722 { return fail(sys0(SYS_pause)); }
   3723 #endif
   3724 unsigned usleep(unsigned us)
   3725 #ifdef C_DECLS_ONLY
   3726 ;
   3727 #else
   3728 { struct timespec t = { us / 1000000, (us % 1000000) * 1000L }; return nanosleep(&t, NULL); }
   3729 #endif
   3730 int clock_getres(int clk, struct timespec *t)
   3731 #ifdef C_DECLS_ONLY
   3732 ;
   3733 #else
   3734 { return fail(sys2(SYS_clock_getres, clk, t)); }
   3735 #endif
   3736 int clock_nanosleep(int clk, int flags, const struct timespec *t, struct timespec *rem)
   3737 #ifdef C_DECLS_ONLY
   3738 ;
   3739 #else
   3740 { long r = sys4(SYS_clock_nanosleep, clk, flags, t, rem); return r < 0 && r > -4096 ? -r : 0; }
   3741 #endif
   3742 int getpagesize(void)
   3743 #ifdef C_DECLS_ONLY
   3744 ;
   3745 #else
   3746 { return 4096; }
   3747 #endif
   3748 int nice(int inc)
   3749 #ifdef C_DECLS_ONLY
   3750 ;
   3751 #else
   3752 { int cur = getpriority(0, 0); if (cur < 0) return -1; return setpriority(0, 0, 20 - cur + inc) < 0 ? -1 : 20 - getpriority(0, 0); }
   3753 #endif
   3754 int fchmodat(int dirfd, const char *p, mode_t m, int flags)
   3755 #ifdef C_DECLS_ONLY
   3756 ;
   3757 #else
   3758 { (void)flags; return fail(sys3(SYS_fchmodat, dirfd, p, m)); }
   3759 #endif
   3760 int fchownat(int dirfd, const char *p, uid_t u, gid_t g, int flags)
   3761 #ifdef C_DECLS_ONLY
   3762 ;
   3763 #else
   3764 { return fail(sys6(SYS_fchownat, dirfd, (long)p, u, g, flags, 0)); }
   3765 #endif
   3766 int faccessat(int dirfd, const char *p, int mode, int flags)
   3767 #ifdef C_DECLS_ONLY
   3768 ;
   3769 #else
   3770 { (void)flags; return fail(sys3(SYS_faccessat, dirfd, p, mode)); }
   3771 #endif
   3772 int renameat(int a, const char *p, int b, const char *q)
   3773 #ifdef C_DECLS_ONLY
   3774 ;
   3775 #else
   3776 { return fail(sys4(SYS_renameat, a, p, b, q)); }
   3777 #endif
   3778 int linkat(int a, const char *p, int b, const char *q, int flags)
   3779 #ifdef C_DECLS_ONLY
   3780 ;
   3781 #else
   3782 { return fail(sys6(SYS_linkat, a, (long)p, b, (long)q, flags, 0)); }
   3783 #endif
   3784 int symlinkat(const char *t, int dirfd, const char *p)
   3785 #ifdef C_DECLS_ONLY
   3786 ;
   3787 #else
   3788 { return fail(sys3(SYS_symlinkat, t, dirfd, p)); }
   3789 #endif
   3790 ssize_t readlinkat(int dirfd, const char *p, char *buf, size_t n)
   3791 #ifdef C_DECLS_ONLY
   3792 ;
   3793 #else
   3794 { return fail(sys4(SYS_readlinkat, dirfd, p, buf, n)); }
   3795 #endif
   3796 int mknodat(int dirfd, const char *p, mode_t m, dev_t d)
   3797 #ifdef C_DECLS_ONLY
   3798 ;
   3799 #else
   3800 { return fail(sys4(SYS_mknodat, dirfd, p, m, d)); }
   3801 #endif
   3802 int mkfifoat(int dirfd, const char *p, mode_t m)
   3803 #ifdef C_DECLS_ONLY
   3804 ;
   3805 #else
   3806 { return mknodat(dirfd, p, (m & 07777) | S_IFIFO, 0); }
   3807 #endif
   3808 int fstatfs(int fd, struct statfs *s)
   3809 #ifdef C_DECLS_ONLY
   3810 ;
   3811 #else
   3812 { return fail(sys2(SYS_fstatfs, fd, s)); }
   3813 #endif
   3814 int utimes(const char *path, const struct timeval tv[2])
   3815 #ifdef C_DECLS_ONLY
   3816 ;
   3817 #else
   3818 {
   3819     struct timespec t[2];
   3820     if (!tv) return utimensat(AT_FDCWD, path, NULL, 0);
   3821     t[0].tv_sec = tv[0].tv_sec; t[0].tv_nsec = tv[0].tv_usec * 1000;
   3822     t[1].tv_sec = tv[1].tv_sec; t[1].tv_nsec = tv[1].tv_usec * 1000;
   3823     return utimensat(AT_FDCWD, path, t, 0);
   3824 }
   3825 #endif
   3826 int futimens(int fd, const struct timespec t[2])
   3827 #ifdef C_DECLS_ONLY
   3828 ;
   3829 #else
   3830 { return fail(sys6(SYS_utimensat, fd, 0, (long)t, 0, 0, 0)); }
   3831 #endif
   3832 struct utimbuf { time_t actime, modtime; };
   3833 int utime(const char *path, const struct utimbuf *u)
   3834 #ifdef C_DECLS_ONLY
   3835 ;
   3836 #else
   3837 {
   3838     struct timespec t[2];
   3839     if (!u) return utimensat(AT_FDCWD, path, NULL, 0);
   3840     t[0].tv_sec = u->actime; t[0].tv_nsec = 0; t[1].tv_sec = u->modtime; t[1].tv_nsec = 0;
   3841     return utimensat(AT_FDCWD, path, t, 0);
   3842 }
   3843 #endif
   3844 int lockf(int fd, int cmd, off_t len)
   3845 #ifdef C_DECLS_ONLY
   3846 ;
   3847 #else
   3848 {
   3849     (void)len;
   3850     return flock(fd, cmd == 0 ? LOCK_UN : cmd == 1 || cmd == 2 ? LOCK_EX : cmd == 3 ? LOCK_EX | LOCK_NB : LOCK_UN);
   3851 }
   3852 #endif
   3853 int posix_fadvise(int fd, off_t o, off_t l, int a)
   3854 #ifdef C_DECLS_ONLY
   3855 ;
   3856 #else
   3857 { (void)fd; (void)o; (void)l; (void)a; return 0; }
   3858 #endif
   3859 int posix_fallocate(int fd, off_t o, off_t l)
   3860 #ifdef C_DECLS_ONLY
   3861 ;
   3862 #else
   3863 { return fail(sys4(285, fd, 0, o, l)) < 0 ? errno : 0; }
   3864 #endif
   3865 ssize_t sendfile(int out, int in, off_t *off, size_t n)
   3866 #ifdef C_DECLS_ONLY
   3867 ;
   3868 #else
   3869 { return fail(sys4(SYS_sendfile, out, in, off, n)); }
   3870 #endif
   3871 ssize_t copy_file_range(int in, off_t *io, int out, off_t *oo, size_t n, unsigned flags)
   3872 #ifdef C_DECLS_ONLY
   3873 ;
   3874 #else
   3875 { return fail(sys6(SYS_copy_file_range, in, (long)io, out, (long)oo, n, flags)); }
   3876 #endif
   3877 
   3878 int gethostname(char *buf, size_t n)
   3879 #ifdef C_DECLS_ONLY
   3880 ;
   3881 #else
   3882 {
   3883     struct utsname u;
   3884     if (uname(&u) < 0) return -1;
   3885     if (strlen(u.nodename) >= n) { errno = ENAMETOOLONG; return -1; }
   3886     strcpy(buf, u.nodename);
   3887     return 0;
   3888 }
   3889 #endif
   3890 int getdomainname(char *buf, size_t n)
   3891 #ifdef C_DECLS_ONLY
   3892 ;
   3893 #else
   3894 { struct utsname u; if (uname(&u) < 0) return -1; strncpy(buf, u.domainname, n); return 0; }
   3895 #endif
   3896 char *getlogin(void)
   3897 #ifdef C_DECLS_ONLY
   3898 ;
   3899 #else
   3900 { struct passwd *p = getpwuid(getuid()); return p ? p->pw_name : getenv("LOGNAME"); }
   3901 #endif
   3902 int getlogin_r(char *buf, size_t n)
   3903 #ifdef C_DECLS_ONLY
   3904 ;
   3905 #else
   3906 { char *l = getlogin(); if (!l) return ENOENT; strncpy(buf, l, n); return 0; }
   3907 #endif
   3908 char *ttyname(int fd)
   3909 #ifdef C_DECLS_ONLY
   3910 ;
   3911 #else
   3912 {
   3913     static char buf[64];
   3914     char p[32];
   3915     ssize_t n;
   3916     snprintf(p, sizeof p, "/proc/self/fd/%d", fd);
   3917     n = readlink(p, buf, sizeof buf - 1);
   3918     if (n < 0) return NULL;
   3919     buf[n] = 0;
   3920     return buf;
   3921 }
   3922 #endif
   3923 int ttyname_r(int fd, char *buf, size_t n)
   3924 #ifdef C_DECLS_ONLY
   3925 ;
   3926 #else
   3927 { char *t = ttyname(fd); if (!t) return errno; strncpy(buf, t, n); return 0; }
   3928 #endif
   3929 char *ctermid(char *s)
   3930 #ifdef C_DECLS_ONLY
   3931 ;
   3932 #else
   3933 { static char d[] = "/dev/tty"; if (!s) return d; strcpy(s, d); return s; }
   3934 #endif
   3935 pid_t tcgetpgrp(int fd)
   3936 #ifdef C_DECLS_ONLY
   3937 ;
   3938 #else
   3939 { int p; if (ioctl(fd, 0x540F, &p) < 0) return -1; return p; }
   3940 #endif
   3941 int tcsetpgrp(int fd, pid_t p)
   3942 #ifdef C_DECLS_ONLY
   3943 ;
   3944 #else
   3945 { return ioctl(fd, 0x5410, &p); }
   3946 #endif
   3947 pid_t tcgetsid(int fd)
   3948 #ifdef C_DECLS_ONLY
   3949 ;
   3950 #else
   3951 { int p; if (ioctl(fd, 0x5429, &p) < 0) return -1; return p; }
   3952 #endif
   3953 long pathconf(const char *p, int name)
   3954 #ifdef C_DECLS_ONLY
   3955 ;
   3956 #else
   3957 { (void)p; (void)name; return name == 3 ? 255 : 4096; }
   3958 #endif
   3959 long fpathconf(int fd, int name)
   3960 #ifdef C_DECLS_ONLY
   3961 ;
   3962 #else
   3963 { (void)fd; return pathconf("/", name); }
   3964 #endif
   3965 char *getpass(const char *prompt)
   3966 #ifdef C_DECLS_ONLY
   3967 ;
   3968 #else
   3969 { static char buf[128]; fputs(prompt, stderr); if (!fgets(buf, sizeof buf, stdin)) return NULL; buf[strcspn(buf, "\n")] = 0; return buf; }
   3970 #endif
   3971 int daemon(int nochdir, int noclose)
   3972 #ifdef C_DECLS_ONLY
   3973 ;
   3974 #else
   3975 {
   3976     pid_t p = fork();
   3977     if (p < 0) return -1;
   3978     if (p) _exit(0);
   3979     if (setsid() < 0) return -1;
   3980     if (!nochdir) chdir("/");
   3981     if (!noclose) { int fd = open("/dev/null", O_RDWR); if (fd >= 0) { dup2(fd, 0); dup2(fd, 1); dup2(fd, 2); if (fd > 2) close(fd); } }
   3982     return 0;
   3983 }
   3984 #endif
   3985 
   3986 /* exec and spawn */
   3987 extern char **environ;
   3988 int execv(const char *path, char *const argv[])
   3989 #ifdef C_DECLS_ONLY
   3990 ;
   3991 #else
   3992 { return execve(path, argv, environ); }
   3993 #endif
   3994 int execvpe(const char *file, char *const argv[], char *const envp[])
   3995 #ifdef C_DECLS_ONLY
   3996 ;
   3997 #else
   3998 {
   3999     char **save = environ;
   4000     int r;
   4001     environ = (char **)envp;
   4002     r = execvp(file, argv);
   4003     environ = save;
   4004     return r;
   4005 }
   4006 #endif
   4007 int execle(const char *path, const char *arg, ...)
   4008 #ifdef C_DECLS_ONLY
   4009 ;
   4010 #else
   4011 {
   4012     char *av[256];
   4013     char **envp;
   4014     int n = 0;
   4015     va_list ap;
   4016     va_start(ap, arg);
   4017     av[n++] = (char *)arg;
   4018     while (n < 255 && (av[n] = va_arg(ap, char *))) n++;
   4019     envp = va_arg(ap, char **);
   4020     va_end(ap);
   4021     return execve(path, av, envp);
   4022 }
   4023 #endif
   4024 int execlp(const char *file, const char *arg, ...)
   4025 #ifdef C_DECLS_ONLY
   4026 ;
   4027 #else
   4028 {
   4029     char *av[256];
   4030     int n = 0;
   4031     va_list ap;
   4032     va_start(ap, arg);
   4033     av[n++] = (char *)arg;
   4034     while (n < 255 && (av[n] = va_arg(ap, char *))) n++;
   4035     va_end(ap);
   4036     return execvp(file, av);
   4037 }
   4038 #endif
   4039 int system(const char *cmd)
   4040 #ifdef C_DECLS_ONLY
   4041 ;
   4042 #else
   4043 {
   4044     pid_t pid;
   4045     int st;
   4046     if (!cmd) return 1;
   4047     fflush(NULL);
   4048     pid = fork();
   4049     if (pid < 0) return -1;
   4050     if (!pid) {
   4051         char *av[] = { "sh", "-c", (char *)cmd, NULL };
   4052         execve("/bin/sh", av, environ);
   4053         _exit(127);
   4054     }
   4055     while (waitpid(pid, &st, 0) < 0) if (errno != EINTR) return -1;
   4056     return st;
   4057 }
   4058 #endif
   4059 FILE *popen(const char *cmd, const char *mode)
   4060 #ifdef C_DECLS_ONLY
   4061 ;
   4062 #else
   4063 {
   4064     int p[2], parent_end, child_end;
   4065     pid_t pid;
   4066     FILE *f;
   4067     int rd = mode[0] == 'r';
   4068     if (pipe(p) < 0) return NULL;
   4069     parent_end = rd ? p[0] : p[1]; child_end = rd ? p[1] : p[0];
   4070     fflush(NULL);
   4071     pid = fork();
   4072     if (pid < 0) { close(p[0]); close(p[1]); return NULL; }
   4073     if (!pid) {
   4074         char *av[] = { "sh", "-c", (char *)cmd, NULL };
   4075         close(parent_end);
   4076         if (child_end != (rd ? 1 : 0)) { dup2(child_end, rd ? 1 : 0); close(child_end); }
   4077         execve("/bin/sh", av, environ);
   4078         _exit(127);
   4079     }
   4080     close(child_end);
   4081     f = fdopen(parent_end, rd ? "r" : "w");
   4082     if (!f) { close(parent_end); return NULL; }
   4083     f->pid = pid;
   4084     return f;
   4085 }
   4086 #endif
   4087 int pclose(FILE *f)
   4088 #ifdef C_DECLS_ONLY
   4089 ;
   4090 #else
   4091 {
   4092     int st;
   4093     pid_t pid = f->pid;
   4094     fclose(f);
   4095     while (waitpid(pid, &st, 0) < 0) if (errno != EINTR) return -1;
   4096     return st;
   4097 }
   4098 #endif
   4099 
   4100 /* wait */
   4101 #define WNOHANG 1
   4102 #define WUNTRACED 2
   4103 #define WCONTINUED 8
   4104 #define WEXITSTATUS(s) (((s) >> 8) & 0xff)
   4105 #define WTERMSIG(s) ((s) & 0x7f)
   4106 #define WSTOPSIG(s) WEXITSTATUS(s)
   4107 #define WIFEXITED(s) (!WTERMSIG(s))
   4108 #define WIFSIGNALED(s) ((signed char)(((s) & 0x7f) + 1) >> 1 > 0)
   4109 #define WIFSTOPPED(s) (((s) & 0xff) == 0x7f)
   4110 #define WIFCONTINUED(s) ((s) == 0xffff)
   4111 #define WCOREDUMP(s) ((s) & 0x80)
   4112 struct rusage { struct timeval ru_utime, ru_stime; long ru_maxrss, ru_ixrss, ru_idrss, ru_isrss, ru_minflt, ru_majflt, ru_nswap, ru_inblock, ru_oublock, ru_msgsnd, ru_msgrcv, ru_nsignals, ru_nvcsw, ru_nivcsw; };
   4113 #define RUSAGE_SELF 0
   4114 #define RUSAGE_CHILDREN (-1)
   4115 pid_t wait3(int *st, int options, struct rusage *ru)
   4116 #ifdef C_DECLS_ONLY
   4117 ;
   4118 #else
   4119 { return fail(sys4(SYS_wait4, -1, st, options, ru)); }
   4120 #endif
   4121 pid_t wait4(pid_t pid, int *st, int options, struct rusage *ru)
   4122 #ifdef C_DECLS_ONLY
   4123 ;
   4124 #else
   4125 { return fail(sys4(SYS_wait4, pid, st, options, ru)); }
   4126 #endif
   4127 int getrusage(int who, struct rusage *ru)
   4128 #ifdef C_DECLS_ONLY
   4129 ;
   4130 #else
   4131 { return fail(sys2(SYS_getrusage, who, ru)); }
   4132 #endif
   4133 struct rlimit { unsigned long rlim_cur, rlim_max; };
   4134 typedef unsigned long rlim_t;
   4135 #define RLIM_INFINITY (~0UL)
   4136 #define RLIMIT_CPU 0
   4137 #define RLIMIT_FSIZE 1
   4138 #define RLIMIT_DATA 2
   4139 #define RLIMIT_STACK 3
   4140 #define RLIMIT_CORE 4
   4141 #define RLIMIT_NOFILE 7
   4142 #define RLIMIT_AS 9
   4143 int getrlimit(int r, struct rlimit *l)
   4144 #ifdef C_DECLS_ONLY
   4145 ;
   4146 #else
   4147 { return fail(sys2(SYS_getrlimit, r, l)); }
   4148 #endif
   4149 int setrlimit(int r, const struct rlimit *l)
   4150 #ifdef C_DECLS_ONLY
   4151 ;
   4152 #else
   4153 { return fail(sys2(SYS_setrlimit, r, l)); }
   4154 #endif
   4155 struct tms { long tms_utime, tms_stime, tms_cutime, tms_cstime; };
   4156 clock_t times(struct tms *t)
   4157 #ifdef C_DECLS_ONLY
   4158 ;
   4159 #else
   4160 { return fail(sys1(SYS_times, t)); }
   4161 #endif
   4162 clock_t clock(void)
   4163 #ifdef C_DECLS_ONLY
   4164 ;
   4165 #else
   4166 { struct timespec t; if (clock_gettime(2, &t) < 0) return -1; return t.tv_sec * 1000000L + t.tv_nsec / 1000; }
   4167 #endif
   4168 struct sysinfo { long uptime; unsigned long loads[3], totalram, freeram, sharedram, bufferram, totalswap, freeswap; unsigned short procs, pad; unsigned long totalhigh, freehigh; unsigned mem_unit; char f[4]; };
   4169 int sysinfo(struct sysinfo *s)
   4170 #ifdef C_DECLS_ONLY
   4171 ;
   4172 #else
   4173 { return fail(sys1(SYS_sysinfo, s)); }
   4174 #endif
   4175 int get_nprocs(void)
   4176 #ifdef C_DECLS_ONLY
   4177 ;
   4178 #else
   4179 { unsigned long m[16]; long r = sys3(SYS_sched_getaffinity, 0, sizeof m, m); int i, n = 0; if (r < 0) return 1; for (i = 0; i < 16; i++) n += __builtin_popcountl(m[i]); return n ? n : 1; }
   4180 #endif
   4181 int prctl(int op, unsigned long a, unsigned long b, unsigned long c, unsigned long d)
   4182 #ifdef C_DECLS_ONLY
   4183 ;
   4184 #else
   4185 { return fail(sys6(SYS_prctl, op, a, b, c, d, 0)); }
   4186 #endif
   4187 
   4188 /* memory */
   4189 #ifndef MAP_PRIVATE
   4190 #define MAP_SHARED 1
   4191 #define MAP_PRIVATE 2
   4192 #endif
   4193 #ifndef MAP_ANONYMOUS
   4194 #define MAP_FIXED 0x10
   4195 #define MAP_ANONYMOUS 0x20
   4196 #define MAP_ANON MAP_ANONYMOUS
   4197 #define MAP_NORESERVE 0x4000
   4198 #define MAP_POPULATE 0x8000
   4199 #endif
   4200 #define MAP_FAILED ((void *)-1)
   4201 #define PROT_NONE 0
   4202 #define MS_ASYNC 1
   4203 #define MS_INVALIDATE 2
   4204 #define MS_SYNC 4
   4205 #define MADV_NORMAL 0
   4206 #define MADV_RANDOM 1
   4207 #define MADV_SEQUENTIAL 2
   4208 #define MADV_WILLNEED 3
   4209 #define MADV_DONTNEED 4
   4210 void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off)
   4211 #ifdef C_DECLS_ONLY
   4212 ;
   4213 #else
   4214 {
   4215 #ifdef __i386__
   4216     long r = sys6(SYS_mmap, (long)addr, len, prot, flags, fd, (long)(off >> 12));  /* mmap2 counts in pages */
   4217 #else
   4218     long r = sys6(SYS_mmap, (long)addr, len, prot, flags, fd, off);
   4219 #endif
   4220     if (r < 0 && r > -4096) { errno = -r; return MAP_FAILED; }
   4221     return (void *)r;
   4222 }
   4223 #endif
   4224 int munmap(void *a, size_t n)
   4225 #ifdef C_DECLS_ONLY
   4226 ;
   4227 #else
   4228 { return fail(sys2(SYS_munmap, a, n)); }
   4229 #endif
   4230 int msync(void *a, size_t n, int f)
   4231 #ifdef C_DECLS_ONLY
   4232 ;
   4233 #else
   4234 { return fail(sys3(SYS_msync, a, n, f)); }
   4235 #endif
   4236 int madvise(void *a, size_t n, int adv)
   4237 #ifdef C_DECLS_ONLY
   4238 ;
   4239 #else
   4240 { return fail(sys3(SYS_madvise, a, n, adv)); }
   4241 #endif
   4242 int posix_madvise(void *a, size_t n, int adv)
   4243 #ifdef C_DECLS_ONLY
   4244 ;
   4245 #else
   4246 { return madvise(a, n, adv); }
   4247 #endif
   4248 int mlock(const void *a, size_t n)
   4249 #ifdef C_DECLS_ONLY
   4250 ;
   4251 #else
   4252 { (void)a; (void)n; return 0; }
   4253 #endif
   4254 int munlock(const void *a, size_t n)
   4255 #ifdef C_DECLS_ONLY
   4256 ;
   4257 #else
   4258 { (void)a; (void)n; return 0; }
   4259 #endif
   4260 
   4261 /* the ioctl / termios corner */
   4262 #define TIOCGWINSZ 0x5413
   4263 #define TIOCSWINSZ 0x5414
   4264 #define FIONREAD 0x541B
   4265 #define FIONBIO 0x5421
   4266 #define TIOCSCTTY 0x540E
   4267 struct winsize { unsigned short ws_row, ws_col, ws_xpixel, ws_ypixel; };
   4268 #define B0 0
   4269 #define B9600 015
   4270 #define B19200 016
   4271 #define B38400 017
   4272 #define B57600 010001
   4273 #define B115200 010002
   4274 #define TCSANOW 0
   4275 #define TCSADRAIN 1
   4276 #define TCSAFLUSH 2
   4277 #define TCIFLUSH 0
   4278 #define TCOFLUSH 1
   4279 #define TCIOFLUSH 2
   4280 #define TCOOFF 0
   4281 #define TCOON 1
   4282 typedef unsigned int speed_t;
   4283 typedef unsigned char cc_t;
   4284 typedef unsigned int tcflag_t;
   4285 #define VINTR 0
   4286 #define VQUIT 1
   4287 #define VERASE 2
   4288 #define VKILL 3
   4289 #define VEOF 4
   4290 #define VTIME 5
   4291 #define VMIN 6
   4292 #define VSTART 8
   4293 #define VSTOP 9
   4294 #define VSUSP 10
   4295 #define NCCS 32
   4296 #define IGNBRK 1
   4297 #define BRKINT 2
   4298 #define IGNPAR 4
   4299 #define PARMRK 8
   4300 #define INPCK 16
   4301 #define ISTRIP 32
   4302 #define INLCR 64
   4303 #define IGNCR 128
   4304 #define ECHONL 0100
   4305 #define IXON 02000
   4306 #define IXANY 04000
   4307 #define IXOFF 010000
   4308 #define CSIZE 060
   4309 #define CS5 0
   4310 #define CS6 020
   4311 #define CS7 040
   4312 #define CS8 060
   4313 #define CSTOPB 0100
   4314 #define CREAD 0200
   4315 #define PARENB 0400
   4316 #define PARODD 01000
   4317 #define HUPCL 02000
   4318 #define CLOCAL 04000
   4319 #define CBAUD 010017
   4320 int tcflush(int fd, int q)
   4321 #ifdef C_DECLS_ONLY
   4322 ;
   4323 #else
   4324 { return ioctl(fd, 0x540B, (void *)(long)q); }
   4325 #endif
   4326 int tcdrain(int fd)
   4327 #ifdef C_DECLS_ONLY
   4328 ;
   4329 #else
   4330 { return ioctl(fd, 0x5409, (void *)1L); }
   4331 #endif
   4332 int tcflow(int fd, int a)
   4333 #ifdef C_DECLS_ONLY
   4334 ;
   4335 #else
   4336 { return ioctl(fd, 0x540A, (void *)(long)a); }
   4337 #endif
   4338 int tcsendbreak(int fd, int d)
   4339 #ifdef C_DECLS_ONLY
   4340 ;
   4341 #else
   4342 { (void)d; return ioctl(fd, 0x5409, (void *)0L); }
   4343 #endif
   4344 speed_t cfgetospeed(const struct termios *t)
   4345 #ifdef C_DECLS_ONLY
   4346 ;
   4347 #else
   4348 { return t->c_cflag & CBAUD; }
   4349 #endif
   4350 speed_t cfgetispeed(const struct termios *t)
   4351 #ifdef C_DECLS_ONLY
   4352 ;
   4353 #else
   4354 { return t->c_cflag & CBAUD; }
   4355 #endif
   4356 int cfsetospeed(struct termios *t, speed_t s)
   4357 #ifdef C_DECLS_ONLY
   4358 ;
   4359 #else
   4360 { t->c_cflag = (t->c_cflag & ~CBAUD) | s; return 0; }
   4361 #endif
   4362 int cfsetispeed(struct termios *t, speed_t s)
   4363 #ifdef C_DECLS_ONLY
   4364 ;
   4365 #else
   4366 { (void)t; (void)s; return 0; }
   4367 #endif
   4368 int cfsetspeed(struct termios *t, speed_t s)
   4369 #ifdef C_DECLS_ONLY
   4370 ;
   4371 #else
   4372 { return cfsetospeed(t, s); }
   4373 #endif
   4374 void cfmakeraw(struct termios *t)
   4375 #ifdef C_DECLS_ONLY
   4376 ;
   4377 #else
   4378 {
   4379     t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
   4380     t->c_oflag &= ~OPOST;
   4381     t->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
   4382     t->c_cflag &= ~(CSIZE | PARENB);
   4383     t->c_cflag |= CS8;
   4384     t->c_cc[VMIN] = 1; t->c_cc[VTIME] = 0;
   4385 }
   4386 #endif
   4387 int posix_openpt(int flags)
   4388 #ifdef C_DECLS_ONLY
   4389 ;
   4390 #else
   4391 { return open("/dev/ptmx", flags); }
   4392 #endif
   4393 int grantpt(int fd)
   4394 #ifdef C_DECLS_ONLY
   4395 ;
   4396 #else
   4397 { (void)fd; return 0; }
   4398 #endif
   4399 int unlockpt(int fd)
   4400 #ifdef C_DECLS_ONLY
   4401 ;
   4402 #else
   4403 { int z = 0; return ioctl(fd, 0x40045431, &z); }
   4404 #endif
   4405 char *ptsname(int fd)
   4406 #ifdef C_DECLS_ONLY
   4407 ;
   4408 #else
   4409 {
   4410     static char buf[32];
   4411     int n;
   4412     if (ioctl(fd, 0x80045430, &n) < 0) return NULL;
   4413     snprintf(buf, sizeof buf, "/dev/pts/%d", n);
   4414     return buf;
   4415 }
   4416 #endif
   4417 
   4418 /* getopt */
   4419 #ifdef C_DECLS_ONLY
   4420 extern char *optarg;
   4421 #else
   4422 char *optarg;
   4423 #endif
   4424 #ifdef C_DECLS_ONLY
   4425 extern int optind;
   4426 #else
   4427 int optind = 1, opterr = 1, optopt;
   4428 #endif
   4429 #ifndef C_DECLS_ONLY
   4430 static int c_optpos;
   4431 #endif
   4432 int getopt(int argc, char *const argv[], const char *shorts)
   4433 #ifdef C_DECLS_ONLY
   4434 ;
   4435 #else
   4436 {
   4437     const char *p;
   4438     char *a;
   4439     int c;
   4440     if (optind == 0) { optind = 1; c_optpos = 0; }
   4441     if (!c_optpos) {
   4442         if (optind >= argc) return -1;
   4443         a = argv[optind];
   4444         if (a[0] != '-' || !a[1]) return -1;
   4445         if (a[1] == '-' && !a[2]) { optind++; return -1; }
   4446         c_optpos = 1;
   4447     }
   4448     a = argv[optind];
   4449     c = a[c_optpos++];
   4450     p = strchr(shorts[0] == '+' || shorts[0] == ':' ? shorts + 1 : shorts, c);
   4451     if (!a[c_optpos]) { optind++; c_optpos = 0; }
   4452     if (!p || c == ':') {
   4453         optopt = c;
   4454         if (opterr && shorts[0] != ':') fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], c);
   4455         return '?';
   4456     }
   4457     if (p[1] == ':') {
   4458         if (c_optpos) { optarg = argv[optind] + c_optpos; optind++; c_optpos = 0; }
   4459         else if (p[2] == ':') optarg = NULL;
   4460         else if (optind >= argc) {
   4461             optopt = c;
   4462             if (opterr && shorts[0] != ':') fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], c);
   4463             return shorts[0] == ':' ? ':' : '?';
   4464         } else optarg = argv[optind++];
   4465     } else optarg = NULL;
   4466     return c;
   4467 }
   4468 #endif
   4469 struct option { const char *name; int has_arg; int *flag; int val; };
   4470 #define no_argument 0
   4471 #define required_argument 1
   4472 #define optional_argument 2
   4473 int getopt_long(int argc, char *const argv[], const char *shorts, const struct option *longs, int *idx)
   4474 #ifdef C_DECLS_ONLY
   4475 ;
   4476 #else
   4477 {
   4478     char *a;
   4479     const struct option *o, *hit = NULL;
   4480     size_t l;
   4481     char *eq;
   4482     if (optind == 0) { optind = 1; c_optpos = 0; }
   4483     if (c_optpos || optind >= argc) return getopt(argc, argv, shorts);
   4484     a = argv[optind];
   4485     if (a[0] != '-' || a[1] != '-') return getopt(argc, argv, shorts);
   4486     if (!a[2]) { optind++; return -1; }
   4487     eq = strchr(a + 2, '=');
   4488     l = eq ? (size_t)(eq - a - 2) : strlen(a + 2);
   4489     for (o = longs; o->name; o++) {
   4490         if (!strncmp(o->name, a + 2, l)) {
   4491             if (strlen(o->name) == l) { hit = o; break; }
   4492             if (!hit) hit = o;
   4493             else if (hit->has_arg != o->has_arg || hit->flag != o->flag || hit->val != o->val) { if (opterr) fprintf(stderr, "%s: option '%s' is ambiguous\n", argv[0], a); optind++; return '?'; }
   4494         }
   4495     }
   4496     if (!hit) { if (opterr) fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], a); optind++; return '?'; }
   4497     optind++;
   4498     if (idx) *idx = hit - longs;
   4499     if (hit->has_arg == no_argument) {
   4500         if (eq) { if (opterr) fprintf(stderr, "%s: option '--%s' doesn't allow an argument\n", argv[0], hit->name); return '?'; }
   4501         optarg = NULL;
   4502     } else if (eq) optarg = eq + 1;
   4503     else if (hit->has_arg == required_argument) {
   4504         if (optind >= argc) { if (opterr) fprintf(stderr, "%s: option '--%s' requires an argument\n", argv[0], hit->name); return shorts[0] == ':' ? ':' : '?'; }
   4505         optarg = argv[optind++];
   4506     } else optarg = NULL;
   4507     if (hit->flag) { *hit->flag = hit->val; return 0; }
   4508     return hit->val;
   4509 }
   4510 #endif
   4511 int getopt_long_only(int argc, char *const argv[], const char *shorts, const struct option *longs, int *idx)
   4512 #ifdef C_DECLS_ONLY
   4513 ;
   4514 #else
   4515 { return getopt_long(argc, argv, shorts, longs, idx); }
   4516 #endif
   4517 /* ---- string, ctype, stdlib, time: what real programs reach for [chamesle] ---- */
   4518 
   4519 /* strings */
   4520 char *strsep(char **sp, const char *delim)
   4521 #ifdef C_DECLS_ONLY
   4522 ;
   4523 #else
   4524 {
   4525     char *s = *sp, *e;
   4526     if (!s) return NULL;
   4527     e = s + strcspn(s, delim);
   4528     if (*e) *e++ = 0; else e = NULL;
   4529     *sp = e;
   4530     return s;
   4531 }
   4532 #endif
   4533 char *strtok_r(char *s, const char *delim, char **save)
   4534 #ifdef C_DECLS_ONLY
   4535 ;
   4536 #else
   4537 {
   4538     char *e;
   4539     if (!s) s = *save;
   4540     if (!s) return NULL;
   4541     s += strspn(s, delim);
   4542     if (!*s) { *save = NULL; return NULL; }
   4543     e = s + strcspn(s, delim);
   4544     if (*e) *e++ = 0; else e = NULL;
   4545     *save = e;
   4546     return s;
   4547 }
   4548 #endif
   4549 char *strtok(char *s, const char *delim)
   4550 #ifdef C_DECLS_ONLY
   4551 ;
   4552 #else
   4553 { static char *save; return strtok_r(s, delim, &save); }
   4554 #endif
   4555 size_t strnlen(const char *s, size_t n)
   4556 #ifdef C_DECLS_ONLY
   4557 ;
   4558 #else
   4559 { size_t i = 0; while (i < n && s[i]) i++; return i; }
   4560 #endif
   4561 size_t strlcpy(char *d, const char *s, size_t n)
   4562 #ifdef C_DECLS_ONLY
   4563 ;
   4564 #else
   4565 {
   4566     size_t l = strlen(s);
   4567     if (n) { size_t c = l < n - 1 ? l : n - 1; memcpy(d, s, c); d[c] = 0; }
   4568     return l;
   4569 }
   4570 #endif
   4571 size_t strlcat(char *d, const char *s, size_t n)
   4572 #ifdef C_DECLS_ONLY
   4573 ;
   4574 #else
   4575 {
   4576     size_t dl = strnlen(d, n), sl = strlen(s);
   4577     if (dl == n) return n + sl;
   4578     strlcpy(d + dl, s, n - dl);
   4579     return dl + sl;
   4580 }
   4581 #endif
   4582 char *strncat(char *d, const char *s, size_t n)
   4583 #ifdef C_DECLS_ONLY
   4584 ;
   4585 #else
   4586 { char *r = d; d += strlen(d); while (n-- && *s) *d++ = *s++; *d = 0; return r; }
   4587 #endif
   4588 char *stpncpy(char *d, const char *s, size_t n)
   4589 #ifdef C_DECLS_ONLY
   4590 ;
   4591 #else
   4592 { size_t l = strnlen(s, n); memcpy(d, s, l); memset(d + l, 0, n - l); return d + l; }
   4593 #endif
   4594 char *strchrnul(const char *s, int c)
   4595 #ifdef C_DECLS_ONLY
   4596 ;
   4597 #else
   4598 { while (*s && *s != (char)c) s++; return (char *)s; }
   4599 #endif
   4600 void *mempcpy(void *d, const void *s, size_t n)
   4601 #ifdef C_DECLS_ONLY
   4602 ;
   4603 #else
   4604 { return (char *)memcpy(d, s, n) + n; }
   4605 #endif
   4606 void *memccpy(void *d, const void *s, int c, size_t n)
   4607 #ifdef C_DECLS_ONLY
   4608 ;
   4609 #else
   4610 {
   4611     unsigned char *dd = d; const unsigned char *ss = s;
   4612     while (n--) { if ((*dd++ = *ss++) == (unsigned char)c) return dd; }
   4613     return NULL;
   4614 }
   4615 #endif
   4616 void *memmem(const void *h, size_t hl, const void *n, size_t nl)
   4617 #ifdef C_DECLS_ONLY
   4618 ;
   4619 #else
   4620 {
   4621     const char *p = h;
   4622     if (!nl) return (void *)h;
   4623     while (hl >= nl) {
   4624         const char *q = memchr(p, *(const char *)n, hl - nl + 1);
   4625         if (!q) return NULL;
   4626         if (!memcmp(q, n, nl)) return (void *)q;
   4627         hl -= q + 1 - p; p = q + 1;
   4628     }
   4629     return NULL;
   4630 }
   4631 #endif
   4632 char *strcasestr(const char *h, const char *n)
   4633 #ifdef C_DECLS_ONLY
   4634 ;
   4635 #else
   4636 {
   4637     size_t nl = strlen(n);
   4638     for (; *h; h++) if (!strncasecmp(h, n, nl)) return (char *)h;
   4639     return nl ? NULL : (char *)h;
   4640 }
   4641 #endif
   4642 size_t strxfrm(char *d, const char *s, size_t n)
   4643 #ifdef C_DECLS_ONLY
   4644 ;
   4645 #else
   4646 { size_t l = strlen(s); if (n) strlcpy(d, s, n); return l; }
   4647 #endif
   4648 int strverscmp(const char *a, const char *b)
   4649 #ifdef C_DECLS_ONLY
   4650 ;
   4651 #else
   4652 {
   4653     while (*a && *b) {
   4654         if (isdigit((unsigned char)*a) && isdigit((unsigned char)*b)) {
   4655             const char *ea = a, *eb = b;
   4656             while (*a == '0') a++;
   4657             while (*b == '0') b++;
   4658             ea = a; eb = b;
   4659             while (isdigit((unsigned char)*ea)) ea++;
   4660             while (isdigit((unsigned char)*eb)) eb++;
   4661             if (ea - a != eb - b) return ea - a < eb - b ? -1 : 1;
   4662             while (a < ea) { if (*a != *b) return (unsigned char)*a - (unsigned char)*b; a++; b++; }
   4663         } else {
   4664             if (*a != *b) return (unsigned char)*a - (unsigned char)*b;
   4665             a++; b++;
   4666         }
   4667     }
   4668     return (unsigned char)*a - (unsigned char)*b;
   4669 }
   4670 #endif
   4671 int ffs(int i)
   4672 #ifdef C_DECLS_ONLY
   4673 ;
   4674 #else
   4675 { return __builtin_ffs(i); }
   4676 #endif
   4677 int ffsl(long i)
   4678 #ifdef C_DECLS_ONLY
   4679 ;
   4680 #else
   4681 { return __builtin_ffsl(i); }
   4682 #endif
   4683 int ffsll(long long i)
   4684 #ifdef C_DECLS_ONLY
   4685 ;
   4686 #else
   4687 { return __builtin_ffsll(i); }
   4688 #endif
   4689 void bcopy(const void *s, void *d, size_t n)
   4690 #ifdef C_DECLS_ONLY
   4691 ;
   4692 #else
   4693 { memmove(d, s, n); }
   4694 #endif
   4695 void bzero(void *d, size_t n)
   4696 #ifdef C_DECLS_ONLY
   4697 ;
   4698 #else
   4699 { memset(d, 0, n); }
   4700 #endif
   4701 int bcmp(const void *a, const void *b, size_t n)
   4702 #ifdef C_DECLS_ONLY
   4703 ;
   4704 #else
   4705 { return memcmp(a, b, n); }
   4706 #endif
   4707 char *index(const char *s, int c)
   4708 #ifdef C_DECLS_ONLY
   4709 ;
   4710 #else
   4711 { return strchr(s, c); }
   4712 #endif
   4713 char *rindex(const char *s, int c)
   4714 #ifdef C_DECLS_ONLY
   4715 ;
   4716 #else
   4717 { return strrchr(s, c); }
   4718 #endif
   4719 void explicit_bzero(void *d, size_t n)
   4720 #ifdef C_DECLS_ONLY
   4721 ;
   4722 #else
   4723 { volatile char *p = d; while (n--) *p++ = 0; }
   4724 #endif
   4725 #ifndef C_DECLS_ONLY
   4726 static const char *const c_signames[] = { "Unknown signal 0", "Hangup", "Interrupt", "Quit", "Illegal instruction", "Trace/breakpoint trap", "Aborted",
   4727     "Bus error", "Floating point exception", "Killed", "User defined signal 1", "Segmentation fault", "User defined signal 2", "Broken pipe",
   4728     "Alarm clock", "Terminated", "Stack fault", "Child exited", "Continued", "Stopped (signal)", "Stopped", "Stopped (tty input)",
   4729     "Stopped (tty output)", "Urgent I/O condition", "CPU time limit exceeded", "File size limit exceeded", "Virtual timer expired",
   4730     "Profiling timer expired", "Window changed", "I/O possible", "Power failure", "Bad system call" };
   4731 #endif
   4732 char *strsignal(int s)
   4733 #ifdef C_DECLS_ONLY
   4734 ;
   4735 #else
   4736 { return (char *)(s > 0 && s < 32 ? c_signames[s] : "Unknown signal"); }
   4737 #endif
   4738 
   4739 /* ctype */
   4740 int isblank(int c)
   4741 #ifdef C_DECLS_ONLY
   4742 ;
   4743 #else
   4744 { return c == ' ' || c == '\t'; }
   4745 #endif
   4746 int iscntrl(int c)
   4747 #ifdef C_DECLS_ONLY
   4748 ;
   4749 #else
   4750 { return (c >= 0 && c < 32) || c == 127; }
   4751 #endif
   4752 int isgraph(int c)
   4753 #ifdef C_DECLS_ONLY
   4754 ;
   4755 #else
   4756 { return c > 32 && c < 127; }
   4757 #endif
   4758 int isascii(int c)
   4759 #ifdef C_DECLS_ONLY
   4760 ;
   4761 #else
   4762 { return c >= 0 && c < 128; }
   4763 #endif
   4764 int toascii(int c)
   4765 #ifdef C_DECLS_ONLY
   4766 ;
   4767 #else
   4768 { return c & 127; }
   4769 #endif
   4770 
   4771 /* numbers */
   4772 typedef struct { int quot, rem; } div_t;
   4773 typedef struct { long quot, rem; } ldiv_t;
   4774 typedef struct { long long quot, rem; } lldiv_t;
   4775 div_t div(int a, int b)
   4776 #ifdef C_DECLS_ONLY
   4777 ;
   4778 #else
   4779 { div_t r = { a / b, a % b }; return r; }
   4780 #endif
   4781 ldiv_t ldiv(long a, long b)
   4782 #ifdef C_DECLS_ONLY
   4783 ;
   4784 #else
   4785 { ldiv_t r = { a / b, a % b }; return r; }
   4786 #endif
   4787 lldiv_t lldiv(long long a, long long b)
   4788 #ifdef C_DECLS_ONLY
   4789 ;
   4790 #else
   4791 { lldiv_t r = { a / b, a % b }; return r; }
   4792 #endif
   4793 long long llabs(long long a)
   4794 #ifdef C_DECLS_ONLY
   4795 ;
   4796 #else
   4797 { return a < 0 ? -a : a; }
   4798 #endif
   4799 intmax_t imaxabs(intmax_t a)
   4800 #ifdef C_DECLS_ONLY
   4801 ;
   4802 #else
   4803 { return a < 0 ? -a : a; }
   4804 #endif
   4805 intmax_t strtoimax(const char *s, char **e, int base)
   4806 #ifdef C_DECLS_ONLY
   4807 ;
   4808 #else
   4809 { return strtoll(s, e, base); }
   4810 #endif
   4811 uintmax_t strtoumax(const char *s, char **e, int base)
   4812 #ifdef C_DECLS_ONLY
   4813 ;
   4814 #else
   4815 { return strtoull(s, e, base); }
   4816 #endif
   4817 #define RAND_MAX 2147483647
   4818 #ifndef C_DECLS_ONLY
   4819 static unsigned long long c_rand_state = 1;
   4820 #endif
   4821 void srandom(unsigned seed)
   4822 #ifdef C_DECLS_ONLY
   4823 ;
   4824 #else
   4825 { c_rand_state = seed ? seed : 1; }
   4826 #endif
   4827 long random(void)
   4828 #ifdef C_DECLS_ONLY
   4829 ;
   4830 #else
   4831 {
   4832     c_rand_state = c_rand_state * 6364136223846793005ULL + 1442695040888963407ULL;
   4833     return (long)(c_rand_state >> 33);
   4834 }
   4835 #endif
   4836 void srand(unsigned seed)
   4837 #ifdef C_DECLS_ONLY
   4838 ;
   4839 #else
   4840 { srandom(seed); }
   4841 #endif
   4842 int rand(void)
   4843 #ifdef C_DECLS_ONLY
   4844 ;
   4845 #else
   4846 { return (int)random(); }
   4847 #endif
   4848 int rand_r(unsigned *seed)
   4849 #ifdef C_DECLS_ONLY
   4850 ;
   4851 #else
   4852 { unsigned long long s = *seed * 6364136223846793005ULL + 1442695040888963407ULL; *seed = s >> 32; return (int)((s >> 33) & RAND_MAX); }
   4853 #endif
   4854 void srand48(long seed)
   4855 #ifdef C_DECLS_ONLY
   4856 ;
   4857 #else
   4858 { srandom(seed); }
   4859 #endif
   4860 long lrand48(void)
   4861 #ifdef C_DECLS_ONLY
   4862 ;
   4863 #else
   4864 { return random(); }
   4865 #endif
   4866 long mrand48(void)
   4867 #ifdef C_DECLS_ONLY
   4868 ;
   4869 #else
   4870 { return (long)(int)random() * 2; }
   4871 #endif
   4872 double drand48(void)
   4873 #ifdef C_DECLS_ONLY
   4874 ;
   4875 #else
   4876 { return (random() >> 7) / 16777216.0 + (random() % 128) / (16777216.0 * 128); }
   4877 #endif
   4878 unsigned arc4random(void)
   4879 #ifdef C_DECLS_ONLY
   4880 ;
   4881 #else
   4882 { unsigned v; if (getrandom(&v, sizeof v, 0) != sizeof v) v = random(); return v; }
   4883 #endif
   4884 void arc4random_buf(void *b, size_t n)
   4885 #ifdef C_DECLS_ONLY
   4886 ;
   4887 #else
   4888 { if (getrandom(b, n, 0) != (ssize_t)n) { unsigned char *p = b; while (n--) *p++ = random(); } }
   4889 #endif
   4890 unsigned arc4random_uniform(unsigned n)
   4891 #ifdef C_DECLS_ONLY
   4892 ;
   4893 #else
   4894 {
   4895     unsigned lim, v;
   4896     if (n < 2) return 0;
   4897     lim = -n % n;
   4898     do v = arc4random(); while (v < lim);
   4899     return v % n;
   4900 }
   4901 #endif
   4902 
   4903 void *bsearch(const void *key, const void *base, size_t n, size_t size, int (*cmp)(const void *, const void *))
   4904 #ifdef C_DECLS_ONLY
   4905 ;
   4906 #else
   4907 {
   4908     size_t lo = 0, hi = n;
   4909     while (lo < hi) {
   4910         size_t mid = lo + (hi - lo) / 2;
   4911         const char *p = (const char *)base + mid * size;
   4912         int c = cmp(key, p);
   4913         if (!c) return (void *)p;
   4914         if (c < 0) hi = mid; else lo = mid + 1;
   4915     }
   4916     return NULL;
   4917 }
   4918 #endif
   4919 #ifndef C_DECLS_ONLY
   4920 static void c_sort_r(char *base, size_t n, size_t size, int (*cmp)(const void *, const void *, void *), void *arg, char *tmp)
   4921 {
   4922     size_t mid, i, j, k;
   4923     if (n < 2) return;
   4924     mid = n / 2;
   4925     c_sort_r(base, mid, size, cmp, arg, tmp);
   4926     c_sort_r(base + mid * size, n - mid, size, cmp, arg, tmp);
   4927     i = 0; j = mid; k = 0;
   4928     while (i < mid && j < n) {
   4929         if (cmp(base + j * size, base + i * size, arg) < 0) memcpy(tmp + k++ * size, base + j++ * size, size);
   4930         else memcpy(tmp + k++ * size, base + i++ * size, size);
   4931     }
   4932     while (i < mid) memcpy(tmp + k++ * size, base + i++ * size, size);
   4933     while (j < n) memcpy(tmp + k++ * size, base + j++ * size, size);
   4934     memcpy(base, tmp, n * size);
   4935 }
   4936 #endif
   4937 void qsort_r(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *, void *), void *arg)
   4938 #ifdef C_DECLS_ONLY
   4939 ;
   4940 #else
   4941 {
   4942     char *tmp;
   4943     if (n < 2 || !size) return;
   4944     tmp = malloc(n * size);
   4945     if (!tmp) return;
   4946     c_sort_r(base, n, size, cmp, arg, tmp);
   4947     free(tmp);
   4948 }
   4949 #endif
   4950 
   4951 /* atexit: exit runs them backwards, then flushes everything */
   4952 #ifndef C_DECLS_ONLY
   4953 static void (*c_atexit_fns[64])(void);
   4954 #endif
   4955 #ifndef C_DECLS_ONLY
   4956 static int c_atexit_n;
   4957 #endif
   4958 int atexit(void (*f)(void))
   4959 #ifdef C_DECLS_ONLY
   4960 ;
   4961 #else
   4962 { if (c_atexit_n == 64) return -1; c_atexit_fns[c_atexit_n++] = f; return 0; }
   4963 #endif
   4964 void c_run_exit(void)
   4965 #ifdef C_DECLS_ONLY
   4966 ;
   4967 #else
   4968 { while (c_atexit_n) c_atexit_fns[--c_atexit_n](); fflush(NULL); }
   4969 #endif
   4970 __attribute__((noreturn)) void _Exit(int code)
   4971 #ifdef C_DECLS_ONLY
   4972 ;
   4973 #else
   4974 { _exit(code); }
   4975 #endif
   4976 int at_quick_exit(void (*f)(void))
   4977 #ifdef C_DECLS_ONLY
   4978 ;
   4979 #else
   4980 { return atexit(f); }
   4981 #endif
   4982 __attribute__((noreturn)) void quick_exit(int code)
   4983 #ifdef C_DECLS_ONLY
   4984 ;
   4985 #else
   4986 { exit(code); }
   4987 #endif
   4988 
   4989 /* memory */
   4990 int posix_memalign(void **out, size_t align, size_t size)
   4991 #ifdef C_DECLS_ONLY
   4992 ;
   4993 #else
   4994 {
   4995     char *p;
   4996     if (align < sizeof(void *) || (align & (align - 1))) return EINVAL;
   4997     p = malloc(size + align + sizeof(void *));
   4998     if (!p) return ENOMEM;
   4999     {
   5000         char *a = (char *)(((unsigned long)p + sizeof(void *) + align - 1) & ~(align - 1));
   5001         ((void **)a)[-1] = p;
   5002         *out = a;
   5003     }
   5004     return 0;
   5005 }
   5006 #endif
   5007 void *aligned_alloc(size_t align, size_t size)
   5008 #ifdef C_DECLS_ONLY
   5009 ;
   5010 #else
   5011 { void *p; return posix_memalign(&p, align, size) ? NULL : p; }
   5012 #endif
   5013 void *memalign(size_t align, size_t size)
   5014 #ifdef C_DECLS_ONLY
   5015 ;
   5016 #else
   5017 { return aligned_alloc(align, size); }
   5018 #endif
   5019 void *valloc(size_t size)
   5020 #ifdef C_DECLS_ONLY
   5021 ;
   5022 #else
   5023 { return aligned_alloc(4096, size); }
   5024 #endif
   5025 void *reallocarray(void *p, size_t n, size_t size)
   5026 #ifdef C_DECLS_ONLY
   5027 ;
   5028 #else
   5029 { if (size && n > (size_t)-1 / size) { errno = ENOMEM; return NULL; } return realloc(p, n * size); }
   5030 #endif
   5031 #define alloca(n) __builtin_alloca(n)
   5032 
   5033 /* temporary files */
   5034 #ifndef C_DECLS_ONLY
   5035 static void c_fill_x(char *x)
   5036 {
   5037     static const char a[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   5038     unsigned char r[8];
   5039     int i;
   5040     arc4random_buf(r, sizeof r);
   5041     for (i = 0; i < 6; i++) x[i] = a[r[i] % 62];
   5042 }
   5043 #endif
   5044 int mkostemps(char *tpl, int suffix, int flags)
   5045 #ifdef C_DECLS_ONLY
   5046 ;
   5047 #else
   5048 {
   5049     size_t l = strlen(tpl);
   5050     int tries;
   5051     if (l < 6 + (size_t)suffix || strncmp(tpl + l - 6 - suffix, "XXXXXX", 6)) { errno = EINVAL; return -1; }
   5052     for (tries = 0; tries < 100; tries++) {
   5053         int fd;
   5054         c_fill_x(tpl + l - 6 - suffix);
   5055         fd = open(tpl, O_RDWR | O_CREAT | O_EXCL | flags, 0600);
   5056         if (fd >= 0 || errno != EEXIST) return fd;
   5057     }
   5058     return -1;
   5059 }
   5060 #endif
   5061 int mkstemp(char *tpl)
   5062 #ifdef C_DECLS_ONLY
   5063 ;
   5064 #else
   5065 { return mkostemps(tpl, 0, 0); }
   5066 #endif
   5067 int mkstemps(char *tpl, int suffix)
   5068 #ifdef C_DECLS_ONLY
   5069 ;
   5070 #else
   5071 { return mkostemps(tpl, suffix, 0); }
   5072 #endif
   5073 int mkostemp(char *tpl, int flags)
   5074 #ifdef C_DECLS_ONLY
   5075 ;
   5076 #else
   5077 { return mkostemps(tpl, 0, flags); }
   5078 #endif
   5079 char *mkdtemp(char *tpl)
   5080 #ifdef C_DECLS_ONLY
   5081 ;
   5082 #else
   5083 {
   5084     size_t l = strlen(tpl);
   5085     int tries;
   5086     if (l < 6 || strcmp(tpl + l - 6, "XXXXXX")) { errno = EINVAL; return NULL; }
   5087     for (tries = 0; tries < 100; tries++) {
   5088         c_fill_x(tpl + l - 6);
   5089         if (mkdir(tpl, 0700) == 0) return tpl;
   5090         if (errno != EEXIST) return NULL;
   5091     }
   5092     return NULL;
   5093 }
   5094 #endif
   5095 char *mktemp(char *tpl)
   5096 #ifdef C_DECLS_ONLY
   5097 ;
   5098 #else
   5099 {
   5100     size_t l = strlen(tpl);
   5101     struct stat st;
   5102     if (l < 6 || strcmp(tpl + l - 6, "XXXXXX")) { errno = EINVAL; tpl[0] = 0; return tpl; }
   5103     do c_fill_x(tpl + l - 6); while (lstat(tpl, &st) == 0);
   5104     return tpl;
   5105 }
   5106 #endif
   5107 char *tmpnam(char *s)
   5108 #ifdef C_DECLS_ONLY
   5109 ;
   5110 #else
   5111 {
   5112     static char buf[L_tmpnam];
   5113     char *d = s ? s : buf;
   5114     strcpy(d, "/tmp/tmpXXXXXX");
   5115     mktemp(d);
   5116     return d;
   5117 }
   5118 #endif
   5119 char *tempnam(const char *dir, const char *prefix)
   5120 #ifdef C_DECLS_ONLY
   5121 ;
   5122 #else
   5123 {
   5124     char *d = malloc(strlen(dir ? dir : "/tmp") + (prefix ? strlen(prefix) : 0) + 16);
   5125     if (!d) return NULL;
   5126     sprintf(d, "%s/%sXXXXXX", dir ? dir : "/tmp", prefix ? prefix : "");
   5127     mktemp(d);
   5128     return d;
   5129 }
   5130 #endif
   5131 FILE *tmpfile(void)
   5132 #ifdef C_DECLS_ONLY
   5133 ;
   5134 #else
   5135 {
   5136     char name[] = "/tmp/tmpfileXXXXXX";
   5137     int fd = mkstemp(name);
   5138     FILE *f;
   5139     if (fd < 0) return NULL;
   5140     unlink(name);
   5141     f = fdopen(fd, "w+");
   5142     if (!f) close(fd);
   5143     return f;
   5144 }
   5145 #endif
   5146 
   5147 /* printf pieces built on the core ones */
   5148 int vasprintf(char **out, const char *fmt, va_list ap)
   5149 #ifdef C_DECLS_ONLY
   5150 ;
   5151 #else
   5152 {
   5153     va_list ap2;
   5154     int n;
   5155     va_copy(ap2, ap);
   5156     n = vsnprintf(NULL, 0, fmt, ap2);
   5157     va_end(ap2);
   5158     if (n < 0) return -1;
   5159     *out = malloc(n + 1);
   5160     if (!*out) return -1;
   5161     return vsnprintf(*out, n + 1, fmt, ap);
   5162 }
   5163 #endif
   5164 int asprintf(char **out, const char *fmt, ...)
   5165 #ifdef C_DECLS_ONLY
   5166 ;
   5167 #else
   5168 { va_list ap; int n; va_start(ap, fmt); n = vasprintf(out, fmt, ap); va_end(ap); return n; }
   5169 #endif
   5170 int vdprintf(int fd, const char *fmt, va_list ap)
   5171 #ifdef C_DECLS_ONLY
   5172 ;
   5173 #else
   5174 {
   5175     char *s;
   5176     int n = vasprintf(&s, fmt, ap);
   5177     if (n < 0) return -1;
   5178     if (write(fd, s, n) < 0) n = -1;
   5179     free(s);
   5180     return n;
   5181 }
   5182 #endif
   5183 int dprintf(int fd, const char *fmt, ...)
   5184 #ifdef C_DECLS_ONLY
   5185 ;
   5186 #else
   5187 { va_list ap; int n; va_start(ap, fmt); n = vdprintf(fd, fmt, ap); va_end(ap); return n; }
   5188 #endif
   5189 ssize_t getdelim(char **lp, size_t *cap, int delim, FILE *f)
   5190 #ifdef C_DECLS_ONLY
   5191 ;
   5192 #else
   5193 {
   5194     size_t n = 0;
   5195     int c;
   5196     if (!*lp || !*cap) { *cap = 128; *lp = malloc(*cap); if (!*lp) return -1; }
   5197     while ((c = fgetc(f)) != EOF) {
   5198         if (n + 2 > *cap) { char *p = realloc(*lp, *cap * 2); if (!p) return -1; *lp = p; *cap *= 2; }
   5199         (*lp)[n++] = c;
   5200         if (c == delim) break;
   5201     }
   5202     if (!n) return -1;
   5203     (*lp)[n] = 0;
   5204     return n;
   5205 }
   5206 #endif
   5207 int fputs_unlocked(const char *s, FILE *f)
   5208 #ifdef C_DECLS_ONLY
   5209 ;
   5210 #else
   5211 { return fputs(s, f); }
   5212 #endif
   5213 
   5214 /* time */
   5215 #define CLOCK_REALTIME 0
   5216 #define CLOCK_MONOTONIC 1
   5217 #define CLOCK_PROCESS_CPUTIME_ID 2
   5218 #define CLOCK_THREAD_CPUTIME_ID 3
   5219 #define CLOCK_MONOTONIC_RAW 4
   5220 #define CLOCK_REALTIME_COARSE 5
   5221 #define CLOCK_MONOTONIC_COARSE 6
   5222 #define CLOCK_BOOTTIME 7
   5223 #define TIMER_ABSTIME 1
   5224 #ifdef C_DECLS_ONLY
   5225 extern long timezone;
   5226 #else
   5227 long timezone;
   5228 #endif
   5229 #ifdef C_DECLS_ONLY
   5230 extern int daylight;
   5231 #else
   5232 int daylight;
   5233 #endif
   5234 #ifdef C_DECLS_ONLY
   5235 extern char *tzname[2];
   5236 #else
   5237 char *tzname[2] = { "UTC", "UTC" };
   5238 #endif
   5239 void tzset(void)
   5240 #ifdef C_DECLS_ONLY
   5241 ;
   5242 #else
   5243 { }
   5244 #endif
   5245 struct tm *gmtime_r(const time_t *t, struct tm *out)
   5246 #ifdef C_DECLS_ONLY
   5247 ;
   5248 #else
   5249 { *out = *gmtime(t); return out; }
   5250 #endif
   5251 struct tm *localtime_r(const time_t *t, struct tm *out)
   5252 #ifdef C_DECLS_ONLY
   5253 ;
   5254 #else
   5255 { *out = *localtime(t); return out; }
   5256 #endif
   5257 double difftime(time_t a, time_t b)
   5258 #ifdef C_DECLS_ONLY
   5259 ;
   5260 #else
   5261 { return (double)(a - b); }
   5262 #endif
   5263 char *asctime_r(const struct tm *t, char *buf)
   5264 #ifdef C_DECLS_ONLY
   5265 ;
   5266 #else
   5267 {
   5268     static const char wd[] = "SunMonTueWedThuFriSat", mo[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
   5269     sprintf(buf, "%.3s %.3s %2d %02d:%02d:%02d %d\n", wd + 3 * (t->tm_wday % 7), mo + 3 * (t->tm_mon % 12), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, t->tm_year + 1900);
   5270     return buf;
   5271 }
   5272 #endif
   5273 char *asctime(const struct tm *t)
   5274 #ifdef C_DECLS_ONLY
   5275 ;
   5276 #else
   5277 { static char buf[32]; return asctime_r(t, buf); }
   5278 #endif
   5279 char *ctime_r(const time_t *t, char *buf)
   5280 #ifdef C_DECLS_ONLY
   5281 ;
   5282 #else
   5283 { return asctime_r(localtime(t), buf); }
   5284 #endif
   5285 char *ctime(const time_t *t)
   5286 #ifdef C_DECLS_ONLY
   5287 ;
   5288 #else
   5289 { static char buf[32]; return ctime_r(t, buf); }
   5290 #endif
   5291 int clock_settime(int clk, const struct timespec *t)
   5292 #ifdef C_DECLS_ONLY
   5293 ;
   5294 #else
   5295 { return fail(sys2(227, clk, t)); }
   5296 #endif
   5297 int settimeofday(const struct timeval *tv, const void *tz)
   5298 #ifdef C_DECLS_ONLY
   5299 ;
   5300 #else
   5301 { return fail(sys2(164, tv, tz)); }
   5302 #endif
   5303 int timespec_get(struct timespec *t, int base)
   5304 #ifdef C_DECLS_ONLY
   5305 ;
   5306 #else
   5307 { if (base != 1) return 0; return clock_gettime(CLOCK_REALTIME, t) < 0 ? 0 : base; }
   5308 #endif
   5309 struct itimerval { struct timeval it_interval, it_value; };
   5310 #define ITIMER_REAL 0
   5311 #define ITIMER_VIRTUAL 1
   5312 #define ITIMER_PROF 2
   5313 int setitimer(int which, const struct itimerval *nw, struct itimerval *old)
   5314 #ifdef C_DECLS_ONLY
   5315 ;
   5316 #else
   5317 { return fail(sys3(SYS_setitimer, which, nw, old)); }
   5318 #endif
   5319 int getitimer(int which, struct itimerval *cur)
   5320 #ifdef C_DECLS_ONLY
   5321 ;
   5322 #else
   5323 { return fail(sys2(SYS_getitimer, which, cur)); }
   5324 #endif
   5325 #define timeradd(a, b, r) do { (r)->tv_sec = (a)->tv_sec + (b)->tv_sec; (r)->tv_usec = (a)->tv_usec + (b)->tv_usec; if ((r)->tv_usec >= 1000000) { (r)->tv_sec++; (r)->tv_usec -= 1000000; } } while (0)
   5326 #define timersub(a, b, r) do { (r)->tv_sec = (a)->tv_sec - (b)->tv_sec; (r)->tv_usec = (a)->tv_usec - (b)->tv_usec; if ((r)->tv_usec < 0) { (r)->tv_sec--; (r)->tv_usec += 1000000; } } while (0)
   5327 #define timercmp(a, b, op) (((a)->tv_sec == (b)->tv_sec) ? ((a)->tv_usec op (b)->tv_usec) : ((a)->tv_sec op (b)->tv_sec))
   5328 #define timerclear(t) ((t)->tv_sec = (t)->tv_usec = 0)
   5329 #define timerisset(t) ((t)->tv_sec || (t)->tv_usec)
   5330 
   5331 char *strptime(const char *s, const char *fmt, struct tm *tm)
   5332 #ifdef C_DECLS_ONLY
   5333 ;
   5334 #else
   5335 {
   5336     static const char *const mon[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
   5337     static const char *const day[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
   5338     for (; *fmt; fmt++) {
   5339         char *e;
   5340         long v;
   5341         int i;
   5342         if (isspace((unsigned char)*fmt)) { while (isspace((unsigned char)*s)) s++; continue; }
   5343         if (*fmt != '%') { if (*s++ != *fmt) return NULL; continue; }
   5344         fmt++;
   5345         if (*fmt == 'E' || *fmt == 'O') fmt++;
   5346         switch (*fmt) {
   5347         case 'Y': v = strtol(s, &e, 10); if (e == s) return NULL; tm->tm_year = v - 1900; s = e; break;
   5348         case 'y': v = strtol(s, &e, 10); if (e == s) return NULL; tm->tm_year = v < 69 ? v + 100 : v; s = e; break;
   5349         case 'm': v = strtol(s, &e, 10); if (e == s || v < 1 || v > 12) return NULL; tm->tm_mon = v - 1; s = e; break;
   5350         case 'd': case 'e': while (*s == ' ') s++; v = strtol(s, &e, 10); if (e == s || v < 1 || v > 31) return NULL; tm->tm_mday = v; s = e; break;
   5351         case 'H': v = strtol(s, &e, 10); if (e == s || v > 23) return NULL; tm->tm_hour = v; s = e; break;
   5352         case 'M': v = strtol(s, &e, 10); if (e == s || v > 59) return NULL; tm->tm_min = v; s = e; break;
   5353         case 'S': v = strtol(s, &e, 10); if (e == s || v > 61) return NULL; tm->tm_sec = v; s = e; break;
   5354         case 'j': v = strtol(s, &e, 10); if (e == s) return NULL; tm->tm_yday = v - 1; s = e; break;
   5355         case 'b': case 'B': case 'h':
   5356             for (i = 0; i < 12; i++) if (!strncasecmp(s, mon[i], 3)) break;
   5357             if (i == 12) return NULL;
   5358             tm->tm_mon = i; s += 3;
   5359             while (isalpha((unsigned char)*s)) s++;
   5360             break;
   5361         case 'a': case 'A':
   5362             for (i = 0; i < 7; i++) if (!strncasecmp(s, day[i], 3)) break;
   5363             if (i == 7) return NULL;
   5364             tm->tm_wday = i; s += 3;
   5365             while (isalpha((unsigned char)*s)) s++;
   5366             break;
   5367         case 'T': s = strptime(s, "%H:%M:%S", tm); if (!s) return NULL; break;
   5368         case 'R': s = strptime(s, "%H:%M", tm); if (!s) return NULL; break;
   5369         case 'F': s = strptime(s, "%Y-%m-%d", tm); if (!s) return NULL; break;
   5370         case 'D': s = strptime(s, "%m/%d/%y", tm); if (!s) return NULL; break;
   5371         case 'n': case 't': while (isspace((unsigned char)*s)) s++; break;
   5372         case 'z': if (*s == 'Z') { s++; break; } if (*s != '+' && *s != '-') return NULL; s++; while (isdigit((unsigned char)*s) || *s == ':') s++; break;
   5373         case 'Z': while (isalpha((unsigned char)*s)) s++; break;
   5374         case '%': if (*s++ != '%') return NULL; break;
   5375         default: return NULL;
   5376         }
   5377     }
   5378     return (char *)s;
   5379 }
   5380 #endif
   5381 /* ---- signals the POSIX way [chamesle] ---- */
   5382 #define SIGILL 4
   5383 #define SIGTRAP 5
   5384 #define SIGABRT 6
   5385 #define SIGIOT 6
   5386 #define SIGBUS 7
   5387 #define SIGFPE 8
   5388 #define SIGUSR1 10
   5389 #define SIGSEGV 11
   5390 #define SIGUSR2 12
   5391 #define SIGPIPE 13
   5392 #define SIGALRM 14
   5393 #define SIGSTKFLT 16
   5394 #define SIGCONT 18
   5395 #define SIGSTOP 19
   5396 #define SIGTSTP 20
   5397 #define SIGTTIN 21
   5398 #define SIGTTOU 22
   5399 #define SIGURG 23
   5400 #define SIGXCPU 24
   5401 #define SIGXFSZ 25
   5402 #define SIGVTALRM 26
   5403 #define SIGPROF 27
   5404 #define SIGWINCH 28
   5405 #define SIGIO 29
   5406 #define SIGPOLL 29
   5407 #define SIGPWR 30
   5408 #define SIGSYS 31
   5409 #define NSIG 65
   5410 #define SA_NOCLDSTOP 1
   5411 #define SA_NOCLDWAIT 2
   5412 #define SA_SIGINFO 4
   5413 #define SA_ONSTACK 0x08000000
   5414 #define SA_RESTART 0x10000000
   5415 #define SA_NODEFER 0x40000000
   5416 #define SA_RESETHAND 0x80000000
   5417 #define SIG_BLOCK 0
   5418 #define SIG_UNBLOCK 1
   5419 #define SIG_SETMASK 2
   5420 typedef int sig_atomic_t;
   5421 typedef struct { unsigned long __bits[16]; } sigset_t;
   5422 typedef struct {
   5423     int si_signo, si_errno, si_code, __pad0;
   5424     union {
   5425         struct { pid_t si_pid; uid_t si_uid; int si_status; };
   5426         void *si_addr;
   5427         char __pad[112];
   5428     };
   5429 } siginfo_t;
   5430 struct sigaction {
   5431     union { sighandler_t sa_handler; void (*sa_sigaction)(int, siginfo_t *, void *); };
   5432     sigset_t sa_mask;
   5433     int sa_flags;
   5434     void (*sa_restorer)(void);
   5435 };
   5436 typedef struct { void *ss_sp; int ss_flags; size_t ss_size; } stack_t;
   5437 #define SS_ONSTACK 1
   5438 #define SS_DISABLE 2
   5439 #define MINSIGSTKSZ 2048
   5440 #define SIGSTKSZ 8192
   5441 
   5442 int sigemptyset(sigset_t *s)
   5443 #ifdef C_DECLS_ONLY
   5444 ;
   5445 #else
   5446 { memset(s, 0, sizeof *s); return 0; }
   5447 #endif
   5448 int sigfillset(sigset_t *s)
   5449 #ifdef C_DECLS_ONLY
   5450 ;
   5451 #else
   5452 { memset(s, 0xff, sizeof *s); return 0; }
   5453 #endif
   5454 int sigaddset(sigset_t *s, int n)
   5455 #ifdef C_DECLS_ONLY
   5456 ;
   5457 #else
   5458 { if (n < 1 || n > 64) { errno = EINVAL; return -1; } s->__bits[(n - 1) / (8 * sizeof(long))] |= 1UL << (n - 1) % (8 * sizeof(long)); return 0; }
   5459 #endif
   5460 int sigdelset(sigset_t *s, int n)
   5461 #ifdef C_DECLS_ONLY
   5462 ;
   5463 #else
   5464 { if (n < 1 || n > 64) { errno = EINVAL; return -1; } s->__bits[(n - 1) / (8 * sizeof(long))] &= ~(1UL << (n - 1) % (8 * sizeof(long))); return 0; }
   5465 #endif
   5466 int sigismember(const sigset_t *s, int n)
   5467 #ifdef C_DECLS_ONLY
   5468 ;
   5469 #else
   5470 { if (n < 1 || n > 64) { errno = EINVAL; return -1; } return (s->__bits[(n - 1) / (8 * sizeof(long))] >> (n - 1) % (8 * sizeof(long))) & 1; }
   5471 #endif
   5472 int sigisemptyset(const sigset_t *s)
   5473 #ifdef C_DECLS_ONLY
   5474 ;
   5475 #else
   5476 { return !s->__bits[0] && !s->__bits[1]; }
   5477 #endif
   5478 int sigaction(int n, const struct sigaction *act, struct sigaction *old)
   5479 #ifdef C_DECLS_ONLY
   5480 ;
   5481 #else
   5482 {
   5483     struct c_sigaction k, ko;
   5484     long r;
   5485     if (act) { k.handler = act->sa_handler; k.flags = (unsigned long)(unsigned)act->sa_flags | 0x04000000; k.restorer = c_restore_rt; memcpy(&k.mask, act->sa_mask.__bits, 8); }
   5486     r = fail(sys4(SYS_rt_sigaction, n, act ? &k : NULL, old ? &ko : NULL, 8));
   5487     if (r < 0) return -1;
   5488     if (old) { memset(old, 0, sizeof *old); old->sa_handler = ko.handler; old->sa_flags = ko.flags & ~0x04000000UL; memcpy(old->sa_mask.__bits, &ko.mask, 8); }
   5489     return 0;
   5490 }
   5491 #endif
   5492 int sigprocmask(int how, const sigset_t *set, sigset_t *old)
   5493 #ifdef C_DECLS_ONLY
   5494 ;
   5495 #else
   5496 {
   5497     unsigned long long o = 0;
   5498     long r = fail(sys4(SYS_rt_sigprocmask, how, set ? set->__bits : NULL, old ? &o : NULL, 8));
   5499     if (r < 0) return -1;
   5500     if (old) { memset(old, 0, sizeof *old); memcpy(old->__bits, &o, 8); }
   5501     return 0;
   5502 }
   5503 #endif
   5504 int pthread_sigmask(int how, const sigset_t *set, sigset_t *old)
   5505 #ifdef C_DECLS_ONLY
   5506 ;
   5507 #else
   5508 { return sigprocmask(how, set, old) < 0 ? errno : 0; }
   5509 #endif
   5510 int sigpending(sigset_t *s)
   5511 #ifdef C_DECLS_ONLY
   5512 ;
   5513 #else
   5514 { unsigned long long o = 0; if (fail(sys2(SYS_rt_sigpending, &o, 8)) < 0) return -1; memset(s, 0, sizeof *s); memcpy(s->__bits, &o, 8); return 0; }
   5515 #endif
   5516 int sigsuspend(const sigset_t *s)
   5517 #ifdef C_DECLS_ONLY
   5518 ;
   5519 #else
   5520 { return fail(sys2(SYS_rt_sigsuspend, s->__bits, 8)); }
   5521 #endif
   5522 int sigaltstack(const stack_t *ss, stack_t *old)
   5523 #ifdef C_DECLS_ONLY
   5524 ;
   5525 #else
   5526 { return fail(sys2(SYS_sigaltstack, ss, old)); }
   5527 #endif
   5528 int raise(int sig)
   5529 #ifdef C_DECLS_ONLY
   5530 ;
   5531 #else
   5532 { return fail(sys3(SYS_tgkill, getpid(), gettid(), sig)); }
   5533 #endif
   5534 int killpg(int pg, int sig)
   5535 #ifdef C_DECLS_ONLY
   5536 ;
   5537 #else
   5538 { return kill(-pg, sig); }
   5539 #endif
   5540 int sigignore(int n)
   5541 #ifdef C_DECLS_ONLY
   5542 ;
   5543 #else
   5544 { return signal(n, SIG_IGN) == SIG_ERR ? -1 : 0; }
   5545 #endif
   5546 sighandler_t sysv_signal(int n, sighandler_t f)
   5547 #ifdef C_DECLS_ONLY
   5548 ;
   5549 #else
   5550 { return signal(n, f); }
   5551 #endif
   5552 void psignal(int sig, const char *s)
   5553 #ifdef C_DECLS_ONLY
   5554 ;
   5555 #else
   5556 { fprintf(stderr, "%s%s%s\n", s ? s : "", s && *s ? ": " : "", strsignal(sig)); }
   5557 #endif
   5558 typedef struct { jmp_buf b; } sigjmp_buf[1];
   5559 #define sigsetjmp(env, save) setjmp((env)->b)
   5560 #define siglongjmp(env, v) longjmp((env)->b, v)
   5561 #define _setjmp(b) setjmp(b)
   5562 #define _longjmp(b, v) longjmp(b, v)
   5563 /* ---- libm: sqrt and the roundings by hand, the transcendental ones on the x87 [chamesle] ---- */
   5564 #define M_E 2.7182818284590452354
   5565 #define M_LOG2E 1.4426950408889634074
   5566 #define M_LOG10E 0.43429448190325182765
   5567 #define M_LN2 0.69314718055994530942
   5568 #define M_LN10 2.30258509299404568402
   5569 #define M_PI 3.14159265358979323846
   5570 #define M_PI_2 1.57079632679489661923
   5571 #define M_PI_4 0.78539816339744830962
   5572 #define M_1_PI 0.31830988618379067154
   5573 #define M_2_PI 0.63661977236758134308
   5574 #define M_2_SQRTPI 1.12837916709551257390
   5575 #define M_SQRT2 1.41421356237309504880
   5576 #define M_SQRT1_2 0.70710678118654752440
   5577 #define HUGE_VAL (__builtin_huge_val())
   5578 #define HUGE_VALF (__builtin_huge_valf())
   5579 #define INFINITY (__builtin_inff())
   5580 #define NAN (__builtin_nanf(""))
   5581 #define FP_NAN 0
   5582 #define FP_INFINITE 1
   5583 #define FP_ZERO 2
   5584 #define FP_SUBNORMAL 3
   5585 #define FP_NORMAL 4
   5586 #define FP_ILOGB0 (-2147483647 - 1)
   5587 #define FP_ILOGBNAN (-2147483647 - 1)
   5588 #define MATH_ERRNO 1
   5589 #define math_errhandling 1
   5590 typedef float float_t;
   5591 typedef double double_t;
   5592 #define isnan(x) __builtin_isnan(x)
   5593 #define isinf(x) __builtin_isinf(x)
   5594 #define isfinite(x) __builtin_isfinite(x)
   5595 #define isnormal(x) __builtin_isnormal(x)
   5596 #define signbit(x) __builtin_signbit(x)
   5597 #define fpclassify(x) __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
   5598 #define isgreater(a, b) __builtin_isgreater(a, b)
   5599 #define isgreaterequal(a, b) __builtin_isgreaterequal(a, b)
   5600 #define isless(a, b) __builtin_isless(a, b)
   5601 #define islessequal(a, b) __builtin_islessequal(a, b)
   5602 #define islessgreater(a, b) __builtin_islessgreater(a, b)
   5603 #define isunordered(a, b) __builtin_isunordered(a, b)
   5604 
   5605 #ifndef C_DECLS_ONLY
   5606 static long double c_fyl2x(long double y, long double x) { long double r; __asm__("fyl2x" : "=t"(r) : "0"(x), "u"(y) : "st(1)"); return r; }
   5607 #endif
   5608 #ifndef C_DECLS_ONLY
   5609 static long double c_f2xm1(long double x) { long double r; __asm__("f2xm1" : "=t"(r) : "0"(x)); return r; }
   5610 #endif
   5611 #ifndef C_DECLS_ONLY
   5612 static long double c_fscale(long double x, long double n) { long double r; __asm__("fscale" : "=t"(r) : "0"(x), "u"(n)); return r; }
   5613 #endif
   5614 #ifndef C_DECLS_ONLY
   5615 static long double c_frndint(long double x) { long double r; __asm__("frndint" : "=t"(r) : "0"(x)); return r; }
   5616 #endif
   5617 #ifndef C_DECLS_ONLY
   5618 static long double c_exp2(long double x)
   5619 {
   5620     long double n, f;
   5621     if (x > 16400) return __builtin_infl();
   5622     if (x < -16400) return 0;
   5623     n = c_frndint(x); f = x - n;
   5624     return c_fscale(c_f2xm1(f) + 1, n);
   5625 }
   5626 #endif
   5627 
   5628 double fabs(double x)
   5629 #ifdef C_DECLS_ONLY
   5630 ;
   5631 #else
   5632 { union { double d; unsigned long long u; } v = { x }; v.u &= ~(1ULL << 63); return v.d; }
   5633 #endif
   5634 float fabsf(float x)
   5635 #ifdef C_DECLS_ONLY
   5636 ;
   5637 #else
   5638 { return (float)fabs(x); }
   5639 #endif
   5640 long double fabsl(long double x)
   5641 #ifdef C_DECLS_ONLY
   5642 ;
   5643 #else
   5644 { return x < 0 ? -x : x; }
   5645 #endif
   5646 double copysign(double x, double y)
   5647 #ifdef C_DECLS_ONLY
   5648 ;
   5649 #else
   5650 { union { double d; unsigned long long u; } a = { x }, b = { y }; a.u = (a.u & ~(1ULL << 63)) | (b.u & (1ULL << 63)); return a.d; }
   5651 #endif
   5652 float copysignf(float x, float y)
   5653 #ifdef C_DECLS_ONLY
   5654 ;
   5655 #else
   5656 { return (float)copysign(x, y); }
   5657 #endif
   5658 double sqrt(double x)
   5659 #ifdef C_DECLS_ONLY
   5660 ;
   5661 #else
   5662 { double r; __asm__("sqrtsd %1, %0" : "=x"(r) : "x"(x)); return r; }
   5663 #endif
   5664 float sqrtf(float x)
   5665 #ifdef C_DECLS_ONLY
   5666 ;
   5667 #else
   5668 { float r; __asm__("sqrtss %1, %0" : "=x"(r) : "x"(x)); return r; }
   5669 #endif
   5670 long double sqrtl(long double x)
   5671 #ifdef C_DECLS_ONLY
   5672 ;
   5673 #else
   5674 { long double r; __asm__("fsqrt" : "=t"(r) : "0"(x)); return r; }
   5675 #endif
   5676 double trunc(double x)
   5677 #ifdef C_DECLS_ONLY
   5678 ;
   5679 #else
   5680 {
   5681     union { double d; unsigned long long u; } v = { x };
   5682     int e = (int)((v.u >> 52) & 0x7ff) - 0x3ff;
   5683     if (e >= 52) return x;
   5684     if (e < 0) { v.u &= 1ULL << 63; return v.d; }
   5685     v.u &= ~(0x000fffffffffffffULL >> e);
   5686     return v.d;
   5687 }
   5688 #endif
   5689 double floor(double x)
   5690 #ifdef C_DECLS_ONLY
   5691 ;
   5692 #else
   5693 { double t = trunc(x); return t > x ? t - 1 : t; }
   5694 #endif
   5695 double ceil(double x)
   5696 #ifdef C_DECLS_ONLY
   5697 ;
   5698 #else
   5699 { double t = trunc(x); return t < x ? t + 1 : t; }
   5700 #endif
   5701 double round(double x)
   5702 #ifdef C_DECLS_ONLY
   5703 ;
   5704 #else
   5705 { double t = trunc(x); if (fabs(x - t) >= 0.5) t += copysign(1.0, x); return t; }
   5706 #endif
   5707 double rint(double x)
   5708 #ifdef C_DECLS_ONLY
   5709 ;
   5710 #else
   5711 { return (double)c_frndint(x); }
   5712 #endif
   5713 double nearbyint(double x)
   5714 #ifdef C_DECLS_ONLY
   5715 ;
   5716 #else
   5717 { return rint(x); }
   5718 #endif
   5719 float floorf(float x)
   5720 #ifdef C_DECLS_ONLY
   5721 ;
   5722 #else
   5723 { return (float)floor(x); }
   5724 #endif
   5725 float ceilf(float x)
   5726 #ifdef C_DECLS_ONLY
   5727 ;
   5728 #else
   5729 { return (float)ceil(x); }
   5730 #endif
   5731 float truncf(float x)
   5732 #ifdef C_DECLS_ONLY
   5733 ;
   5734 #else
   5735 { return (float)trunc(x); }
   5736 #endif
   5737 float roundf(float x)
   5738 #ifdef C_DECLS_ONLY
   5739 ;
   5740 #else
   5741 { return (float)round(x); }
   5742 #endif
   5743 float rintf(float x)
   5744 #ifdef C_DECLS_ONLY
   5745 ;
   5746 #else
   5747 { return (float)rint(x); }
   5748 #endif
   5749 long double floorl(long double x)
   5750 #ifdef C_DECLS_ONLY
   5751 ;
   5752 #else
   5753 { long double t = (long double)(long long)x; return t > x ? t - 1 : t; }
   5754 #endif
   5755 long double ceill(long double x)
   5756 #ifdef C_DECLS_ONLY
   5757 ;
   5758 #else
   5759 { long double t = (long double)(long long)x; return t < x ? t + 1 : t; }
   5760 #endif
   5761 long double truncl(long double x)
   5762 #ifdef C_DECLS_ONLY
   5763 ;
   5764 #else
   5765 { return (long double)(long long)x; }
   5766 #endif
   5767 long lround(double x)
   5768 #ifdef C_DECLS_ONLY
   5769 ;
   5770 #else
   5771 { return (long)round(x); }
   5772 #endif
   5773 long long llround(double x)
   5774 #ifdef C_DECLS_ONLY
   5775 ;
   5776 #else
   5777 { return (long long)round(x); }
   5778 #endif
   5779 long lrint(double x)
   5780 #ifdef C_DECLS_ONLY
   5781 ;
   5782 #else
   5783 { return (long)rint(x); }
   5784 #endif
   5785 long long llrint(double x)
   5786 #ifdef C_DECLS_ONLY
   5787 ;
   5788 #else
   5789 { return (long long)rint(x); }
   5790 #endif
   5791 double fmod(double x, double y)
   5792 #ifdef C_DECLS_ONLY
   5793 ;
   5794 #else
   5795 {
   5796     long double r = x, d = y;
   5797     if (y == 0 || isinf(x) || isnan(x) || isnan(y)) return __builtin_nan("");
   5798     if (isinf(y)) return x;
   5799     __asm__ __volatile__("1: fprem\n fnstsw %%ax\n testb $4, %%ah\n jnz 1b" : "+t"(r) : "u"(d) : "ax", "cc");
   5800     return (double)r;
   5801 }
   5802 #endif
   5803 float fmodf(float x, float y)
   5804 #ifdef C_DECLS_ONLY
   5805 ;
   5806 #else
   5807 { return (float)fmod(x, y); }
   5808 #endif
   5809 double remainder(double x, double y)
   5810 #ifdef C_DECLS_ONLY
   5811 ;
   5812 #else
   5813 { double n = rint(x / y); return x - n * y; }
   5814 #endif
   5815 double modf(double x, double *ip)
   5816 #ifdef C_DECLS_ONLY
   5817 ;
   5818 #else
   5819 { double t = trunc(x); *ip = t; return isinf(x) ? copysign(0.0, x) : x - t; }
   5820 #endif
   5821 double frexp(double x, int *e)
   5822 #ifdef C_DECLS_ONLY
   5823 ;
   5824 #else
   5825 {
   5826     union { double d; unsigned long long u; } v = { x };
   5827     int ex = (int)((v.u >> 52) & 0x7ff);
   5828     if (!ex) {
   5829         if (x == 0) { *e = 0; return x; }
   5830         x *= 18014398509481984.0;
   5831         v.d = x; ex = (int)((v.u >> 52) & 0x7ff) - 54;
   5832     } else if (ex == 0x7ff) { *e = 0; return x; }
   5833     *e = ex - 0x3fe;
   5834     v.u = (v.u & ~(0x7ffULL << 52)) | (0x3feULL << 52);
   5835     return v.d;
   5836 }
   5837 #endif
   5838 double scalbn(double x, int n)
   5839 #ifdef C_DECLS_ONLY
   5840 ;
   5841 #else
   5842 { return ldexp(x, n); }
   5843 #endif
   5844 double scalbln(double x, long n)
   5845 #ifdef C_DECLS_ONLY
   5846 ;
   5847 #else
   5848 { return ldexp(x, n > 100000 ? 100000 : n < -100000 ? -100000 : (int)n); }
   5849 #endif
   5850 int ilogb(double x)
   5851 #ifdef C_DECLS_ONLY
   5852 ;
   5853 #else
   5854 { int e; if (x == 0) return FP_ILOGB0; if (isnan(x)) return FP_ILOGBNAN; if (isinf(x)) return 2147483647; frexp(x, &e); return e - 1; }
   5855 #endif
   5856 double logb(double x)
   5857 #ifdef C_DECLS_ONLY
   5858 ;
   5859 #else
   5860 { return (double)ilogb(x); }
   5861 #endif
   5862 double fmin(double a, double b)
   5863 #ifdef C_DECLS_ONLY
   5864 ;
   5865 #else
   5866 { return isnan(a) ? b : isnan(b) ? a : a < b ? a : b; }
   5867 #endif
   5868 double fmax(double a, double b)
   5869 #ifdef C_DECLS_ONLY
   5870 ;
   5871 #else
   5872 { return isnan(a) ? b : isnan(b) ? a : a > b ? a : b; }
   5873 #endif
   5874 float fminf(float a, float b)
   5875 #ifdef C_DECLS_ONLY
   5876 ;
   5877 #else
   5878 { return (float)fmin(a, b); }
   5879 #endif
   5880 float fmaxf(float a, float b)
   5881 #ifdef C_DECLS_ONLY
   5882 ;
   5883 #else
   5884 { return (float)fmax(a, b); }
   5885 #endif
   5886 double fdim(double a, double b)
   5887 #ifdef C_DECLS_ONLY
   5888 ;
   5889 #else
   5890 { return a > b ? a - b : 0; }
   5891 #endif
   5892 double fma(double a, double b, double c)
   5893 #ifdef C_DECLS_ONLY
   5894 ;
   5895 #else
   5896 { return (double)((long double)a * b + c); }
   5897 #endif
   5898 double nan(const char *s)
   5899 #ifdef C_DECLS_ONLY
   5900 ;
   5901 #else
   5902 { (void)s; return __builtin_nan(""); }
   5903 #endif
   5904 double nextafter(double x, double y)
   5905 #ifdef C_DECLS_ONLY
   5906 ;
   5907 #else
   5908 {
   5909     union { double d; unsigned long long u; } v = { x };
   5910     if (isnan(x) || isnan(y)) return x + y;
   5911     if (x == y) return y;
   5912     if (x == 0) { v.u = 1; return y > 0 ? v.d : -v.d; }
   5913     if ((x < y) == (x > 0)) v.u++; else v.u--;
   5914     return v.d;
   5915 }
   5916 #endif
   5917 
   5918 double exp2(double x)
   5919 #ifdef C_DECLS_ONLY
   5920 ;
   5921 #else
   5922 { return (double)c_exp2(x); }
   5923 #endif
   5924 double exp(double x)
   5925 #ifdef C_DECLS_ONLY
   5926 ;
   5927 #else
   5928 { return (double)c_exp2((long double)x * 1.4426950408889634073599246810018921L); }
   5929 #endif
   5930 double expm1(double x)
   5931 #ifdef C_DECLS_ONLY
   5932 ;
   5933 #else
   5934 {
   5935     long double t = (long double)x * 1.4426950408889634073599246810018921L;
   5936     if (fabs(x) < 0.3) return (double)(c_f2xm1(t));
   5937     return (double)(c_exp2(t) - 1);
   5938 }
   5939 #endif
   5940 double log(double x)
   5941 #ifdef C_DECLS_ONLY
   5942 ;
   5943 #else
   5944 { if (x < 0) return __builtin_nan(""); return (double)c_fyl2x(0.693147180559945309417232121458176568L, x); }
   5945 #endif
   5946 double log2(double x)
   5947 #ifdef C_DECLS_ONLY
   5948 ;
   5949 #else
   5950 { if (x < 0) return __builtin_nan(""); return (double)c_fyl2x(1, x); }
   5951 #endif
   5952 double log10(double x)
   5953 #ifdef C_DECLS_ONLY
   5954 ;
   5955 #else
   5956 { if (x < 0) return __builtin_nan(""); return (double)c_fyl2x(0.301029995663981195213738894724493027L, x); }
   5957 #endif
   5958 double log1p(double x)
   5959 #ifdef C_DECLS_ONLY
   5960 ;
   5961 #else
   5962 {
   5963     long double r;
   5964     if (x < -1) return __builtin_nan("");
   5965     if (fabs(x) < 0.29) { __asm__("fyl2xp1" : "=t"(r) : "0"((long double)x), "u"(0.693147180559945309417232121458176568L) : "st(1)"); return (double)r; }
   5966     return log(1 + x);
   5967 }
   5968 #endif
   5969 double pow(double x, double y)
   5970 #ifdef C_DECLS_ONLY
   5971 ;
   5972 #else
   5973 {
   5974     long double r;
   5975     int yint;
   5976     if (y == 0 || x == 1) return 1;
   5977     if (isnan(x) || isnan(y)) return __builtin_nan("");
   5978     yint = fabs(y) < 9007199254740992.0 && y == trunc(y);
   5979     if (x == 0) {
   5980         if (y < 0) return yint && fmod(y, 2) != 0 ? copysign(HUGE_VAL, x) : HUGE_VAL;
   5981         return yint && fmod(y, 2) != 0 ? x : 0;
   5982     }
   5983     if (isinf(y)) { double a = fabs(x); return a == 1 ? 1 : (a > 1) == (y > 0) ? HUGE_VAL : 0; }
   5984     if (isinf(x)) { if (x > 0) return y > 0 ? HUGE_VAL : 0; return y > 0 ? (yint && fmod(y, 2) != 0 ? -HUGE_VAL : HUGE_VAL) : (yint && fmod(y, 2) != 0 ? -0.0 : 0.0); }
   5985     if (x < 0) {
   5986         if (!yint) return __builtin_nan("");
   5987         r = c_exp2((long double)y * c_fyl2x(1, -(long double)x));
   5988         return fmod(y, 2) != 0 ? -(double)r : (double)r;
   5989     }
   5990     return (double)c_exp2((long double)y * c_fyl2x(1, (long double)x));
   5991 }
   5992 #endif
   5993 double sin(double x)
   5994 #ifdef C_DECLS_ONLY
   5995 ;
   5996 #else
   5997 { long double r = x; if (isinf(x) || isnan(x)) return __builtin_nan(""); if (fabs(x) > 1e18) r = fmod(x, 6.283185307179586476925286766559); __asm__("fsin" : "+t"(r)); return (double)r; }
   5998 #endif
   5999 double cos(double x)
   6000 #ifdef C_DECLS_ONLY
   6001 ;
   6002 #else
   6003 { long double r = x; if (isinf(x) || isnan(x)) return __builtin_nan(""); if (fabs(x) > 1e18) r = fmod(x, 6.283185307179586476925286766559); __asm__("fcos" : "+t"(r)); return (double)r; }
   6004 #endif
   6005 double tan(double x)
   6006 #ifdef C_DECLS_ONLY
   6007 ;
   6008 #else
   6009 { long double r = x, one; if (isinf(x) || isnan(x)) return __builtin_nan(""); __asm__("fptan" : "+t"(r), "=u"(one)); return (double)one; }
   6010 #endif
   6011 double atan2(double y, double x)
   6012 #ifdef C_DECLS_ONLY
   6013 ;
   6014 #else
   6015 { long double r; __asm__("fpatan" : "=t"(r) : "0"((long double)x), "u"((long double)y) : "st(1)"); return (double)r; }
   6016 #endif
   6017 double atan(double x)
   6018 #ifdef C_DECLS_ONLY
   6019 ;
   6020 #else
   6021 { return atan2(x, 1); }
   6022 #endif
   6023 double asin(double x)
   6024 #ifdef C_DECLS_ONLY
   6025 ;
   6026 #else
   6027 { if (fabs(x) > 1) return __builtin_nan(""); return atan2(x, sqrt((1 - x) * (1 + x))); }
   6028 #endif
   6029 double acos(double x)
   6030 #ifdef C_DECLS_ONLY
   6031 ;
   6032 #else
   6033 { if (fabs(x) > 1) return __builtin_nan(""); return atan2(sqrt((1 - x) * (1 + x)), x); }
   6034 #endif
   6035 double sinh(double x)
   6036 #ifdef C_DECLS_ONLY
   6037 ;
   6038 #else
   6039 { double e = expm1(fabs(x)); double r = e < 1e300 ? (e + e / (e + 1)) / 2 : exp(fabs(x)) / 2; return copysign(r, x); }
   6040 #endif
   6041 double cosh(double x)
   6042 #ifdef C_DECLS_ONLY
   6043 ;
   6044 #else
   6045 { double e = exp(fabs(x)); return (e + 1 / e) / 2; }
   6046 #endif
   6047 double tanh(double x)
   6048 #ifdef C_DECLS_ONLY
   6049 ;
   6050 #else
   6051 { double a = fabs(x), e; if (a > 20) return copysign(1.0, x); e = expm1(2 * a); return copysign(e / (e + 2), x); }
   6052 #endif
   6053 double asinh(double x)
   6054 #ifdef C_DECLS_ONLY
   6055 ;
   6056 #else
   6057 { double a = fabs(x); return copysign(log(a + sqrt(a * a + 1)), x); }
   6058 #endif
   6059 double acosh(double x)
   6060 #ifdef C_DECLS_ONLY
   6061 ;
   6062 #else
   6063 { if (x < 1) return __builtin_nan(""); return log(x + sqrt(x * x - 1)); }
   6064 #endif
   6065 double atanh(double x)
   6066 #ifdef C_DECLS_ONLY
   6067 ;
   6068 #else
   6069 { return 0.5 * log1p(2 * x / (1 - x)); }
   6070 #endif
   6071 double hypot(double x, double y)
   6072 #ifdef C_DECLS_ONLY
   6073 ;
   6074 #else
   6075 {
   6076     double a = fabs(x), b = fabs(y), t;
   6077     if (isinf(a) || isinf(b)) return HUGE_VAL;
   6078     if (a < b) { t = a; a = b; b = t; }
   6079     if (a == 0) return 0;
   6080     t = b / a;
   6081     return a * sqrt(1 + t * t);
   6082 }
   6083 #endif
   6084 double cbrt(double x)
   6085 #ifdef C_DECLS_ONLY
   6086 ;
   6087 #else
   6088 {
   6089     double r;
   6090     if (x == 0 || isinf(x) || isnan(x)) return x;
   6091     r = copysign(pow(fabs(x), 1.0 / 3.0), x);
   6092     return r - (r * r * r - x) / (3 * r * r);
   6093 }
   6094 #endif
   6095 double c_erf_series(double x)
   6096 #ifdef C_DECLS_ONLY
   6097 ;
   6098 #else
   6099 { double t = x, s = x; int n; for (n = 1; n < 80; n++) { t *= -x * x / n; s += t / (2 * n + 1); } return 1.1283791670955126 * s; }
   6100 #endif
   6101 #ifdef C_DECLS_ONLY
   6102 extern double erfc(double x);
   6103 #else
   6104 double erfc(double x);
   6105 #endif
   6106 double erf(double x)
   6107 #ifdef C_DECLS_ONLY
   6108 ;
   6109 #else
   6110 { if (fabs(x) < 2.5) return c_erf_series(x); return x > 0 ? 1 - erfc(x) : erfc(-x) - 1; }
   6111 #endif
   6112 double erfc(double x)
   6113 #ifdef C_DECLS_ONLY
   6114 ;
   6115 #else
   6116 {
   6117     double f = 0;
   6118     int n;
   6119     if (x < 2.5) return 1 - erf(x);
   6120     for (n = 60; n >= 1; n--) f = n / 2.0 / (x + f);
   6121     return exp(-x * x) / (1.7724538509055160 * (x + f));
   6122 }
   6123 #endif
   6124 double lgamma(double x)
   6125 #ifdef C_DECLS_ONLY
   6126 ;
   6127 #else
   6128 {
   6129     static const double c[] = { 76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5 };
   6130     double y, tmp, ser;
   6131     int j;
   6132     if (x < 0.5) return log(M_PI / fabs(sin(M_PI * x))) - lgamma(1 - x);
   6133     y = x; tmp = x + 5.5; tmp -= (x + 0.5) * log(tmp); ser = 1.000000000190015;
   6134     for (j = 0; j < 6; j++) ser += c[j] / ++y;
   6135     return -tmp + log(2.5066282746310005 * ser / x);
   6136 }
   6137 #endif
   6138 double tgamma(double x)
   6139 #ifdef C_DECLS_ONLY
   6140 ;
   6141 #else
   6142 {
   6143     if (x == trunc(x) && x > 0 && x < 24) { double r = 1; int i; for (i = 2; i < (int)x; i++) r *= i; return r; }
   6144     if (x < 0.5) return M_PI / (sin(M_PI * x) * tgamma(1 - x));
   6145     return exp(lgamma(x));
   6146 }
   6147 #endif
   6148 #ifdef C_DECLS_ONLY
   6149 #define c_float_fn(name) float name##f(float x);
   6150 #else
   6151 #define c_float_fn(name) float name##f(float x) { return (float)name(x); }
   6152 #endif
   6153 c_float_fn(sin) c_float_fn(cos) c_float_fn(tan) c_float_fn(asin) c_float_fn(acos) c_float_fn(atan) c_float_fn(exp) c_float_fn(log) c_float_fn(log2)
   6154 c_float_fn(log10) c_float_fn(sinh) c_float_fn(cosh) c_float_fn(tanh) c_float_fn(exp2) c_float_fn(expm1) c_float_fn(log1p) c_float_fn(cbrt)
   6155 float powf(float x, float y)
   6156 #ifdef C_DECLS_ONLY
   6157 ;
   6158 #else
   6159 { return (float)pow(x, y); }
   6160 #endif
   6161 float atan2f(float y, float x)
   6162 #ifdef C_DECLS_ONLY
   6163 ;
   6164 #else
   6165 { return (float)atan2(y, x); }
   6166 #endif
   6167 float hypotf(float x, float y)
   6168 #ifdef C_DECLS_ONLY
   6169 ;
   6170 #else
   6171 { return (float)hypot(x, y); }
   6172 #endif
   6173 float ldexpf(float x, int n)
   6174 #ifdef C_DECLS_ONLY
   6175 ;
   6176 #else
   6177 { return (float)ldexp(x, n); }
   6178 #endif
   6179 float frexpf(float x, int *e)
   6180 #ifdef C_DECLS_ONLY
   6181 ;
   6182 #else
   6183 { return (float)frexp(x, e); }
   6184 #endif
   6185 long double expl(long double x)
   6186 #ifdef C_DECLS_ONLY
   6187 ;
   6188 #else
   6189 { return c_exp2(x * 1.4426950408889634073599246810018921L); }
   6190 #endif
   6191 long double logl(long double x)
   6192 #ifdef C_DECLS_ONLY
   6193 ;
   6194 #else
   6195 { return c_fyl2x(0.693147180559945309417232121458176568L, x); }
   6196 #endif
   6197 long double powl(long double x, long double y)
   6198 #ifdef C_DECLS_ONLY
   6199 ;
   6200 #else
   6201 { return (long double)pow((double)x, (double)y); }
   6202 #endif
   6203 long double sinl(long double x)
   6204 #ifdef C_DECLS_ONLY
   6205 ;
   6206 #else
   6207 { return sin((double)x); }
   6208 #endif
   6209 long double cosl(long double x)
   6210 #ifdef C_DECLS_ONLY
   6211 ;
   6212 #else
   6213 { return cos((double)x); }
   6214 #endif
   6215 /* ---- scanf, locale and wide char stubs, threads that aren't, dlopen that can't [chamesle] ---- */
   6216 #ifndef C_DECLS_ONLY
   6217 static int c_scan(int (*get)(void *), void (*unget)(int, void *), void *src, const char *fmt, va_list ap)
   6218 {
   6219     int n = 0, c, count = 0;
   6220     for (; *fmt; fmt++) {
   6221         int sup = 0, width = 0, lng = 0, hh = 0, ok;
   6222         if (isspace((unsigned char)*fmt)) { while (isspace(c = get(src))) ; unget(c, src); continue; }
   6223         if (*fmt != '%') {
   6224             c = get(src);
   6225             if (c != (unsigned char)*fmt) { unget(c, src); return n ? n : (c == EOF ? EOF : 0); }
   6226             count++;
   6227             continue;
   6228         }
   6229         fmt++;
   6230         if (*fmt == '%') { while (isspace(c = get(src))) ; if (c != '%') { unget(c, src); return n; } continue; }
   6231         if (*fmt == '*') { sup = 1; fmt++; }
   6232         while (isdigit((unsigned char)*fmt)) width = width * 10 + *fmt++ - '0';
   6233         for (;; fmt++) {
   6234             if (*fmt == 'l') lng++;
   6235             else if (*fmt == 'h') hh++;
   6236             else if (*fmt == 'z' || *fmt == 'j' || *fmt == 't' || *fmt == 'L' || *fmt == 'q') lng = 2;
   6237             else break;
   6238         }
   6239         if (!width) width = 1 << 30;
   6240         if (*fmt != 'c' && *fmt != '[' && *fmt != 'n') { while (isspace(c = get(src))) ; unget(c, src); }
   6241         if (*fmt == 'n') { if (!sup) *va_arg(ap, int *) = count; continue; }
   6242         {
   6243             char tok[512];
   6244             int len = 0;
   6245             switch (*fmt) {
   6246             case 'd': case 'i': case 'u': case 'x': case 'X': case 'o': case 'p': {
   6247                 int base = *fmt == 'd' || *fmt == 'u' ? 10 : *fmt == 'o' ? 8 : *fmt == 'i' ? 0 : 16;
   6248                 int neg = 0;
   6249                 unsigned long long v = 0;
   6250                 int digits = 0;
   6251                 c = get(src);
   6252                 if (c == '-' || c == '+') { neg = c == '-'; width--; c = get(src); }
   6253                 if (width > 0 && c == '0' && (base == 0 || base == 16)) {
   6254                     int c2;
   6255                     count++; width--;
   6256                     c2 = get(src);
   6257                     if ((c2 == 'x' || c2 == 'X') && width > 0) { base = 16; width--; count++; c = get(src); }
   6258                     else { unget(c2, src); if (base == 0) base = 8; digits = 1; c = '0'; if (c2 != 'x') { c = get(src); unget(c, src); c = c2; (void)c; c = get(src); } }
   6259                     if (digits) { c = c2; }
   6260                 }
   6261                 if (base == 0) base = 10;
   6262                 while (width > 0) {
   6263                     int d = c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 : 99;
   6264                     if (d >= base) break;
   6265                     v = v * base + d; digits = 1; width--; count++;
   6266                     c = get(src);
   6267                 }
   6268                 unget(c, src);
   6269                 if (!digits) return n ? n : (c == EOF ? EOF : 0);
   6270                 if (neg) v = -v;
   6271                 if (!sup) {
   6272                     if (*fmt == 'd' || *fmt == 'i') {
   6273                         if (lng >= 2) *va_arg(ap, long long *) = v; else if (lng == 1) *va_arg(ap, long *) = v; else if (hh == 1) *va_arg(ap, short *) = v; else if (hh > 1) *va_arg(ap, signed char *) = v; else *va_arg(ap, int *) = v;
   6274                     } else if (*fmt == 'p') *va_arg(ap, void **) = (void *)(unsigned long)v;
   6275                     else if (lng >= 2) *va_arg(ap, unsigned long long *) = v; else if (lng == 1) *va_arg(ap, unsigned long *) = v; else if (hh == 1) *va_arg(ap, unsigned short *) = v; else if (hh > 1) *va_arg(ap, unsigned char *) = v; else *va_arg(ap, unsigned *) = v;
   6276                     n++;
   6277                 }
   6278                 break;
   6279             }
   6280             case 'f': case 'e': case 'g': case 'E': case 'G': case 'a': {
   6281                 char *end;
   6282                 double v;
   6283                 c = get(src);
   6284                 while (len < 500 && width > 0 && (isdigit(c) || c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E' || c == 'x' || c == 'X' || c == 'n' || c == 'a' || c == 'i' || c == 'f' || c == 'N' || c == 'A' || c == 'I' || c == 'p' || (c >= 'b' && c <= 'd'))) {
   6285                     tok[len++] = c; width--; c = get(src);
   6286                 }
   6287                 unget(c, src);
   6288                 tok[len] = 0;
   6289                 v = strtod(tok, &end);
   6290                 if (end == tok) return n ? n : (c == EOF ? EOF : 0);
   6291                 count += len;
   6292                 if (!sup) { if (lng) *va_arg(ap, double *) = v; else *va_arg(ap, float *) = v; n++; }
   6293                 break;
   6294             }
   6295             case 's': {
   6296                 char *d = sup ? NULL : va_arg(ap, char *);
   6297                 c = get(src);
   6298                 while (c != EOF && !isspace(c) && width > 0) { if (d) d[len] = c; len++; width--; c = get(src); }
   6299                 unget(c, src);
   6300                 if (!len) return n ? n : EOF;
   6301                 if (d) { d[len] = 0; n++; }
   6302                 count += len;
   6303                 break;
   6304             }
   6305             case 'c': {
   6306                 char *d = sup ? NULL : va_arg(ap, char *);
   6307                 if (width == 1 << 30) width = 1;
   6308                 while (width-- > 0) { c = get(src); if (c == EOF) return n ? n : EOF; if (d) d[len] = c; len++; }
   6309                 if (d) n++;
   6310                 count += len;
   6311                 break;
   6312             }
   6313             case '[': {
   6314                 char set[256];
   6315                 int neg = 0, i;
   6316                 char *d = sup ? NULL : va_arg(ap, char *);
   6317                 memset(set, 0, sizeof set);
   6318                 fmt++;
   6319                 if (*fmt == '^') { neg = 1; fmt++; }
   6320                 if (*fmt == ']') { set[']'] = 1; fmt++; }
   6321                 while (*fmt && *fmt != ']') {
   6322                     if (fmt[1] == '-' && fmt[2] && fmt[2] != ']') { for (i = (unsigned char)fmt[0]; i <= (unsigned char)fmt[2]; i++) set[i] = 1; fmt += 3; }
   6323                     else set[(unsigned char)*fmt++] = 1;
   6324                 }
   6325                 c = get(src);
   6326                 while (c != EOF && width > 0 && set[c] != neg) { if (d) d[len] = c; len++; width--; c = get(src); }
   6327                 unget(c, src);
   6328                 if (!len) return n ? n : (c == EOF ? EOF : 0);
   6329                 if (d) { d[len] = 0; n++; }
   6330                 count += len;
   6331                 break;
   6332             }
   6333             default: return n;
   6334             }
   6335             (void)ok;
   6336         }
   6337     }
   6338     return n;
   6339 }
   6340 #endif
   6341 #ifndef C_DECLS_ONLY
   6342 static int c_sget(void *p) { const char **s = p; int c = (unsigned char)**s; if (!c) return EOF; (*s)++; return c; }
   6343 #endif
   6344 #ifndef C_DECLS_ONLY
   6345 static void c_sunget(int c, void *p) { const char **s = p; if (c != EOF) (*s)--; }
   6346 #endif
   6347 #ifndef C_DECLS_ONLY
   6348 static int c_fget(void *p) { return fgetc(p); }
   6349 #endif
   6350 #ifndef C_DECLS_ONLY
   6351 static void c_funget(int c, void *p) { if (c != EOF) ungetc(c, p); }
   6352 #endif
   6353 int vsscanf(const char *s, const char *fmt, va_list ap)
   6354 #ifdef C_DECLS_ONLY
   6355 ;
   6356 #else
   6357 { return c_scan(c_sget, c_sunget, &s, fmt, ap); }
   6358 #endif
   6359 int sscanf(const char *s, const char *fmt, ...)
   6360 #ifdef C_DECLS_ONLY
   6361 ;
   6362 #else
   6363 { va_list ap; int n; va_start(ap, fmt); n = vsscanf(s, fmt, ap); va_end(ap); return n; }
   6364 #endif
   6365 int vfscanf(FILE *f, const char *fmt, va_list ap)
   6366 #ifdef C_DECLS_ONLY
   6367 ;
   6368 #else
   6369 { return c_scan(c_fget, c_funget, f, fmt, ap); }
   6370 #endif
   6371 int fscanf(FILE *f, const char *fmt, ...)
   6372 #ifdef C_DECLS_ONLY
   6373 ;
   6374 #else
   6375 { va_list ap; int n; va_start(ap, fmt); n = vfscanf(f, fmt, ap); va_end(ap); return n; }
   6376 #endif
   6377 int scanf(const char *fmt, ...)
   6378 #ifdef C_DECLS_ONLY
   6379 ;
   6380 #else
   6381 { va_list ap; int n; va_start(ap, fmt); n = vfscanf(stdin, fmt, ap); va_end(ap); return n; }
   6382 #endif
   6383 int vscanf(const char *fmt, va_list ap)
   6384 #ifdef C_DECLS_ONLY
   6385 ;
   6386 #else
   6387 { return vfscanf(stdin, fmt, ap); }
   6388 #endif
   6389 
   6390 /* locale: there is only the C one */
   6391 #define LC_CTYPE 0
   6392 #define LC_NUMERIC 1
   6393 #define LC_TIME 2
   6394 #define LC_COLLATE 3
   6395 #define LC_MONETARY 4
   6396 #define LC_MESSAGES 5
   6397 #define LC_ALL 6
   6398 struct lconv { char *decimal_point, *thousands_sep, *grouping, *int_curr_symbol, *currency_symbol, *mon_decimal_point, *mon_thousands_sep, *mon_grouping, *positive_sign, *negative_sign;
   6399     char int_frac_digits, frac_digits, p_cs_precedes, p_sep_by_space, n_cs_precedes, n_sep_by_space, p_sign_posn, n_sign_posn; };
   6400 char *setlocale(int cat, const char *name)
   6401 #ifdef C_DECLS_ONLY
   6402 ;
   6403 #else
   6404 { (void)cat; if (!name || !*name || !strcmp(name, "C") || !strcmp(name, "POSIX") || !strcmp(name, "C.UTF-8")) return "C"; return NULL; }
   6405 #endif
   6406 struct lconv *localeconv(void)
   6407 #ifdef C_DECLS_ONLY
   6408 ;
   6409 #else
   6410 {
   6411     static struct lconv l = { ".", "", "", "", "", "", "", "", "", "", 127, 127, 127, 127, 127, 127, 127, 127 };
   6412     return &l;
   6413 }
   6414 #endif
   6415 typedef void *locale_t;
   6416 locale_t newlocale(int mask, const char *name, locale_t base)
   6417 #ifdef C_DECLS_ONLY
   6418 ;
   6419 #else
   6420 { (void)mask; (void)name; (void)base; return (locale_t)1; }
   6421 #endif
   6422 void freelocale(locale_t l)
   6423 #ifdef C_DECLS_ONLY
   6424 ;
   6425 #else
   6426 { (void)l; }
   6427 #endif
   6428 locale_t uselocale(locale_t l)
   6429 #ifdef C_DECLS_ONLY
   6430 ;
   6431 #else
   6432 { (void)l; return (locale_t)1; }
   6433 #endif
   6434 #define LC_GLOBAL_LOCALE ((locale_t)-1)
   6435 #define CODESET 14
   6436 #define D_T_FMT 0x20028
   6437 #define RADIXCHAR 0x10000
   6438 char *nl_langinfo(int item)
   6439 #ifdef C_DECLS_ONLY
   6440 ;
   6441 #else
   6442 { return item == CODESET ? "ANSI_X3.4-1968" : item == RADIXCHAR ? "." : ""; }
   6443 #endif
   6444 char *gettext(const char *s)
   6445 #ifdef C_DECLS_ONLY
   6446 ;
   6447 #else
   6448 { return (char *)s; }
   6449 #endif
   6450 char *dgettext(const char *d, const char *s)
   6451 #ifdef C_DECLS_ONLY
   6452 ;
   6453 #else
   6454 { (void)d; return (char *)s; }
   6455 #endif
   6456 char *ngettext(const char *a, const char *b, unsigned long n)
   6457 #ifdef C_DECLS_ONLY
   6458 ;
   6459 #else
   6460 { return (char *)(n == 1 ? a : b); }
   6461 #endif
   6462 char *textdomain(const char *d)
   6463 #ifdef C_DECLS_ONLY
   6464 ;
   6465 #else
   6466 { return (char *)d; }
   6467 #endif
   6468 char *bindtextdomain(const char *d, const char *dir)
   6469 #ifdef C_DECLS_ONLY
   6470 ;
   6471 #else
   6472 { (void)d; return (char *)dir; }
   6473 #endif
   6474 #define _(s) (s)
   6475 int isalpha_l(int c, locale_t l)
   6476 #ifdef C_DECLS_ONLY
   6477 ;
   6478 #else
   6479 { (void)l; return isalpha(c); }
   6480 #endif
   6481 int strcoll_l(const char *a, const char *b, locale_t l)
   6482 #ifdef C_DECLS_ONLY
   6483 ;
   6484 #else
   6485 { (void)l; return strcmp(a, b); }
   6486 #endif
   6487 int strcasecmp_l(const char *a, const char *b, locale_t l)
   6488 #ifdef C_DECLS_ONLY
   6489 ;
   6490 #else
   6491 { (void)l; return strcasecmp(a, b); }
   6492 #endif
   6493 
   6494 typedef int wchar_t;
   6495 typedef int wint_t;
   6496 typedef int wctype_t;
   6497 typedef struct { int c_; } mbstate_t;
   6498 #define WEOF (-1)
   6499 #define MB_LEN_MAX 4
   6500 #define MB_CUR_MAX 1
   6501 int mbtowc(wchar_t *w, const char *s, size_t n)
   6502 #ifdef C_DECLS_ONLY
   6503 ;
   6504 #else
   6505 { if (!s) return 0; if (!n) return -1; if (w) *w = (unsigned char)*s; return *s ? 1 : 0; }
   6506 #endif
   6507 int wctomb(char *s, wchar_t w)
   6508 #ifdef C_DECLS_ONLY
   6509 ;
   6510 #else
   6511 { if (!s) return 0; *s = w; return 1; }
   6512 #endif
   6513 size_t mbrtowc(wchar_t *w, const char *s, size_t n, mbstate_t *st)
   6514 #ifdef C_DECLS_ONLY
   6515 ;
   6516 #else
   6517 { (void)st; if (!s) return 0; if (!n) return (size_t)-2; if (w) *w = (unsigned char)*s; return *s ? 1 : 0; }
   6518 #endif
   6519 size_t wcrtomb(char *s, wchar_t w, mbstate_t *st)
   6520 #ifdef C_DECLS_ONLY
   6521 ;
   6522 #else
   6523 { (void)st; if (!s) return 1; *s = w; return 1; }
   6524 #endif
   6525 size_t mbstowcs(wchar_t *d, const char *s, size_t n)
   6526 #ifdef C_DECLS_ONLY
   6527 ;
   6528 #else
   6529 { size_t i = 0; while (s[i] && (!d || i < n)) { if (d) d[i] = (unsigned char)s[i]; i++; } if (d && i < n) d[i] = 0; return i; }
   6530 #endif
   6531 size_t wcstombs(char *d, const wchar_t *s, size_t n)
   6532 #ifdef C_DECLS_ONLY
   6533 ;
   6534 #else
   6535 { size_t i = 0; while (s[i] && (!d || i < n)) { if (d) d[i] = s[i]; i++; } if (d && i < n) d[i] = 0; return i; }
   6536 #endif
   6537 size_t mbrlen(const char *s, size_t n, mbstate_t *st)
   6538 #ifdef C_DECLS_ONLY
   6539 ;
   6540 #else
   6541 { return mbrtowc(NULL, s, n, st); }
   6542 #endif
   6543 int mbsinit(const mbstate_t *st)
   6544 #ifdef C_DECLS_ONLY
   6545 ;
   6546 #else
   6547 { (void)st; return 1; }
   6548 #endif
   6549 int mblen(const char *s, size_t n)
   6550 #ifdef C_DECLS_ONLY
   6551 ;
   6552 #else
   6553 { return mbtowc(NULL, s, n); }
   6554 #endif
   6555 size_t wcslen(const wchar_t *s)
   6556 #ifdef C_DECLS_ONLY
   6557 ;
   6558 #else
   6559 { size_t n = 0; while (s[n]) n++; return n; }
   6560 #endif
   6561 int wcscmp(const wchar_t *a, const wchar_t *b)
   6562 #ifdef C_DECLS_ONLY
   6563 ;
   6564 #else
   6565 { while (*a && *a == *b) { a++; b++; } return *a - *b; }
   6566 #endif
   6567 wchar_t *wcscpy(wchar_t *d, const wchar_t *s)
   6568 #ifdef C_DECLS_ONLY
   6569 ;
   6570 #else
   6571 { wchar_t *r = d; while ((*d++ = *s++)) ; return r; }
   6572 #endif
   6573 wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n)
   6574 #ifdef C_DECLS_ONLY
   6575 ;
   6576 #else
   6577 { return memcpy(d, s, n * sizeof *d); }
   6578 #endif
   6579 wchar_t *wmemset(wchar_t *d, wchar_t c, size_t n)
   6580 #ifdef C_DECLS_ONLY
   6581 ;
   6582 #else
   6583 { size_t i; for (i = 0; i < n; i++) d[i] = c; return d; }
   6584 #endif
   6585 int iswalpha(wint_t c)
   6586 #ifdef C_DECLS_ONLY
   6587 ;
   6588 #else
   6589 { return isalpha(c); }
   6590 #endif
   6591 int iswdigit(wint_t c)
   6592 #ifdef C_DECLS_ONLY
   6593 ;
   6594 #else
   6595 { return isdigit(c); }
   6596 #endif
   6597 int iswspace(wint_t c)
   6598 #ifdef C_DECLS_ONLY
   6599 ;
   6600 #else
   6601 { return isspace(c); }
   6602 #endif
   6603 int iswupper(wint_t c)
   6604 #ifdef C_DECLS_ONLY
   6605 ;
   6606 #else
   6607 { return isupper(c); }
   6608 #endif
   6609 int iswlower(wint_t c)
   6610 #ifdef C_DECLS_ONLY
   6611 ;
   6612 #else
   6613 { return islower(c); }
   6614 #endif
   6615 int iswalnum(wint_t c)
   6616 #ifdef C_DECLS_ONLY
   6617 ;
   6618 #else
   6619 { return isalnum(c); }
   6620 #endif
   6621 int iswprint(wint_t c)
   6622 #ifdef C_DECLS_ONLY
   6623 ;
   6624 #else
   6625 { return c >= 32 && c < 127; }
   6626 #endif
   6627 int iswpunct(wint_t c)
   6628 #ifdef C_DECLS_ONLY
   6629 ;
   6630 #else
   6631 { return ispunct(c); }
   6632 #endif
   6633 int iswcntrl(wint_t c)
   6634 #ifdef C_DECLS_ONLY
   6635 ;
   6636 #else
   6637 { return iscntrl(c); }
   6638 #endif
   6639 wint_t towlower(wint_t c)
   6640 #ifdef C_DECLS_ONLY
   6641 ;
   6642 #else
   6643 { return tolower(c); }
   6644 #endif
   6645 wint_t towupper(wint_t c)
   6646 #ifdef C_DECLS_ONLY
   6647 ;
   6648 #else
   6649 { return toupper(c); }
   6650 #endif
   6651 int wcwidth(wchar_t c)
   6652 #ifdef C_DECLS_ONLY
   6653 ;
   6654 #else
   6655 { return c == 0 ? 0 : c < 32 || (c >= 127 && c < 160) ? -1 : 1; }
   6656 #endif
   6657 int wcswidth(const wchar_t *s, size_t n)
   6658 #ifdef C_DECLS_ONLY
   6659 ;
   6660 #else
   6661 { int w = 0; while (n-- && *s) { int c = wcwidth(*s++); if (c < 0) return -1; w += c; } return w; }
   6662 #endif
   6663 wint_t btowc(int c)
   6664 #ifdef C_DECLS_ONLY
   6665 ;
   6666 #else
   6667 { return c; }
   6668 #endif
   6669 int wctob(wint_t c)
   6670 #ifdef C_DECLS_ONLY
   6671 ;
   6672 #else
   6673 { return c < 256 ? c : EOF; }
   6674 #endif
   6675 
   6676 /* threads: one of them. a mutex always locks, a thread is run to the end when it's made */
   6677 typedef unsigned long pthread_t;
   6678 typedef struct { int k; } pthread_mutex_t, pthread_mutexattr_t, pthread_cond_t, pthread_condattr_t, pthread_rwlock_t, pthread_attr_t, pthread_rwlockattr_t, pthread_spinlock_t_;
   6679 typedef struct { int done; } pthread_once_t;
   6680 typedef unsigned pthread_key_t;
   6681 #define PTHREAD_MUTEX_INITIALIZER { 0 }
   6682 #define PTHREAD_COND_INITIALIZER { 0 }
   6683 #define PTHREAD_RWLOCK_INITIALIZER { 0 }
   6684 #define PTHREAD_ONCE_INIT { 0 }
   6685 #define PTHREAD_MUTEX_RECURSIVE 1
   6686 #define PTHREAD_MUTEX_NORMAL 0
   6687 #define PTHREAD_MUTEX_ERRORCHECK 2
   6688 #define PTHREAD_CREATE_DETACHED 1
   6689 #define PTHREAD_CREATE_JOINABLE 0
   6690 int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a)
   6691 #ifdef C_DECLS_ONLY
   6692 ;
   6693 #else
   6694 { (void)m; (void)a; return 0; }
   6695 #endif
   6696 int pthread_mutex_lock(pthread_mutex_t *m)
   6697 #ifdef C_DECLS_ONLY
   6698 ;
   6699 #else
   6700 { (void)m; return 0; }
   6701 #endif
   6702 int pthread_mutex_trylock(pthread_mutex_t *m)
   6703 #ifdef C_DECLS_ONLY
   6704 ;
   6705 #else
   6706 { (void)m; return 0; }
   6707 #endif
   6708 int pthread_mutex_unlock(pthread_mutex_t *m)
   6709 #ifdef C_DECLS_ONLY
   6710 ;
   6711 #else
   6712 { (void)m; return 0; }
   6713 #endif
   6714 int pthread_mutex_destroy(pthread_mutex_t *m)
   6715 #ifdef C_DECLS_ONLY
   6716 ;
   6717 #else
   6718 { (void)m; return 0; }
   6719 #endif
   6720 int pthread_mutexattr_init(pthread_mutexattr_t *a)
   6721 #ifdef C_DECLS_ONLY
   6722 ;
   6723 #else
   6724 { (void)a; return 0; }
   6725 #endif
   6726 int pthread_mutexattr_settype(pthread_mutexattr_t *a, int t)
   6727 #ifdef C_DECLS_ONLY
   6728 ;
   6729 #else
   6730 { (void)a; (void)t; return 0; }
   6731 #endif
   6732 int pthread_mutexattr_destroy(pthread_mutexattr_t *a)
   6733 #ifdef C_DECLS_ONLY
   6734 ;
   6735 #else
   6736 { (void)a; return 0; }
   6737 #endif
   6738 int pthread_cond_init(pthread_cond_t *c, const pthread_condattr_t *a)
   6739 #ifdef C_DECLS_ONLY
   6740 ;
   6741 #else
   6742 { (void)c; (void)a; return 0; }
   6743 #endif
   6744 int pthread_cond_signal(pthread_cond_t *c)
   6745 #ifdef C_DECLS_ONLY
   6746 ;
   6747 #else
   6748 { (void)c; return 0; }
   6749 #endif
   6750 int pthread_cond_broadcast(pthread_cond_t *c)
   6751 #ifdef C_DECLS_ONLY
   6752 ;
   6753 #else
   6754 { (void)c; return 0; }
   6755 #endif
   6756 int pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m)
   6757 #ifdef C_DECLS_ONLY
   6758 ;
   6759 #else
   6760 { (void)c; (void)m; return 0; }
   6761 #endif
   6762 int pthread_cond_timedwait(pthread_cond_t *c, pthread_mutex_t *m, const struct timespec *t)
   6763 #ifdef C_DECLS_ONLY
   6764 ;
   6765 #else
   6766 { (void)c; (void)m; (void)t; return ETIMEDOUT; }
   6767 #endif
   6768 int pthread_cond_destroy(pthread_cond_t *c)
   6769 #ifdef C_DECLS_ONLY
   6770 ;
   6771 #else
   6772 { (void)c; return 0; }
   6773 #endif
   6774 int pthread_rwlock_init(pthread_rwlock_t *l, const pthread_rwlockattr_t *a)
   6775 #ifdef C_DECLS_ONLY
   6776 ;
   6777 #else
   6778 { (void)l; (void)a; return 0; }
   6779 #endif
   6780 int pthread_rwlock_rdlock(pthread_rwlock_t *l)
   6781 #ifdef C_DECLS_ONLY
   6782 ;
   6783 #else
   6784 { (void)l; return 0; }
   6785 #endif
   6786 int pthread_rwlock_wrlock(pthread_rwlock_t *l)
   6787 #ifdef C_DECLS_ONLY
   6788 ;
   6789 #else
   6790 { (void)l; return 0; }
   6791 #endif
   6792 int pthread_rwlock_unlock(pthread_rwlock_t *l)
   6793 #ifdef C_DECLS_ONLY
   6794 ;
   6795 #else
   6796 { (void)l; return 0; }
   6797 #endif
   6798 int pthread_rwlock_destroy(pthread_rwlock_t *l)
   6799 #ifdef C_DECLS_ONLY
   6800 ;
   6801 #else
   6802 { (void)l; return 0; }
   6803 #endif
   6804 int pthread_once(pthread_once_t *o, void (*f)(void))
   6805 #ifdef C_DECLS_ONLY
   6806 ;
   6807 #else
   6808 { if (!o->done) { o->done = 1; f(); } return 0; }
   6809 #endif
   6810 pthread_t pthread_self(void)
   6811 #ifdef C_DECLS_ONLY
   6812 ;
   6813 #else
   6814 { return 1; }
   6815 #endif
   6816 int pthread_equal(pthread_t a, pthread_t b)
   6817 #ifdef C_DECLS_ONLY
   6818 ;
   6819 #else
   6820 { return a == b; }
   6821 #endif
   6822 #ifndef C_DECLS_ONLY
   6823 static void *c_tls[64];
   6824 #endif
   6825 #ifndef C_DECLS_ONLY
   6826 static int c_tls_n;
   6827 #endif
   6828 int pthread_key_create(pthread_key_t *k, void (*d)(void *))
   6829 #ifdef C_DECLS_ONLY
   6830 ;
   6831 #else
   6832 { (void)d; if (c_tls_n == 64) return EAGAIN; *k = c_tls_n++; return 0; }
   6833 #endif
   6834 int pthread_key_delete(pthread_key_t k)
   6835 #ifdef C_DECLS_ONLY
   6836 ;
   6837 #else
   6838 { (void)k; return 0; }
   6839 #endif
   6840 void *pthread_getspecific(pthread_key_t k)
   6841 #ifdef C_DECLS_ONLY
   6842 ;
   6843 #else
   6844 { return k < 64 ? c_tls[k] : NULL; }
   6845 #endif
   6846 int pthread_setspecific(pthread_key_t k, const void *v)
   6847 #ifdef C_DECLS_ONLY
   6848 ;
   6849 #else
   6850 { if (k >= 64) return EINVAL; c_tls[k] = (void *)v; return 0; }
   6851 #endif
   6852 int pthread_attr_init(pthread_attr_t *a)
   6853 #ifdef C_DECLS_ONLY
   6854 ;
   6855 #else
   6856 { (void)a; return 0; }
   6857 #endif
   6858 int pthread_attr_destroy(pthread_attr_t *a)
   6859 #ifdef C_DECLS_ONLY
   6860 ;
   6861 #else
   6862 { (void)a; return 0; }
   6863 #endif
   6864 int pthread_attr_setdetachstate(pthread_attr_t *a, int s)
   6865 #ifdef C_DECLS_ONLY
   6866 ;
   6867 #else
   6868 { (void)a; (void)s; return 0; }
   6869 #endif
   6870 int pthread_attr_setstacksize(pthread_attr_t *a, size_t s)
   6871 #ifdef C_DECLS_ONLY
   6872 ;
   6873 #else
   6874 { (void)a; (void)s; return 0; }
   6875 #endif
   6876 int pthread_create(pthread_t *t, const pthread_attr_t *a, void *(*fn)(void *), void *arg)
   6877 #ifdef C_DECLS_ONLY
   6878 ;
   6879 #else
   6880 { (void)a; (void)fn; (void)arg; (void)t; return EAGAIN; }
   6881 #endif
   6882 int pthread_join(pthread_t t, void **r)
   6883 #ifdef C_DECLS_ONLY
   6884 ;
   6885 #else
   6886 { (void)t; (void)r; return ESRCH; }
   6887 #endif
   6888 int pthread_detach(pthread_t t)
   6889 #ifdef C_DECLS_ONLY
   6890 ;
   6891 #else
   6892 { (void)t; return 0; }
   6893 #endif
   6894 int pthread_atfork(void (*a)(void), void (*b)(void), void (*c)(void))
   6895 #ifdef C_DECLS_ONLY
   6896 ;
   6897 #else
   6898 { (void)a; (void)b; (void)c; return 0; }
   6899 #endif
   6900 int pthread_setcancelstate(int s, int *o)
   6901 #ifdef C_DECLS_ONLY
   6902 ;
   6903 #else
   6904 { (void)s; if (o) *o = 0; return 0; }
   6905 #endif
   6906 int pthread_kill(pthread_t t, int sig)
   6907 #ifdef C_DECLS_ONLY
   6908 ;
   6909 #else
   6910 { (void)t; return raise(sig); }
   6911 #endif
   6912 int sched_yield(void)
   6913 #ifdef C_DECLS_ONLY
   6914 ;
   6915 #else
   6916 { return fail(sys0(SYS_sched_yield)); }
   6917 #endif
   6918 
   6919 /* dlopen: static binaries have nothing to open */
   6920 #define RTLD_LAZY 1
   6921 #define RTLD_NOW 2
   6922 #define RTLD_GLOBAL 0x100
   6923 #define RTLD_LOCAL 0
   6924 #define RTLD_DEFAULT ((void *)0)
   6925 void *dlopen(const char *f, int flags)
   6926 #ifdef C_DECLS_ONLY
   6927 ;
   6928 #else
   6929 { (void)f; (void)flags; return NULL; }
   6930 #endif
   6931 void *dlsym(void *h, const char *s)
   6932 #ifdef C_DECLS_ONLY
   6933 ;
   6934 #else
   6935 { (void)h; (void)s; return NULL; }
   6936 #endif
   6937 int dlclose(void *h)
   6938 #ifdef C_DECLS_ONLY
   6939 ;
   6940 #else
   6941 { (void)h; return 0; }
   6942 #endif
   6943 char *dlerror(void)
   6944 #ifdef C_DECLS_ONLY
   6945 ;
   6946 #else
   6947 { return "dynamic loading is not available"; }
   6948 #endif
   6949 
   6950 void endpwent(void)
   6951 #ifdef C_DECLS_ONLY
   6952 ;
   6953 #else
   6954 { }
   6955 #endif
   6956 void endgrent(void)
   6957 #ifdef C_DECLS_ONLY
   6958 ;
   6959 #else
   6960 { }
   6961 #endif
   6962 /* ---- sockets, poll, select, name lookup [chamesle] ---- */
   6963 #define AF_UNSPEC 0
   6964 #define AF_UNIX 1
   6965 #define AF_LOCAL 1
   6966 #define AF_INET 2
   6967 #define AF_INET6 10
   6968 #define PF_UNSPEC 0
   6969 #define PF_UNIX 1
   6970 #define PF_INET 2
   6971 #define PF_INET6 10
   6972 #define SOCK_STREAM 1
   6973 #define SOCK_DGRAM 2
   6974 #define SOCK_RAW 3
   6975 #define SOCK_NONBLOCK 04000
   6976 #define SOCK_CLOEXEC 02000000
   6977 #define SOL_SOCKET 1
   6978 #define SO_DEBUG 1
   6979 #define SO_REUSEADDR 2
   6980 #define SO_TYPE 3
   6981 #define SO_ERROR 4
   6982 #define SO_BROADCAST 6
   6983 #define SO_SNDBUF 7
   6984 #define SO_RCVBUF 8
   6985 #define SO_KEEPALIVE 9
   6986 #define SO_LINGER 13
   6987 #define SO_REUSEPORT 15
   6988 #define SO_RCVTIMEO 20
   6989 #define SO_SNDTIMEO 21
   6990 #define SOMAXCONN 4096
   6991 #define MSG_PEEK 2
   6992 #define MSG_DONTWAIT 0x40
   6993 #define MSG_NOSIGNAL 0x4000
   6994 #define MSG_WAITALL 0x100
   6995 #define SHUT_RD 0
   6996 #define SHUT_WR 1
   6997 #define SHUT_RDWR 2
   6998 #define IPPROTO_IP 0
   6999 #define IPPROTO_TCP 6
   7000 #define IPPROTO_UDP 17
   7001 #define IPPROTO_IPV6 41
   7002 #define TCP_NODELAY 1
   7003 #define IPV6_V6ONLY 26
   7004 #define INADDR_ANY ((in_addr_t)0)
   7005 #define INADDR_LOOPBACK ((in_addr_t)0x7f000001)
   7006 #define INET_ADDRSTRLEN 16
   7007 #define INET6_ADDRSTRLEN 46
   7008 typedef unsigned socklen_t;
   7009 typedef unsigned short sa_family_t;
   7010 typedef unsigned short in_port_t;
   7011 typedef unsigned in_addr_t;
   7012 struct sockaddr { sa_family_t sa_family; char sa_data[14]; };
   7013 struct sockaddr_storage { sa_family_t ss_family; char pad[126]; unsigned long align; };
   7014 struct in_addr { in_addr_t s_addr; };
   7015 struct in6_addr { unsigned char s6_addr[16]; };
   7016 struct sockaddr_in { sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; };
   7017 struct sockaddr_in6 { sa_family_t sin6_family; in_port_t sin6_port; unsigned sin6_flowinfo; struct in6_addr sin6_addr; unsigned sin6_scope_id; };
   7018 struct sockaddr_un { sa_family_t sun_family; char sun_path[108]; };
   7019 struct linger { int l_onoff, l_linger; };
   7020 struct msghdr { void *msg_name; socklen_t msg_namelen; struct iovec *msg_iov; size_t msg_iovlen; void *msg_control; size_t msg_controllen; int msg_flags; };
   7021 #ifndef C_DECLS_ONLY
   7022 static const struct in6_addr in6addr_any = { { 0 } };
   7023 #endif
   7024 #ifndef C_DECLS_ONLY
   7025 static const struct in6_addr in6addr_loopback = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } };
   7026 #endif
   7027 #define IN6ADDR_ANY_INIT { { 0 } }
   7028 unsigned short htons(unsigned short x)
   7029 #ifdef C_DECLS_ONLY
   7030 ;
   7031 #else
   7032 { return __builtin_bswap16(x); }
   7033 #endif
   7034 unsigned short ntohs(unsigned short x)
   7035 #ifdef C_DECLS_ONLY
   7036 ;
   7037 #else
   7038 { return __builtin_bswap16(x); }
   7039 #endif
   7040 unsigned htonl(unsigned x)
   7041 #ifdef C_DECLS_ONLY
   7042 ;
   7043 #else
   7044 { return __builtin_bswap32(x); }
   7045 #endif
   7046 unsigned ntohl(unsigned x)
   7047 #ifdef C_DECLS_ONLY
   7048 ;
   7049 #else
   7050 { return __builtin_bswap32(x); }
   7051 #endif
   7052 int socket(int d, int t, int p)
   7053 #ifdef C_DECLS_ONLY
   7054 ;
   7055 #else
   7056 { return fail(sys3(SYS_socket, d, t, p)); }
   7057 #endif
   7058 int socketpair(int d, int t, int p, int sv[2])
   7059 #ifdef C_DECLS_ONLY
   7060 ;
   7061 #else
   7062 { return fail(sys4(SYS_socketpair, d, t, p, sv)); }
   7063 #endif
   7064 int bind(int fd, const struct sockaddr *a, socklen_t l)
   7065 #ifdef C_DECLS_ONLY
   7066 ;
   7067 #else
   7068 { return fail(sys3(SYS_bind, fd, a, l)); }
   7069 #endif
   7070 int listen(int fd, int n)
   7071 #ifdef C_DECLS_ONLY
   7072 ;
   7073 #else
   7074 { return fail(sys2(SYS_listen, fd, n)); }
   7075 #endif
   7076 int accept(int fd, struct sockaddr *a, socklen_t *l)
   7077 #ifdef C_DECLS_ONLY
   7078 ;
   7079 #else
   7080 { return fail(sys3(SYS_accept, fd, a, l)); }
   7081 #endif
   7082 int accept4(int fd, struct sockaddr *a, socklen_t *l, int fl)
   7083 #ifdef C_DECLS_ONLY
   7084 ;
   7085 #else
   7086 { return fail(sys4(SYS_accept4, fd, a, l, fl)); }
   7087 #endif
   7088 int connect(int fd, const struct sockaddr *a, socklen_t l)
   7089 #ifdef C_DECLS_ONLY
   7090 ;
   7091 #else
   7092 { return fail(sys3(SYS_connect, fd, a, l)); }
   7093 #endif
   7094 int shutdown(int fd, int how)
   7095 #ifdef C_DECLS_ONLY
   7096 ;
   7097 #else
   7098 { return fail(sys2(SYS_shutdown, fd, how)); }
   7099 #endif
   7100 int getsockname(int fd, struct sockaddr *a, socklen_t *l)
   7101 #ifdef C_DECLS_ONLY
   7102 ;
   7103 #else
   7104 { return fail(sys3(SYS_getsockname, fd, a, l)); }
   7105 #endif
   7106 int getpeername(int fd, struct sockaddr *a, socklen_t *l)
   7107 #ifdef C_DECLS_ONLY
   7108 ;
   7109 #else
   7110 { return fail(sys3(SYS_getpeername, fd, a, l)); }
   7111 #endif
   7112 int setsockopt(int fd, int lv, int n, const void *v, socklen_t l)
   7113 #ifdef C_DECLS_ONLY
   7114 ;
   7115 #else
   7116 { return fail(sys5(SYS_setsockopt, fd, lv, n, v, l)); }
   7117 #endif
   7118 int getsockopt(int fd, int lv, int n, void *v, socklen_t *l)
   7119 #ifdef C_DECLS_ONLY
   7120 ;
   7121 #else
   7122 { return fail(sys5(SYS_getsockopt, fd, lv, n, v, l)); }
   7123 #endif
   7124 ssize_t send(int fd, const void *b, size_t n, int fl)
   7125 #ifdef C_DECLS_ONLY
   7126 ;
   7127 #else
   7128 { return fail(sys6(SYS_sendto, fd, (long)b, n, fl, 0, 0)); }
   7129 #endif
   7130 ssize_t recv(int fd, void *b, size_t n, int fl)
   7131 #ifdef C_DECLS_ONLY
   7132 ;
   7133 #else
   7134 { return fail(sys6(SYS_recvfrom, fd, (long)b, n, fl, 0, 0)); }
   7135 #endif
   7136 ssize_t sendto(int fd, const void *b, size_t n, int fl, const struct sockaddr *a, socklen_t l)
   7137 #ifdef C_DECLS_ONLY
   7138 ;
   7139 #else
   7140 { return fail(sys6(SYS_sendto, fd, (long)b, n, fl, (long)a, l)); }
   7141 #endif
   7142 ssize_t recvfrom(int fd, void *b, size_t n, int fl, struct sockaddr *a, socklen_t *l)
   7143 #ifdef C_DECLS_ONLY
   7144 ;
   7145 #else
   7146 { return fail(sys6(SYS_recvfrom, fd, (long)b, n, fl, (long)a, (long)l)); }
   7147 #endif
   7148 ssize_t sendmsg(int fd, const struct msghdr *m, int fl)
   7149 #ifdef C_DECLS_ONLY
   7150 ;
   7151 #else
   7152 { return fail(sys3(SYS_sendmsg, fd, m, fl)); }
   7153 #endif
   7154 ssize_t recvmsg(int fd, struct msghdr *m, int fl)
   7155 #ifdef C_DECLS_ONLY
   7156 ;
   7157 #else
   7158 { return fail(sys3(SYS_recvmsg, fd, m, fl)); }
   7159 #endif
   7160 
   7161 struct pollfd { int fd; short events, revents; };
   7162 #define POLLIN 1
   7163 #define POLLPRI 2
   7164 #define POLLOUT 4
   7165 #define POLLERR 8
   7166 #define POLLHUP 16
   7167 #define POLLNVAL 32
   7168 typedef unsigned long nfds_t;
   7169 int poll(struct pollfd *f, nfds_t n, int ms)
   7170 #ifdef C_DECLS_ONLY
   7171 ;
   7172 #else
   7173 {
   7174     struct timespec t, *tp = NULL;
   7175     if (ms >= 0) { t.tv_sec = ms / 1000; t.tv_nsec = (ms % 1000) * 1000000L; tp = &t; }
   7176     return fail(sys5(SYS_ppoll, f, n, tp, 0, 8));
   7177 }
   7178 #endif
   7179 int ppoll(struct pollfd *f, nfds_t n, const struct timespec *t, const sigset_t *m)
   7180 #ifdef C_DECLS_ONLY
   7181 ;
   7182 #else
   7183 { return fail(sys5(SYS_ppoll, f, n, t, m, 8)); }
   7184 #endif
   7185 #define FD_SETSIZE 1024
   7186 #define C_FDBITS (8 * (int)sizeof(long))
   7187 typedef struct { unsigned long fds_bits[FD_SETSIZE / C_FDBITS]; } fd_set;
   7188 #define FD_ZERO(s) memset((s), 0, sizeof(fd_set))
   7189 #define FD_SET(fd, s) ((s)->fds_bits[(fd) / C_FDBITS] |= 1UL << ((fd) % C_FDBITS))
   7190 #define FD_CLR(fd, s) ((s)->fds_bits[(fd) / C_FDBITS] &= ~(1UL << ((fd) % C_FDBITS)))
   7191 #define FD_ISSET(fd, s) (((s)->fds_bits[(fd) / C_FDBITS] >> ((fd) % C_FDBITS)) & 1)
   7192 int select(int n, fd_set *r, fd_set *w, fd_set *e, struct timeval *tv)
   7193 #ifdef C_DECLS_ONLY
   7194 ;
   7195 #else
   7196 { return fail(sys5(SYS_select, n, r, w, e, tv)); }
   7197 #endif
   7198 int pselect(int n, fd_set *r, fd_set *w, fd_set *e, const struct timespec *t, const sigset_t *m)
   7199 #ifdef C_DECLS_ONLY
   7200 ;
   7201 #else
   7202 {
   7203     struct timeval tv, *tp = NULL;
   7204     if (t) { tv.tv_sec = t->tv_sec; tv.tv_usec = t->tv_nsec / 1000; tp = &tv; }
   7205     (void)m;
   7206     return select(n, r, w, e, tp);
   7207 }
   7208 #endif
   7209 
   7210 #define EPOLLIN 1
   7211 #define EPOLLOUT 4
   7212 #define EPOLLERR 8
   7213 #define EPOLLHUP 16
   7214 #define EPOLLET (1u << 31)
   7215 #define EPOLL_CTL_ADD 1
   7216 #define EPOLL_CTL_DEL 2
   7217 #define EPOLL_CTL_MOD 3
   7218 #define EPOLL_CLOEXEC 02000000
   7219 struct epoll_event { unsigned events; union { void *ptr; int fd; unsigned u32; unsigned long u64; } data; } __attribute__((packed));
   7220 int epoll_create1(int fl)
   7221 #ifdef C_DECLS_ONLY
   7222 ;
   7223 #else
   7224 { return fail(sys1(291, fl)); }
   7225 #endif
   7226 int epoll_create(int n)
   7227 #ifdef C_DECLS_ONLY
   7228 ;
   7229 #else
   7230 { (void)n; return epoll_create1(0); }
   7231 #endif
   7232 int epoll_ctl(int ep, int op, int fd, struct epoll_event *ev)
   7233 #ifdef C_DECLS_ONLY
   7234 ;
   7235 #else
   7236 { return fail(sys4(233, ep, op, fd, ev)); }
   7237 #endif
   7238 int epoll_wait(int ep, struct epoll_event *ev, int max, int ms)
   7239 #ifdef C_DECLS_ONLY
   7240 ;
   7241 #else
   7242 { return fail(sys6(232, ep, (long)ev, max, ms, 0, 0)); }
   7243 #endif
   7244 
   7245 int inet_aton(const char *s, struct in_addr *a)
   7246 #ifdef C_DECLS_ONLY
   7247 ;
   7248 #else
   7249 {
   7250     unsigned v = 0;
   7251     int i;
   7252     for (i = 0; i < 4; i++) {
   7253         char *e;
   7254         unsigned long p = strtoul(s, &e, 10);
   7255         if (e == s || p > 255) return 0;
   7256         v = v << 8 | p;
   7257         if (i < 3) { if (*e != '.') return 0; s = e + 1; } else if (*e) return 0;
   7258     }
   7259     a->s_addr = htonl(v);
   7260     return 1;
   7261 }
   7262 #endif
   7263 in_addr_t inet_addr(const char *s)
   7264 #ifdef C_DECLS_ONLY
   7265 ;
   7266 #else
   7267 { struct in_addr a; return inet_aton(s, &a) ? a.s_addr : (in_addr_t)-1; }
   7268 #endif
   7269 char *inet_ntoa(struct in_addr a)
   7270 #ifdef C_DECLS_ONLY
   7271 ;
   7272 #else
   7273 { static char b[16]; unsigned char *p = (unsigned char *)&a; sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); return b; }
   7274 #endif
   7275 #ifndef C_DECLS_ONLY
   7276 static int c_pton6(const char *s, unsigned char *d)
   7277 {
   7278     unsigned short w[8] = { 0 };
   7279     int n = 0, gap = -1;
   7280     if (s[0] == ':' && s[1] == ':') { gap = 0; s += 2; if (!*s) goto done; }
   7281     for (;;) {
   7282         char *e;
   7283         unsigned long v = strtoul(s, &e, 16);
   7284         if (e == s || e - s > 4 || n >= 8) return 0;
   7285         w[n++] = v;
   7286         s = e;
   7287         if (!*s) break;
   7288         if (*s != ':') return 0;
   7289         s++;
   7290         if (*s == ':') { if (gap >= 0) return 0; gap = n; s++; if (!*s) break; }
   7291     }
   7292 done:
   7293     if (gap < 0 && n != 8) return 0;
   7294     if (gap >= 0) { int k = 8 - n; memmove(w + gap + k, w + gap, (n - gap) * 2); memset(w + gap, 0, k * 2); }
   7295     for (n = 0; n < 8; n++) { d[2 * n] = w[n] >> 8; d[2 * n + 1] = w[n]; }
   7296     return 1;
   7297 }
   7298 #endif
   7299 int inet_pton(int af, const char *s, void *d)
   7300 #ifdef C_DECLS_ONLY
   7301 ;
   7302 #else
   7303 {
   7304     if (af == AF_INET) return inet_aton(s, d) ? 1 : 0;
   7305     if (af == AF_INET6) return c_pton6(s, d);
   7306     errno = EAFNOSUPPORT; return -1;
   7307 }
   7308 #endif
   7309 const char *inet_ntop(int af, const void *s, char *d, socklen_t n)
   7310 #ifdef C_DECLS_ONLY
   7311 ;
   7312 #else
   7313 {
   7314     const unsigned char *p = s;
   7315     char buf[48];
   7316     if (af == AF_INET) snprintf(buf, sizeof buf, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
   7317     else if (af == AF_INET6) {
   7318         int i, best = -1, bl = 0, cur = -1, cl = 0;
   7319         char *o = buf;
   7320         for (i = 0; i < 8; i++) {
   7321             if (!p[2 * i] && !p[2 * i + 1]) { if (cur < 0) { cur = i; cl = 0; } if (++cl > bl) { bl = cl; best = cur; } } else cur = -1;
   7322         }
   7323         if (bl < 2) best = -1;
   7324         for (i = 0; i < 8; i++) {
   7325             if (i == best) { *o++ = ':'; i += bl - 1; if (i == 7) *o++ = ':'; continue; }
   7326             if (i) *o++ = ':';
   7327             o += sprintf(o, "%x", p[2 * i] << 8 | p[2 * i + 1]);
   7328         }
   7329         *o = 0;
   7330     } else { errno = EAFNOSUPPORT; return NULL; }
   7331     if (strlen(buf) >= n) { errno = ENOSPC; return NULL; }
   7332     strcpy(d, buf);
   7333     return d;
   7334 }
   7335 #endif
   7336 
   7337 /* names: /etc/hosts, then a plain UDP query to the first nameserver in /etc/resolv.conf */
   7338 struct hostent { char *h_name, **h_aliases; int h_addrtype, h_length; char **h_addr_list; };
   7339 #define h_addr h_addr_list[0]
   7340 struct addrinfo { int ai_flags, ai_family, ai_socktype, ai_protocol; socklen_t ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; };
   7341 struct servent { char *s_name, **s_aliases; int s_port; char *s_proto; };
   7342 #define AI_PASSIVE 1
   7343 #define AI_CANONNAME 2
   7344 #define AI_NUMERICHOST 4
   7345 #define AI_NUMERICSERV 0x400
   7346 #define AI_ADDRCONFIG 0x20
   7347 #define AI_V4MAPPED 8
   7348 #define AI_ALL 0x10
   7349 #define NI_NUMERICHOST 1
   7350 #define NI_NUMERICSERV 2
   7351 #define NI_NAMEREQD 8
   7352 #define NI_DGRAM 16
   7353 #define NI_MAXHOST 1025
   7354 #define NI_MAXSERV 32
   7355 #define EAI_BADFLAGS (-1)
   7356 #define EAI_NONAME (-2)
   7357 #define EAI_AGAIN (-3)
   7358 #define EAI_FAIL (-4)
   7359 #define EAI_FAMILY (-6)
   7360 #define EAI_SOCKTYPE (-7)
   7361 #define EAI_SERVICE (-8)
   7362 #define EAI_MEMORY (-10)
   7363 #define EAI_SYSTEM (-11)
   7364 #define HOST_NOT_FOUND 1
   7365 #ifdef C_DECLS_ONLY
   7366 extern int h_errno;
   7367 #else
   7368 int h_errno;
   7369 #endif
   7370 #ifndef C_DECLS_ONLY
   7371 static int c_hosts(const char *name, int fam, unsigned char *out, int *outfam)
   7372 {
   7373     char line[512];
   7374     FILE *f = fopen("/etc/hosts", "r");
   7375     if (!f) return 0;
   7376     while (fgets(line, sizeof line, f)) {
   7377         char *p = line, *tok, *save;
   7378         unsigned char a[16];
   7379         int af;
   7380         line[strcspn(line, "#\n")] = 0;
   7381         tok = strtok_r(p, " \t", &save);
   7382         if (!tok) continue;
   7383         af = inet_pton(AF_INET, tok, a) == 1 ? AF_INET : inet_pton(AF_INET6, tok, a) == 1 ? AF_INET6 : 0;
   7384         if (!af || (fam != AF_UNSPEC && fam != af)) continue;
   7385         while ((tok = strtok_r(NULL, " \t", &save))) if (!strcasecmp(tok, name)) { memcpy(out, a, 16); *outfam = af; fclose(f); return 1; }
   7386     }
   7387     fclose(f);
   7388     return 0;
   7389 }
   7390 #endif
   7391 #ifndef C_DECLS_ONLY
   7392 static int c_dns(const char *name, unsigned char *out)
   7393 {
   7394     unsigned char q[300], r[600];
   7395     char line[256], ns[64] = "127.0.0.1";
   7396     struct sockaddr_in sa;
   7397     struct timeval tv = { 3, 0 };
   7398     int fd, n = 0, i, an, pos;
   7399     FILE *f = fopen("/etc/resolv.conf", "r");
   7400     if (f) { while (fgets(line, sizeof line, f)) if (!strncmp(line, "nameserver", 10)) { sscanf(line + 10, "%63s", ns); break; } fclose(f); }
   7401     memset(q, 0, 12);
   7402     q[0] = 0x43; q[1] = 0x68; q[2] = 1; q[5] = 1;
   7403     n = 12;
   7404     while (*name) {
   7405         const char *dot = strchr(name, '.');
   7406         int l = dot ? (int)(dot - name) : (int)strlen(name);
   7407         if (l > 63 || n + l + 6 > (int)sizeof q) return 0;
   7408         q[n++] = l; memcpy(q + n, name, l); n += l;
   7409         name += l; if (*name == '.') name++;
   7410     }
   7411     q[n++] = 0; q[n++] = 0; q[n++] = 1; q[n++] = 0; q[n++] = 1;
   7412     memset(&sa, 0, sizeof sa);
   7413     sa.sin_family = AF_INET; sa.sin_port = htons(53);
   7414     if (!inet_aton(ns, &sa.sin_addr)) return 0;
   7415     fd = socket(AF_INET, SOCK_DGRAM, 0);
   7416     if (fd < 0) return 0;
   7417     setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
   7418     for (i = 0; i < 2; i++) {
   7419         long len;
   7420         sendto(fd, q, n, 0, (struct sockaddr *)&sa, sizeof sa);
   7421         len = recv(fd, r, sizeof r, 0);
   7422         if (len < 12 || r[0] != q[0] || r[1] != q[1]) continue;
   7423         close(fd);
   7424         if ((r[3] & 15) != 0) return 0;
   7425         an = r[6] << 8 | r[7];
   7426         pos = n;
   7427         while (an-- > 0 && pos < len) {
   7428             int rl, ty;
   7429             while (pos < len && r[pos]) { if ((r[pos] & 0xc0) == 0xc0) { pos++; break; } pos += r[pos] + 1; }
   7430             pos++;
   7431             if (pos + 10 > len) return 0;
   7432             ty = r[pos] << 8 | r[pos + 1]; rl = r[pos + 8] << 8 | r[pos + 9]; pos += 10;
   7433             if (ty == 1 && rl == 4 && pos + 4 <= len) { memcpy(out, r + pos, 4); return 1; }
   7434             pos += rl;
   7435         }
   7436         return 0;
   7437     }
   7438     close(fd);
   7439     return 0;
   7440 }
   7441 #endif
   7442 int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
   7443 #ifdef C_DECLS_ONLY
   7444 ;
   7445 #else
   7446 {
   7447     int fam = hints ? hints->ai_family : AF_UNSPEC, socktype = hints ? hints->ai_socktype : 0, flags = hints ? hints->ai_flags : 0;
   7448     unsigned char addr[16];
   7449     int af = 0, port = 0;
   7450     struct addrinfo *ai;
   7451     struct sockaddr_in *s4;
   7452     struct sockaddr_in6 *s6;
   7453     char *e;
   7454     if (!node && !service) return EAI_NONAME;
   7455     if (service) {
   7456         port = strtol(service, &e, 10);
   7457         if (*e) {
   7458             if (flags & AI_NUMERICSERV) return EAI_NONAME;
   7459             port = !strcmp(service, "http") ? 80 : !strcmp(service, "https") ? 443 : !strcmp(service, "ftp") ? 21 : !strcmp(service, "ssh") ? 22 : !strcmp(service, "domain") ? 53 : !strcmp(service, "smtp") ? 25 : !strcmp(service, "git") ? 9418 : 0;
   7460             if (!port) return EAI_SERVICE;
   7461         }
   7462     }
   7463     if (!node) { af = fam == AF_INET6 ? AF_INET6 : AF_INET; memset(addr, 0, 16); if (!(flags & AI_PASSIVE)) { if (af == AF_INET) { addr[0] = 127; addr[3] = 1; } else addr[15] = 1; } }
   7464     else if (inet_pton(AF_INET, node, addr) == 1) af = AF_INET;
   7465     else if (inet_pton(AF_INET6, node, addr) == 1) af = AF_INET6;
   7466     else if (flags & AI_NUMERICHOST) return EAI_NONAME;
   7467     else if (!strcmp(node, "localhost")) { af = fam == AF_INET6 ? AF_INET6 : AF_INET; memset(addr, 0, 16); if (af == AF_INET) { addr[0] = 127; addr[3] = 1; } else addr[15] = 1; }
   7468     else if (c_hosts(node, fam, addr, &af)) ;
   7469     else if (fam != AF_INET6 && c_dns(node, addr)) af = AF_INET;
   7470     else return EAI_NONAME;
   7471     if (fam != AF_UNSPEC && fam != af) return EAI_FAMILY;
   7472     ai = calloc(1, sizeof *ai + sizeof(struct sockaddr_in6) + (node ? strlen(node) + 1 : 1));
   7473     if (!ai) return EAI_MEMORY;
   7474     ai->ai_family = af; ai->ai_socktype = socktype ? socktype : SOCK_STREAM;
   7475     ai->ai_protocol = ai->ai_socktype == SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP;
   7476     ai->ai_addr = (struct sockaddr *)(ai + 1);
   7477     if (af == AF_INET) { s4 = (struct sockaddr_in *)ai->ai_addr; s4->sin_family = AF_INET; s4->sin_port = htons(port); memcpy(&s4->sin_addr, addr, 4); ai->ai_addrlen = sizeof *s4; }
   7478     else { s6 = (struct sockaddr_in6 *)ai->ai_addr; s6->sin6_family = AF_INET6; s6->sin6_port = htons(port); memcpy(&s6->sin6_addr, addr, 16); ai->ai_addrlen = sizeof *s6; }
   7479     if ((flags & AI_CANONNAME) && node) { ai->ai_canonname = (char *)(ai + 1) + sizeof(struct sockaddr_in6); strcpy(ai->ai_canonname, node); }
   7480     *res = ai;
   7481     return 0;
   7482 }
   7483 #endif
   7484 void freeaddrinfo(struct addrinfo *ai)
   7485 #ifdef C_DECLS_ONLY
   7486 ;
   7487 #else
   7488 { while (ai) { struct addrinfo *n = ai->ai_next; free(ai); ai = n; } }
   7489 #endif
   7490 const char *gai_strerror(int e)
   7491 #ifdef C_DECLS_ONLY
   7492 ;
   7493 #else
   7494 { return e == EAI_NONAME ? "Name or service not known" : e == EAI_AGAIN ? "Temporary failure in name resolution" : e == EAI_FAMILY ? "Address family not supported" : e == EAI_SERVICE ? "Service not known" : e == EAI_MEMORY ? "Out of memory" : "Name resolution failed"; }
   7495 #endif
   7496 int getnameinfo(const struct sockaddr *sa, socklen_t l, char *host, socklen_t hl, char *serv, socklen_t sl, int flags)
   7497 #ifdef C_DECLS_ONLY
   7498 ;
   7499 #else
   7500 {
   7501     (void)l; (void)flags;
   7502     if (host && hl) { if (!inet_ntop(sa->sa_family, sa->sa_family == AF_INET ? (void *)&((struct sockaddr_in *)sa)->sin_addr : (void *)&((struct sockaddr_in6 *)sa)->sin6_addr, host, hl)) return EAI_FAMILY; }
   7503     if (serv && sl) snprintf(serv, sl, "%d", ntohs(((struct sockaddr_in *)sa)->sin_port));
   7504     return 0;
   7505 }
   7506 #endif
   7507 struct hostent *gethostbyname(const char *name)
   7508 #ifdef C_DECLS_ONLY
   7509 ;
   7510 #else
   7511 {
   7512     static struct hostent h;
   7513     static char *list[2], *al[1], buf[16], nm[256];
   7514     struct addrinfo hints, *r;
   7515     memset(&hints, 0, sizeof hints);
   7516     hints.ai_family = AF_INET;
   7517     if (getaddrinfo(name, NULL, &hints, &r)) { h_errno = HOST_NOT_FOUND; return NULL; }
   7518     memcpy(buf, &((struct sockaddr_in *)r->ai_addr)->sin_addr, 4);
   7519     freeaddrinfo(r);
   7520     strlcpy(nm, name, sizeof nm);
   7521     list[0] = buf; list[1] = NULL; al[0] = NULL;
   7522     h.h_name = nm; h.h_aliases = al; h.h_addrtype = AF_INET; h.h_length = 4; h.h_addr_list = list;
   7523     return &h;
   7524 }
   7525 #endif
   7526 struct servent *getservbyname(const char *n, const char *p)
   7527 #ifdef C_DECLS_ONLY
   7528 ;
   7529 #else
   7530 { (void)n; (void)p; return NULL; }
   7531 #endif
   7532 const char *hstrerror(int e)
   7533 #ifdef C_DECLS_ONLY
   7534 ;
   7535 #else
   7536 { (void)e; return "Unknown host"; }
   7537 #endif
   7538 /* ---- fnmatch, glob, ftw, regex, path names [chamesle] ---- */
   7539 #define FNM_NOMATCH 1
   7540 #define FNM_PATHNAME 1
   7541 #define FNM_NOESCAPE 2
   7542 #define FNM_PERIOD 4
   7543 #define FNM_CASEFOLD 16
   7544 #ifndef C_DECLS_ONLY
   7545 static int c_fn_class(const char *name, int c)
   7546 {
   7547     if (!strncmp(name, "alpha:", 6)) return isalpha(c);
   7548     if (!strncmp(name, "digit:", 6)) return isdigit(c);
   7549     if (!strncmp(name, "alnum:", 6)) return isalnum(c);
   7550     if (!strncmp(name, "upper:", 6)) return isupper(c);
   7551     if (!strncmp(name, "lower:", 6)) return islower(c);
   7552     if (!strncmp(name, "space:", 6)) return isspace(c);
   7553     if (!strncmp(name, "blank:", 6)) return c == ' ' || c == '\t';
   7554     if (!strncmp(name, "punct:", 6)) return ispunct(c);
   7555     if (!strncmp(name, "print:", 6)) return c >= 32 && c < 127;
   7556     if (!strncmp(name, "graph:", 6)) return c > 32 && c < 127;
   7557     if (!strncmp(name, "cntrl:", 6)) return iscntrl(c);
   7558     if (!strncmp(name, "xdigit:", 7)) return isxdigit(c);
   7559     return 0;
   7560 }
   7561 #endif
   7562 int fnmatch(const char *p, const char *s, int flags)
   7563 #ifdef C_DECLS_ONLY
   7564 ;
   7565 #else
   7566 {
   7567     const char *start = s;
   7568     for (; *p; p++, s++) {
   7569         int fold = flags & FNM_CASEFOLD;
   7570         if (*p == '*') {
   7571             while (*p == '*') p++;
   7572             if ((flags & FNM_PERIOD) && *s == '.' && (s == start || ((flags & FNM_PATHNAME) && s[-1] == '/'))) return FNM_NOMATCH;
   7573             if (!*p) return (flags & FNM_PATHNAME) && strchr(s, '/') ? FNM_NOMATCH : 0;
   7574             for (;; s++) {
   7575                 if (!fnmatch(p, s, flags & ~FNM_PERIOD)) return 0;
   7576                 if (!*s || ((flags & FNM_PATHNAME) && *s == '/')) return FNM_NOMATCH;
   7577             }
   7578         }
   7579         if (!*s) return FNM_NOMATCH;
   7580         if ((flags & FNM_PERIOD) && *s == '.' && *p != '.' && (s == start || ((flags & FNM_PATHNAME) && s[-1] == '/'))) return FNM_NOMATCH;
   7581         if (*p == '?') { if ((flags & FNM_PATHNAME) && *s == '/') return FNM_NOMATCH; continue; }
   7582         if (*p == '[') {
   7583             const char *q = p + 1;
   7584             int neg = 0, ok = 0, c = (unsigned char)*s, first = 1;
   7585             if (*q == '!' || *q == '^') { neg = 1; q++; }
   7586             if ((flags & FNM_PATHNAME) && c == '/') return FNM_NOMATCH;
   7587             while (*q && (*q != ']' || first)) {
   7588                 first = 0;
   7589                 if (q[0] == '[' && q[1] == ':') { const char *e = strstr(q, ":]"); if (e) { if (c_fn_class(q + 2, c)) ok = 1; q = e + 2; continue; } }
   7590                 if (*q == '\\' && !(flags & FNM_NOESCAPE) && q[1]) q++;
   7591                 if (q[1] == '-' && q[2] && q[2] != ']') {
   7592                     int lo = (unsigned char)q[0], hi = (unsigned char)q[2];
   7593                     if (fold) { if (tolower(c) >= tolower(lo) && tolower(c) <= tolower(hi)) ok = 1; }
   7594                     if (c >= lo && c <= hi) ok = 1;
   7595                     q += 3;
   7596                 } else { if (fold ? tolower(c) == tolower((unsigned char)*q) : c == (unsigned char)*q) ok = 1; q++; }
   7597             }
   7598             if (!*q) { if (*s != '[') return FNM_NOMATCH; continue; }
   7599             if (ok == neg) return FNM_NOMATCH;
   7600             p = q;
   7601             continue;
   7602         }
   7603         if (*p == '\\' && !(flags & FNM_NOESCAPE) && p[1]) p++;
   7604         if (fold ? tolower((unsigned char)*p) != tolower((unsigned char)*s) : *p != *s) return FNM_NOMATCH;
   7605     }
   7606     return *s ? FNM_NOMATCH : 0;
   7607 }
   7608 #endif
   7609 
   7610 #define GLOB_ERR 1
   7611 #define GLOB_MARK 2
   7612 #define GLOB_NOSORT 4
   7613 #define GLOB_DOOFFS 8
   7614 #define GLOB_NOCHECK 16
   7615 #define GLOB_APPEND 32
   7616 #define GLOB_NOESCAPE 64
   7617 #define GLOB_PERIOD 128
   7618 #define GLOB_BRACE 1024
   7619 #define GLOB_TILDE 4096
   7620 #define GLOB_NOSPACE 1
   7621 #define GLOB_ABORTED 2
   7622 #define GLOB_NOMATCH 3
   7623 typedef struct { size_t gl_pathc; char **gl_pathv; size_t gl_offs; } glob_t;
   7624 #ifndef C_DECLS_ONLY
   7625 static int c_glob_add(glob_t *g, size_t *cap, const char *path)
   7626 {
   7627     if (g->gl_pathc + g->gl_offs + 2 > *cap) {
   7628         char **n;
   7629         *cap = *cap * 2 + 16;
   7630         n = realloc(g->gl_pathv, *cap * sizeof *n);
   7631         if (!n) return -1;
   7632         g->gl_pathv = n;
   7633     }
   7634     g->gl_pathv[g->gl_offs + g->gl_pathc] = strdup(path);
   7635     if (!g->gl_pathv[g->gl_offs + g->gl_pathc]) return -1;
   7636     g->gl_pathv[g->gl_offs + ++g->gl_pathc] = NULL;
   7637     return 0;
   7638 }
   7639 #endif
   7640 #ifndef C_DECLS_ONLY
   7641 static int c_glob_walk(const char *dir, const char *pat, int flags, glob_t *g, size_t *cap)
   7642 {
   7643     char seg[512];
   7644     const char *rest;
   7645     size_t l;
   7646     int magic;
   7647     DIR *d;
   7648     struct dirent *e;
   7649     char **names = NULL;
   7650     size_t nn = 0, i;
   7651     while (*pat == '/') pat++;
   7652     if (!*pat) return c_glob_add(g, cap, dir);
   7653     l = strcspn(pat, "/");
   7654     if (l >= sizeof seg) return 0;
   7655     memcpy(seg, pat, l); seg[l] = 0;
   7656     rest = pat + l;
   7657     magic = strpbrk(seg, "*?[") != NULL;
   7658     if (!magic) {
   7659         char full[4096];
   7660         struct stat st;
   7661         char *u = seg;
   7662         if (!(flags & GLOB_NOESCAPE)) { char *r = seg, *w = seg; while (*r) { if (*r == '\\' && r[1]) r++; *w++ = *r++; } *w = 0; }
   7663         snprintf(full, sizeof full, "%s%s%s", dir, !*dir || dir[strlen(dir) - 1] == '/' ? "" : "/", u);
   7664         if (lstat(full, &st) < 0) return 0;
   7665         if (*rest && !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode)) return 0;
   7666         return c_glob_walk(full, rest, flags, g, cap);
   7667     }
   7668     d = opendir(*dir ? dir : ".");
   7669     if (!d) return (flags & GLOB_ERR) ? -2 : 0;
   7670     while ((e = readdir(d))) {
   7671         if (e->d_name[0] == '.' && seg[0] != '.' && !(flags & GLOB_PERIOD)) continue;
   7672         if (fnmatch(seg, e->d_name, (flags & GLOB_NOESCAPE ? FNM_NOESCAPE : 0) | FNM_PERIOD * !(flags & GLOB_PERIOD))) continue;
   7673         names = realloc(names, (nn + 1) * sizeof *names);
   7674         names[nn++] = strdup(e->d_name);
   7675     }
   7676     closedir(d);
   7677     for (i = 0; i < nn; i++) {
   7678         char full[4096];
   7679         struct stat st;
   7680         snprintf(full, sizeof full, "%s%s%s", dir, !*dir || dir[strlen(dir) - 1] == '/' ? "" : "/", names[i]);
   7681         if (*rest && (stat(full, &st) < 0 || !S_ISDIR(st.st_mode))) continue;
   7682         if (c_glob_walk(full, rest, flags, g, cap) < 0) return -1;
   7683     }
   7684     for (i = 0; i < nn; i++) free(names[i]);
   7685     free(names);
   7686     return 0;
   7687 }
   7688 #endif
   7689 #ifndef C_DECLS_ONLY
   7690 static int c_cmp_str(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); }
   7691 #endif
   7692 int glob(const char *pat, int flags, int (*err)(const char *, int), glob_t *g)
   7693 #ifdef C_DECLS_ONLY
   7694 ;
   7695 #else
   7696 {
   7697     size_t cap = 0, start;
   7698     int r;
   7699     (void)err;
   7700     if (!(flags & GLOB_APPEND)) { g->gl_pathc = 0; g->gl_pathv = NULL; if (!(flags & GLOB_DOOFFS)) g->gl_offs = 0; }
   7701     else cap = g->gl_pathc + g->gl_offs + 2;
   7702     if (!g->gl_pathv) { g->gl_pathv = calloc(g->gl_offs + 16, sizeof(char *)); cap = g->gl_offs + 16; }
   7703     start = g->gl_pathc;
   7704     if (flags & GLOB_BRACE) {
   7705         const char *o = strchr(pat, '{');
   7706         if (o) {
   7707             const char *p = o + 1, *c = NULL;
   7708             int depth = 1;
   7709             for (; *p; p++) { if (*p == '{') depth++; else if (*p == '}' && !--depth) { c = p; break; } }
   7710             if (c) {
   7711                 const char *a = o + 1, *q;
   7712                 int any = 0;
   7713                 depth = 0;
   7714                 for (q = a; q <= c; q++) {
   7715                     if (*q == '{') depth++;
   7716                     else if ((*q == ',' && !depth) || q == c) {
   7717                         char *np = malloc(strlen(pat) + (q - a) + 1);
   7718                         memcpy(np, pat, o - pat);
   7719                         memcpy(np + (o - pat), a, q - a);
   7720                         strcpy(np + (o - pat) + (q - a), c + 1);
   7721                         r = glob(np, (flags & ~GLOB_NOCHECK) | GLOB_APPEND, err, g);
   7722                         free(np);
   7723                         if (r && r != GLOB_NOMATCH) return r;
   7724                         if (!r) any = 1;
   7725                         a = q + 1;
   7726                     } else if (*q == '}') depth--;
   7727                 }
   7728                 if (!any && (flags & GLOB_NOCHECK)) { c_glob_add(g, &cap, pat); return 0; }
   7729                 return any ? 0 : GLOB_NOMATCH;
   7730             }
   7731         }
   7732     }
   7733     r = pat[0] == '/' ? c_glob_walk("/", pat, flags, g, &cap) : c_glob_walk("", pat, flags, g, &cap);
   7734     if (r == -2) return GLOB_ABORTED;
   7735     if (r < 0) return GLOB_NOSPACE;
   7736     if (g->gl_pathc == start) {
   7737         if (flags & GLOB_NOCHECK) { c_glob_add(g, &cap, pat); return 0; }
   7738         return GLOB_NOMATCH;
   7739     }
   7740     if (!(flags & GLOB_NOSORT)) qsort(g->gl_pathv + g->gl_offs + start, g->gl_pathc - start, sizeof(char *), c_cmp_str);
   7741     if (flags & GLOB_MARK) {
   7742         size_t i;
   7743         for (i = start; i < g->gl_pathc; i++) {
   7744             struct stat st;
   7745             char *s = g->gl_pathv[g->gl_offs + i];
   7746             if (stat(s, &st) == 0 && S_ISDIR(st.st_mode) && s[strlen(s) - 1] != '/') { char *n = malloc(strlen(s) + 2); strcpy(n, s); strcat(n, "/"); free(s); g->gl_pathv[g->gl_offs + i] = n; }
   7747         }
   7748     }
   7749     return 0;
   7750 }
   7751 #endif
   7752 void globfree(glob_t *g)
   7753 #ifdef C_DECLS_ONLY
   7754 ;
   7755 #else
   7756 { size_t i; for (i = 0; i < g->gl_pathc; i++) free(g->gl_pathv[g->gl_offs + i]); free(g->gl_pathv); g->gl_pathv = NULL; g->gl_pathc = 0; }
   7757 #endif
   7758 
   7759 char *basename(char *path)
   7760 #ifdef C_DECLS_ONLY
   7761 ;
   7762 #else
   7763 {
   7764     char *s;
   7765     size_t l;
   7766     if (!path || !*path) return ".";
   7767     l = strlen(path);
   7768     while (l > 1 && path[l - 1] == '/') path[--l] = 0;
   7769     if (l == 1 && *path == '/') return path;
   7770     s = strrchr(path, '/');
   7771     return s ? s + 1 : path;
   7772 }
   7773 #endif
   7774 char *dirname(char *path)
   7775 #ifdef C_DECLS_ONLY
   7776 ;
   7777 #else
   7778 {
   7779     size_t l;
   7780     if (!path || !*path) return ".";
   7781     l = strlen(path);
   7782     while (l > 1 && path[l - 1] == '/') l--;
   7783     while (l > 0 && path[l - 1] != '/') l--;
   7784     if (!l) return ".";
   7785     while (l > 1 && path[l - 1] == '/') l--;
   7786     path[l] = 0;
   7787     return path;
   7788 }
   7789 #endif
   7790 
   7791 #define FTW_F 0
   7792 #define FTW_D 1
   7793 #define FTW_DNR 2
   7794 #define FTW_NS 3
   7795 #define FTW_SL 4
   7796 #define FTW_DP 5
   7797 #define FTW_SLN 6
   7798 #define FTW_PHYS 1
   7799 #define FTW_MOUNT 2
   7800 #define FTW_CHDIR 4
   7801 #define FTW_DEPTH 8
   7802 struct FTW { int base, level; };
   7803 #ifndef C_DECLS_ONLY
   7804 static int c_nftw_rec(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int depth, int flags, int level, int maxfd)
   7805 {
   7806     struct stat st;
   7807     struct FTW ftw;
   7808     int type, r;
   7809     size_t l = strlen(path);
   7810     char *b = strrchr(path, '/');
   7811     ftw.base = b ? b - path + 1 : 0; ftw.level = level;
   7812     if ((flags & FTW_PHYS ? lstat(path, &st) : stat(path, &st)) < 0) {
   7813         if (!(flags & FTW_PHYS) && lstat(path, &st) == 0) return fn(path, &st, FTW_SLN, &ftw);
   7814         return fn(path, &st, FTW_NS, &ftw);
   7815     }
   7816     type = S_ISDIR(st.st_mode) ? FTW_D : S_ISLNK(st.st_mode) ? FTW_SL : FTW_F;
   7817     if (type != FTW_D) return fn(path, &st, type, &ftw);
   7818     if (!(flags & FTW_DEPTH) && (r = fn(path, &st, FTW_D, &ftw))) return r;
   7819     {
   7820         DIR *d = opendir(path);
   7821         struct dirent *e;
   7822         if (!d) { if (flags & FTW_DEPTH) return fn(path, &st, FTW_DNR, &ftw); return fn(path, &st, FTW_DNR, &ftw); }
   7823         while ((e = readdir(d))) {
   7824             char *child;
   7825             if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue;
   7826             child = malloc(l + strlen(e->d_name) + 2);
   7827             sprintf(child, "%s%s%s", path, l && path[l - 1] == '/' ? "" : "/", e->d_name);
   7828             r = c_nftw_rec(child, fn, depth, flags, level + 1, maxfd);
   7829             free(child);
   7830             if (r) { closedir(d); return r; }
   7831         }
   7832         closedir(d);
   7833     }
   7834     if (flags & FTW_DEPTH) return fn(path, &st, FTW_DP, &ftw);
   7835     return 0;
   7836 }
   7837 #endif
   7838 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd, int flags)
   7839 #ifdef C_DECLS_ONLY
   7840 ;
   7841 #else
   7842 {
   7843     char *p = strdup(path);
   7844     int r = c_nftw_rec(p, fn, fd, flags, 0, fd);
   7845     free(p);
   7846     return r;
   7847 }
   7848 #endif
   7849 #ifndef C_DECLS_ONLY
   7850 static int (*c_ftw_fn)(const char *, const struct stat *, int);
   7851 #endif
   7852 #ifndef C_DECLS_ONLY
   7853 static int c_ftw_adapt(const char *p, const struct stat *s, int t, struct FTW *f) { (void)f; return c_ftw_fn(p, s, t); }
   7854 #endif
   7855 int ftw(const char *path, int (*fn)(const char *, const struct stat *, int), int fd)
   7856 #ifdef C_DECLS_ONLY
   7857 ;
   7858 #else
   7859 { c_ftw_fn = fn; return nftw(path, c_ftw_adapt, fd, 0); }
   7860 #endif
   7861 
   7862 char *get_current_dir_name(void)
   7863 #ifdef C_DECLS_ONLY
   7864 ;
   7865 #else
   7866 { return getcwd(NULL, 0); }
   7867 #endif
   7868 char *canonicalize_file_name(const char *p)
   7869 #ifdef C_DECLS_ONLY
   7870 ;
   7871 #else
   7872 { return realpath(p, NULL); }
   7873 #endif
   7874 
   7875 /* regular expressions: backtracking matcher over a parsed tree. basic and extended, with the usual GNU-ish extras (\w \s \b \< \>) */
   7876 #define REG_EXTENDED 1
   7877 #define REG_ICASE 2
   7878 #define REG_NOSUB 4
   7879 #define REG_NEWLINE 8
   7880 #define REG_NOTBOL 1
   7881 #define REG_NOTEOL 2
   7882 #define REG_STARTEND 4
   7883 #define REG_NOMATCH 1
   7884 #define REG_BADPAT 2
   7885 #define REG_ECOLLATE 3
   7886 #define REG_ECTYPE 4
   7887 #define REG_EESCAPE 5
   7888 #define REG_ESUBREG 6
   7889 #define REG_EBRACK 7
   7890 #define REG_EPAREN 8
   7891 #define REG_EBRACE 9
   7892 #define REG_BADBR 10
   7893 #define REG_ERANGE 11
   7894 #define REG_ESPACE 12
   7895 #define REG_BADRPT 13
   7896 typedef long regoff_t;
   7897 typedef struct { regoff_t rm_so, rm_eo; } regmatch_t;
   7898 enum { RX_CHAR, RX_ANY, RX_SET, RX_BOL, RX_EOL, RX_WORDB, RX_NWORDB, RX_WORDS, RX_WORDE, RX_GROUP, RX_BACKREF, RX_ALT };
   7899 struct rx_node {
   7900     int type, min, max, group, c;
   7901     char *set;
   7902     struct rx_node *sub, *next, *alt;
   7903 };
   7904 struct rx_inst;
   7905 typedef struct { size_t re_nsub; struct rx_inst *prog; int flags; int nregs, len; } regex_t;
   7906 struct rx_parse { const char *p; int ext, icase, ngroups, err; };
   7907 #ifndef C_DECLS_ONLY
   7908 static struct rx_node *rx_parse_alt(struct rx_parse *ps, int depth);
   7909 #endif
   7910 #ifndef C_DECLS_ONLY
   7911 static struct rx_node *rx_new(int type) { struct rx_node *n = calloc(1, sizeof *n); n->type = type; n->min = n->max = 1; return n; }
   7912 #endif
   7913 #ifndef C_DECLS_ONLY
   7914 static void rx_free(struct rx_node *n)
   7915 {
   7916     while (n) { struct rx_node *nx = n->next; rx_free(n->sub); rx_free(n->alt); free(n->set); free(n); n = nx; }
   7917 }
   7918 #endif
   7919 #ifndef C_DECLS_ONLY
   7920 static struct rx_node *rx_parse_set(struct rx_parse *ps)
   7921 {
   7922     struct rx_node *n = rx_new(RX_SET);
   7923     const char *p = ps->p;
   7924     int neg = 0, first = 1, c;
   7925     n->set = calloc(1, 256);
   7926     if (*p == '^') { neg = 1; p++; }
   7927     while (*p && (*p != ']' || first)) {
   7928         first = 0;
   7929         if (p[0] == '[' && p[1] == ':') {
   7930             const char *e = strstr(p + 2, ":]");
   7931             if (e) {
   7932                 char nm[16]; size_t l = e - p - 2;
   7933                 if (l > 8) { ps->err = REG_ECTYPE; return n; }
   7934                 memcpy(nm, p + 2, l); nm[l] = ':'; nm[l + 1] = 0;
   7935                 for (c = 0; c < 256; c++) if (c_fn_class(nm, c)) n->set[c] = 1;
   7936                 p = e + 2;
   7937                 continue;
   7938             }
   7939         }
   7940         c = (unsigned char)*p++;
   7941         if (*p == '-' && p[1] && p[1] != ']') {
   7942             int hi = (unsigned char)p[1], i;
   7943             p += 2;
   7944             if (hi < c) { ps->err = REG_ERANGE; return n; }
   7945             for (i = c; i <= hi; i++) n->set[i] = 1;
   7946         } else n->set[c] = 1;
   7947     }
   7948     if (*p != ']') { ps->err = REG_EBRACK; return n; }
   7949     p++;
   7950     if (ps->icase) for (c = 0; c < 256; c++) if (n->set[c]) { n->set[tolower(c)] = 1; n->set[toupper(c)] = 1; }
   7951     if (neg) for (c = 0; c < 256; c++) n->set[c] = !n->set[c];
   7952     ps->p = p;
   7953     return n;
   7954 }
   7955 #endif
   7956 #ifndef C_DECLS_ONLY
   7957 static struct rx_node *rx_parse_atom(struct rx_parse *ps, int depth, int *bol_ok)
   7958 {
   7959     const char *p = ps->p;
   7960     struct rx_node *n;
   7961     int ext = ps->ext;
   7962     (void)bol_ok;
   7963     if (*p == '(' && ext) {
   7964         int g = ++ps->ngroups;
   7965         ps->p = p + 1;
   7966         n = rx_new(RX_GROUP); n->group = g;
   7967         n->sub = rx_parse_alt(ps, depth + 1);
   7968         if (*ps->p != ')') { if (!ps->err) ps->err = REG_EPAREN; return n; }
   7969         ps->p++;
   7970         return n;
   7971     }
   7972     if (*p == '\\' && !ext && p[1] == '(') {
   7973         int g = ++ps->ngroups;
   7974         ps->p = p + 2;
   7975         n = rx_new(RX_GROUP); n->group = g;
   7976         n->sub = rx_parse_alt(ps, depth + 1);
   7977         if (ps->p[0] != '\\' || ps->p[1] != ')') { if (!ps->err) ps->err = REG_EPAREN; return n; }
   7978         ps->p += 2;
   7979         return n;
   7980     }
   7981     if (*p == '.') { ps->p++; return rx_new(RX_ANY); }
   7982     if (*p == '[') { ps->p++; return rx_parse_set(ps); }
   7983     if (*p == '^' && (ext || p == ps->p)) { ps->p++; return rx_new(RX_BOL); }
   7984     if (*p == '$') { ps->p++; return rx_new(RX_EOL); }
   7985     if (*p == '\\') {
   7986         int c = p[1];
   7987         ps->p += 2;
   7988         if (!c) { ps->err = REG_EESCAPE; return rx_new(RX_CHAR); }
   7989         if (c >= '1' && c <= '9') { n = rx_new(RX_BACKREF); n->group = c - '0'; return n; }
   7990         if (c == 'b') return rx_new(RX_WORDB);
   7991         if (c == 'B') return rx_new(RX_NWORDB);
   7992         if (c == '<') return rx_new(RX_WORDS);
   7993         if (c == '>') return rx_new(RX_WORDE);
   7994         if (c == 'w' || c == 'W' || c == 's' || c == 'S' || c == 'd' || c == 'D') {
   7995             int i;
   7996             n = rx_new(RX_SET); n->set = calloc(1, 256);
   7997             for (i = 0; i < 256; i++) {
   7998                 int in = (c == 'w' || c == 'W') ? (isalnum(i) || i == '_') : (c == 's' || c == 'S') ? isspace(i) : isdigit(i);
   7999                 n->set[i] = isupper(c) ? !in : in;
   8000             }
   8001             return n;
   8002         }
   8003         n = rx_new(RX_CHAR); n->c = c == 'n' ? '\n' : c == 't' ? '\t' : c; return n;
   8004     }
   8005     ps->p++;
   8006     n = rx_new(RX_CHAR); n->c = (unsigned char)*p;
   8007     if (*p == '*' && !ext) { /* a leading star in a basic expression is a plain one */ }
   8008     return n;
   8009 }
   8010 #endif
   8011 #ifndef C_DECLS_ONLY
   8012 static struct rx_node *rx_parse_seq(struct rx_parse *ps, int depth)
   8013 {
   8014     struct rx_node *head = NULL, **tail = &head;
   8015     while (*ps->p && !ps->err) {
   8016         const char *p = ps->p;
   8017         struct rx_node *a;
   8018         int ext = ps->ext;
   8019         if (ext && (*p == '|' || (*p == ')' && depth > 0))) break;
   8020         if (!ext && p[0] == '\\' && (p[1] == '|' || (p[1] == ')' && depth > 0))) break;
   8021         a = rx_parse_atom(ps, depth, NULL);
   8022         if (ps->err) { rx_free(a); break; }
   8023         for (;;) {
   8024             p = ps->p;
   8025             if (*p == '*' && !(a->type == RX_BOL && !ext)) { a->min = 0; a->max = -1; ps->p++; }
   8026             else if (ext && *p == '+') { a->min = 1; a->max = -1; ps->p++; }
   8027             else if (ext && *p == '?') { a->min = 0; a->max = 1; ps->p++; }
   8028             else if (!ext && p[0] == '\\' && (p[1] == '+' || p[1] == '?')) { a->min = p[1] == '+'; a->max = p[1] == '+' ? -1 : 1; ps->p += 2; }
   8029             else if ((ext && *p == '{') || (!ext && p[0] == '\\' && p[1] == '{')) {
   8030                 char *e;
   8031                 long lo, hi;
   8032                 p += ext ? 1 : 2;
   8033                 lo = strtol(p, &e, 10);
   8034                 if (e == p) { ps->err = REG_BADBR; break; }
   8035                 hi = lo;
   8036                 if (*e == ',') { p = e + 1; if (*p == '}' || *p == '\\') { hi = -1; e = (char *)p; } else { hi = strtol(p, &e, 10); } }
   8037                 if (ext ? *e != '}' : (e[0] != '\\' || e[1] != '}')) { ps->err = REG_EBRACE; break; }
   8038                 ps->p = e + (ext ? 1 : 2);
   8039                 if (hi >= 0 && hi < lo) { ps->err = REG_BADBR; break; }
   8040                 a->min = lo; a->max = hi;
   8041             } else break;
   8042             if (a->type == RX_BOL || a->type == RX_EOL) break;
   8043             {
   8044                 /* a repeat on a repeat: wrap what we have */
   8045                 if ((ext && (*ps->p == '*' || *ps->p == '+' || *ps->p == '?'))) {
   8046                     struct rx_node *w = rx_new(RX_GROUP);
   8047                     w->group = 0; w->sub = a; a = w;
   8048                 } else break;
   8049             }
   8050         }
   8051         *tail = a; tail = &a->next;
   8052     }
   8053     return head;
   8054 }
   8055 #endif
   8056 #ifndef C_DECLS_ONLY
   8057 static struct rx_node *rx_parse_alt(struct rx_parse *ps, int depth)
   8058 {
   8059     struct rx_node *left = rx_parse_seq(ps, depth), *n;
   8060     int ext = ps->ext;
   8061     if ((ext && *ps->p == '|') || (!ext && ps->p[0] == '\\' && ps->p[1] == '|')) {
   8062         ps->p += ext ? 1 : 2;
   8063         n = rx_new(RX_ALT);
   8064         n->sub = left;
   8065         n->alt = rx_parse_alt(ps, depth);
   8066         return n;
   8067     }
   8068     return left;
   8069 }
   8070 #endif
   8071 enum { I_CHAR, I_ANY, I_SET, I_SPLIT, I_JMP, I_SAVE, I_BOL, I_EOL, I_WORDB, I_NWORDB, I_WORDS, I_WORDE, I_BACKREF, I_MARK, I_CHK, I_MATCH };
   8072 struct rx_inst { int op, x, y; char *set; };
   8073 struct rx_gen { struct rx_inst *p; int n, cap, nregs, err; int icase; };
   8074 #ifndef C_DECLS_ONLY
   8075 static int rx_emit(struct rx_gen *g, int op, int x, int y, char *set)
   8076 {
   8077     if (g->n == g->cap) {
   8078         struct rx_inst *np;
   8079         if (g->cap > 200000) { g->err = REG_ESPACE; return 0; }
   8080         g->cap = g->cap * 2 + 32;
   8081         np = realloc(g->p, g->cap * sizeof *np);
   8082         if (!np) { g->err = REG_ESPACE; return 0; }
   8083         g->p = np;
   8084     }
   8085     g->p[g->n].op = op; g->p[g->n].x = x; g->p[g->n].y = y; g->p[g->n].set = set;
   8086     return g->n++;
   8087 }
   8088 #endif
   8089 #ifndef C_DECLS_ONLY
   8090 static void rx_gen_list(struct rx_gen *g, struct rx_node *n);
   8091 #endif
   8092 #ifndef C_DECLS_ONLY
   8093 static void rx_gen_one(struct rx_gen *g, struct rx_node *n)
   8094 {
   8095     switch (n->type) {
   8096     case RX_CHAR: rx_emit(g, I_CHAR, n->c, 0, NULL); break;
   8097     case RX_ANY: rx_emit(g, I_ANY, 0, 0, NULL); break;
   8098     case RX_SET: rx_emit(g, I_SET, 0, 0, n->set); break;
   8099     case RX_BOL: rx_emit(g, I_BOL, 0, 0, NULL); break;
   8100     case RX_EOL: rx_emit(g, I_EOL, 0, 0, NULL); break;
   8101     case RX_WORDB: rx_emit(g, I_WORDB, 0, 0, NULL); break;
   8102     case RX_NWORDB: rx_emit(g, I_NWORDB, 0, 0, NULL); break;
   8103     case RX_WORDS: rx_emit(g, I_WORDS, 0, 0, NULL); break;
   8104     case RX_WORDE: rx_emit(g, I_WORDE, 0, 0, NULL); break;
   8105     case RX_BACKREF: rx_emit(g, I_BACKREF, n->group, 0, NULL); break;
   8106     case RX_GROUP:
   8107         if (n->group) rx_emit(g, I_SAVE, 2 * n->group, 0, NULL);
   8108         rx_gen_list(g, n->sub);
   8109         if (n->group) rx_emit(g, I_SAVE, 2 * n->group + 1, 0, NULL);
   8110         break;
   8111     case RX_ALT: {
   8112         int sp = rx_emit(g, I_SPLIT, 0, 0, NULL), j;
   8113         if (g->err) return;
   8114         g->p[sp].x = g->n;
   8115         rx_gen_list(g, n->sub);
   8116         j = rx_emit(g, I_JMP, 0, 0, NULL);
   8117         if (g->err) return;
   8118         g->p[sp].y = g->n;
   8119         rx_gen_list(g, n->alt);
   8120         g->p[j].x = g->n;
   8121         break;
   8122     }
   8123     }
   8124 }
   8125 #endif
   8126 #ifndef C_DECLS_ONLY
   8127 static void rx_gen_rep(struct rx_gen *g, struct rx_node *n)
   8128 {
   8129     int i, ends[256], ne = 0;
   8130     struct rx_node one = *n;
   8131     one.next = NULL;
   8132     if (n->type == RX_ALT) { one.min = one.max = 1; }
   8133     for (i = 0; i < n->min && !g->err; i++) rx_gen_one(g, &one);
   8134     if (n->max < 0) {
   8135         int l1 = g->n, sp = rx_emit(g, I_SPLIT, 0, 0, NULL), reg = g->nregs++;
   8136         if (g->err) return;
   8137         g->p[sp].x = g->n;
   8138         rx_emit(g, I_MARK, reg, 0, NULL);
   8139         rx_gen_one(g, &one);
   8140         rx_emit(g, I_CHK, reg, 0, NULL);
   8141         rx_emit(g, I_JMP, l1, 0, NULL);
   8142         g->p[sp].y = g->n;
   8143     } else {
   8144         for (i = n->min; i < n->max && !g->err; i++) {
   8145             int sp = rx_emit(g, I_SPLIT, 0, 0, NULL);
   8146             if (g->err) return;
   8147             g->p[sp].x = g->n;
   8148             if (ne < 256) ends[ne++] = sp;
   8149             rx_gen_one(g, &one);
   8150         }
   8151         for (i = 0; i < ne; i++) g->p[ends[i]].y = g->n;
   8152     }
   8153 }
   8154 #endif
   8155 #ifndef C_DECLS_ONLY
   8156 static void rx_gen_list(struct rx_gen *g, struct rx_node *n)
   8157 {
   8158     for (; n && !g->err; n = n->next) {
   8159         if (n->min == 1 && n->max == 1) rx_gen_one(g, n); else rx_gen_rep(g, n);
   8160     }
   8161 }
   8162 #endif
   8163 #ifndef C_DECLS_ONLY
   8164 static int rx_nopt(struct rx_node *n) { (void)n; return 0; }
   8165 #endif
   8166 int regcomp(regex_t *re, const char *pat, int flags)
   8167 #ifdef C_DECLS_ONLY
   8168 ;
   8169 #else
   8170 {
   8171     struct rx_parse ps = { pat, flags & REG_EXTENDED, flags & REG_ICASE, 0, 0 };
   8172     struct rx_gen g = { NULL, 0, 0, 0, 0, flags & REG_ICASE };
   8173     struct rx_node *tree;
   8174     (void)rx_nopt;
   8175     tree = rx_parse_alt(&ps, 0);
   8176     if (!ps.err && *ps.p) ps.err = REG_EPAREN;
   8177     if (ps.err) { rx_free(tree); return ps.err; }
   8178     rx_emit(&g, I_SAVE, 0, 0, NULL);
   8179     rx_gen_list(&g, tree);
   8180     rx_emit(&g, I_SAVE, 1, 0, NULL);
   8181     rx_emit(&g, I_MATCH, 0, 0, NULL);
   8182     if (g.err) { rx_free(tree); free(g.p); return g.err; }
   8183     /* sets are owned by the program from here on; give the tree's away without freeing them */
   8184     {
   8185         struct rx_inst *pi;
   8186         for (pi = g.p; pi < g.p + g.n; pi++) if (pi->op == I_SET) { char *c = malloc(256); memcpy(c, pi->set, 256); pi->set = c; }
   8187     }
   8188     rx_free(tree);
   8189     re->prog = g.p; re->len = g.n; re->nregs = g.nregs; re->re_nsub = ps.ngroups; re->flags = flags;
   8190     return 0;
   8191 }
   8192 #endif
   8193 struct rx_m { const char *s, *end; const struct rx_inst *p; long *cap, *reg; int ncap, icase, newline, notbol, noteol; long steps; };
   8194 #ifndef C_DECLS_ONLY
   8195 static int rx_word(int c) { return isalnum(c) || c == '_'; }
   8196 #endif
   8197 #ifndef C_DECLS_ONLY
   8198 static int rx_exec(struct rx_m *m, int pc, const char *s)
   8199 {
   8200     for (;;) {
   8201         const struct rx_inst *i = &m->p[pc];
   8202         if (++m->steps > 20000000) return 0;
   8203         switch (i->op) {
   8204         case I_CHAR: if (s < m->end && (m->icase ? tolower((unsigned char)*s) == tolower(i->x) : (unsigned char)*s == i->x)) { s++; pc++; continue; } return 0;
   8205         case I_ANY: if (s < m->end && !(m->newline && *s == '\n')) { s++; pc++; continue; } return 0;
   8206         case I_SET: if (s < m->end && i->set[(unsigned char)*s]) { s++; pc++; continue; } return 0;
   8207         case I_JMP: pc = i->x; continue;
   8208         case I_SPLIT: if (rx_exec(m, i->x, s)) return 1; pc = i->y; continue;
   8209         case I_SAVE: {
   8210             long old;
   8211             if (i->x >= m->ncap) { pc++; continue; }
   8212             old = m->cap[i->x]; m->cap[i->x] = s - m->s;
   8213             if (rx_exec(m, pc + 1, s)) return 1;
   8214             m->cap[i->x] = old;
   8215             return 0;
   8216         }
   8217         case I_MARK: {
   8218             long old = m->reg[i->x];
   8219             m->reg[i->x] = s - m->s;
   8220             if (rx_exec(m, pc + 1, s)) return 1;
   8221             m->reg[i->x] = old;
   8222             return 0;
   8223         }
   8224         case I_CHK: if (m->reg[i->x] == s - m->s) return 0; pc++; continue;
   8225         case I_BOL: if ((s == m->s && !m->notbol) || (m->newline && s > m->s && s[-1] == '\n')) { pc++; continue; } return 0;
   8226         case I_EOL: if ((s == m->end && !m->noteol) || (m->newline && s < m->end && *s == '\n')) { pc++; continue; } return 0;
   8227         case I_WORDB: case I_NWORDB: case I_WORDS: case I_WORDE: {
   8228             int a = s > m->s && rx_word((unsigned char)s[-1]), b = s < m->end && rx_word((unsigned char)*s), ok;
   8229             ok = i->op == I_WORDB ? a != b : i->op == I_NWORDB ? a == b : i->op == I_WORDS ? !a && b : a && !b;
   8230             if (!ok) return 0;
   8231             pc++; continue;
   8232         }
   8233         case I_BACKREF: {
   8234             long so, eo;
   8235             if (2 * i->x + 1 >= m->ncap) return 0;
   8236             so = m->cap[2 * i->x]; eo = m->cap[2 * i->x + 1];
   8237             if (so < 0 || eo < 0) return 0;
   8238             if (s + (eo - so) > m->end) return 0;
   8239             if (m->icase ? strncasecmp(s, m->s + so, eo - so) : memcmp(s, m->s + so, eo - so)) return 0;
   8240             s += eo - so; pc++; continue;
   8241         }
   8242         case I_MATCH: m->cap[1] = s - m->s; return 1;
   8243         }
   8244         return 0;
   8245     }
   8246 }
   8247 #endif
   8248 int regexec(const regex_t *re, const char *str, size_t nmatch, regmatch_t pmatch[], int eflags)
   8249 #ifdef C_DECLS_ONLY
   8250 ;
   8251 #else
   8252 {
   8253     struct rx_m m;
   8254     const char *s = str, *end = str + strlen(str);
   8255     int ncap = 2 * (re->re_nsub + 1), i, found = 0;
   8256     long *cap = malloc((ncap + re->nregs + 1) * sizeof *cap);
   8257     if (!cap) return REG_ESPACE;
   8258     if (eflags & REG_STARTEND) { s = str + pmatch[0].rm_so; end = str + pmatch[0].rm_eo; }
   8259     m.s = str; m.end = end; m.p = re->prog; m.cap = cap; m.reg = cap + ncap; m.ncap = ncap;
   8260     m.icase = re->flags & REG_ICASE; m.newline = re->flags & REG_NEWLINE; m.notbol = eflags & REG_NOTBOL; m.noteol = eflags & REG_NOTEOL; m.steps = 0;
   8261     for (; s <= end; s++) {
   8262         for (i = 0; i < ncap + re->nregs; i++) cap[i] = -1;
   8263         if (rx_exec(&m, 0, s)) { found = 1; break; }
   8264         if (m.steps > 20000000) break;
   8265     }
   8266     if (found && !(re->flags & REG_NOSUB) && pmatch) {
   8267         size_t k;
   8268         for (k = 0; k < nmatch; k++) {
   8269             if ((int)(2 * k + 1) < ncap && cap[2 * k] >= 0 && cap[2 * k + 1] >= 0) { pmatch[k].rm_so = cap[2 * k]; pmatch[k].rm_eo = cap[2 * k + 1]; }
   8270             else { pmatch[k].rm_so = -1; pmatch[k].rm_eo = -1; }
   8271         }
   8272     }
   8273     free(cap);
   8274     return found ? 0 : REG_NOMATCH;
   8275 }
   8276 #endif
   8277 size_t regerror(int code, const regex_t *re, char *buf, size_t n)
   8278 #ifdef C_DECLS_ONLY
   8279 ;
   8280 #else
   8281 {
   8282     static const char *const msg[] = { "Success", "No match", "Invalid regular expression", "Invalid collation character", "Invalid character class name", "Trailing backslash", "Invalid back reference", "Unmatched [, [^, [:, [., or [=", "Unmatched ( or \\(", "Unmatched \\{", "Invalid content of \\{\\}", "Invalid range end", "Memory exhausted", "Invalid preceding regular expression" };
   8283     const char *m = code >= 0 && code <= REG_BADRPT ? msg[code] : "Unknown error";
   8284     (void)re;
   8285     if (n) strlcpy(buf, m, n);
   8286     return strlen(m) + 1;
   8287 }
   8288 #endif
   8289 void regfree(regex_t *re)
   8290 #ifdef C_DECLS_ONLY
   8291 ;
   8292 #else
   8293 {
   8294     int i;
   8295     if (!re->prog) return;
   8296     for (i = 0; i < re->len; i++) if (re->prog[i].op == I_SET) free(re->prog[i].set);
   8297     free(re->prog);
   8298     re->prog = NULL;
   8299 }
   8300 #endif
   8301 
   8302 #endif