commit 381f494ea057bfe51905a5824b6b4fa9d0566bd3
parent c586c75de892d57c7f6e44cecc00540e21acebeb
Author: torter <koubbamohamedrayan@gmail.com>
Date: Wed, 25 Nov 2026 11:01:25 +0000
i386: ./build.sh 32 makes one binary for 32 and 64-bit x86, c.h with the i386 layer, factor and nproc without 64-bit longs
Diffstat:
| M | build.sh | | | 3 | ++- |
| M | c.h | | | 2610 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
| M | rifle.c | | | 18 | ++++++++++++++++-- |
3 files changed, 2595 insertions(+), 36 deletions(-)
diff --git a/build.sh b/build.sh
@@ -3,8 +3,9 @@
set -e
cd "$(dirname "$0")"
CC=${CC:-cc}
+[ "$1" = 32 ] && M32="-m32 -msse2" # ./build.sh 32: one i386 binary, runs on 32 and 64-bit x86
$CC -std=gnu11 -O2 -Wall -Wextra -Wno-missing-field-initializers -Wno-empty-body -Wno-deprecated-non-prototype -Wno-unused-parameter \
- -static -nostdlib -ffreestanding -fno-builtin -fno-stack-protector -fno-asynchronous-unwind-tables \
+ $M32 -static -nostdlib -ffreestanding -fno-builtin -fno-stack-protector -fno-asynchronous-unwind-tables \
-fno-pie -ffunction-sections -fdata-sections -fuse-ld=lld -Wl,--gc-sections -Wl,-s -Wl,-z,norelro \
-o rifle rifle.c
rm -rf bin && mkdir bin
diff --git a/c.h b/c.h
@@ -1,4 +1,4 @@
-/* c.h: the whole libc of Chamesle Source Linux, in one file. x86-64 Linux only.
+/* c.h: the whole libc of Chamesle Source Linux, in one file. x86-64 and i386 Linux.
* this copy lives in rifle and has grown: directories, links, users and groups, the errno messages, a real
* malloc, strftime, setenv. what rifle added is marked [rifle]. nothing the original had was taken out.
* include it once (in one .c file) and you get everything: the start-up code, system calls, strings, memory,
@@ -7,19 +7,28 @@
#define C_H
/* ---- types ---- */
-typedef unsigned long size_t;
-typedef long ssize_t;
-typedef long ptrdiff_t;
+typedef __SIZE_TYPE__ size_t;
+typedef __PTRDIFF_TYPE__ ssize_t;
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
typedef long intptr_t;
typedef unsigned long uintptr_t;
+#ifdef __i386__
+typedef long long off_t; /* files past 2G on i386 too */
+#else
typedef long off_t;
+#endif
typedef long time_t;
typedef int pid_t;
typedef int mode_t;
typedef unsigned int uid_t; /* [rifle] */
typedef unsigned int gid_t;
+#ifdef __i386__
+typedef unsigned long long dev_t;
+typedef unsigned long long ino_t;
+#else
typedef unsigned long dev_t;
typedef unsigned long ino_t;
+#endif
typedef unsigned long nlink_t;
typedef long clock_t;
typedef signed char int8_t;
@@ -39,12 +48,17 @@ typedef int wchar_t_c; /* not wchar_t: tcc and gcc both predefine their own */
#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)
#define UINT_MAX 4294967295U
+#ifdef __i386__
+#define LONG_MAX 2147483647L
+#define ULONG_MAX 4294967295UL
+#else
#define LONG_MAX 9223372036854775807L
-#define LONG_MIN (-LONG_MAX - 1)
#define ULONG_MAX 18446744073709551615UL
-#define LLONG_MAX LONG_MAX
-#define LLONG_MIN LONG_MIN
-#define ULLONG_MAX ULONG_MAX
+#endif
+#define LONG_MIN (-LONG_MAX - 1)
+#define LLONG_MAX 9223372036854775807LL
+#define LLONG_MIN (-LLONG_MAX - 1)
+#define ULLONG_MAX 18446744073709551615ULL
#define PATH_MAX 4096
#define NAME_MAX 255
#define SIZE_MAX ULONG_MAX
@@ -76,16 +90,192 @@ extern long sys6(long n, long a, long b, long c, long d, long e, long f);
#else
long sys6(long n, long a, long b, long c, long d, long e, long f);
#endif
+#ifdef __i386__
+/* i386: int 0x80, the number in eax, the arguments in ebx ecx edx esi edi ebp */
+#ifndef C_DECLS_ONLY
+__asm__(".text\n.global sys6\nsys6:\n push %ebx\n push %esi\n push %edi\n push %ebp\n"
+ " mov 20(%esp),%eax\n mov 24(%esp),%ebx\n mov 28(%esp),%ecx\n mov 32(%esp),%edx\n"
+ " mov 36(%esp),%esi\n mov 40(%esp),%edi\n mov 44(%esp),%ebp\n int $0x80\n"
+ " pop %ebp\n pop %edi\n pop %esi\n pop %ebx\n ret\n");
+#endif
+#else
#ifndef C_DECLS_ONLY
__asm__(".text\n.global sys6\nsys6:\n"
" mov %rdi,%rax\n mov %rsi,%rdi\n mov %rdx,%rsi\n mov %rcx,%rdx\n mov %r8,%r10\n mov %r9,%r8\n"
" mov 8(%rsp),%r9\n syscall\n ret\n");
#endif
+#endif
#define sys0(n) sys6(n, 0, 0, 0, 0, 0, 0)
#define sys1(n, a) sys6(n, (long)(a), 0, 0, 0, 0, 0)
#define sys2(n, a, b) sys6(n, (long)(a), (long)(b), 0, 0, 0, 0)
#define sys3(n, a, b, c) sys6(n, (long)(a), (long)(b), (long)(c), 0, 0, 0)
+#ifdef __i386__
+/* where i386 has an old call and a new one, the new one: stat64, mmap2, _llseek, the 32-bit uids */
+#define SYS_accept 364
+#define SYS_accept4 364
+#define SYS_access 33
+#define SYS_alarm 27
+#define SYS_arch_prctl 384
+#define SYS_bind 361
+#define SYS_brk 45
+#define SYS_chdir 12
+#define SYS_chmod 15
+#define SYS_chown 212
+#define SYS_chroot 61
+#define SYS_clock_getres 266
+#define SYS_clock_gettime 265
+#define SYS_clock_nanosleep 267
+#define SYS_clone 120
+#define SYS_close 6
+#define SYS_connect 362
+#define SYS_copy_file_range 377
+#define SYS_creat 8
+#define SYS_dup 41
+#define SYS_dup2 63
+#define SYS_dup3 330
+#define SYS_execve 11
+#define SYS_exit 1
+#define SYS_exit_group 252
+#define SYS_faccessat 307
+#define SYS_fchdir 133
+#define SYS_fchmod 94
+#define SYS_fchmodat 306
+#define SYS_fchmodat2 452
+#define SYS_fchown 207
+#define SYS_fchownat 298
+#define SYS_fcntl 221
+#define SYS_fdatasync 148
+#define SYS_flock 143
+#define SYS_fork 2
+#define SYS_fstat 197
+#define SYS_fstatfs 100
+#define SYS_fsync 118
+#define SYS_ftruncate 194
+#define SYS_futex 240
+#define SYS_getcwd 183
+#define SYS_getdents 141
+#define SYS_getdents64 220
+#define SYS_getegid 202
+#define SYS_geteuid 201
+#define SYS_getgid 200
+#define SYS_getgroups 205
+#define SYS_getitimer 105
+#define SYS_getpeername 368
+#define SYS_getpgid 132
+#define SYS_getpgrp 65
+#define SYS_getpid 20
+#define SYS_getppid 64
+#define SYS_getpriority 96
+#define SYS_getrandom 355
+#define SYS_getrlimit 191
+#define SYS_getrusage 77
+#define SYS_getsid 147
+#define SYS_getsockname 367
+#define SYS_getsockopt 365
+#define SYS_gettid 224
+#define SYS_gettimeofday 78
+#define SYS_getuid 199
+#define SYS_ioctl 54
+#define SYS_kill 37
+#define SYS_lchown 198
+#define SYS_link 9
+#define SYS_linkat 303
+#define SYS_listen 363
+#define SYS_lseek 140
+#define SYS_lstat 196
+#define SYS_madvise 219
+#define SYS_mkdir 39
+#define SYS_mkdirat 296
+#define SYS_mknod 14
+#define SYS_mknodat 297
+#define SYS_mmap 192
+#define SYS_mount 21
+#define SYS_mprotect 125
+#define SYS_mremap 163
+#define SYS_msync 144
+#define SYS_munmap 91
+#define SYS_nanosleep 162
+#define SYS_newfstatat 300
+#define SYS_open 5
+#define SYS_openat 295
+#define SYS_pause 29
+#define SYS_pipe 42
+#define SYS_pipe2 331
+#define SYS_poll 168
+#define SYS_ppoll 309
+#define SYS_prctl 172
+#define SYS_pread64 180
+#define SYS_pwrite64 181
+#define SYS_read 3
+#define SYS_readlink 85
+#define SYS_readlinkat 305
+#define SYS_readv 145
+#define SYS_recvfrom 371
+#define SYS_recvmsg 372
+#define SYS_rename 38
+#define SYS_renameat 302
+#define SYS_rmdir 40
+#define SYS_rt_sigaction 174
+#define SYS_rt_sigpending 176
+#define SYS_rt_sigprocmask 175
+#define SYS_rt_sigreturn 173
+#define SYS_rt_sigsuspend 179
+#define SYS_rt_sigtimedwait 177
+#define SYS_sched_getaffinity 242
+#define SYS_sched_yield 158
+#define SYS_select 142
+#define SYS_sendfile 239
+#define SYS_sendmsg 370
+#define SYS_sendto 369
+#define SYS_set_tid_address 258
+#define SYS_setgid 214
+#define SYS_setgroups 206
+#define SYS_sethostname 74
+#define SYS_setitimer 104
+#define SYS_setpgid 57
+#define SYS_setpriority 97
+#define SYS_setregid 204
+#define SYS_setresgid 210
+#define SYS_setresuid 208
+#define SYS_setreuid 203
+#define SYS_setrlimit 75
+#define SYS_setsid 66
+#define SYS_setsockopt 366
+#define SYS_setuid 213
+#define SYS_shutdown 373
+#define SYS_sigaltstack 186
+#define SYS_socket 359
+#define SYS_socketpair 360
+#define SYS_stat 195
+#define SYS_statfs 99
+#define SYS_symlink 83
+#define SYS_symlinkat 304
+#define SYS_sync 36
+#define SYS_syncfs 344
+#define SYS_sysinfo 116
+#define SYS_tgkill 270
+#define SYS_time 13
+#define SYS_times 43
+#define SYS_truncate 193
+#define SYS_umask 60
+#define SYS_umount2 52
+#define SYS_uname 122
+#define SYS_unlink 10
+#define SYS_unlinkat 301
+#define SYS_utime 30
+#define SYS_utimensat 320
+#define SYS_utimes 271
+#define SYS_vfork 190
+#define SYS_wait4 114
+#define SYS_write 4
+#define SYS_writev 146
+#define O_LARGEFILE 0100000
+#else
+#define O_LARGEFILE 0
+#endif
+
+#ifndef __i386__
#define SYS_read 0
#define SYS_write 1
#define SYS_open 2
@@ -117,6 +307,7 @@ __asm__(".text\n.global sys6\nsys6:\n"
#define SYS_pipe 22
#define SYS_dup 32
#define SYS_getuid 102
+#endif
/* errno: the libc keeps the last error here. one program, one thread: a plain variable does it */
#ifdef C_DECLS_ONLY
@@ -216,7 +407,7 @@ int open(const char *path, int flags, ...)
{
va_list ap; int mode;
va_start(ap, flags); mode = va_arg(ap, int); va_end(ap);
- return fail(sys3(SYS_open, path, flags, flags & O_CREAT ? mode : 0));
+ return fail(sys3(SYS_open, path, flags | O_LARGEFILE, flags & O_CREAT ? mode : 0));
}
#endif
int close(int fd)
@@ -225,12 +416,26 @@ int close(int fd)
#else
{ return fail(sys1(SYS_close, fd)); }
#endif
+#ifdef __i386__
+/* i386 hands 64-bit offsets over in two halves, high first for _llseek */
+off_t lseek(int fd, off_t off, int whence)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ off_t r;
+ if (fail(sys6(SYS_lseek, fd, (long)(off >> 32), (long)off, (long)&r, whence, 0)) < 0) return -1;
+ return r;
+}
+#endif
+#else
off_t lseek(int fd, off_t off, int whence)
#ifdef C_DECLS_ONLY
;
#else
{ return fail(sys3(SYS_lseek, fd, off, whence)); }
#endif
+#endif
int ioctl(int fd, unsigned long req, void *arg)
#ifdef C_DECLS_ONLY
;
@@ -377,6 +582,20 @@ void *sbrk(long n)
#endif
/* ---- files' details ---- */
+#ifdef __i386__
+/* the kernel's stat64 */
+struct stat {
+ unsigned long long st_dev;
+ unsigned int pad0, st_ino32, st_mode, st_nlink, st_uid, st_gid;
+ unsigned long long st_rdev;
+ unsigned int pad1;
+ long long st_size;
+ unsigned long st_blksize;
+ unsigned long long st_blocks;
+ unsigned long st_atime, st_atime_nsec, st_mtime, st_mtime_nsec, st_ctime, st_ctime_nsec;
+ unsigned long long st_ino;
+};
+#else
struct stat {
unsigned long st_dev, st_ino, st_nlink;
unsigned int st_mode, st_uid, st_gid, pad0;
@@ -385,6 +604,7 @@ struct stat {
unsigned long st_atime, st_atime_nsec, st_mtime, st_mtime_nsec, st_ctime, st_ctime_nsec;
long unused[3];
};
+#endif
#define S_IFMT 0170000
#define S_IFDIR 0040000
#define S_IFCHR 0020000
@@ -485,10 +705,16 @@ extern void c_restore_rt(void);
#else
void c_restore_rt(void);
#endif
+#ifdef __i386__
+#ifndef C_DECLS_ONLY
+__asm__(".text\n.global c_restore_rt\nc_restore_rt:\n mov $173,%eax\n int $0x80\n");
+#endif
+#else
#ifndef C_DECLS_ONLY
__asm__(".text\n.global c_restore_rt\nc_restore_rt:\n mov $15,%rax\n syscall\n");
#endif
-struct c_sigaction { sighandler_t handler; unsigned long flags; void (*restorer)(void); unsigned long mask; };
+#endif
+struct c_sigaction { sighandler_t handler; unsigned long flags; void (*restorer)(void); unsigned long long mask; };
sighandler_t signal(int n, sighandler_t f)
#ifdef C_DECLS_ONLY
;
@@ -507,6 +733,7 @@ __attribute__((noreturn)) void _exit(int code)
#endif
/* ---- more system calls [rifle] ---- */
+#ifndef __i386__
#define SYS_lstat 6
#define SYS_access 21
#define SYS_mremap 25
@@ -561,6 +788,7 @@ __attribute__((noreturn)) void _exit(int code)
#define SYS_setsid 112
#define SYS_sendfile 40
#define SYS_copy_file_range 326
+#endif
#define F_GETFL 3
#define F_SETFL 4
#define F_GETFD 1
@@ -599,7 +827,7 @@ int openat(int dirfd, const char *path, int flags, ...)
{
va_list ap; int mode;
va_start(ap, flags); mode = va_arg(ap, int); va_end(ap);
- return fail(sys6(SYS_openat, dirfd, (long)path, flags, flags & O_CREAT ? mode : 0, 0, 0));
+ return fail(sys6(SYS_openat, dirfd, (long)path, flags | O_LARGEFILE, flags & O_CREAT ? mode : 0, 0, 0));
}
#endif
int unlinkat(int dirfd, const char *path, int flags)
@@ -680,6 +908,20 @@ int access(const char *path, int mode)
#else
{ return fail(sys2(SYS_access, path, mode)); }
#endif
+#ifdef __i386__
+int truncate(const char *path, off_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_truncate, path, (long)n, (long)(n >> 32))); }
+#endif
+int ftruncate(int fd, off_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_ftruncate, fd, (long)n, (long)(n >> 32))); }
+#endif
+#else
int truncate(const char *path, off_t n)
#ifdef C_DECLS_ONLY
;
@@ -692,6 +934,7 @@ int ftruncate(int fd, off_t n)
#else
{ return fail(sys2(SYS_ftruncate, fd, n)); }
#endif
+#endif
int fsync(int fd)
#ifdef C_DECLS_ONLY
;
@@ -830,6 +1073,20 @@ ssize_t getrandom(void *buf, size_t n, unsigned flags)
#else
{ return fail(sys3(SYS_getrandom, buf, n, flags)); }
#endif
+#ifdef __i386__
+ssize_t pread(int fd, void *buf, size_t n, off_t off)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_pread64, fd, (long)buf, n, (long)off, (long)(off >> 32), 0)); }
+#endif
+ssize_t pwrite(int fd, const void *buf, size_t n, off_t off)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_pwrite64, fd, (long)buf, n, (long)off, (long)(off >> 32), 0)); }
+#endif
+#else
ssize_t pread(int fd, void *buf, size_t n, off_t off)
#ifdef C_DECLS_ONLY
;
@@ -842,6 +1099,7 @@ ssize_t pwrite(int fd, const void *buf, size_t n, off_t off)
#else
{ return fail(sys6(SYS_pwrite64, fd, (long)buf, n, off, 0, 0)); }
#endif
+#endif
int setuid(uid_t u)
#ifdef C_DECLS_ONLY
;
@@ -1385,6 +1643,16 @@ extern __attribute__((noreturn)) void longjmp(jmp_buf b, int v);
#else
__attribute__((noreturn)) void longjmp(jmp_buf b, int v);
#endif
+#ifdef __i386__
+#ifndef C_DECLS_ONLY
+__asm__(".text\n.global setjmp\n.global _setjmp\nsetjmp:\n_setjmp:\n"
+ " mov 4(%esp),%eax\n mov %ebx,(%eax)\n mov %esi,4(%eax)\n mov %edi,8(%eax)\n mov %ebp,12(%eax)\n"
+ " 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"
+ ".global longjmp\nlongjmp:\n"
+ " mov 4(%esp),%edx\n mov 8(%esp),%eax\n test %eax,%eax\n jnz 1f\n inc %eax\n1:\n"
+ " 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");
+#endif
+#else
#ifndef C_DECLS_ONLY
__asm__(".text\n.global setjmp\n.global _setjmp\nsetjmp:\n_setjmp:\n"
" mov %rbx,(%rdi)\n mov %rbp,8(%rdi)\n mov %r12,16(%rdi)\n mov %r13,24(%rdi)\n mov %r14,32(%rdi)\n"
@@ -1395,6 +1663,7 @@ __asm__(".text\n.global setjmp\n.global _setjmp\nsetjmp:\n_setjmp:\n"
" mov (%rdi),%rbx\n mov 8(%rdi),%rbp\n mov 16(%rdi),%r12\n mov 24(%rdi),%r13\n mov 32(%rdi),%r14\n"
" mov 40(%rdi),%r15\n mov 48(%rdi),%rsp\n jmp *56(%rdi)\n");
#endif
+#endif
/* ---- time ---- */
struct tm { int tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst; };
@@ -1973,14 +2242,15 @@ static int format(struct out *o, const char *fmt, va_list ap)
for (;; fmt++) {
if (*fmt == 'h') size = size == -1 ? -2 : -1;
else if (*fmt == 'l') size++;
- else if (*fmt == 'z' || *fmt == 'j' || *fmt == 't') size = 2;
+ else if (*fmt == 'j') size = 2;
+ else if (*fmt == 'z' || *fmt == 't') size = sizeof(size_t) == 8 ? 2 : 0;
else if (*fmt == 'L') size = 3;
else break;
}
char b[128], *s = b; int len = 0; const char *pre = "";
switch (*fmt) {
case 'd': case 'i': {
- long long v = size >= 1 ? va_arg(ap, long long) : va_arg(ap, int);
+ long long v = size >= 2 ? va_arg(ap, long long) : size == 1 ? va_arg(ap, long) : va_arg(ap, int);
if (size == -1) v = (short)v; else if (size == -2) v = (signed char)v;
unsigned long long u = v < 0 ? -(unsigned long long)v : v;
pre = v < 0 ? "-" : plus ? "+" : space ? " " : "";
@@ -1997,7 +2267,7 @@ static int format(struct out *o, const char *fmt, va_list ap)
int base = *fmt == 'o' ? 8 : *fmt == 'u' ? 10 : 16;
const char *dig = *fmt == 'X' ? "0123456789ABCDEF" : "0123456789abcdef";
if (*fmt == 'p') { u = (uintptr_t)va_arg(ap, void *); alt = 1; }
- else u = size >= 1 ? va_arg(ap, unsigned long long) : va_arg(ap, unsigned int);
+ else u = size >= 2 ? va_arg(ap, unsigned long long) : size == 1 ? va_arg(ap, unsigned long) : va_arg(ap, unsigned int);
if (size == -1) u = (unsigned short)u; else if (size == -2) u = (unsigned char)u;
if (alt && base == 16 && u) pre = *fmt == 'X' ? "0X" : "0x";
char *p = b + sizeof b; *--p = 0;
@@ -2197,7 +2467,7 @@ int execvp(const char *file, char *const argv[])
/* ---- directories [rifle] ----
* opendir/readdir over getdents64. readdir hands back a pointer into the buffer, good until the next call */
-struct dirent { ino_t d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; };
+struct dirent { ino_t d_ino; long long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; };
#define DT_UNKNOWN 0
#define DT_FIFO 1
#define DT_CHR 2
@@ -2511,6 +2781,61 @@ int clearenv(void)
{ if (c_env_copy()) return -1; environ[0] = NULL; return 0; }
#endif
+#ifdef __i386__
+/* i386 has no 64-bit divide: the compiler calls these, as it would call libgcc's */
+unsigned long long __udivmoddi4(unsigned long long a, unsigned long long b, unsigned long long *rem)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ unsigned long long q = 0, r = 0;
+ int i;
+ if (!(a >> 32) && !(b >> 32)) {
+ q = (unsigned)a / (unsigned)b;
+ if (rem) *rem = (unsigned)a % (unsigned)b;
+ return q;
+ }
+ for (i = 63; i >= 0; i--) {
+ r = r << 1 | (a >> i & 1);
+ if (r >= b) { r -= b; q |= 1ULL << i; }
+ }
+ if (rem) *rem = r;
+ return q;
+}
+#endif
+unsigned long long __udivdi3(unsigned long long a, unsigned long long b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return __udivmoddi4(a, b, 0); }
+#endif
+unsigned long long __umoddi3(unsigned long long a, unsigned long long b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ unsigned long long r; __udivmoddi4(a, b, &r); return r; }
+#endif
+long long __divdi3(long long a, long long b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ unsigned long long q = __udivmoddi4(a < 0 ? -(unsigned long long)a : a, b < 0 ? -(unsigned long long)b : b, 0);
+ return (a < 0) != (b < 0) ? -(long long)q : (long long)q;
+}
+#endif
+long long __moddi3(long long a, long long b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ unsigned long long r;
+ __udivmoddi4(a < 0 ? -(unsigned long long)a : a, b < 0 ? -(unsigned long long)b : b, &r);
+ return a < 0 ? -(long long)r : (long long)r;
+}
+#endif
+#endif
+
/* ---- start-up ----
* the kernel leaves argc, argv and the environment on the stack, then jumps to _start */
#ifdef C_DECLS_ONLY
@@ -2530,10 +2855,16 @@ void c_start(long *sp)
}
#endif
#ifndef C_NO_START
+#ifdef __i386__
+#ifndef C_DECLS_ONLY
+__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");
+#endif
+#else
#ifndef C_DECLS_ONLY
__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");
#endif
#endif
+#endif
/* [rifle] priorities and getline */
int strcoll(const char *a, const char *b)
@@ -2542,8 +2873,10 @@ int strcoll(const char *a, const char *b)
#else
{ return strcmp(a, b); }
#endif
+#ifndef __i386__
#define SYS_getpriority 140
#define SYS_setpriority 141
+#endif
int getpriority(int which, int who)
#ifdef C_DECLS_ONLY
;
@@ -2577,6 +2910,123 @@ ssize_t getline(char **lp, size_t *cap, FILE *f)
/* ---- what the rest of the additions lean on [chamesle] ---- */
#define sys4(n, a, b, c, d) sys6(n, (long)(a), (long)(b), (long)(c), (long)(d), 0, 0)
#define sys5(n, a, b, c, d, e) sys6(n, (long)(a), (long)(b), (long)(c), (long)(d), (long)(e), 0)
+#ifndef ENOTBLK
+#define ENOTBLK 15
+#endif
+#ifndef ENOMSG
+#define ENOMSG 42
+#endif
+#ifndef EPROTO
+#define EPROTO 71
+#endif
+#ifndef EBADMSG
+#define EBADMSG 74
+#endif
+#ifndef ENOTSOCK
+#define ENOTSOCK 88
+#endif
+#ifndef EDESTADDRREQ
+#define EDESTADDRREQ 89
+#endif
+#ifndef EMSGSIZE
+#define EMSGSIZE 90
+#endif
+#ifndef EPROTOTYPE
+#define EPROTOTYPE 91
+#endif
+#ifndef ENOPROTOOPT
+#define ENOPROTOOPT 92
+#endif
+#ifndef EPROTONOSUPPORT
+#define EPROTONOSUPPORT 93
+#endif
+#ifndef EAFNOSUPPORT
+#define EAFNOSUPPORT 97
+#endif
+#ifndef EADDRINUSE
+#define EADDRINUSE 98
+#endif
+#ifndef EADDRNOTAVAIL
+#define EADDRNOTAVAIL 99
+#endif
+#ifndef ENETDOWN
+#define ENETDOWN 100
+#endif
+#ifndef ENETUNREACH
+#define ENETUNREACH 101
+#endif
+#ifndef ECONNABORTED
+#define ECONNABORTED 103
+#endif
+#ifndef ECONNRESET
+#define ECONNRESET 104
+#endif
+#ifndef ENOBUFS
+#define ENOBUFS 105
+#endif
+#ifndef EISCONN
+#define EISCONN 106
+#endif
+#ifndef ENOTCONN
+#define ENOTCONN 107
+#endif
+#ifndef ECONNREFUSED
+#define ECONNREFUSED 111
+#endif
+#ifndef EHOSTUNREACH
+#define EHOSTUNREACH 113
+#endif
+#ifndef EALREADY
+#define EALREADY 114
+#endif
+#ifndef EINPROGRESS
+#define EINPROGRESS 115
+#endif
+#ifndef ECANCELED
+#define ECANCELED 125
+#endif
+#ifndef ENOTSUP
+#define ENOTSUP 95
+#endif
+#ifndef EWOULDBLOCK
+#define EWOULDBLOCK 11
+#endif
+#ifndef ENOLCK
+#define ENOLCK 37
+#endif
+#ifndef EIDRM
+#define EIDRM 43
+#endif
+#ifndef ENOLINK
+#define ENOLINK 67
+#endif
+#ifndef ETIME
+#define ETIME 62
+#endif
+#ifndef ENOSR
+#define ENOSR 63
+#endif
+#ifndef ENOSTR
+#define ENOSTR 60
+#endif
+#ifndef EMULTIHOP
+#define EMULTIHOP 72
+#endif
+#ifndef EOWNERDEAD
+#define EOWNERDEAD 130
+#endif
+#ifndef ENOTRECOVERABLE
+#define ENOTRECOVERABLE 131
+#endif
+#ifndef ESHUTDOWN
+#define ESHUTDOWN 108
+#endif
+#ifndef ETOOMANYREFS
+#define ETOOMANYREFS 109
+#endif
+#ifndef EHOSTDOWN
+#define EHOSTDOWN 112
+#endif
/* ---- system call numbers [chamesle] ---- */
#ifndef SYS_read
#define SYS_read 0
@@ -3046,7 +3496,7 @@ ssize_t getline(char **lp, size_t *cap, FILE *f)
#ifndef SYS_fchmodat2
#define SYS_fchmodat2 452
#endif
-/* ---- more of unistd, fcntl, sys/*: the thin system calls [chamesle] ---- */
+/* ---- more of unistd, fcntl, sys/...: the thin system calls [chamesle] ---- */
#ifndef O_NONBLOCK
#define O_NONBLOCK 04000
@@ -3762,7 +4212,11 @@ void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off)
;
#else
{
+#ifdef __i386__
+ long r = sys6(SYS_mmap, (long)addr, len, prot, flags, fd, (long)(off >> 12)); /* mmap2 counts in pages */
+#else
long r = sys6(SYS_mmap, (long)addr, len, prot, flags, fd, off);
+#endif
if (r < 0 && r > -4096) { errno = -r; return MAP_FAILED; }
return (void *)r;
}
@@ -5001,25 +5455,25 @@ int sigaddset(sigset_t *s, int n)
#ifdef C_DECLS_ONLY
;
#else
-{ if (n < 1 || n > 64) { errno = EINVAL; return -1; } s->__bits[0] |= 1UL << (n - 1); return 0; }
+{ if (n < 1 || n > 64) { errno = EINVAL; return -1; } s->__bits[(n - 1) / (8 * sizeof(long))] |= 1UL << (n - 1) % (8 * sizeof(long)); return 0; }
#endif
int sigdelset(sigset_t *s, int n)
#ifdef C_DECLS_ONLY
;
#else
-{ if (n < 1 || n > 64) { errno = EINVAL; return -1; } s->__bits[0] &= ~(1UL << (n - 1)); return 0; }
+{ if (n < 1 || n > 64) { errno = EINVAL; return -1; } s->__bits[(n - 1) / (8 * sizeof(long))] &= ~(1UL << (n - 1) % (8 * sizeof(long))); return 0; }
#endif
int sigismember(const sigset_t *s, int n)
#ifdef C_DECLS_ONLY
;
#else
-{ if (n < 1 || n > 64) { errno = EINVAL; return -1; } return (s->__bits[0] >> (n - 1)) & 1; }
+{ if (n < 1 || n > 64) { errno = EINVAL; return -1; } return (s->__bits[(n - 1) / (8 * sizeof(long))] >> (n - 1) % (8 * sizeof(long))) & 1; }
#endif
int sigisemptyset(const sigset_t *s)
#ifdef C_DECLS_ONLY
;
#else
-{ return !s->__bits[0]; }
+{ return !s->__bits[0] && !s->__bits[1]; }
#endif
int sigaction(int n, const struct sigaction *act, struct sigaction *old)
#ifdef C_DECLS_ONLY
@@ -5028,10 +5482,10 @@ int sigaction(int n, const struct sigaction *act, struct sigaction *old)
{
struct c_sigaction k, ko;
long r;
- if (act) { k.handler = act->sa_handler; k.flags = (unsigned long)(unsigned)act->sa_flags | 0x04000000; k.restorer = c_restore_rt; k.mask = act->sa_mask.__bits[0]; }
+ 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); }
r = fail(sys4(SYS_rt_sigaction, n, act ? &k : NULL, old ? &ko : NULL, 8));
if (r < 0) return -1;
- if (old) { memset(old, 0, sizeof *old); old->sa_handler = ko.handler; old->sa_flags = ko.flags & ~0x04000000UL; old->sa_mask.__bits[0] = ko.mask; }
+ 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); }
return 0;
}
#endif
@@ -5040,10 +5494,10 @@ int sigprocmask(int how, const sigset_t *set, sigset_t *old)
;
#else
{
- unsigned long o = 0;
+ unsigned long long o = 0;
long r = fail(sys4(SYS_rt_sigprocmask, how, set ? set->__bits : NULL, old ? &o : NULL, 8));
if (r < 0) return -1;
- if (old) { memset(old, 0, sizeof *old); old->__bits[0] = o; }
+ if (old) { memset(old, 0, sizeof *old); memcpy(old->__bits, &o, 8); }
return 0;
}
#endif
@@ -5057,7 +5511,7 @@ int sigpending(sigset_t *s)
#ifdef C_DECLS_ONLY
;
#else
-{ unsigned long o = 0; if (fail(sys2(SYS_rt_sigpending, &o, 8)) < 0) return -1; memset(s, 0, sizeof *s); s->__bits[0] = o; return 0; }
+{ 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; }
#endif
int sigsuspend(const sigset_t *s)
#ifdef C_DECLS_ONLY
@@ -5175,7 +5629,7 @@ double fabs(double x)
#ifdef C_DECLS_ONLY
;
#else
-{ union { double d; unsigned long u; } v = { x }; v.u &= ~(1UL << 63); return v.d; }
+{ union { double d; unsigned long long u; } v = { x }; v.u &= ~(1ULL << 63); return v.d; }
#endif
float fabsf(float x)
#ifdef C_DECLS_ONLY
@@ -5193,7 +5647,7 @@ double copysign(double x, double y)
#ifdef C_DECLS_ONLY
;
#else
-{ union { double d; unsigned long u; } a = { x }, b = { y }; a.u = (a.u & ~(1UL << 63)) | (b.u & (1UL << 63)); return a.d; }
+{ union { double d; unsigned long long u; } a = { x }, b = { y }; a.u = (a.u & ~(1ULL << 63)) | (b.u & (1ULL << 63)); return a.d; }
#endif
float copysignf(float x, float y)
#ifdef C_DECLS_ONLY
@@ -5224,11 +5678,11 @@ double trunc(double x)
;
#else
{
- union { double d; unsigned long u; } v = { x };
+ union { double d; unsigned long long u; } v = { x };
int e = (int)((v.u >> 52) & 0x7ff) - 0x3ff;
if (e >= 52) return x;
- if (e < 0) { v.u &= 1UL << 63; return v.d; }
- v.u &= ~(0x000fffffffffffffUL >> e);
+ if (e < 0) { v.u &= 1ULL << 63; return v.d; }
+ v.u &= ~(0x000fffffffffffffULL >> e);
return v.d;
}
#endif
@@ -5369,7 +5823,7 @@ double frexp(double x, int *e)
;
#else
{
- union { double d; unsigned long u; } v = { x };
+ union { double d; unsigned long long u; } v = { x };
int ex = (int)((v.u >> 52) & 0x7ff);
if (!ex) {
if (x == 0) { *e = 0; return x; }
@@ -5377,7 +5831,7 @@ double frexp(double x, int *e)
v.d = x; ex = (int)((v.u >> 52) & 0x7ff) - 54;
} else if (ex == 0x7ff) { *e = 0; return x; }
*e = ex - 0x3fe;
- v.u = (v.u & ~(0x7ffUL << 52)) | (0x3feUL << 52);
+ v.u = (v.u & ~(0x7ffULL << 52)) | (0x3feULL << 52);
return v.d;
}
#endif
@@ -5452,7 +5906,7 @@ double nextafter(double x, double y)
;
#else
{
- union { double d; unsigned long u; } v = { x };
+ union { double d; unsigned long long u; } v = { x };
if (isnan(x) || isnan(y)) return x + y;
if (x == y) return y;
if (x == 0) { v.u = 1; return y > 0 ? v.d : -v.d; }
@@ -5691,7 +6145,11 @@ double tgamma(double x)
return exp(lgamma(x));
}
#endif
+#ifdef C_DECLS_ONLY
+#define c_float_fn(name) float name##f(float x);
+#else
#define c_float_fn(name) float name##f(float x) { return (float)name(x); }
+#endif
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)
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)
float powf(float x, float y)
@@ -5754,5 +6212,2091 @@ long double cosl(long double x)
#else
{ return cos((double)x); }
#endif
+/* ---- scanf, locale and wide char stubs, threads that aren't, dlopen that can't [chamesle] ---- */
+#ifndef C_DECLS_ONLY
+static int c_scan(int (*get)(void *), void (*unget)(int, void *), void *src, const char *fmt, va_list ap)
+{
+ int n = 0, c, count = 0;
+ for (; *fmt; fmt++) {
+ int sup = 0, width = 0, lng = 0, hh = 0, ok;
+ if (isspace((unsigned char)*fmt)) { while (isspace(c = get(src))) ; unget(c, src); continue; }
+ if (*fmt != '%') {
+ c = get(src);
+ if (c != (unsigned char)*fmt) { unget(c, src); return n ? n : (c == EOF ? EOF : 0); }
+ count++;
+ continue;
+ }
+ fmt++;
+ if (*fmt == '%') { while (isspace(c = get(src))) ; if (c != '%') { unget(c, src); return n; } continue; }
+ if (*fmt == '*') { sup = 1; fmt++; }
+ while (isdigit((unsigned char)*fmt)) width = width * 10 + *fmt++ - '0';
+ for (;; fmt++) {
+ if (*fmt == 'l') lng++;
+ else if (*fmt == 'h') hh++;
+ else if (*fmt == 'z' || *fmt == 'j' || *fmt == 't' || *fmt == 'L' || *fmt == 'q') lng = 2;
+ else break;
+ }
+ if (!width) width = 1 << 30;
+ if (*fmt != 'c' && *fmt != '[' && *fmt != 'n') { while (isspace(c = get(src))) ; unget(c, src); }
+ if (*fmt == 'n') { if (!sup) *va_arg(ap, int *) = count; continue; }
+ {
+ char tok[512];
+ int len = 0;
+ switch (*fmt) {
+ case 'd': case 'i': case 'u': case 'x': case 'X': case 'o': case 'p': {
+ int base = *fmt == 'd' || *fmt == 'u' ? 10 : *fmt == 'o' ? 8 : *fmt == 'i' ? 0 : 16;
+ int neg = 0;
+ unsigned long long v = 0;
+ int digits = 0;
+ c = get(src);
+ if (c == '-' || c == '+') { neg = c == '-'; width--; c = get(src); }
+ if (width > 0 && c == '0' && (base == 0 || base == 16)) {
+ int c2;
+ count++; width--;
+ c2 = get(src);
+ if ((c2 == 'x' || c2 == 'X') && width > 0) { base = 16; width--; count++; c = get(src); }
+ 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); } }
+ if (digits) { c = c2; }
+ }
+ if (base == 0) base = 10;
+ while (width > 0) {
+ int d = c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 : 99;
+ if (d >= base) break;
+ v = v * base + d; digits = 1; width--; count++;
+ c = get(src);
+ }
+ unget(c, src);
+ if (!digits) return n ? n : (c == EOF ? EOF : 0);
+ if (neg) v = -v;
+ if (!sup) {
+ if (*fmt == 'd' || *fmt == 'i') {
+ 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;
+ } else if (*fmt == 'p') *va_arg(ap, void **) = (void *)(unsigned long)v;
+ 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;
+ n++;
+ }
+ break;
+ }
+ case 'f': case 'e': case 'g': case 'E': case 'G': case 'a': {
+ char *end;
+ double v;
+ c = get(src);
+ 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'))) {
+ tok[len++] = c; width--; c = get(src);
+ }
+ unget(c, src);
+ tok[len] = 0;
+ v = strtod(tok, &end);
+ if (end == tok) return n ? n : (c == EOF ? EOF : 0);
+ count += len;
+ if (!sup) { if (lng) *va_arg(ap, double *) = v; else *va_arg(ap, float *) = v; n++; }
+ break;
+ }
+ case 's': {
+ char *d = sup ? NULL : va_arg(ap, char *);
+ c = get(src);
+ while (c != EOF && !isspace(c) && width > 0) { if (d) d[len] = c; len++; width--; c = get(src); }
+ unget(c, src);
+ if (!len) return n ? n : EOF;
+ if (d) { d[len] = 0; n++; }
+ count += len;
+ break;
+ }
+ case 'c': {
+ char *d = sup ? NULL : va_arg(ap, char *);
+ if (width == 1 << 30) width = 1;
+ while (width-- > 0) { c = get(src); if (c == EOF) return n ? n : EOF; if (d) d[len] = c; len++; }
+ if (d) n++;
+ count += len;
+ break;
+ }
+ case '[': {
+ char set[256];
+ int neg = 0, i;
+ char *d = sup ? NULL : va_arg(ap, char *);
+ memset(set, 0, sizeof set);
+ fmt++;
+ if (*fmt == '^') { neg = 1; fmt++; }
+ if (*fmt == ']') { set[']'] = 1; fmt++; }
+ while (*fmt && *fmt != ']') {
+ if (fmt[1] == '-' && fmt[2] && fmt[2] != ']') { for (i = (unsigned char)fmt[0]; i <= (unsigned char)fmt[2]; i++) set[i] = 1; fmt += 3; }
+ else set[(unsigned char)*fmt++] = 1;
+ }
+ c = get(src);
+ while (c != EOF && width > 0 && set[c] != neg) { if (d) d[len] = c; len++; width--; c = get(src); }
+ unget(c, src);
+ if (!len) return n ? n : (c == EOF ? EOF : 0);
+ if (d) { d[len] = 0; n++; }
+ count += len;
+ break;
+ }
+ default: return n;
+ }
+ (void)ok;
+ }
+ }
+ return n;
+}
+#endif
+#ifndef C_DECLS_ONLY
+static int c_sget(void *p) { const char **s = p; int c = (unsigned char)**s; if (!c) return EOF; (*s)++; return c; }
+#endif
+#ifndef C_DECLS_ONLY
+static void c_sunget(int c, void *p) { const char **s = p; if (c != EOF) (*s)--; }
+#endif
+#ifndef C_DECLS_ONLY
+static int c_fget(void *p) { return fgetc(p); }
+#endif
+#ifndef C_DECLS_ONLY
+static void c_funget(int c, void *p) { if (c != EOF) ungetc(c, p); }
+#endif
+int vsscanf(const char *s, const char *fmt, va_list ap)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c_scan(c_sget, c_sunget, &s, fmt, ap); }
+#endif
+int sscanf(const char *s, const char *fmt, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ va_list ap; int n; va_start(ap, fmt); n = vsscanf(s, fmt, ap); va_end(ap); return n; }
+#endif
+int vfscanf(FILE *f, const char *fmt, va_list ap)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c_scan(c_fget, c_funget, f, fmt, ap); }
+#endif
+int fscanf(FILE *f, const char *fmt, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ va_list ap; int n; va_start(ap, fmt); n = vfscanf(f, fmt, ap); va_end(ap); return n; }
+#endif
+int scanf(const char *fmt, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ va_list ap; int n; va_start(ap, fmt); n = vfscanf(stdin, fmt, ap); va_end(ap); return n; }
+#endif
+int vscanf(const char *fmt, va_list ap)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return vfscanf(stdin, fmt, ap); }
+#endif
+
+/* locale: there is only the C one */
+#define LC_CTYPE 0
+#define LC_NUMERIC 1
+#define LC_TIME 2
+#define LC_COLLATE 3
+#define LC_MONETARY 4
+#define LC_MESSAGES 5
+#define LC_ALL 6
+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;
+ 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; };
+char *setlocale(int cat, const char *name)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)cat; if (!name || !*name || !strcmp(name, "C") || !strcmp(name, "POSIX") || !strcmp(name, "C.UTF-8")) return "C"; return NULL; }
+#endif
+struct lconv *localeconv(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ static struct lconv l = { ".", "", "", "", "", "", "", "", "", "", 127, 127, 127, 127, 127, 127, 127, 127 };
+ return &l;
+}
+#endif
+typedef void *locale_t;
+locale_t newlocale(int mask, const char *name, locale_t base)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)mask; (void)name; (void)base; return (locale_t)1; }
+#endif
+void freelocale(locale_t l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)l; }
+#endif
+locale_t uselocale(locale_t l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)l; return (locale_t)1; }
+#endif
+#define LC_GLOBAL_LOCALE ((locale_t)-1)
+#define CODESET 14
+#define D_T_FMT 0x20028
+#define RADIXCHAR 0x10000
+char *nl_langinfo(int item)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return item == CODESET ? "ANSI_X3.4-1968" : item == RADIXCHAR ? "." : ""; }
+#endif
+char *gettext(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (char *)s; }
+#endif
+char *dgettext(const char *d, const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)d; return (char *)s; }
+#endif
+char *ngettext(const char *a, const char *b, unsigned long n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (char *)(n == 1 ? a : b); }
+#endif
+char *textdomain(const char *d)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (char *)d; }
+#endif
+char *bindtextdomain(const char *d, const char *dir)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)d; return (char *)dir; }
+#endif
+#define _(s) (s)
+int isalpha_l(int c, locale_t l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)l; return isalpha(c); }
+#endif
+int strcoll_l(const char *a, const char *b, locale_t l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)l; return strcmp(a, b); }
+#endif
+int strcasecmp_l(const char *a, const char *b, locale_t l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)l; return strcasecmp(a, b); }
+#endif
+
+typedef int wchar_t;
+typedef int wint_t;
+typedef int wctype_t;
+typedef struct { int c_; } mbstate_t;
+#define WEOF (-1)
+#define MB_LEN_MAX 4
+#define MB_CUR_MAX 1
+int mbtowc(wchar_t *w, const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (!s) return 0; if (!n) return -1; if (w) *w = (unsigned char)*s; return *s ? 1 : 0; }
+#endif
+int wctomb(char *s, wchar_t w)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (!s) return 0; *s = w; return 1; }
+#endif
+size_t mbrtowc(wchar_t *w, const char *s, size_t n, mbstate_t *st)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)st; if (!s) return 0; if (!n) return (size_t)-2; if (w) *w = (unsigned char)*s; return *s ? 1 : 0; }
+#endif
+size_t wcrtomb(char *s, wchar_t w, mbstate_t *st)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)st; if (!s) return 1; *s = w; return 1; }
+#endif
+size_t mbstowcs(wchar_t *d, const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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; }
+#endif
+size_t wcstombs(char *d, const wchar_t *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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; }
+#endif
+size_t mbrlen(const char *s, size_t n, mbstate_t *st)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return mbrtowc(NULL, s, n, st); }
+#endif
+int mbsinit(const mbstate_t *st)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)st; return 1; }
+#endif
+int mblen(const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return mbtowc(NULL, s, n); }
+#endif
+size_t wcslen(const wchar_t *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ size_t n = 0; while (s[n]) n++; return n; }
+#endif
+int wcscmp(const wchar_t *a, const wchar_t *b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ while (*a && *a == *b) { a++; b++; } return *a - *b; }
+#endif
+wchar_t *wcscpy(wchar_t *d, const wchar_t *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ wchar_t *r = d; while ((*d++ = *s++)) ; return r; }
+#endif
+wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return memcpy(d, s, n * sizeof *d); }
+#endif
+wchar_t *wmemset(wchar_t *d, wchar_t c, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ size_t i; for (i = 0; i < n; i++) d[i] = c; return d; }
+#endif
+int iswalpha(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isalpha(c); }
+#endif
+int iswdigit(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isdigit(c); }
+#endif
+int iswspace(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isspace(c); }
+#endif
+int iswupper(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isupper(c); }
+#endif
+int iswlower(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return islower(c); }
+#endif
+int iswalnum(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isalnum(c); }
+#endif
+int iswprint(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c >= 32 && c < 127; }
+#endif
+int iswpunct(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return ispunct(c); }
+#endif
+int iswcntrl(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return iscntrl(c); }
+#endif
+wint_t towlower(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return tolower(c); }
+#endif
+wint_t towupper(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return toupper(c); }
+#endif
+int wcwidth(wchar_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c == 0 ? 0 : c < 32 || (c >= 127 && c < 160) ? -1 : 1; }
+#endif
+int wcswidth(const wchar_t *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ int w = 0; while (n-- && *s) { int c = wcwidth(*s++); if (c < 0) return -1; w += c; } return w; }
+#endif
+wint_t btowc(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c; }
+#endif
+int wctob(wint_t c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c < 256 ? c : EOF; }
+#endif
+
+/* threads: one of them. a mutex always locks, a thread is run to the end when it's made */
+typedef unsigned long pthread_t;
+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_;
+typedef struct { int done; } pthread_once_t;
+typedef unsigned pthread_key_t;
+#define PTHREAD_MUTEX_INITIALIZER { 0 }
+#define PTHREAD_COND_INITIALIZER { 0 }
+#define PTHREAD_RWLOCK_INITIALIZER { 0 }
+#define PTHREAD_ONCE_INIT { 0 }
+#define PTHREAD_MUTEX_RECURSIVE 1
+#define PTHREAD_MUTEX_NORMAL 0
+#define PTHREAD_MUTEX_ERRORCHECK 2
+#define PTHREAD_CREATE_DETACHED 1
+#define PTHREAD_CREATE_JOINABLE 0
+int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)m; (void)a; return 0; }
+#endif
+int pthread_mutex_lock(pthread_mutex_t *m)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)m; return 0; }
+#endif
+int pthread_mutex_trylock(pthread_mutex_t *m)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)m; return 0; }
+#endif
+int pthread_mutex_unlock(pthread_mutex_t *m)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)m; return 0; }
+#endif
+int pthread_mutex_destroy(pthread_mutex_t *m)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)m; return 0; }
+#endif
+int pthread_mutexattr_init(pthread_mutexattr_t *a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; return 0; }
+#endif
+int pthread_mutexattr_settype(pthread_mutexattr_t *a, int t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; (void)t; return 0; }
+#endif
+int pthread_mutexattr_destroy(pthread_mutexattr_t *a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; return 0; }
+#endif
+int pthread_cond_init(pthread_cond_t *c, const pthread_condattr_t *a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)c; (void)a; return 0; }
+#endif
+int pthread_cond_signal(pthread_cond_t *c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)c; return 0; }
+#endif
+int pthread_cond_broadcast(pthread_cond_t *c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)c; return 0; }
+#endif
+int pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)c; (void)m; return 0; }
+#endif
+int pthread_cond_timedwait(pthread_cond_t *c, pthread_mutex_t *m, const struct timespec *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)c; (void)m; (void)t; return ETIMEDOUT; }
+#endif
+int pthread_cond_destroy(pthread_cond_t *c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)c; return 0; }
+#endif
+int pthread_rwlock_init(pthread_rwlock_t *l, const pthread_rwlockattr_t *a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)l; (void)a; return 0; }
+#endif
+int pthread_rwlock_rdlock(pthread_rwlock_t *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)l; return 0; }
+#endif
+int pthread_rwlock_wrlock(pthread_rwlock_t *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)l; return 0; }
+#endif
+int pthread_rwlock_unlock(pthread_rwlock_t *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)l; return 0; }
+#endif
+int pthread_rwlock_destroy(pthread_rwlock_t *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)l; return 0; }
+#endif
+int pthread_once(pthread_once_t *o, void (*f)(void))
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (!o->done) { o->done = 1; f(); } return 0; }
+#endif
+pthread_t pthread_self(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return 1; }
+#endif
+int pthread_equal(pthread_t a, pthread_t b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return a == b; }
+#endif
+#ifndef C_DECLS_ONLY
+static void *c_tls[64];
+#endif
+#ifndef C_DECLS_ONLY
+static int c_tls_n;
+#endif
+int pthread_key_create(pthread_key_t *k, void (*d)(void *))
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)d; if (c_tls_n == 64) return EAGAIN; *k = c_tls_n++; return 0; }
+#endif
+int pthread_key_delete(pthread_key_t k)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)k; return 0; }
+#endif
+void *pthread_getspecific(pthread_key_t k)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return k < 64 ? c_tls[k] : NULL; }
+#endif
+int pthread_setspecific(pthread_key_t k, const void *v)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (k >= 64) return EINVAL; c_tls[k] = (void *)v; return 0; }
+#endif
+int pthread_attr_init(pthread_attr_t *a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; return 0; }
+#endif
+int pthread_attr_destroy(pthread_attr_t *a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; return 0; }
+#endif
+int pthread_attr_setdetachstate(pthread_attr_t *a, int s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; (void)s; return 0; }
+#endif
+int pthread_attr_setstacksize(pthread_attr_t *a, size_t s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; (void)s; return 0; }
+#endif
+int pthread_create(pthread_t *t, const pthread_attr_t *a, void *(*fn)(void *), void *arg)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; (void)fn; (void)arg; (void)t; return EAGAIN; }
+#endif
+int pthread_join(pthread_t t, void **r)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)t; (void)r; return ESRCH; }
+#endif
+int pthread_detach(pthread_t t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)t; return 0; }
+#endif
+int pthread_atfork(void (*a)(void), void (*b)(void), void (*c)(void))
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; (void)b; (void)c; return 0; }
+#endif
+int pthread_setcancelstate(int s, int *o)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)s; if (o) *o = 0; return 0; }
+#endif
+int pthread_kill(pthread_t t, int sig)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)t; return raise(sig); }
+#endif
+int sched_yield(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys0(SYS_sched_yield)); }
+#endif
+
+/* dlopen: static binaries have nothing to open */
+#define RTLD_LAZY 1
+#define RTLD_NOW 2
+#define RTLD_GLOBAL 0x100
+#define RTLD_LOCAL 0
+#define RTLD_DEFAULT ((void *)0)
+void *dlopen(const char *f, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)f; (void)flags; return NULL; }
+#endif
+void *dlsym(void *h, const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)h; (void)s; return NULL; }
+#endif
+int dlclose(void *h)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)h; return 0; }
+#endif
+char *dlerror(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return "dynamic loading is not available"; }
+#endif
+
+void endpwent(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ }
+#endif
+void endgrent(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ }
+#endif
+/* ---- sockets, poll, select, name lookup [chamesle] ---- */
+#define AF_UNSPEC 0
+#define AF_UNIX 1
+#define AF_LOCAL 1
+#define AF_INET 2
+#define AF_INET6 10
+#define PF_UNSPEC 0
+#define PF_UNIX 1
+#define PF_INET 2
+#define PF_INET6 10
+#define SOCK_STREAM 1
+#define SOCK_DGRAM 2
+#define SOCK_RAW 3
+#define SOCK_NONBLOCK 04000
+#define SOCK_CLOEXEC 02000000
+#define SOL_SOCKET 1
+#define SO_DEBUG 1
+#define SO_REUSEADDR 2
+#define SO_TYPE 3
+#define SO_ERROR 4
+#define SO_BROADCAST 6
+#define SO_SNDBUF 7
+#define SO_RCVBUF 8
+#define SO_KEEPALIVE 9
+#define SO_LINGER 13
+#define SO_REUSEPORT 15
+#define SO_RCVTIMEO 20
+#define SO_SNDTIMEO 21
+#define SOMAXCONN 4096
+#define MSG_PEEK 2
+#define MSG_DONTWAIT 0x40
+#define MSG_NOSIGNAL 0x4000
+#define MSG_WAITALL 0x100
+#define SHUT_RD 0
+#define SHUT_WR 1
+#define SHUT_RDWR 2
+#define IPPROTO_IP 0
+#define IPPROTO_TCP 6
+#define IPPROTO_UDP 17
+#define IPPROTO_IPV6 41
+#define TCP_NODELAY 1
+#define IPV6_V6ONLY 26
+#define INADDR_ANY ((in_addr_t)0)
+#define INADDR_LOOPBACK ((in_addr_t)0x7f000001)
+#define INET_ADDRSTRLEN 16
+#define INET6_ADDRSTRLEN 46
+typedef unsigned socklen_t;
+typedef unsigned short sa_family_t;
+typedef unsigned short in_port_t;
+typedef unsigned in_addr_t;
+struct sockaddr { sa_family_t sa_family; char sa_data[14]; };
+struct sockaddr_storage { sa_family_t ss_family; char pad[126]; unsigned long align; };
+struct in_addr { in_addr_t s_addr; };
+struct in6_addr { unsigned char s6_addr[16]; };
+struct sockaddr_in { sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; };
+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; };
+struct sockaddr_un { sa_family_t sun_family; char sun_path[108]; };
+struct linger { int l_onoff, l_linger; };
+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; };
+#ifndef C_DECLS_ONLY
+static const struct in6_addr in6addr_any = { { 0 } };
+#endif
+#ifndef C_DECLS_ONLY
+static const struct in6_addr in6addr_loopback = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } };
+#endif
+#define IN6ADDR_ANY_INIT { { 0 } }
+unsigned short htons(unsigned short x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return __builtin_bswap16(x); }
+#endif
+unsigned short ntohs(unsigned short x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return __builtin_bswap16(x); }
+#endif
+unsigned htonl(unsigned x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return __builtin_bswap32(x); }
+#endif
+unsigned ntohl(unsigned x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return __builtin_bswap32(x); }
+#endif
+int socket(int d, int t, int p)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_socket, d, t, p)); }
+#endif
+int socketpair(int d, int t, int p, int sv[2])
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys4(SYS_socketpair, d, t, p, sv)); }
+#endif
+int bind(int fd, const struct sockaddr *a, socklen_t l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_bind, fd, a, l)); }
+#endif
+int listen(int fd, int n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_listen, fd, n)); }
+#endif
+int accept(int fd, struct sockaddr *a, socklen_t *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_accept, fd, a, l)); }
+#endif
+int accept4(int fd, struct sockaddr *a, socklen_t *l, int fl)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys4(SYS_accept4, fd, a, l, fl)); }
+#endif
+int connect(int fd, const struct sockaddr *a, socklen_t l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_connect, fd, a, l)); }
+#endif
+int shutdown(int fd, int how)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_shutdown, fd, how)); }
+#endif
+int getsockname(int fd, struct sockaddr *a, socklen_t *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_getsockname, fd, a, l)); }
+#endif
+int getpeername(int fd, struct sockaddr *a, socklen_t *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_getpeername, fd, a, l)); }
+#endif
+int setsockopt(int fd, int lv, int n, const void *v, socklen_t l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys5(SYS_setsockopt, fd, lv, n, v, l)); }
+#endif
+int getsockopt(int fd, int lv, int n, void *v, socklen_t *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys5(SYS_getsockopt, fd, lv, n, v, l)); }
+#endif
+ssize_t send(int fd, const void *b, size_t n, int fl)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_sendto, fd, (long)b, n, fl, 0, 0)); }
+#endif
+ssize_t recv(int fd, void *b, size_t n, int fl)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_recvfrom, fd, (long)b, n, fl, 0, 0)); }
+#endif
+ssize_t sendto(int fd, const void *b, size_t n, int fl, const struct sockaddr *a, socklen_t l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_sendto, fd, (long)b, n, fl, (long)a, l)); }
+#endif
+ssize_t recvfrom(int fd, void *b, size_t n, int fl, struct sockaddr *a, socklen_t *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_recvfrom, fd, (long)b, n, fl, (long)a, (long)l)); }
+#endif
+ssize_t sendmsg(int fd, const struct msghdr *m, int fl)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_sendmsg, fd, m, fl)); }
+#endif
+ssize_t recvmsg(int fd, struct msghdr *m, int fl)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_recvmsg, fd, m, fl)); }
+#endif
+
+struct pollfd { int fd; short events, revents; };
+#define POLLIN 1
+#define POLLPRI 2
+#define POLLOUT 4
+#define POLLERR 8
+#define POLLHUP 16
+#define POLLNVAL 32
+typedef unsigned long nfds_t;
+int poll(struct pollfd *f, nfds_t n, int ms)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ struct timespec t, *tp = NULL;
+ if (ms >= 0) { t.tv_sec = ms / 1000; t.tv_nsec = (ms % 1000) * 1000000L; tp = &t; }
+ return fail(sys5(SYS_ppoll, f, n, tp, 0, 8));
+}
+#endif
+int ppoll(struct pollfd *f, nfds_t n, const struct timespec *t, const sigset_t *m)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys5(SYS_ppoll, f, n, t, m, 8)); }
+#endif
+#define FD_SETSIZE 1024
+#define C_FDBITS (8 * (int)sizeof(long))
+typedef struct { unsigned long fds_bits[FD_SETSIZE / C_FDBITS]; } fd_set;
+#define FD_ZERO(s) memset((s), 0, sizeof(fd_set))
+#define FD_SET(fd, s) ((s)->fds_bits[(fd) / C_FDBITS] |= 1UL << ((fd) % C_FDBITS))
+#define FD_CLR(fd, s) ((s)->fds_bits[(fd) / C_FDBITS] &= ~(1UL << ((fd) % C_FDBITS)))
+#define FD_ISSET(fd, s) (((s)->fds_bits[(fd) / C_FDBITS] >> ((fd) % C_FDBITS)) & 1)
+int select(int n, fd_set *r, fd_set *w, fd_set *e, struct timeval *tv)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys5(SYS_select, n, r, w, e, tv)); }
+#endif
+int pselect(int n, fd_set *r, fd_set *w, fd_set *e, const struct timespec *t, const sigset_t *m)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ struct timeval tv, *tp = NULL;
+ if (t) { tv.tv_sec = t->tv_sec; tv.tv_usec = t->tv_nsec / 1000; tp = &tv; }
+ (void)m;
+ return select(n, r, w, e, tp);
+}
+#endif
+
+#define EPOLLIN 1
+#define EPOLLOUT 4
+#define EPOLLERR 8
+#define EPOLLHUP 16
+#define EPOLLET (1u << 31)
+#define EPOLL_CTL_ADD 1
+#define EPOLL_CTL_DEL 2
+#define EPOLL_CTL_MOD 3
+#define EPOLL_CLOEXEC 02000000
+struct epoll_event { unsigned events; union { void *ptr; int fd; unsigned u32; unsigned long u64; } data; } __attribute__((packed));
+int epoll_create1(int fl)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(291, fl)); }
+#endif
+int epoll_create(int n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)n; return epoll_create1(0); }
+#endif
+int epoll_ctl(int ep, int op, int fd, struct epoll_event *ev)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys4(233, ep, op, fd, ev)); }
+#endif
+int epoll_wait(int ep, struct epoll_event *ev, int max, int ms)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(232, ep, (long)ev, max, ms, 0, 0)); }
+#endif
+
+int inet_aton(const char *s, struct in_addr *a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ unsigned v = 0;
+ int i;
+ for (i = 0; i < 4; i++) {
+ char *e;
+ unsigned long p = strtoul(s, &e, 10);
+ if (e == s || p > 255) return 0;
+ v = v << 8 | p;
+ if (i < 3) { if (*e != '.') return 0; s = e + 1; } else if (*e) return 0;
+ }
+ a->s_addr = htonl(v);
+ return 1;
+}
+#endif
+in_addr_t inet_addr(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ struct in_addr a; return inet_aton(s, &a) ? a.s_addr : (in_addr_t)-1; }
+#endif
+char *inet_ntoa(struct in_addr a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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; }
+#endif
+#ifndef C_DECLS_ONLY
+static int c_pton6(const char *s, unsigned char *d)
+{
+ unsigned short w[8] = { 0 };
+ int n = 0, gap = -1;
+ if (s[0] == ':' && s[1] == ':') { gap = 0; s += 2; if (!*s) goto done; }
+ for (;;) {
+ char *e;
+ unsigned long v = strtoul(s, &e, 16);
+ if (e == s || e - s > 4 || n >= 8) return 0;
+ w[n++] = v;
+ s = e;
+ if (!*s) break;
+ if (*s != ':') return 0;
+ s++;
+ if (*s == ':') { if (gap >= 0) return 0; gap = n; s++; if (!*s) break; }
+ }
+done:
+ if (gap < 0 && n != 8) return 0;
+ if (gap >= 0) { int k = 8 - n; memmove(w + gap + k, w + gap, (n - gap) * 2); memset(w + gap, 0, k * 2); }
+ for (n = 0; n < 8; n++) { d[2 * n] = w[n] >> 8; d[2 * n + 1] = w[n]; }
+ return 1;
+}
+#endif
+int inet_pton(int af, const char *s, void *d)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ if (af == AF_INET) return inet_aton(s, d) ? 1 : 0;
+ if (af == AF_INET6) return c_pton6(s, d);
+ errno = EAFNOSUPPORT; return -1;
+}
+#endif
+const char *inet_ntop(int af, const void *s, char *d, socklen_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ const unsigned char *p = s;
+ char buf[48];
+ if (af == AF_INET) snprintf(buf, sizeof buf, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
+ else if (af == AF_INET6) {
+ int i, best = -1, bl = 0, cur = -1, cl = 0;
+ char *o = buf;
+ for (i = 0; i < 8; i++) {
+ 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;
+ }
+ if (bl < 2) best = -1;
+ for (i = 0; i < 8; i++) {
+ if (i == best) { *o++ = ':'; i += bl - 1; if (i == 7) *o++ = ':'; continue; }
+ if (i) *o++ = ':';
+ o += sprintf(o, "%x", p[2 * i] << 8 | p[2 * i + 1]);
+ }
+ *o = 0;
+ } else { errno = EAFNOSUPPORT; return NULL; }
+ if (strlen(buf) >= n) { errno = ENOSPC; return NULL; }
+ strcpy(d, buf);
+ return d;
+}
+#endif
+
+/* names: /etc/hosts, then a plain UDP query to the first nameserver in /etc/resolv.conf */
+struct hostent { char *h_name, **h_aliases; int h_addrtype, h_length; char **h_addr_list; };
+#define h_addr h_addr_list[0]
+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; };
+struct servent { char *s_name, **s_aliases; int s_port; char *s_proto; };
+#define AI_PASSIVE 1
+#define AI_CANONNAME 2
+#define AI_NUMERICHOST 4
+#define AI_NUMERICSERV 0x400
+#define AI_ADDRCONFIG 0x20
+#define AI_V4MAPPED 8
+#define AI_ALL 0x10
+#define NI_NUMERICHOST 1
+#define NI_NUMERICSERV 2
+#define NI_NAMEREQD 8
+#define NI_DGRAM 16
+#define NI_MAXHOST 1025
+#define NI_MAXSERV 32
+#define EAI_BADFLAGS (-1)
+#define EAI_NONAME (-2)
+#define EAI_AGAIN (-3)
+#define EAI_FAIL (-4)
+#define EAI_FAMILY (-6)
+#define EAI_SOCKTYPE (-7)
+#define EAI_SERVICE (-8)
+#define EAI_MEMORY (-10)
+#define EAI_SYSTEM (-11)
+#define HOST_NOT_FOUND 1
+#ifdef C_DECLS_ONLY
+extern int h_errno;
+#else
+int h_errno;
+#endif
+#ifndef C_DECLS_ONLY
+static int c_hosts(const char *name, int fam, unsigned char *out, int *outfam)
+{
+ char line[512];
+ FILE *f = fopen("/etc/hosts", "r");
+ if (!f) return 0;
+ while (fgets(line, sizeof line, f)) {
+ char *p = line, *tok, *save;
+ unsigned char a[16];
+ int af;
+ line[strcspn(line, "#\n")] = 0;
+ tok = strtok_r(p, " \t", &save);
+ if (!tok) continue;
+ af = inet_pton(AF_INET, tok, a) == 1 ? AF_INET : inet_pton(AF_INET6, tok, a) == 1 ? AF_INET6 : 0;
+ if (!af || (fam != AF_UNSPEC && fam != af)) continue;
+ while ((tok = strtok_r(NULL, " \t", &save))) if (!strcasecmp(tok, name)) { memcpy(out, a, 16); *outfam = af; fclose(f); return 1; }
+ }
+ fclose(f);
+ return 0;
+}
+#endif
+#ifndef C_DECLS_ONLY
+static int c_dns(const char *name, unsigned char *out)
+{
+ unsigned char q[300], r[600];
+ char line[256], ns[64] = "127.0.0.1";
+ struct sockaddr_in sa;
+ struct timeval tv = { 3, 0 };
+ int fd, n = 0, i, an, pos;
+ FILE *f = fopen("/etc/resolv.conf", "r");
+ if (f) { while (fgets(line, sizeof line, f)) if (!strncmp(line, "nameserver", 10)) { sscanf(line + 10, "%63s", ns); break; } fclose(f); }
+ memset(q, 0, 12);
+ q[0] = 0x43; q[1] = 0x68; q[2] = 1; q[5] = 1;
+ n = 12;
+ while (*name) {
+ const char *dot = strchr(name, '.');
+ int l = dot ? (int)(dot - name) : (int)strlen(name);
+ if (l > 63 || n + l + 6 > (int)sizeof q) return 0;
+ q[n++] = l; memcpy(q + n, name, l); n += l;
+ name += l; if (*name == '.') name++;
+ }
+ q[n++] = 0; q[n++] = 0; q[n++] = 1; q[n++] = 0; q[n++] = 1;
+ memset(&sa, 0, sizeof sa);
+ sa.sin_family = AF_INET; sa.sin_port = htons(53);
+ if (!inet_aton(ns, &sa.sin_addr)) return 0;
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (fd < 0) return 0;
+ setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
+ for (i = 0; i < 2; i++) {
+ long len;
+ sendto(fd, q, n, 0, (struct sockaddr *)&sa, sizeof sa);
+ len = recv(fd, r, sizeof r, 0);
+ if (len < 12 || r[0] != q[0] || r[1] != q[1]) continue;
+ close(fd);
+ if ((r[3] & 15) != 0) return 0;
+ an = r[6] << 8 | r[7];
+ pos = n;
+ while (an-- > 0 && pos < len) {
+ int rl, ty;
+ while (pos < len && r[pos]) { if ((r[pos] & 0xc0) == 0xc0) { pos++; break; } pos += r[pos] + 1; }
+ pos++;
+ if (pos + 10 > len) return 0;
+ ty = r[pos] << 8 | r[pos + 1]; rl = r[pos + 8] << 8 | r[pos + 9]; pos += 10;
+ if (ty == 1 && rl == 4 && pos + 4 <= len) { memcpy(out, r + pos, 4); return 1; }
+ pos += rl;
+ }
+ return 0;
+ }
+ close(fd);
+ return 0;
+}
+#endif
+int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ int fam = hints ? hints->ai_family : AF_UNSPEC, socktype = hints ? hints->ai_socktype : 0, flags = hints ? hints->ai_flags : 0;
+ unsigned char addr[16];
+ int af = 0, port = 0;
+ struct addrinfo *ai;
+ struct sockaddr_in *s4;
+ struct sockaddr_in6 *s6;
+ char *e;
+ if (!node && !service) return EAI_NONAME;
+ if (service) {
+ port = strtol(service, &e, 10);
+ if (*e) {
+ if (flags & AI_NUMERICSERV) return EAI_NONAME;
+ 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;
+ if (!port) return EAI_SERVICE;
+ }
+ }
+ 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; } }
+ else if (inet_pton(AF_INET, node, addr) == 1) af = AF_INET;
+ else if (inet_pton(AF_INET6, node, addr) == 1) af = AF_INET6;
+ else if (flags & AI_NUMERICHOST) return EAI_NONAME;
+ 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; }
+ else if (c_hosts(node, fam, addr, &af)) ;
+ else if (fam != AF_INET6 && c_dns(node, addr)) af = AF_INET;
+ else return EAI_NONAME;
+ if (fam != AF_UNSPEC && fam != af) return EAI_FAMILY;
+ ai = calloc(1, sizeof *ai + sizeof(struct sockaddr_in6) + (node ? strlen(node) + 1 : 1));
+ if (!ai) return EAI_MEMORY;
+ ai->ai_family = af; ai->ai_socktype = socktype ? socktype : SOCK_STREAM;
+ ai->ai_protocol = ai->ai_socktype == SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP;
+ ai->ai_addr = (struct sockaddr *)(ai + 1);
+ 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; }
+ 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; }
+ if ((flags & AI_CANONNAME) && node) { ai->ai_canonname = (char *)(ai + 1) + sizeof(struct sockaddr_in6); strcpy(ai->ai_canonname, node); }
+ *res = ai;
+ return 0;
+}
+#endif
+void freeaddrinfo(struct addrinfo *ai)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ while (ai) { struct addrinfo *n = ai->ai_next; free(ai); ai = n; } }
+#endif
+const char *gai_strerror(int e)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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"; }
+#endif
+int getnameinfo(const struct sockaddr *sa, socklen_t l, char *host, socklen_t hl, char *serv, socklen_t sl, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ (void)l; (void)flags;
+ 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; }
+ if (serv && sl) snprintf(serv, sl, "%d", ntohs(((struct sockaddr_in *)sa)->sin_port));
+ return 0;
+}
+#endif
+struct hostent *gethostbyname(const char *name)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ static struct hostent h;
+ static char *list[2], *al[1], buf[16], nm[256];
+ struct addrinfo hints, *r;
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = AF_INET;
+ if (getaddrinfo(name, NULL, &hints, &r)) { h_errno = HOST_NOT_FOUND; return NULL; }
+ memcpy(buf, &((struct sockaddr_in *)r->ai_addr)->sin_addr, 4);
+ freeaddrinfo(r);
+ strlcpy(nm, name, sizeof nm);
+ list[0] = buf; list[1] = NULL; al[0] = NULL;
+ h.h_name = nm; h.h_aliases = al; h.h_addrtype = AF_INET; h.h_length = 4; h.h_addr_list = list;
+ return &h;
+}
+#endif
+struct servent *getservbyname(const char *n, const char *p)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)n; (void)p; return NULL; }
+#endif
+const char *hstrerror(int e)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)e; return "Unknown host"; }
+#endif
+/* ---- fnmatch, glob, ftw, regex, path names [chamesle] ---- */
+#define FNM_NOMATCH 1
+#define FNM_PATHNAME 1
+#define FNM_NOESCAPE 2
+#define FNM_PERIOD 4
+#define FNM_CASEFOLD 16
+#ifndef C_DECLS_ONLY
+static int c_fn_class(const char *name, int c)
+{
+ if (!strncmp(name, "alpha:", 6)) return isalpha(c);
+ if (!strncmp(name, "digit:", 6)) return isdigit(c);
+ if (!strncmp(name, "alnum:", 6)) return isalnum(c);
+ if (!strncmp(name, "upper:", 6)) return isupper(c);
+ if (!strncmp(name, "lower:", 6)) return islower(c);
+ if (!strncmp(name, "space:", 6)) return isspace(c);
+ if (!strncmp(name, "blank:", 6)) return c == ' ' || c == '\t';
+ if (!strncmp(name, "punct:", 6)) return ispunct(c);
+ if (!strncmp(name, "print:", 6)) return c >= 32 && c < 127;
+ if (!strncmp(name, "graph:", 6)) return c > 32 && c < 127;
+ if (!strncmp(name, "cntrl:", 6)) return iscntrl(c);
+ if (!strncmp(name, "xdigit:", 7)) return isxdigit(c);
+ return 0;
+}
+#endif
+int fnmatch(const char *p, const char *s, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ const char *start = s;
+ for (; *p; p++, s++) {
+ int fold = flags & FNM_CASEFOLD;
+ if (*p == '*') {
+ while (*p == '*') p++;
+ if ((flags & FNM_PERIOD) && *s == '.' && (s == start || ((flags & FNM_PATHNAME) && s[-1] == '/'))) return FNM_NOMATCH;
+ if (!*p) return (flags & FNM_PATHNAME) && strchr(s, '/') ? FNM_NOMATCH : 0;
+ for (;; s++) {
+ if (!fnmatch(p, s, flags & ~FNM_PERIOD)) return 0;
+ if (!*s || ((flags & FNM_PATHNAME) && *s == '/')) return FNM_NOMATCH;
+ }
+ }
+ if (!*s) return FNM_NOMATCH;
+ if ((flags & FNM_PERIOD) && *s == '.' && *p != '.' && (s == start || ((flags & FNM_PATHNAME) && s[-1] == '/'))) return FNM_NOMATCH;
+ if (*p == '?') { if ((flags & FNM_PATHNAME) && *s == '/') return FNM_NOMATCH; continue; }
+ if (*p == '[') {
+ const char *q = p + 1;
+ int neg = 0, ok = 0, c = (unsigned char)*s, first = 1;
+ if (*q == '!' || *q == '^') { neg = 1; q++; }
+ if ((flags & FNM_PATHNAME) && c == '/') return FNM_NOMATCH;
+ while (*q && (*q != ']' || first)) {
+ first = 0;
+ 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; } }
+ if (*q == '\\' && !(flags & FNM_NOESCAPE) && q[1]) q++;
+ if (q[1] == '-' && q[2] && q[2] != ']') {
+ int lo = (unsigned char)q[0], hi = (unsigned char)q[2];
+ if (fold) { if (tolower(c) >= tolower(lo) && tolower(c) <= tolower(hi)) ok = 1; }
+ if (c >= lo && c <= hi) ok = 1;
+ q += 3;
+ } else { if (fold ? tolower(c) == tolower((unsigned char)*q) : c == (unsigned char)*q) ok = 1; q++; }
+ }
+ if (!*q) { if (*s != '[') return FNM_NOMATCH; continue; }
+ if (ok == neg) return FNM_NOMATCH;
+ p = q;
+ continue;
+ }
+ if (*p == '\\' && !(flags & FNM_NOESCAPE) && p[1]) p++;
+ if (fold ? tolower((unsigned char)*p) != tolower((unsigned char)*s) : *p != *s) return FNM_NOMATCH;
+ }
+ return *s ? FNM_NOMATCH : 0;
+}
+#endif
+
+#define GLOB_ERR 1
+#define GLOB_MARK 2
+#define GLOB_NOSORT 4
+#define GLOB_DOOFFS 8
+#define GLOB_NOCHECK 16
+#define GLOB_APPEND 32
+#define GLOB_NOESCAPE 64
+#define GLOB_PERIOD 128
+#define GLOB_BRACE 1024
+#define GLOB_TILDE 4096
+#define GLOB_NOSPACE 1
+#define GLOB_ABORTED 2
+#define GLOB_NOMATCH 3
+typedef struct { size_t gl_pathc; char **gl_pathv; size_t gl_offs; } glob_t;
+#ifndef C_DECLS_ONLY
+static int c_glob_add(glob_t *g, size_t *cap, const char *path)
+{
+ if (g->gl_pathc + g->gl_offs + 2 > *cap) {
+ char **n;
+ *cap = *cap * 2 + 16;
+ n = realloc(g->gl_pathv, *cap * sizeof *n);
+ if (!n) return -1;
+ g->gl_pathv = n;
+ }
+ g->gl_pathv[g->gl_offs + g->gl_pathc] = strdup(path);
+ if (!g->gl_pathv[g->gl_offs + g->gl_pathc]) return -1;
+ g->gl_pathv[g->gl_offs + ++g->gl_pathc] = NULL;
+ return 0;
+}
+#endif
+#ifndef C_DECLS_ONLY
+static int c_glob_walk(const char *dir, const char *pat, int flags, glob_t *g, size_t *cap)
+{
+ char seg[512];
+ const char *rest;
+ size_t l;
+ int magic;
+ DIR *d;
+ struct dirent *e;
+ char **names = NULL;
+ size_t nn = 0, i;
+ while (*pat == '/') pat++;
+ if (!*pat) return c_glob_add(g, cap, dir);
+ l = strcspn(pat, "/");
+ if (l >= sizeof seg) return 0;
+ memcpy(seg, pat, l); seg[l] = 0;
+ rest = pat + l;
+ magic = strpbrk(seg, "*?[") != NULL;
+ if (!magic) {
+ char full[4096];
+ struct stat st;
+ char *u = seg;
+ if (!(flags & GLOB_NOESCAPE)) { char *r = seg, *w = seg; while (*r) { if (*r == '\\' && r[1]) r++; *w++ = *r++; } *w = 0; }
+ snprintf(full, sizeof full, "%s%s%s", dir, !*dir || dir[strlen(dir) - 1] == '/' ? "" : "/", u);
+ if (lstat(full, &st) < 0) return 0;
+ if (*rest && !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode)) return 0;
+ return c_glob_walk(full, rest, flags, g, cap);
+ }
+ d = opendir(*dir ? dir : ".");
+ if (!d) return (flags & GLOB_ERR) ? -2 : 0;
+ while ((e = readdir(d))) {
+ if (e->d_name[0] == '.' && seg[0] != '.' && !(flags & GLOB_PERIOD)) continue;
+ if (fnmatch(seg, e->d_name, (flags & GLOB_NOESCAPE ? FNM_NOESCAPE : 0) | FNM_PERIOD * !(flags & GLOB_PERIOD))) continue;
+ names = realloc(names, (nn + 1) * sizeof *names);
+ names[nn++] = strdup(e->d_name);
+ }
+ closedir(d);
+ for (i = 0; i < nn; i++) {
+ char full[4096];
+ struct stat st;
+ snprintf(full, sizeof full, "%s%s%s", dir, !*dir || dir[strlen(dir) - 1] == '/' ? "" : "/", names[i]);
+ if (*rest && (stat(full, &st) < 0 || !S_ISDIR(st.st_mode))) continue;
+ if (c_glob_walk(full, rest, flags, g, cap) < 0) return -1;
+ }
+ for (i = 0; i < nn; i++) free(names[i]);
+ free(names);
+ return 0;
+}
+#endif
+#ifndef C_DECLS_ONLY
+static int c_cmp_str(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); }
+#endif
+int glob(const char *pat, int flags, int (*err)(const char *, int), glob_t *g)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ size_t cap = 0, start;
+ int r;
+ (void)err;
+ if (!(flags & GLOB_APPEND)) { g->gl_pathc = 0; g->gl_pathv = NULL; if (!(flags & GLOB_DOOFFS)) g->gl_offs = 0; }
+ else cap = g->gl_pathc + g->gl_offs + 2;
+ if (!g->gl_pathv) { g->gl_pathv = calloc(g->gl_offs + 16, sizeof(char *)); cap = g->gl_offs + 16; }
+ start = g->gl_pathc;
+ if (flags & GLOB_BRACE) {
+ const char *o = strchr(pat, '{');
+ if (o) {
+ const char *p = o + 1, *c = NULL;
+ int depth = 1;
+ for (; *p; p++) { if (*p == '{') depth++; else if (*p == '}' && !--depth) { c = p; break; } }
+ if (c) {
+ const char *a = o + 1, *q;
+ int any = 0;
+ depth = 0;
+ for (q = a; q <= c; q++) {
+ if (*q == '{') depth++;
+ else if ((*q == ',' && !depth) || q == c) {
+ char *np = malloc(strlen(pat) + (q - a) + 1);
+ memcpy(np, pat, o - pat);
+ memcpy(np + (o - pat), a, q - a);
+ strcpy(np + (o - pat) + (q - a), c + 1);
+ r = glob(np, (flags & ~GLOB_NOCHECK) | GLOB_APPEND, err, g);
+ free(np);
+ if (r && r != GLOB_NOMATCH) return r;
+ if (!r) any = 1;
+ a = q + 1;
+ } else if (*q == '}') depth--;
+ }
+ if (!any && (flags & GLOB_NOCHECK)) { c_glob_add(g, &cap, pat); return 0; }
+ return any ? 0 : GLOB_NOMATCH;
+ }
+ }
+ }
+ r = pat[0] == '/' ? c_glob_walk("/", pat, flags, g, &cap) : c_glob_walk("", pat, flags, g, &cap);
+ if (r == -2) return GLOB_ABORTED;
+ if (r < 0) return GLOB_NOSPACE;
+ if (g->gl_pathc == start) {
+ if (flags & GLOB_NOCHECK) { c_glob_add(g, &cap, pat); return 0; }
+ return GLOB_NOMATCH;
+ }
+ if (!(flags & GLOB_NOSORT)) qsort(g->gl_pathv + g->gl_offs + start, g->gl_pathc - start, sizeof(char *), c_cmp_str);
+ if (flags & GLOB_MARK) {
+ size_t i;
+ for (i = start; i < g->gl_pathc; i++) {
+ struct stat st;
+ char *s = g->gl_pathv[g->gl_offs + i];
+ 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; }
+ }
+ }
+ return 0;
+}
+#endif
+void globfree(glob_t *g)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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; }
+#endif
+
+char *basename(char *path)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *s;
+ size_t l;
+ if (!path || !*path) return ".";
+ l = strlen(path);
+ while (l > 1 && path[l - 1] == '/') path[--l] = 0;
+ if (l == 1 && *path == '/') return path;
+ s = strrchr(path, '/');
+ return s ? s + 1 : path;
+}
+#endif
+char *dirname(char *path)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ size_t l;
+ if (!path || !*path) return ".";
+ l = strlen(path);
+ while (l > 1 && path[l - 1] == '/') l--;
+ while (l > 0 && path[l - 1] != '/') l--;
+ if (!l) return ".";
+ while (l > 1 && path[l - 1] == '/') l--;
+ path[l] = 0;
+ return path;
+}
+#endif
+
+#define FTW_F 0
+#define FTW_D 1
+#define FTW_DNR 2
+#define FTW_NS 3
+#define FTW_SL 4
+#define FTW_DP 5
+#define FTW_SLN 6
+#define FTW_PHYS 1
+#define FTW_MOUNT 2
+#define FTW_CHDIR 4
+#define FTW_DEPTH 8
+struct FTW { int base, level; };
+#ifndef C_DECLS_ONLY
+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)
+{
+ struct stat st;
+ struct FTW ftw;
+ int type, r;
+ size_t l = strlen(path);
+ char *b = strrchr(path, '/');
+ ftw.base = b ? b - path + 1 : 0; ftw.level = level;
+ if ((flags & FTW_PHYS ? lstat(path, &st) : stat(path, &st)) < 0) {
+ if (!(flags & FTW_PHYS) && lstat(path, &st) == 0) return fn(path, &st, FTW_SLN, &ftw);
+ return fn(path, &st, FTW_NS, &ftw);
+ }
+ type = S_ISDIR(st.st_mode) ? FTW_D : S_ISLNK(st.st_mode) ? FTW_SL : FTW_F;
+ if (type != FTW_D) return fn(path, &st, type, &ftw);
+ if (!(flags & FTW_DEPTH) && (r = fn(path, &st, FTW_D, &ftw))) return r;
+ {
+ DIR *d = opendir(path);
+ struct dirent *e;
+ if (!d) { if (flags & FTW_DEPTH) return fn(path, &st, FTW_DNR, &ftw); return fn(path, &st, FTW_DNR, &ftw); }
+ while ((e = readdir(d))) {
+ char *child;
+ if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue;
+ child = malloc(l + strlen(e->d_name) + 2);
+ sprintf(child, "%s%s%s", path, l && path[l - 1] == '/' ? "" : "/", e->d_name);
+ r = c_nftw_rec(child, fn, depth, flags, level + 1, maxfd);
+ free(child);
+ if (r) { closedir(d); return r; }
+ }
+ closedir(d);
+ }
+ if (flags & FTW_DEPTH) return fn(path, &st, FTW_DP, &ftw);
+ return 0;
+}
+#endif
+int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *p = strdup(path);
+ int r = c_nftw_rec(p, fn, fd, flags, 0, fd);
+ free(p);
+ return r;
+}
+#endif
+#ifndef C_DECLS_ONLY
+static int (*c_ftw_fn)(const char *, const struct stat *, int);
+#endif
+#ifndef C_DECLS_ONLY
+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); }
+#endif
+int ftw(const char *path, int (*fn)(const char *, const struct stat *, int), int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ c_ftw_fn = fn; return nftw(path, c_ftw_adapt, fd, 0); }
+#endif
+
+char *get_current_dir_name(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return getcwd(NULL, 0); }
+#endif
+char *canonicalize_file_name(const char *p)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return realpath(p, NULL); }
+#endif
+
+/* regular expressions: backtracking matcher over a parsed tree. basic and extended, with the usual GNU-ish extras (\w \s \b \< \>) */
+#define REG_EXTENDED 1
+#define REG_ICASE 2
+#define REG_NOSUB 4
+#define REG_NEWLINE 8
+#define REG_NOTBOL 1
+#define REG_NOTEOL 2
+#define REG_STARTEND 4
+#define REG_NOMATCH 1
+#define REG_BADPAT 2
+#define REG_ECOLLATE 3
+#define REG_ECTYPE 4
+#define REG_EESCAPE 5
+#define REG_ESUBREG 6
+#define REG_EBRACK 7
+#define REG_EPAREN 8
+#define REG_EBRACE 9
+#define REG_BADBR 10
+#define REG_ERANGE 11
+#define REG_ESPACE 12
+#define REG_BADRPT 13
+typedef long regoff_t;
+typedef struct { regoff_t rm_so, rm_eo; } regmatch_t;
+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 };
+struct rx_node {
+ int type, min, max, group, c;
+ char *set;
+ struct rx_node *sub, *next, *alt;
+};
+struct rx_inst;
+typedef struct { size_t re_nsub; struct rx_inst *prog; int flags; int nregs, len; } regex_t;
+struct rx_parse { const char *p; int ext, icase, ngroups, err; };
+#ifndef C_DECLS_ONLY
+static struct rx_node *rx_parse_alt(struct rx_parse *ps, int depth);
+#endif
+#ifndef C_DECLS_ONLY
+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; }
+#endif
+#ifndef C_DECLS_ONLY
+static void rx_free(struct rx_node *n)
+{
+ while (n) { struct rx_node *nx = n->next; rx_free(n->sub); rx_free(n->alt); free(n->set); free(n); n = nx; }
+}
+#endif
+#ifndef C_DECLS_ONLY
+static struct rx_node *rx_parse_set(struct rx_parse *ps)
+{
+ struct rx_node *n = rx_new(RX_SET);
+ const char *p = ps->p;
+ int neg = 0, first = 1, c;
+ n->set = calloc(1, 256);
+ if (*p == '^') { neg = 1; p++; }
+ while (*p && (*p != ']' || first)) {
+ first = 0;
+ if (p[0] == '[' && p[1] == ':') {
+ const char *e = strstr(p + 2, ":]");
+ if (e) {
+ char nm[16]; size_t l = e - p - 2;
+ if (l > 8) { ps->err = REG_ECTYPE; return n; }
+ memcpy(nm, p + 2, l); nm[l] = ':'; nm[l + 1] = 0;
+ for (c = 0; c < 256; c++) if (c_fn_class(nm, c)) n->set[c] = 1;
+ p = e + 2;
+ continue;
+ }
+ }
+ c = (unsigned char)*p++;
+ if (*p == '-' && p[1] && p[1] != ']') {
+ int hi = (unsigned char)p[1], i;
+ p += 2;
+ if (hi < c) { ps->err = REG_ERANGE; return n; }
+ for (i = c; i <= hi; i++) n->set[i] = 1;
+ } else n->set[c] = 1;
+ }
+ if (*p != ']') { ps->err = REG_EBRACK; return n; }
+ p++;
+ if (ps->icase) for (c = 0; c < 256; c++) if (n->set[c]) { n->set[tolower(c)] = 1; n->set[toupper(c)] = 1; }
+ if (neg) for (c = 0; c < 256; c++) n->set[c] = !n->set[c];
+ ps->p = p;
+ return n;
+}
+#endif
+#ifndef C_DECLS_ONLY
+static struct rx_node *rx_parse_atom(struct rx_parse *ps, int depth, int *bol_ok)
+{
+ const char *p = ps->p;
+ struct rx_node *n;
+ int ext = ps->ext;
+ (void)bol_ok;
+ if (*p == '(' && ext) {
+ int g = ++ps->ngroups;
+ ps->p = p + 1;
+ n = rx_new(RX_GROUP); n->group = g;
+ n->sub = rx_parse_alt(ps, depth + 1);
+ if (*ps->p != ')') { if (!ps->err) ps->err = REG_EPAREN; return n; }
+ ps->p++;
+ return n;
+ }
+ if (*p == '\\' && !ext && p[1] == '(') {
+ int g = ++ps->ngroups;
+ ps->p = p + 2;
+ n = rx_new(RX_GROUP); n->group = g;
+ n->sub = rx_parse_alt(ps, depth + 1);
+ if (ps->p[0] != '\\' || ps->p[1] != ')') { if (!ps->err) ps->err = REG_EPAREN; return n; }
+ ps->p += 2;
+ return n;
+ }
+ if (*p == '.') { ps->p++; return rx_new(RX_ANY); }
+ if (*p == '[') { ps->p++; return rx_parse_set(ps); }
+ if (*p == '^' && (ext || p == ps->p)) { ps->p++; return rx_new(RX_BOL); }
+ if (*p == '$') { ps->p++; return rx_new(RX_EOL); }
+ if (*p == '\\') {
+ int c = p[1];
+ ps->p += 2;
+ if (!c) { ps->err = REG_EESCAPE; return rx_new(RX_CHAR); }
+ if (c >= '1' && c <= '9') { n = rx_new(RX_BACKREF); n->group = c - '0'; return n; }
+ if (c == 'b') return rx_new(RX_WORDB);
+ if (c == 'B') return rx_new(RX_NWORDB);
+ if (c == '<') return rx_new(RX_WORDS);
+ if (c == '>') return rx_new(RX_WORDE);
+ if (c == 'w' || c == 'W' || c == 's' || c == 'S' || c == 'd' || c == 'D') {
+ int i;
+ n = rx_new(RX_SET); n->set = calloc(1, 256);
+ for (i = 0; i < 256; i++) {
+ int in = (c == 'w' || c == 'W') ? (isalnum(i) || i == '_') : (c == 's' || c == 'S') ? isspace(i) : isdigit(i);
+ n->set[i] = isupper(c) ? !in : in;
+ }
+ return n;
+ }
+ n = rx_new(RX_CHAR); n->c = c == 'n' ? '\n' : c == 't' ? '\t' : c; return n;
+ }
+ ps->p++;
+ n = rx_new(RX_CHAR); n->c = (unsigned char)*p;
+ if (*p == '*' && !ext) { /* a leading star in a basic expression is a plain one */ }
+ return n;
+}
+#endif
+#ifndef C_DECLS_ONLY
+static struct rx_node *rx_parse_seq(struct rx_parse *ps, int depth)
+{
+ struct rx_node *head = NULL, **tail = &head;
+ while (*ps->p && !ps->err) {
+ const char *p = ps->p;
+ struct rx_node *a;
+ int ext = ps->ext;
+ if (ext && (*p == '|' || (*p == ')' && depth > 0))) break;
+ if (!ext && p[0] == '\\' && (p[1] == '|' || (p[1] == ')' && depth > 0))) break;
+ a = rx_parse_atom(ps, depth, NULL);
+ if (ps->err) { rx_free(a); break; }
+ for (;;) {
+ p = ps->p;
+ if (*p == '*' && !(a->type == RX_BOL && !ext)) { a->min = 0; a->max = -1; ps->p++; }
+ else if (ext && *p == '+') { a->min = 1; a->max = -1; ps->p++; }
+ else if (ext && *p == '?') { a->min = 0; a->max = 1; ps->p++; }
+ else if (!ext && p[0] == '\\' && (p[1] == '+' || p[1] == '?')) { a->min = p[1] == '+'; a->max = p[1] == '+' ? -1 : 1; ps->p += 2; }
+ else if ((ext && *p == '{') || (!ext && p[0] == '\\' && p[1] == '{')) {
+ char *e;
+ long lo, hi;
+ p += ext ? 1 : 2;
+ lo = strtol(p, &e, 10);
+ if (e == p) { ps->err = REG_BADBR; break; }
+ hi = lo;
+ if (*e == ',') { p = e + 1; if (*p == '}' || *p == '\\') { hi = -1; e = (char *)p; } else { hi = strtol(p, &e, 10); } }
+ if (ext ? *e != '}' : (e[0] != '\\' || e[1] != '}')) { ps->err = REG_EBRACE; break; }
+ ps->p = e + (ext ? 1 : 2);
+ if (hi >= 0 && hi < lo) { ps->err = REG_BADBR; break; }
+ a->min = lo; a->max = hi;
+ } else break;
+ if (a->type == RX_BOL || a->type == RX_EOL) break;
+ {
+ /* a repeat on a repeat: wrap what we have */
+ if ((ext && (*ps->p == '*' || *ps->p == '+' || *ps->p == '?'))) {
+ struct rx_node *w = rx_new(RX_GROUP);
+ w->group = 0; w->sub = a; a = w;
+ } else break;
+ }
+ }
+ *tail = a; tail = &a->next;
+ }
+ return head;
+}
+#endif
+#ifndef C_DECLS_ONLY
+static struct rx_node *rx_parse_alt(struct rx_parse *ps, int depth)
+{
+ struct rx_node *left = rx_parse_seq(ps, depth), *n;
+ int ext = ps->ext;
+ if ((ext && *ps->p == '|') || (!ext && ps->p[0] == '\\' && ps->p[1] == '|')) {
+ ps->p += ext ? 1 : 2;
+ n = rx_new(RX_ALT);
+ n->sub = left;
+ n->alt = rx_parse_alt(ps, depth);
+ return n;
+ }
+ return left;
+}
+#endif
+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 };
+struct rx_inst { int op, x, y; char *set; };
+struct rx_gen { struct rx_inst *p; int n, cap, nregs, err; int icase; };
+#ifndef C_DECLS_ONLY
+static int rx_emit(struct rx_gen *g, int op, int x, int y, char *set)
+{
+ if (g->n == g->cap) {
+ struct rx_inst *np;
+ if (g->cap > 200000) { g->err = REG_ESPACE; return 0; }
+ g->cap = g->cap * 2 + 32;
+ np = realloc(g->p, g->cap * sizeof *np);
+ if (!np) { g->err = REG_ESPACE; return 0; }
+ g->p = np;
+ }
+ g->p[g->n].op = op; g->p[g->n].x = x; g->p[g->n].y = y; g->p[g->n].set = set;
+ return g->n++;
+}
+#endif
+#ifndef C_DECLS_ONLY
+static void rx_gen_list(struct rx_gen *g, struct rx_node *n);
+#endif
+#ifndef C_DECLS_ONLY
+static void rx_gen_one(struct rx_gen *g, struct rx_node *n)
+{
+ switch (n->type) {
+ case RX_CHAR: rx_emit(g, I_CHAR, n->c, 0, NULL); break;
+ case RX_ANY: rx_emit(g, I_ANY, 0, 0, NULL); break;
+ case RX_SET: rx_emit(g, I_SET, 0, 0, n->set); break;
+ case RX_BOL: rx_emit(g, I_BOL, 0, 0, NULL); break;
+ case RX_EOL: rx_emit(g, I_EOL, 0, 0, NULL); break;
+ case RX_WORDB: rx_emit(g, I_WORDB, 0, 0, NULL); break;
+ case RX_NWORDB: rx_emit(g, I_NWORDB, 0, 0, NULL); break;
+ case RX_WORDS: rx_emit(g, I_WORDS, 0, 0, NULL); break;
+ case RX_WORDE: rx_emit(g, I_WORDE, 0, 0, NULL); break;
+ case RX_BACKREF: rx_emit(g, I_BACKREF, n->group, 0, NULL); break;
+ case RX_GROUP:
+ if (n->group) rx_emit(g, I_SAVE, 2 * n->group, 0, NULL);
+ rx_gen_list(g, n->sub);
+ if (n->group) rx_emit(g, I_SAVE, 2 * n->group + 1, 0, NULL);
+ break;
+ case RX_ALT: {
+ int sp = rx_emit(g, I_SPLIT, 0, 0, NULL), j;
+ if (g->err) return;
+ g->p[sp].x = g->n;
+ rx_gen_list(g, n->sub);
+ j = rx_emit(g, I_JMP, 0, 0, NULL);
+ if (g->err) return;
+ g->p[sp].y = g->n;
+ rx_gen_list(g, n->alt);
+ g->p[j].x = g->n;
+ break;
+ }
+ }
+}
+#endif
+#ifndef C_DECLS_ONLY
+static void rx_gen_rep(struct rx_gen *g, struct rx_node *n)
+{
+ int i, ends[256], ne = 0;
+ struct rx_node one = *n;
+ one.next = NULL;
+ if (n->type == RX_ALT) { one.min = one.max = 1; }
+ for (i = 0; i < n->min && !g->err; i++) rx_gen_one(g, &one);
+ if (n->max < 0) {
+ int l1 = g->n, sp = rx_emit(g, I_SPLIT, 0, 0, NULL), reg = g->nregs++;
+ if (g->err) return;
+ g->p[sp].x = g->n;
+ rx_emit(g, I_MARK, reg, 0, NULL);
+ rx_gen_one(g, &one);
+ rx_emit(g, I_CHK, reg, 0, NULL);
+ rx_emit(g, I_JMP, l1, 0, NULL);
+ g->p[sp].y = g->n;
+ } else {
+ for (i = n->min; i < n->max && !g->err; i++) {
+ int sp = rx_emit(g, I_SPLIT, 0, 0, NULL);
+ if (g->err) return;
+ g->p[sp].x = g->n;
+ if (ne < 256) ends[ne++] = sp;
+ rx_gen_one(g, &one);
+ }
+ for (i = 0; i < ne; i++) g->p[ends[i]].y = g->n;
+ }
+}
+#endif
+#ifndef C_DECLS_ONLY
+static void rx_gen_list(struct rx_gen *g, struct rx_node *n)
+{
+ for (; n && !g->err; n = n->next) {
+ if (n->min == 1 && n->max == 1) rx_gen_one(g, n); else rx_gen_rep(g, n);
+ }
+}
+#endif
+#ifndef C_DECLS_ONLY
+static int rx_nopt(struct rx_node *n) { (void)n; return 0; }
+#endif
+int regcomp(regex_t *re, const char *pat, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ struct rx_parse ps = { pat, flags & REG_EXTENDED, flags & REG_ICASE, 0, 0 };
+ struct rx_gen g = { NULL, 0, 0, 0, 0, flags & REG_ICASE };
+ struct rx_node *tree;
+ (void)rx_nopt;
+ tree = rx_parse_alt(&ps, 0);
+ if (!ps.err && *ps.p) ps.err = REG_EPAREN;
+ if (ps.err) { rx_free(tree); return ps.err; }
+ rx_emit(&g, I_SAVE, 0, 0, NULL);
+ rx_gen_list(&g, tree);
+ rx_emit(&g, I_SAVE, 1, 0, NULL);
+ rx_emit(&g, I_MATCH, 0, 0, NULL);
+ if (g.err) { rx_free(tree); free(g.p); return g.err; }
+ /* sets are owned by the program from here on; give the tree's away without freeing them */
+ {
+ struct rx_inst *pi;
+ 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; }
+ }
+ rx_free(tree);
+ re->prog = g.p; re->len = g.n; re->nregs = g.nregs; re->re_nsub = ps.ngroups; re->flags = flags;
+ return 0;
+}
+#endif
+struct rx_m { const char *s, *end; const struct rx_inst *p; long *cap, *reg; int ncap, icase, newline, notbol, noteol; long steps; };
+#ifndef C_DECLS_ONLY
+static int rx_word(int c) { return isalnum(c) || c == '_'; }
+#endif
+#ifndef C_DECLS_ONLY
+static int rx_exec(struct rx_m *m, int pc, const char *s)
+{
+ for (;;) {
+ const struct rx_inst *i = &m->p[pc];
+ if (++m->steps > 20000000) return 0;
+ switch (i->op) {
+ 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;
+ case I_ANY: if (s < m->end && !(m->newline && *s == '\n')) { s++; pc++; continue; } return 0;
+ case I_SET: if (s < m->end && i->set[(unsigned char)*s]) { s++; pc++; continue; } return 0;
+ case I_JMP: pc = i->x; continue;
+ case I_SPLIT: if (rx_exec(m, i->x, s)) return 1; pc = i->y; continue;
+ case I_SAVE: {
+ long old;
+ if (i->x >= m->ncap) { pc++; continue; }
+ old = m->cap[i->x]; m->cap[i->x] = s - m->s;
+ if (rx_exec(m, pc + 1, s)) return 1;
+ m->cap[i->x] = old;
+ return 0;
+ }
+ case I_MARK: {
+ long old = m->reg[i->x];
+ m->reg[i->x] = s - m->s;
+ if (rx_exec(m, pc + 1, s)) return 1;
+ m->reg[i->x] = old;
+ return 0;
+ }
+ case I_CHK: if (m->reg[i->x] == s - m->s) return 0; pc++; continue;
+ case I_BOL: if ((s == m->s && !m->notbol) || (m->newline && s > m->s && s[-1] == '\n')) { pc++; continue; } return 0;
+ case I_EOL: if ((s == m->end && !m->noteol) || (m->newline && s < m->end && *s == '\n')) { pc++; continue; } return 0;
+ case I_WORDB: case I_NWORDB: case I_WORDS: case I_WORDE: {
+ int a = s > m->s && rx_word((unsigned char)s[-1]), b = s < m->end && rx_word((unsigned char)*s), ok;
+ ok = i->op == I_WORDB ? a != b : i->op == I_NWORDB ? a == b : i->op == I_WORDS ? !a && b : a && !b;
+ if (!ok) return 0;
+ pc++; continue;
+ }
+ case I_BACKREF: {
+ long so, eo;
+ if (2 * i->x + 1 >= m->ncap) return 0;
+ so = m->cap[2 * i->x]; eo = m->cap[2 * i->x + 1];
+ if (so < 0 || eo < 0) return 0;
+ if (s + (eo - so) > m->end) return 0;
+ if (m->icase ? strncasecmp(s, m->s + so, eo - so) : memcmp(s, m->s + so, eo - so)) return 0;
+ s += eo - so; pc++; continue;
+ }
+ case I_MATCH: m->cap[1] = s - m->s; return 1;
+ }
+ return 0;
+ }
+}
+#endif
+int regexec(const regex_t *re, const char *str, size_t nmatch, regmatch_t pmatch[], int eflags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ struct rx_m m;
+ const char *s = str, *end = str + strlen(str);
+ int ncap = 2 * (re->re_nsub + 1), i, found = 0;
+ long *cap = malloc((ncap + re->nregs + 1) * sizeof *cap);
+ if (!cap) return REG_ESPACE;
+ if (eflags & REG_STARTEND) { s = str + pmatch[0].rm_so; end = str + pmatch[0].rm_eo; }
+ m.s = str; m.end = end; m.p = re->prog; m.cap = cap; m.reg = cap + ncap; m.ncap = ncap;
+ 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;
+ for (; s <= end; s++) {
+ for (i = 0; i < ncap + re->nregs; i++) cap[i] = -1;
+ if (rx_exec(&m, 0, s)) { found = 1; break; }
+ if (m.steps > 20000000) break;
+ }
+ if (found && !(re->flags & REG_NOSUB) && pmatch) {
+ size_t k;
+ for (k = 0; k < nmatch; k++) {
+ 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]; }
+ else { pmatch[k].rm_so = -1; pmatch[k].rm_eo = -1; }
+ }
+ }
+ free(cap);
+ return found ? 0 : REG_NOMATCH;
+}
+#endif
+size_t regerror(int code, const regex_t *re, char *buf, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ 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" };
+ const char *m = code >= 0 && code <= REG_BADRPT ? msg[code] : "Unknown error";
+ (void)re;
+ if (n) strlcpy(buf, m, n);
+ return strlen(m) + 1;
+}
+#endif
+void regfree(regex_t *re)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ int i;
+ if (!re->prog) return;
+ for (i = 0; i < re->len; i++) if (re->prog[i].op == I_SET) free(re->prog[i].set);
+ free(re->prog);
+ re->prog = NULL;
+}
+#endif
#endif
diff --git a/rifle.c b/rifle.c
@@ -809,11 +809,11 @@ static int t_nproc(int argc, char **argv)
}
if (!n) {
unsigned long mask[16], limit;
- int i, k;
+ int i;
if (!all && (n = omp_value("OMP_NUM_THREADS"))) goto done;
memset(mask, 0, sizeof mask);
if (sched_getaffinity(0, sizeof mask, mask) < 0) n = 1;
- else for (i = 0; i < 16; i++) for (k = 0; k < 64; k++) if (mask[i] >> k & 1) n++;
+ else for (i = 0; i < 16; i++) n += __builtin_popcountl(mask[i]);
if (!all && (limit = omp_value("OMP_THREAD_LIMIT")) && limit < n) n = limit;
if (!n) n = 1;
}
@@ -7177,12 +7177,26 @@ static int t_tsort(int argc, char **argv)
typedef unsigned long long u64;
+#ifdef __i386__
+static u64 mulmod(u64 a, u64 b, u64 m)
+{
+ u64 r = 0;
+ a %= m;
+ while (b) {
+ if (b & 1) r = r >= m - a ? r - (m - a) : r + a;
+ a = a >= m - a ? a - (m - a) : a + a;
+ b >>= 1;
+ }
+ return r;
+}
+#else
static u64 mulmod(u64 a, u64 b, u64 m)
{
u64 r;
__asm__("mulq %2\n divq %3" : "=&d"(r), "+a"(a) : "r"(b), "r"(m) : "cc");
return r;
}
+#endif
static u64 powmod(u64 b, u64 e, u64 m)
{