commit a9aedf84888f2f1b9e366d909900db53b27a1624
parent 3d9056b23e65c49604b039dd1e367630346dd341
Author: torter <koubbamohamedrayan@gmail.com>
Date: Sat, 14 Nov 2026 23:36:13 +0000
xargs: quotes, -I, -n, -x, exit codes, matched against GNU
Diffstat:
| M | c.h | | | 4784 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
| M | rifle.c | | | 175 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
| M | tests/cases | | | 145 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | tests/run.sh | | | 2 | +- |
4 files changed, 4926 insertions(+), 180 deletions(-)
diff --git a/c.h b/c.h
@@ -71,10 +71,16 @@ typedef __builtin_va_list va_list;
/* ---- system calls ----
* sys6(n, a..f): the number goes in rax, the arguments in rdi rsi rdx r10 r8 r9. written in plain asm so it
* builds the same under gcc and tcc. returns -errno on failure, like the kernel */
+#ifdef C_DECLS_ONLY
+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
+#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
#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)
@@ -113,7 +119,11 @@ __asm__(".text\n.global sys6\nsys6:\n"
#define SYS_getuid 102
/* errno: the libc keeps the last error here. one program, one thread: a plain variable does it */
+#ifdef C_DECLS_ONLY
+extern int errno;
+#else
int errno;
+#endif
#define EPERM 1
#define ENOENT 2
#define ESRCH 3
@@ -159,7 +169,9 @@ int errno;
#define ETIMEDOUT 110
#define ESTALE 116
#define EDQUOT 122
+#ifndef C_DECLS_ONLY
static long fail(long r) { if (r < 0 && r > -4096) { errno = -r; return -1; } return r; }
+#endif
#define O_RDONLY 0
#define O_WRONLY 1
@@ -185,49 +197,176 @@ static long fail(long r) { if (r < 0 && r > -4096) { errno = -r; return -1; } re
#define PROT_EXEC 4
#define _SC_PAGESIZE 30
-ssize_t read(int fd, void *buf, size_t n) { return fail(sys3(SYS_read, fd, buf, n)); }
-ssize_t write(int fd, const void *buf, size_t n) { return fail(sys3(SYS_write, fd, buf, n)); }
+ssize_t read(int fd, void *buf, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_read, fd, buf, n)); }
+#endif
+ssize_t write(int fd, const void *buf, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_write, fd, buf, n)); }
+#endif
int open(const char *path, int flags, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
{
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));
}
-int close(int fd) { return fail(sys1(SYS_close, fd)); }
-off_t lseek(int fd, off_t off, int whence) { return fail(sys3(SYS_lseek, fd, off, whence)); }
-int ioctl(int fd, unsigned long req, void *arg) { return fail(sys3(SYS_ioctl, fd, req, arg)); }
-int mprotect(void *p, size_t n, int prot) { return fail(sys3(SYS_mprotect, p, n, prot)); }
-int unlink(const char *path) { return fail(sys1(SYS_unlink, path)); }
-int mkdir(const char *path, mode_t mode) { return fail(sys2(SYS_mkdir, path, mode)); }
-pid_t fork(void) { return fail(sys0(SYS_fork)); }
-int execve(const char *path, char *const argv[], char *const envp[]) { return fail(sys3(SYS_execve, path, argv, envp)); }
-pid_t waitpid(pid_t pid, int *status, int options) { return fail(sys6(SYS_wait4, pid, (long)status, options, 0, 0, 0)); }
+#endif
+int close(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_close, fd)); }
+#endif
+off_t lseek(int fd, off_t off, int whence)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_lseek, fd, off, whence)); }
+#endif
+int ioctl(int fd, unsigned long req, void *arg)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_ioctl, fd, req, arg)); }
+#endif
+int mprotect(void *p, size_t n, int prot)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_mprotect, p, n, prot)); }
+#endif
+int unlink(const char *path)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_unlink, path)); }
+#endif
+int mkdir(const char *path, mode_t mode)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_mkdir, path, mode)); }
+#endif
+pid_t fork(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys0(SYS_fork)); }
+#endif
+int execve(const char *path, char *const argv[], char *const envp[])
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_execve, path, argv, envp)); }
+#endif
+pid_t waitpid(pid_t pid, int *status, int options)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_wait4, pid, (long)status, options, 0, 0, 0)); }
+#endif
int mount(const char *src, const char *dst, const char *type, unsigned long flags, const void *data)
+#ifdef C_DECLS_ONLY
+;
+#else
{
return fail(sys6(SYS_mount, (long)src, (long)dst, (long)type, flags, (long)data, 0));
}
-char *getcwd(char *buf, size_t n) { return fail(sys2(SYS_getcwd, buf, n)) < 0 ? NULL : buf; }
-time_t time(time_t *t) { time_t now = sys1(SYS_time, 0); if (t) *t = now; return now; }
-long sysconf(int name) { return name == _SC_PAGESIZE ? 4096 : -1; }
-int creat(const char *path, mode_t mode) { return fail(sys2(SYS_creat, path, mode)); }
-int chdir(const char *path) { return fail(sys1(SYS_chdir, path)); }
-int chmod(const char *path, mode_t mode) { return fail(sys2(SYS_chmod, path, mode)); }
-int dup(int fd) { return fail(sys1(SYS_dup, fd)); }
-int pipe(int fd[2]) { return fail(sys1(SYS_pipe, fd)); }
-pid_t getpid(void) { return sys0(SYS_getpid); }
-int getuid(void) { return sys0(SYS_getuid); }
-pid_t wait(int *status) { return waitpid(-1, status, 0); }
+#endif
+char *getcwd(char *buf, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_getcwd, buf, n)) < 0 ? NULL : buf; }
+#endif
+time_t time(time_t *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ time_t now = sys1(SYS_time, 0); if (t) *t = now; return now; }
+#endif
+long sysconf(int name)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return name == _SC_PAGESIZE ? 4096 : -1; }
+#endif
+int creat(const char *path, mode_t mode)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_creat, path, mode)); }
+#endif
+int chdir(const char *path)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_chdir, path)); }
+#endif
+int chmod(const char *path, mode_t mode)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_chmod, path, mode)); }
+#endif
+int dup(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_dup, fd)); }
+#endif
+int pipe(int fd[2])
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_pipe, fd)); }
+#endif
+pid_t getpid(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sys0(SYS_getpid); }
+#endif
+int getuid(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sys0(SYS_getuid); }
+#endif
+pid_t wait(int *status)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return waitpid(-1, status, 0); }
+#endif
/* 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 */
+#ifndef C_DECLS_ONLY
static char *cur_brk;
+#endif
int brk(void *p)
+#ifdef C_DECLS_ONLY
+;
+#else
{
char *r = (char *)sys1(SYS_brk, p);
cur_brk = r;
if (r != p) { errno = ENOMEM; return -1; }
return 0;
}
+#endif
void *sbrk(long n)
+#ifdef C_DECLS_ONLY
+;
+#else
{
char *old;
if (!cur_brk) cur_brk = (char *)sys1(SYS_brk, 0);
@@ -235,6 +374,7 @@ void *sbrk(long n)
if (n && brk(old + n) < 0) return (void *)-1;
return old;
}
+#endif
/* ---- files' details ---- */
struct stat {
@@ -275,8 +415,18 @@ struct stat {
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
-int stat(const char *path, struct stat *st) { return fail(sys2(SYS_stat, path, st)); }
-int fstat(int fd, struct stat *st) { return fail(sys2(SYS_fstat, fd, st)); }
+int stat(const char *path, struct stat *st)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_stat, path, st)); }
+#endif
+int fstat(int fd, struct stat *st)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_fstat, fd, st)); }
+#endif
/* ---- the terminal ---- */
struct termios { unsigned int c_iflag, c_oflag, c_cflag, c_lflag; unsigned char c_line, c_cc[19]; };
@@ -298,9 +448,24 @@ struct termios { unsigned int c_iflag, c_oflag, c_cflag, c_lflag; unsigned char
#define ICANON 02
#define ECHO 010
#define IEXTEN 0100000
-int tcgetattr(int fd, struct termios *t) { return ioctl(fd, TCGETS, t); }
-int tcsetattr(int fd, int when, struct termios *t) { (void)when; return ioctl(fd, TCSETSW, t); }
-int isatty(int fd) { struct termios t; return tcgetattr(fd, &t) == 0; }
+int tcgetattr(int fd, struct termios *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return ioctl(fd, TCGETS, t); }
+#endif
+int tcsetattr(int fd, int when, struct termios *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)when; return ioctl(fd, TCSETSW, t); }
+#endif
+int isatty(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ struct termios t; return tcgetattr(fd, &t) == 0; }
+#endif
/* ---- signals ----
* signal(n, f): f runs when signal n comes. SIG_DFL (0) puts back what the kernel does, SIG_IGN (1) ignores it.
@@ -315,16 +480,31 @@ typedef void (*sighandler_t)(int);
#define SIGKILL 9
#define SIGTERM 15
#define SIGCHLD 17
+#ifdef C_DECLS_ONLY
+extern void c_restore_rt(void);
+#else
void c_restore_rt(void);
+#endif
+#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; };
sighandler_t signal(int n, sighandler_t f)
+#ifdef C_DECLS_ONLY
+;
+#else
{
struct c_sigaction new = { f, 0x04000000 | 0x10000000 /* restorer, restart */, c_restore_rt, 0 }, old;
if (fail(sys6(SYS_rt_sigaction, n, (long)&new, (long)&old, 8, 0, 0)) < 0) return SIG_ERR;
return old.handler;
}
-__attribute__((noreturn)) void _exit(int code) { for (;;) sys1(SYS_exit, code); }
+#endif
+__attribute__((noreturn)) void _exit(int code)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ for (;;) sys1(SYS_exit, code); }
+#endif
/* ---- more system calls [rifle] ---- */
#define SYS_lstat 6
@@ -400,112 +580,468 @@ struct statfs {
int f_fsid[2];
long f_namelen, f_frsize, f_flags, f_spare[4];
};
-int lstat(const char *path, struct stat *st) { return fail(sys2(SYS_lstat, path, st)); }
-int fstatat(int dirfd, const char *path, struct stat *st, int flags) { return fail(sys6(SYS_newfstatat, dirfd, (long)path, (long)st, flags, 0, 0)); }
+int lstat(const char *path, struct stat *st)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_lstat, path, st)); }
+#endif
+int fstatat(int dirfd, const char *path, struct stat *st, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_newfstatat, dirfd, (long)path, (long)st, flags, 0, 0)); }
+#endif
int openat(int dirfd, const char *path, int flags, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
{
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));
}
-int unlinkat(int dirfd, const char *path, int flags) { return fail(sys3(SYS_unlinkat, dirfd, path, flags)); }
-int mkdirat(int dirfd, const char *path, mode_t mode) { return fail(sys3(SYS_mkdirat, dirfd, path, mode)); }
-int rmdir(const char *path) { return fail(sys1(SYS_rmdir, path)); }
-int rename(const char *a, const char *b) { return fail(sys2(SYS_rename, a, b)); }
-int link(const char *a, const char *b) { return fail(sys2(SYS_link, a, b)); }
-int symlink(const char *a, const char *b) { return fail(sys2(SYS_symlink, a, b)); }
-ssize_t readlink(const char *path, char *buf, size_t n) { return fail(sys3(SYS_readlink, path, buf, n)); }
-int fchmod(int fd, mode_t mode) { return fail(sys2(SYS_fchmod, fd, mode)); }
-int chown(const char *path, uid_t u, gid_t g) { return fail(sys3(SYS_chown, path, u, g)); }
-int lchown(const char *path, uid_t u, gid_t g) { return fail(sys3(SYS_lchown, path, u, g)); }
-int fchown(int fd, uid_t u, gid_t g) { return fail(sys3(SYS_fchown, fd, u, g)); }
-mode_t umask(mode_t m) { return sys1(SYS_umask, m); }
-int access(const char *path, int mode) { return fail(sys2(SYS_access, path, mode)); }
-int truncate(const char *path, off_t n) { return fail(sys2(SYS_truncate, path, n)); }
-int ftruncate(int fd, off_t n) { return fail(sys2(SYS_ftruncate, fd, n)); }
-int fsync(int fd) { return fail(sys1(SYS_fsync, fd)); }
-int fdatasync(int fd) { return fail(sys1(SYS_fdatasync, fd)); }
-void sync(void) { sys0(SYS_sync); }
-int syncfs(int fd) { return fail(sys1(SYS_syncfs, fd)); }
-int mknod(const char *path, mode_t mode, dev_t dev) { return fail(sys3(SYS_mknod, path, mode, dev)); }
-int mkfifo(const char *path, mode_t mode) { return mknod(path, (mode & 07777) | S_IFIFO, 0); }
-int dup2(int a, int b) { return fail(sys2(SYS_dup2, a, b)); }
-int fcntl(int fd, int cmd, long arg) { return fail(sys3(SYS_fcntl, fd, cmd, arg)); }
-int fchdir(int fd) { return fail(sys1(SYS_fchdir, fd)); }
-int kill(pid_t pid, int sig) { return fail(sys2(SYS_kill, pid, sig)); }
-int uname(struct utsname *u) { return fail(sys1(SYS_uname, u)); }
-int sethostname(const char *name, size_t n) { return fail(sys2(SYS_sethostname, name, n)); }
-int nanosleep(const struct timespec *req, struct timespec *rem) { return fail(sys2(SYS_nanosleep, req, rem)); }
-int clock_gettime(int clk, struct timespec *t) { return fail(sys2(SYS_clock_gettime, clk, t)); }
-int utimensat(int dirfd, const char *path, const struct timespec t[2], int flags) { return fail(sys6(SYS_utimensat, dirfd, (long)path, (long)t, flags, 0, 0)); }
-int statfs(const char *path, struct statfs *s) { return fail(sys2(SYS_statfs, path, s)); }
-pid_t getppid(void) { return sys0(SYS_getppid); }
-uid_t geteuid(void) { return sys0(SYS_geteuid); }
-gid_t getgid(void) { return sys0(SYS_getgid); }
-gid_t getegid(void) { return sys0(SYS_getegid); }
-int getgroups(int n, gid_t *list) { return fail(sys2(SYS_getgroups, n, list)); }
-int sched_getaffinity(pid_t pid, size_t n, void *mask) { return fail(sys3(SYS_sched_getaffinity, pid, n, mask)); }
-ssize_t getrandom(void *buf, size_t n, unsigned flags) { return fail(sys3(SYS_getrandom, buf, n, flags)); }
-ssize_t pread(int fd, void *buf, size_t n, off_t off) { return fail(sys6(SYS_pread64, fd, (long)buf, n, off, 0, 0)); }
-ssize_t pwrite(int fd, const void *buf, size_t n, off_t off) { return fail(sys6(SYS_pwrite64, fd, (long)buf, n, off, 0, 0)); }
-int setuid(uid_t u) { return fail(sys1(SYS_setuid, u)); }
-int setgid(gid_t g) { return fail(sys1(SYS_setgid, g)); }
-int chroot(const char *path) { return fail(sys1(SYS_chroot, path)); }
+#endif
+int unlinkat(int dirfd, const char *path, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_unlinkat, dirfd, path, flags)); }
+#endif
+int mkdirat(int dirfd, const char *path, mode_t mode)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_mkdirat, dirfd, path, mode)); }
+#endif
+int rmdir(const char *path)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_rmdir, path)); }
+#endif
+int rename(const char *a, const char *b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_rename, a, b)); }
+#endif
+int link(const char *a, const char *b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_link, a, b)); }
+#endif
+int symlink(const char *a, const char *b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_symlink, a, b)); }
+#endif
+ssize_t readlink(const char *path, char *buf, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_readlink, path, buf, n)); }
+#endif
+int fchmod(int fd, mode_t mode)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_fchmod, fd, mode)); }
+#endif
+int chown(const char *path, uid_t u, gid_t g)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_chown, path, u, g)); }
+#endif
+int lchown(const char *path, uid_t u, gid_t g)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_lchown, path, u, g)); }
+#endif
+int fchown(int fd, uid_t u, gid_t g)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_fchown, fd, u, g)); }
+#endif
+mode_t umask(mode_t m)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sys1(SYS_umask, m); }
+#endif
+int access(const char *path, int mode)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_access, path, mode)); }
+#endif
+int truncate(const char *path, off_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_truncate, path, n)); }
+#endif
+int ftruncate(int fd, off_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_ftruncate, fd, n)); }
+#endif
+int fsync(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_fsync, fd)); }
+#endif
+int fdatasync(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_fdatasync, fd)); }
+#endif
+void sync(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ sys0(SYS_sync); }
+#endif
+int syncfs(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_syncfs, fd)); }
+#endif
+int mknod(const char *path, mode_t mode, dev_t dev)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_mknod, path, mode, dev)); }
+#endif
+int mkfifo(const char *path, mode_t mode)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return mknod(path, (mode & 07777) | S_IFIFO, 0); }
+#endif
+int dup2(int a, int b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_dup2, a, b)); }
+#endif
+int fcntl(int fd, int cmd, long arg)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_fcntl, fd, cmd, arg)); }
+#endif
+int fchdir(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_fchdir, fd)); }
+#endif
+int kill(pid_t pid, int sig)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_kill, pid, sig)); }
+#endif
+int uname(struct utsname *u)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_uname, u)); }
+#endif
+int sethostname(const char *name, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_sethostname, name, n)); }
+#endif
+int nanosleep(const struct timespec *req, struct timespec *rem)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_nanosleep, req, rem)); }
+#endif
+int clock_gettime(int clk, struct timespec *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_clock_gettime, clk, t)); }
+#endif
+int utimensat(int dirfd, const char *path, const struct timespec t[2], int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_utimensat, dirfd, (long)path, (long)t, flags, 0, 0)); }
+#endif
+int statfs(const char *path, struct statfs *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_statfs, path, s)); }
+#endif
+pid_t getppid(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sys0(SYS_getppid); }
+#endif
+uid_t geteuid(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sys0(SYS_geteuid); }
+#endif
+gid_t getgid(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sys0(SYS_getgid); }
+#endif
+gid_t getegid(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sys0(SYS_getegid); }
+#endif
+int getgroups(int n, gid_t *list)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_getgroups, n, list)); }
+#endif
+int sched_getaffinity(pid_t pid, size_t n, void *mask)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_sched_getaffinity, pid, n, mask)); }
+#endif
+ssize_t getrandom(void *buf, size_t n, unsigned flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_getrandom, buf, n, flags)); }
+#endif
+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, off, 0, 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, off, 0, 0)); }
+#endif
+int setuid(uid_t u)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_setuid, u)); }
+#endif
+int setgid(gid_t g)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_setgid, g)); }
+#endif
+int chroot(const char *path)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_chroot, path)); }
+#endif
#define major(d) ((unsigned)((((d) >> 32) & 0xfffff000) | (((d) >> 8) & 0xfff)))
#define minor(d) ((unsigned)((((d) >> 12) & 0xffffff00) | ((d) & 0xff)))
#define makedev(ma, mi) ((dev_t)((((unsigned long)(ma) & 0xfffff000UL) << 32) | (((unsigned long)(ma) & 0xfffUL) << 8) | \
(((unsigned long)(mi) & 0xffffff00UL) << 12) | ((unsigned long)(mi) & 0xffUL)))
/* ---- characters ---- */
-int isdigit(int c) { return c >= '0' && c <= '9'; }
-int isalpha(int c) { return (c | 32) >= 'a' && (c | 32) <= 'z'; }
-int isalnum(int c) { return isalpha(c) || isdigit(c); }
-int isspace(int c) { return c == ' ' || (c >= '\t' && c <= '\r'); }
-int isupper(int c) { return c >= 'A' && c <= 'Z'; }
-int islower(int c) { return c >= 'a' && c <= 'z'; }
-int isxdigit(int c) { return isdigit(c) || ((c | 32) >= 'a' && (c | 32) <= 'f'); }
-int isprint(int c) { return c >= 32 && c < 127; }
-int ispunct(int c) { return isprint(c) && c != ' ' && !isalnum(c); } /* [rifle] */
-int toupper(int c) { return islower(c) ? c - 32 : c; }
-int tolower(int c) { return isupper(c) ? c + 32 : c; }
+int isdigit(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c >= '0' && c <= '9'; }
+#endif
+int isalpha(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (c | 32) >= 'a' && (c | 32) <= 'z'; }
+#endif
+int isalnum(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isalpha(c) || isdigit(c); }
+#endif
+int isspace(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c == ' ' || (c >= '\t' && c <= '\r'); }
+#endif
+int isupper(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c >= 'A' && c <= 'Z'; }
+#endif
+int islower(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c >= 'a' && c <= 'z'; }
+#endif
+int isxdigit(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isdigit(c) || ((c | 32) >= 'a' && (c | 32) <= 'f'); }
+#endif
+int isprint(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c >= 32 && c < 127; }
+#endif
+int ispunct(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isprint(c) && c != ' ' && !isalnum(c); }
+#endif /* [rifle] */
+int toupper(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return islower(c) ? c - 32 : c; }
+#endif
+int tolower(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isupper(c) ? c + 32 : c; }
+#endif
/* ---- strings ---- */
-size_t strlen(const char *s) { size_t n = 0; while (s[n]) n++; return n; }
-int strcmp(const char *a, const char *b) { while (*a && *a == *b) a++, b++; return (unsigned char)*a - (unsigned char)*b; }
+size_t strlen(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ size_t n = 0; while (s[n]) n++; return n; }
+#endif
+int strcmp(const char *a, const char *b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ while (*a && *a == *b) a++, b++; return (unsigned char)*a - (unsigned char)*b; }
+#endif
int strncmp(const char *a, const char *b, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
{
for (; n && *a && *a == *b; n--) a++, b++;
return n ? (unsigned char)*a - (unsigned char)*b : 0;
}
-char *strcpy(char *d, const char *s) { char *r = d; while ((*d++ = *s++)); return r; }
-char *strncpy(char *d, const char *s, size_t n) { size_t i = 0; for (; i < n && s[i]; i++) d[i] = s[i]; for (; i < n; i++) d[i] = 0; return d; }
-char *strcat(char *d, const char *s) { strcpy(d + strlen(d), s); return d; }
-char *strchr(const char *s, int c) { for (;; s++) { if (*s == (char)c) return (char *)s; if (!*s) return NULL; } }
-char *strrchr(const char *s, int c) { const char *r = NULL; for (;; s++) { if (*s == (char)c) r = s; if (!*s) return (char *)r; } }
+#endif
+char *strcpy(char *d, const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ char *r = d; while ((*d++ = *s++)); return r; }
+#endif
+char *strncpy(char *d, const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ size_t i = 0; for (; i < n && s[i]; i++) d[i] = s[i]; for (; i < n; i++) d[i] = 0; return d; }
+#endif
+char *strcat(char *d, const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ strcpy(d + strlen(d), s); return d; }
+#endif
+char *strchr(const char *s, int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ for (;; s++) { if (*s == (char)c) return (char *)s; if (!*s) return NULL; } }
+#endif
+char *strrchr(const char *s, int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ const char *r = NULL; for (;; s++) { if (*s == (char)c) r = s; if (!*s) return (char *)r; } }
+#endif
char *strstr(const char *h, const char *n)
+#ifdef C_DECLS_ONLY
+;
+#else
{
size_t l = strlen(n);
for (; *h; h++) if (!strncmp(h, n, l)) return (char *)h;
return l ? NULL : (char *)h;
}
-char *strpbrk(const char *s, const char *set) { for (; *s; s++) if (strchr(set, *s)) return (char *)s; return NULL; }
-void *memcpy(void *d, const void *s, size_t n) { char *a = d; const char *b = s; while (n--) *a++ = *b++; return d; }
+#endif
+char *strpbrk(const char *s, const char *set)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ for (; *s; s++) if (strchr(set, *s)) return (char *)s; return NULL; }
+#endif
+void *memcpy(void *d, const void *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ char *a = d; const char *b = s; while (n--) *a++ = *b++; return d; }
+#endif
void *memmove(void *d, const void *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
{
char *a = d; const char *b = s;
if (a < b) while (n--) *a++ = *b++;
else while (n--) a[n] = b[n];
return d;
}
-void *memset(void *d, int c, size_t n) { char *a = d; while (n--) *a++ = c; return d; }
+#endif
+void *memset(void *d, int c, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ char *a = d; while (n--) *a++ = c; return d; }
+#endif
int memcmp(const void *x, const void *y, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
{
const unsigned char *a = x, *b = y;
for (; n; n--, a++, b++) if (*a != *b) return *a - *b;
return 0;
}
-void *memchr(const void *s, int c, size_t n) { const unsigned char *p = s; for (; n; n--, p++) if (*p == (unsigned char)c) return (void *)p; return NULL; }
+#endif
+void *memchr(const void *s, int c, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ const unsigned char *p = s; for (; n; n--, p++) if (*p == (unsigned char)c) return (void *)p; return NULL; }
+#endif
/* the messages are glibc's, so what rifle says matches what everyone's scripts expect [rifle] */
+#ifndef C_DECLS_ONLY
static const struct { int e; const char *m; } c_errlist[] = {
{ EPERM, "Operation not permitted" }, { ENOENT, "No such file or directory" }, { ESRCH, "No such process" },
{ EINTR, "Interrupted system call" }, { EIO, "Input/output error" }, { ENXIO, "No such device or address" },
@@ -525,8 +1061,16 @@ static const struct { int e; const char *m; } c_errlist[] = {
{ EILSEQ, "Invalid or incomplete multibyte or wide character" }, { EOPNOTSUPP, "Operation not supported" },
{ ETIMEDOUT, "Connection timed out" }, { ESTALE, "Stale file handle" }, { EDQUOT, "Disk quota exceeded" },
};
+#endif
+#ifdef C_DECLS_ONLY
+extern int snprintf(char *buf, size_t cap, const char *fmt, ...);
+#else
int snprintf(char *buf, size_t cap, const char *fmt, ...);
+#endif
char *strerror(int e)
+#ifdef C_DECLS_ONLY
+;
+#else
{
static char unknown[32];
unsigned i;
@@ -534,19 +1078,29 @@ char *strerror(int e)
snprintf(unknown, sizeof unknown, "Unknown error %d", e);
return unknown;
}
+#endif
/* ---- memory ----
* [rifle] small blocks (up to 2032 bytes) come from 256K chunks and go on a list for their size when freed, big
* blocks are their own mmap and grow with mremap. every block has 16 bytes in front of it holding its size. */
#define MALLOC_BIG 2048
+#ifndef C_DECLS_ONLY
static void *malloc_lists[12];
+#endif
+#ifndef C_DECLS_ONLY
static char *malloc_cur, *malloc_end;
+#endif
+#ifndef C_DECLS_ONLY
static void *c_mmap(size_t len)
{
long p = sys6(SYS_mmap, 0, len, PROT_READ | PROT_WRITE, 0x22 /* private, anonymous */, -1, 0);
return (p < 0 && p > -4096) ? NULL : (void *)p;
}
+#endif
void *malloc(size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
{
size_t need = n + 16;
if (need < n) { errno = ENOMEM; return NULL; }
@@ -571,7 +1125,11 @@ void *malloc(size_t n)
*(size_t *)p = len;
return p + 16; /* 16: what a long double or an SSE value wants */
}
+#endif
void free(void *p)
+#ifdef C_DECLS_ONLY
+;
+#else
{
if (!p) return;
char *h = (char *)p - 16;
@@ -582,7 +1140,11 @@ void free(void *p)
*(void **)p = malloc_lists[cl]; malloc_lists[cl] = h;
} else sys2(SYS_munmap, h, sz);
}
+#endif
void *calloc(size_t n, size_t m)
+#ifdef C_DECLS_ONLY
+;
+#else
{
size_t total = n * m;
if (m && total / m != n) { errno = ENOMEM; return NULL; }
@@ -590,7 +1152,11 @@ void *calloc(size_t n, size_t m)
if (p && total + 16 <= MALLOC_BIG) memset(p, 0, total); /* big blocks come from mmap, already zero */
return p;
}
+#endif
void *realloc(void *p, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
{
if (!p) return malloc(n);
char *h = (char *)p - 16;
@@ -607,24 +1173,79 @@ void *realloc(void *p, size_t n)
if (q) { memcpy(q, p, old); free(p); }
return q;
}
-size_t strspn(const char *s, const char *set) { size_t n = 0; while (s[n] && strchr(set, s[n])) n++; return n; } /* [rifle] */
-size_t strcspn(const char *s, const char *set) { size_t n = 0; while (s[n] && !strchr(set, s[n])) n++; return n; }
-int strcasecmp(const char *a, const char *b) { while (*a && tolower(*a) == tolower(*b)) a++, b++; return tolower((unsigned char)*a) - tolower((unsigned char)*b); }
+#endif
+size_t strspn(const char *s, const char *set)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ size_t n = 0; while (s[n] && strchr(set, s[n])) n++; return n; }
+#endif /* [rifle] */
+size_t strcspn(const char *s, const char *set)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ size_t n = 0; while (s[n] && !strchr(set, s[n])) n++; return n; }
+#endif
+int strcasecmp(const char *a, const char *b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ while (*a && tolower(*a) == tolower(*b)) a++, b++; return tolower((unsigned char)*a) - tolower((unsigned char)*b); }
+#endif
int strncasecmp(const char *a, const char *b, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
{
for (; n && *a && tolower(*a) == tolower(*b); n--) a++, b++;
return n ? tolower((unsigned char)*a) - tolower((unsigned char)*b) : 0;
}
-void *memrchr(const void *s, int c, size_t n) { const unsigned char *p = (const unsigned char *)s + n; while (n--) if (*--p == (unsigned char)c) return (void *)p; return NULL; }
-char *strndup(const char *s, size_t n) { 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; }
-char *stpcpy(char *d, const char *s) { while ((*d = *s++)) d++; return d; }
-int abs(int x) { return x < 0 ? -x : x; }
-long labs(long x) { return x < 0 ? -x : x; }
-char *strdup(const char *s) { char *d = malloc(strlen(s) + 1); return d ? strcpy(d, s) : NULL; }
+#endif
+void *memrchr(const void *s, int c, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ const unsigned char *p = (const unsigned char *)s + n; while (n--) if (*--p == (unsigned char)c) return (void *)p; return NULL; }
+#endif
+char *strndup(const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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; }
+#endif
+char *stpcpy(char *d, const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ while ((*d = *s++)) d++; return d; }
+#endif
+int abs(int x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return x < 0 ? -x : x; }
+#endif
+long labs(long x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return x < 0 ? -x : x; }
+#endif
+char *strdup(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ char *d = malloc(strlen(s) + 1); return d ? strcpy(d, s) : NULL; }
+#endif
/* ---- numbers from text ---- */
+#ifndef C_DECLS_ONLY
static int digit_of(int c) { return isdigit(c) ? c - '0' : isalpha(c) ? (c | 32) - 'a' + 10 : 99; }
+#endif
unsigned long long strtoull(const char *s, char **end, int base)
+#ifdef C_DECLS_ONLY
+;
+#else
{
const char *p = s; unsigned long long v = 0; int neg = 0, any = 0;
while (isspace(*p)) p++;
@@ -635,22 +1256,65 @@ unsigned long long strtoull(const char *s, char **end, int base)
if (end) *end = (char *)(any ? p : s);
return neg ? -v : v;
}
-long long strtoll(const char *s, char **end, int base) { return (long long)strtoull(s, end, base); }
-unsigned long strtoul(const char *s, char **end, int base) { return strtoull(s, end, base); }
-long strtol(const char *s, char **end, int base) { return strtoull(s, end, base); }
-int atoi(const char *s) { return strtol(s, NULL, 10); }
-long atol(const char *s) { return strtol(s, NULL, 10); }
-long long atoll(const char *s) { return strtoll(s, NULL, 10); }
+#endif
+long long strtoll(const char *s, char **end, int base)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (long long)strtoull(s, end, base); }
+#endif
+unsigned long strtoul(const char *s, char **end, int base)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strtoull(s, end, base); }
+#endif
+long strtol(const char *s, char **end, int base)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strtoull(s, end, base); }
+#endif
+int atoi(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strtol(s, NULL, 10); }
+#endif
+long atol(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strtol(s, NULL, 10); }
+#endif
+long long atoll(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strtoll(s, NULL, 10); }
+#endif
long double ldexpl(long double x, int e)
+#ifdef C_DECLS_ONLY
+;
+#else
{
while (e > 0) { x *= 2; e--; }
while (e < 0) { x /= 2; e++; }
return x;
}
-double ldexp(double x, int e) { return ldexpl(x, e); }
+#endif
+double ldexp(double x, int e)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return ldexpl(x, e); }
+#endif
/* decimal and hex floating point. good to the last digit or so, not correctly rounded in every case */
long double strtold(const char *s, char **end)
+#ifdef C_DECLS_ONLY
+;
+#else
{
const char *p = s; long double v = 0; int neg = 0, any = 0, ex = 0;
while (isspace(*p)) p++;
@@ -671,13 +1335,34 @@ long double strtold(const char *s, char **end)
if (end) *end = (char *)(any ? p : s);
return neg ? -v : v;
}
-double strtod(const char *s, char **end) { return strtold(s, end); }
-float strtof(const char *s, char **end) { return strtold(s, end); }
-double atof(const char *s) { return strtod(s, NULL); }
+#endif
+double strtod(const char *s, char **end)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strtold(s, end); }
+#endif
+float strtof(const char *s, char **end)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strtold(s, end); }
+#endif
+double atof(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strtod(s, NULL); }
+#endif
/* ---- sorting ---- */
+#ifndef C_DECLS_ONLY
static void swap_bytes(char *a, char *b, size_t n) { while (n--) { char t = *a; *a++ = *b; *b++ = t; } }
+#endif
void qsort(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *))
+#ifdef C_DECLS_ONLY
+;
+#else
{
/* shell sort: short, no recursion, fine for the sizes a compiler sorts */
char *b = base; size_t gap, i, j;
@@ -686,11 +1371,21 @@ void qsort(void *base, size_t n, size_t size, int (*cmp)(const void *, const voi
for (j = i; j >= gap && cmp(b + (j - gap) * size, b + j * size) > 0; j -= gap)
swap_bytes(b + (j - gap) * size, b + j * size, size);
}
+#endif
/* ---- jumps ---- */
typedef long jmp_buf[8];
+#ifdef C_DECLS_ONLY
+extern int setjmp(jmp_buf b);
+#else
int setjmp(jmp_buf b);
+#endif
+#ifdef C_DECLS_ONLY
+extern __attribute__((noreturn)) void longjmp(jmp_buf b, int v);
+#else
__attribute__((noreturn)) void longjmp(jmp_buf b, int v);
+#endif
+#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"
" mov %r15,40(%rdi)\n lea 8(%rsp),%rdx\n mov %rdx,48(%rdi)\n mov (%rsp),%rdx\n mov %rdx,56(%rdi)\n"
@@ -699,11 +1394,15 @@ __asm__(".text\n.global setjmp\n.global _setjmp\nsetjmp:\n_setjmp:\n"
" mov %esi,%eax\n test %eax,%eax\n jnz 1f\n inc %eax\n1:\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
/* ---- time ---- */
struct tm { int tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst; };
/* there are no time zones here: local time is UTC */
struct tm *localtime(const time_t *t)
+#ifdef C_DECLS_ONLY
+;
+#else
{
static struct tm r;
long s = *t, days = s / 86400, rem = s % 86400, y = 1970;
@@ -724,103 +1423,292 @@ struct tm *localtime(const time_t *t)
r.tm_mday = days + 1; r.tm_isdst = 0;
return &r;
}
-struct tm *gmtime(const time_t *t) { return localtime(t); }
+#endif
+struct tm *gmtime(const time_t *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return localtime(t); }
+#endif
struct timeval { long tv_sec, tv_usec; };
-int gettimeofday(struct timeval *tv, void *tz) { (void)tz; return fail(sys2(SYS_gettimeofday, tv, 0)); }
+int gettimeofday(struct timeval *tv, void *tz)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)tz; return fail(sys2(SYS_gettimeofday, tv, 0)); }
+#endif
-/* ---- files with buffers ---- */
-typedef struct FILE { int fd, write, err, eof, n, pos; char buf[4096]; int line; } FILE; /* [rifle] line: 0 not asked yet, 1 yes, 2 no */
-static FILE files_std[3] = { { 0 }, { 1, 1 }, { 2, 1 } }; /* the rest of each is zero */
+/* ---- files with buffers ----
+ * a FILE reads or writes through one buffer. write: n bytes wait to go out. read: [pos, n) is what is left.
+ * ug is a pushed back character plus one, mode is 0 full, 1 line, 2 none buffered, 3 not asked yet */
+typedef struct FILE { int fd, write, err, eof, n, pos, line, ug, mode, rd, wr, pid, append; struct FILE *next; char buf[4096]; } FILE;
+typedef long fpos_t;
+#define _IOFBF 0
+#define _IOLBF 1
+#define _IONBF 2
+#ifndef C_DECLS_ONLY
+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 } };
+#endif
+#ifndef C_DECLS_ONLY
+static FILE *c_files;
+#endif
+/*nosplit*/
+#ifdef C_DECLS_ONLY
+extern FILE *stdin, *stdout, *stderr;
+#else
+#ifdef C_DECLS_ONLY
+extern FILE *stdin;
+#else
FILE *stdin = &files_std[0], *stdout = &files_std[1], *stderr = &files_std[2];
+#endif
+#endif
#define EOF (-1)
#define BUFSIZ 4096
+#define FILENAME_MAX 4096
+#define FOPEN_MAX 16
+#define L_tmpnam 20
+#define TMP_MAX 238328
-int fflush(FILE *f)
+#ifndef C_DECLS_ONLY
+static int flush_one(FILE *f)
{
- if (f && f->write && f->n) {
+ if (f->write && f->n) {
int off = 0;
- while (off < f->n) { ssize_t w = write(f->fd, f->buf + off, f->n - off); if (w <= 0) { f->err = 1; break; } off += w; }
+ 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; }
f->n = 0;
}
- return f && f->err ? EOF : 0;
+ return f->err ? EOF : 0;
+}
+#endif
+int fflush(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ if (!f) {
+ FILE *p;
+ int r = 0;
+ for (p = c_files; p; p = p->next) if (flush_one(p)) r = EOF;
+ for (p = files_std; p < files_std + 3; p++) if (flush_one(p)) r = EOF;
+ return r;
+ }
+ if (!f->write && f->n > f->pos && f->rd && !f->wr) return 0;
+ return flush_one(f);
+}
+#endif
+/* going from writing to reading, or back: whatever is buffered goes first, or the file position is put right */
+#ifndef C_DECLS_ONLY
+static int switch_to(FILE *f, int writing)
+{
+ if (writing && !f->write) {
+ if (f->n > f->pos || f->ug) lseek(f->fd, -(long)(f->n - f->pos) - (f->ug ? 1 : 0), SEEK_CUR);
+ f->n = f->pos = 0; f->ug = 0; f->write = 1;
+ } else if (!writing && f->write) {
+ if (flush_one(f)) return -1;
+ f->n = f->pos = 0; f->write = 0;
+ }
+ return 0;
}
+#endif
+#ifndef C_DECLS_ONLY
static int open_flags(const char *mode)
{
int f = mode[0] == 'r' ? O_RDONLY : mode[0] == 'w' ? O_WRONLY | O_CREAT | O_TRUNC : O_WRONLY | O_CREAT | O_APPEND;
+ const char *m;
if (strchr(mode, '+')) f = (f & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
+ for (m = mode; *m; m++) { if (*m == 'x') f |= O_EXCL; else if (*m == 'e') f |= O_CLOEXEC; }
return f;
}
+#endif
FILE *fdopen(int fd, const char *mode)
+#ifdef C_DECLS_ONLY
+;
+#else
{
FILE *f = calloc(1, sizeof *f);
if (!f) return NULL;
- f->fd = fd; f->write = mode[0] != 'r' || strchr(mode, '+');
+ f->fd = fd; f->mode = 3;
+ f->rd = mode[0] == 'r' || strchr(mode, '+') != NULL;
+ f->wr = mode[0] != 'r' || strchr(mode, '+') != NULL;
+ f->write = !f->rd;
+ f->append = mode[0] == 'a';
+ f->next = c_files; c_files = f;
return f;
}
+#endif
FILE *fopen(const char *path, const char *mode)
+#ifdef C_DECLS_ONLY
+;
+#else
{
- int fd = open(path, open_flags(mode), 0666);
+ int fd;
+ if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { errno = EINVAL; return NULL; }
+ fd = open(path, open_flags(mode), 0666);
return fd < 0 ? NULL : fdopen(fd, mode);
}
+#endif
FILE *freopen(const char *path, const char *mode, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
{
int fd;
fflush(f);
if (!path) return f;
if ((fd = open(path, open_flags(mode), 0666)) < 0) return NULL;
- close(f->fd);
- f->fd = fd; f->write = mode[0] != 'r' || strchr(mode, '+'); f->n = f->pos = f->err = f->eof = 0;
+ dup2(fd, f->fd); close(fd);
+ f->rd = mode[0] == 'r' || strchr(mode, '+') != NULL;
+ f->wr = mode[0] != 'r' || strchr(mode, '+') != NULL;
+ f->write = !f->rd; f->n = f->pos = f->err = f->eof = f->ug = 0;
return f;
}
+#endif
int fclose(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
{
- int r = fflush(f);
+ int r = flush_one(f);
+ FILE **pp;
close(f->fd);
+ for (pp = &c_files; *pp; pp = &(*pp)->next) if (*pp == f) { *pp = f->next; break; }
if (f < files_std || f > files_std + 2) free(f);
return r;
}
+#endif
+int setvbuf(FILE *f, char *buf, int mode, size_t size)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)buf; (void)size; flush_one(f); f->mode = mode; return 0; }
+#endif
+void setbuf(FILE *f, char *buf)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ setvbuf(f, buf, buf ? _IOFBF : _IONBF, BUFSIZ); }
+#endif
+void setbuffer(FILE *f, char *buf, size_t size)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ setvbuf(f, buf, buf ? _IOFBF : _IONBF, size); }
+#endif
+void setlinebuf(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ setvbuf(f, NULL, _IOLBF, 0); }
+#endif
/* stdout is flushed at every newline when it's a terminal, and when it's a pipe or a file only when full, like
* everything else does. stderr always goes out at once [rifle] */
size_t fwrite(const void *p, size_t size, size_t n, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
{
const char *s = p; size_t len = size * n, left = len;
if (size && len / size != n) return 0;
+ if (!f->wr && f->rd) { f->err = 1; errno = EBADF; return 0; }
+ if (switch_to(f, 1)) return 0;
+ if (f->append) lseek(f->fd, 0, SEEK_END);
while (left) {
size_t room = BUFSIZ - f->n, c = left < room ? left : room;
memcpy(f->buf + f->n, s, c);
f->n += c; s += c; left -= c;
- if (f->n == BUFSIZ) fflush(f);
- }
- if (f == stderr) fflush(f);
- else if (f == stdout && len) {
- if (!f->line) f->line = isatty(1) ? 1 : 2;
- if (f->line == 1 && memchr(p, '\n', len)) fflush(f);
+ if (f->n == BUFSIZ) flush_one(f);
}
+ if (f->mode == 3) f->mode = f == stdout ? (isatty(1) ? _IOLBF : _IOFBF) : _IOFBF;
+ if (f->mode == _IONBF) flush_one(f);
+ else if (f->mode == _IOLBF && len && memchr(p, '\n', len)) flush_one(f);
return f->err ? 0 : n;
}
-int fputc(int c, FILE *f) { unsigned char ch = c; return fwrite(&ch, 1, 1, f) ? ch : EOF; }
-int putc(int c, FILE *f) { return fputc(c, f); }
-int putchar(int c) { return fputc(c, stdout); }
-int fputs(const char *s, FILE *f) { return fwrite(s, 1, strlen(s), f) ? 0 : EOF; }
-int puts(const char *s) { fputs(s, stdout); return putchar('\n'); }
+#endif
+int fputc(int c, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ unsigned char ch = c; return fwrite(&ch, 1, 1, f) ? ch : EOF; }
+#endif
+int putc(int c, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fputc(c, f); }
+#endif
+int putchar(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fputc(c, stdout); }
+#endif
+int fputs(const char *s, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ size_t l = strlen(s); return l && !fwrite(s, 1, l, f) ? EOF : 0; }
+#endif
+int puts(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (fputs(s, stdout) == EOF) return EOF; return putchar('\n') == EOF ? EOF : 0; }
+#endif
+#ifndef C_DECLS_ONLY
+static int fill(FILE *f)
+{
+ ssize_t r;
+ if (!f->rd && f->wr) { f->err = 1; errno = EBADF; return EOF; }
+ if (switch_to(f, 0)) return EOF;
+ if (f == stdin) fflush(stdout);
+ do r = read(f->fd, f->buf, BUFSIZ); while (r < 0 && errno == EINTR);
+ if (r <= 0) { if (r < 0) f->err = 1; else f->eof = 1; f->n = f->pos = 0; return EOF; }
+ f->n = r; f->pos = 0;
+ return 0;
+}
+#endif
int fgetc(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
{
- if (f->pos == f->n) {
- if (f == stdin) fflush(stdout);
- ssize_t r = read(f->fd, f->buf, BUFSIZ);
- if (r <= 0) { if (r < 0) f->err = 1; else f->eof = 1; f->n = f->pos = 0; return EOF; }
- f->n = r; f->pos = 0;
- }
+ if (f->ug) { int c = f->ug - 1; f->ug = 0; return c; }
+ if (f->write || f->pos == f->n) { if (fill(f) == EOF) return EOF; }
return (unsigned char)f->buf[f->pos++];
}
-int getc(FILE *f) { return fgetc(f); }
-int getchar(void) { return fgetc(stdin); }
+#endif
+int ungetc(int c, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ if (c == EOF || f->ug) return EOF;
+ if (f->write && switch_to(f, 0)) return EOF;
+ f->ug = (unsigned char)c + 1; f->eof = 0;
+ return (unsigned char)c;
+}
+#endif
+int getc(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fgetc(f); }
+#endif
+int getchar(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fgetc(stdin); }
+#endif
size_t fread(void *p, size_t size, size_t n, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
{
char *d = p; size_t len = size * n, i = 0; int c;
if (size && len / size != n) return 0;
while (i < len) {
- if (f->pos < f->n) { /* [rifle] what's buffered goes over in one piece */
+ if (!f->ug && !f->write && f->pos < f->n) {
size_t c2 = f->n - f->pos; if (c2 > len - i) c2 = len - i;
memcpy(d + i, f->buf + f->pos, c2); f->pos += c2; i += c2;
} else if ((c = fgetc(f)) == EOF) break;
@@ -828,41 +1716,194 @@ size_t fread(void *p, size_t size, size_t n, FILE *f)
}
return size ? i / size : 0;
}
+#endif
char *fgets(char *s, int n, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
{
int i = 0, c;
+ if (n < 1) return NULL;
while (i < n - 1 && (c = fgetc(f)) != EOF) { s[i++] = c; if (c == '\n') break; }
s[i] = 0;
return i ? s : NULL;
}
-int feof(FILE *f) { return f->eof; }
-int ferror(FILE *f) { return f->err; }
-int fileno(FILE *f) { return f->fd; }
+#endif
+int feof(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return f->eof; }
+#endif
+int ferror(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return f->err; }
+#endif
+void clearerr(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ f->err = f->eof = 0; }
+#endif
+int fileno(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return f->fd; }
+#endif
int fseek(FILE *f, long off, int whence)
+#ifdef C_DECLS_ONLY
+;
+#else
{
- fflush(f);
- if (whence == SEEK_CUR && !f->write) off -= f->n - f->pos;
- f->n = f->pos = 0; f->eof = 0;
+ if (f->write) { if (flush_one(f)) return -1; }
+ else if (whence == SEEK_CUR) off -= (f->n - f->pos) + (f->ug ? 1 : 0);
+ f->n = f->pos = 0; f->eof = 0; f->ug = 0;
return lseek(f->fd, off, whence) < 0 ? -1 : 0;
}
+#endif
long ftell(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
{
long p = lseek(f->fd, 0, SEEK_CUR);
- return p < 0 ? -1 : f->write ? p + f->n : p - (f->n - f->pos);
+ return p < 0 ? -1 : f->write ? p + f->n : p - (f->n - f->pos) - (f->ug ? 1 : 0);
}
-int remove(const char *path) { return unlink(path); }
+#endif
+int fseeko(FILE *f, off_t off, int whence)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fseek(f, off, whence); }
+#endif
+off_t ftello(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return ftell(f); }
+#endif
+void rewind(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ fseek(f, 0, SEEK_SET); f->err = 0; }
+#endif
+int fgetpos(FILE *f, fpos_t *p)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ long v = ftell(f); if (v < 0) return -1; *p = v; return 0; }
+#endif
+int fsetpos(FILE *f, const fpos_t *p)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fseek(f, *p, SEEK_SET); }
+#endif
+int remove(const char *path)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (unlink(path) == 0) return 0; if (errno == EISDIR || errno == EPERM) return rmdir(path); return -1; }
+#endif
+void flockfile(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)f; }
+#endif
+void funlockfile(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)f; }
+#endif
+int ftrylockfile(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)f; return 0; }
+#endif
+int getc_unlocked(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fgetc(f); }
+#endif
+int getchar_unlocked(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fgetc(stdin); }
+#endif
+int putc_unlocked(int c, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fputc(c, f); }
+#endif
+int putchar_unlocked(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fputc(c, stdout); }
+#endif
+size_t fread_unlocked(void *p, size_t s, size_t n, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fread(p, s, n, f); }
+#endif
+size_t fwrite_unlocked(const void *p, size_t s, size_t n, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fwrite(p, s, n, f); }
+#endif
+int fflush_unlocked(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fflush(f); }
+#endif
+int feof_unlocked(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return f->eof; }
+#endif
+int ferror_unlocked(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return f->err; }
+#endif
+void perror(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ fflush(stdout); if (s && *s) { fputs(s, stderr); fputs(": ", stderr); } fputs(strerror(errno), stderr); fputc('\n', stderr); }
+#endif
/* ---- printf ----
* flags - + space # 0, width and precision (numbers or *), sizes hh h l ll z j t L,
* conversions d i u o x X c s p f F e E g G and %% */
struct out { char *buf; size_t cap, len; FILE *f; };
+#ifndef C_DECLS_ONLY
static void out_c(struct out *o, char c)
{
if (o->f) fputc(c, o->f);
else if (o->len + 1 < o->cap) o->buf[o->len] = c;
o->len++;
}
+#endif
+#ifndef C_DECLS_ONLY
static void out_pad(struct out *o, char c, int n) { while (n-- > 0) out_c(o, c); }
+#endif
+#ifndef C_DECLS_ONLY
static void out_field(struct out *o, const char *pre, const char *s, int len, int width, int left, int zero)
{
int plen = strlen(pre), pad = width - plen - len;
@@ -872,7 +1913,9 @@ static void out_field(struct out *o, const char *pre, const char *s, int len, in
while (len-- > 0) out_c(o, *s++);
if (left) out_pad(o, ' ', pad);
}
+#endif
/* a long double as digits: fixed (f), exponent (e) or the shorter of both (g) */
+#ifndef C_DECLS_ONLY
static int fmt_float(char *b, long double v, int prec, char conv, int alt)
{
int n = 0, e = 0, i, upper = conv == 'E' || conv == 'G' || conv == 'F';
@@ -908,6 +1951,8 @@ static int fmt_float(char *b, long double v, int prec, char conv, int alt)
b[n] = 0;
return n;
}
+#endif
+#ifndef C_DECLS_ONLY
static int format(struct out *o, const char *fmt, va_list ap)
{
for (; *fmt; fmt++) {
@@ -980,29 +2025,87 @@ static int format(struct out *o, const char *fmt, va_list ap)
}
return o->len;
}
+#endif
int vsnprintf(char *buf, size_t cap, const char *fmt, va_list ap)
+#ifdef C_DECLS_ONLY
+;
+#else
{
struct out o = { buf, cap, 0, NULL };
int n = format(&o, fmt, ap);
if (cap) buf[o.len < cap ? o.len : cap - 1] = 0;
return n;
}
-int vsprintf(char *buf, const char *fmt, va_list ap) { return vsnprintf(buf, (size_t)-1 / 2, fmt, ap); }
-int vfprintf(FILE *f, const char *fmt, va_list ap) { struct out o = { NULL, 0, 0, f }; return format(&o, fmt, ap); }
-int vprintf(const char *fmt, va_list ap) { return vfprintf(stdout, fmt, ap); }
-int snprintf(char *buf, size_t cap, const char *fmt, ...) { va_list ap; va_start(ap, fmt); int n = vsnprintf(buf, cap, fmt, ap); va_end(ap); return n; }
-int sprintf(char *buf, const char *fmt, ...) { va_list ap; va_start(ap, fmt); int n = vsprintf(buf, fmt, ap); va_end(ap); return n; }
-int fprintf(FILE *f, const char *fmt, ...) { va_list ap; va_start(ap, fmt); int n = vfprintf(f, fmt, ap); va_end(ap); return n; }
-int printf(const char *fmt, ...) { va_list ap; va_start(ap, fmt); int n = vfprintf(stdout, fmt, ap); va_end(ap); return n; }
+#endif
+int vsprintf(char *buf, const char *fmt, va_list ap)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return vsnprintf(buf, (size_t)-1 / 2, fmt, ap); }
+#endif
+int vfprintf(FILE *f, const char *fmt, va_list ap)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ struct out o = { NULL, 0, 0, f }; return format(&o, fmt, ap); }
+#endif
+int vprintf(const char *fmt, va_list ap)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return vfprintf(stdout, fmt, ap); }
+#endif
+int snprintf(char *buf, size_t cap, const char *fmt, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ va_list ap; va_start(ap, fmt); int n = vsnprintf(buf, cap, fmt, ap); va_end(ap); return n; }
+#endif
+int sprintf(char *buf, const char *fmt, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ va_list ap; va_start(ap, fmt); int n = vsprintf(buf, fmt, ap); va_end(ap); return n; }
+#endif
+int fprintf(FILE *f, const char *fmt, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ va_list ap; va_start(ap, fmt); int n = vfprintf(f, fmt, ap); va_end(ap); return n; }
+#endif
+int printf(const char *fmt, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ va_list ap; va_start(ap, fmt); int n = vfprintf(stdout, fmt, ap); va_end(ap); return n; }
+#endif
/* ---- the end of a program ---- */
-__attribute__((noreturn)) void exit(int code) { fflush(stdout); fflush(stderr); _exit(code); }
-__attribute__((noreturn)) void abort(void) { fputs("abort\n", stderr); _exit(134); }
+#ifdef C_DECLS_ONLY
+extern void c_run_exit(void);
+#else
+void c_run_exit(void);
+#endif
+__attribute__((noreturn)) void exit(int code)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ c_run_exit(); _exit(code); }
+#endif
+__attribute__((noreturn)) void abort(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ fputs("abort\n", stderr); _exit(134); }
+#endif
#define assert(x) ((x) ? (void)0 : (fprintf(stderr, "%s:%d: assertion failed: %s\n", __FILE__, __LINE__, #x), abort()))
/* ---- paths ---- */
/* the absolute path, with . and .. taken out (links aren't followed, there are none to follow here) */
char *realpath(const char *path, char *out)
+#ifdef C_DECLS_ONLY
+;
+#else
{
char tmp[PATH_MAX], *r = out ? out : malloc(PATH_MAX), *p, *seg;
size_t n = 0;
@@ -1022,12 +2125,16 @@ char *realpath(const char *path, char *out)
if (!n) strcpy(r, "/");
return r;
}
+#endif
/* ---- tcc's helper for va_arg ----
* under tcc every va_arg calls __va_arg(ap, kind, size, align): kind 0 = a general register, 1 = an SSE register,
* 2 = the stack. the layout is the x86-64 ABI's: 6 general registers (48 bytes), then 8 SSE ones of 16 */
#ifdef __TINYC__
void *__va_arg(__builtin_va_list ap, int kind, int size, int align)
+#ifdef C_DECLS_ONLY
+;
+#else
{
size = (size + 7) & ~7; align = (align + 7) & ~7;
if (kind == 0 && ap->gp_offset + size <= 48) { ap->gp_offset += size; return ap->reg_save_area + ap->gp_offset - size; }
@@ -1037,17 +2144,29 @@ void *__va_arg(__builtin_va_list ap, int kind, int size, int align)
return ap->overflow_arg_area - size;
}
#endif
+#endif
/* ---- the environment and running programs ---- */
+#ifdef C_DECLS_ONLY
+extern char **environ;
+#else
char **environ;
+#endif
char *getenv(const char *name)
+#ifdef C_DECLS_ONLY
+;
+#else
{
size_t l = strlen(name); char **e;
for (e = environ; e && *e; e++) if (!strncmp(*e, name, l) && (*e)[l] == '=') return *e + l + 1;
return NULL;
}
+#endif
/* execl(path, arg0, arg1, ..., 0): execve with the arguments written out */
int execl(const char *path, const char *arg, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
{
char *argv[64]; int n = 0; va_list ap;
argv[n++] = (char *)arg;
@@ -1057,8 +2176,12 @@ int execl(const char *path, const char *arg, ...)
argv[n] = NULL;
return execve(path, argv, environ);
}
+#endif
/* execve, looking through $PATH when the name has no / in it */
int execvp(const char *file, char *const argv[])
+#ifdef C_DECLS_ONLY
+;
+#else
{
char path[PATH_MAX]; const char *p = getenv("PATH"), *q;
if (strchr(file, '/')) return execve(file, argv, environ);
@@ -1070,6 +2193,7 @@ int execvp(const char *file, char *const argv[])
}
return -1;
}
+#endif
/* ---- directories [rifle] ----
* opendir/readdir over getdents64. readdir hands back a pointer into the buffer, good until the next call */
@@ -1084,13 +2208,20 @@ struct dirent { ino_t d_ino; long d_off; unsigned short d_reclen; unsigned char
#define DT_SOCK 12
typedef struct DIR { int fd, pos, len; char buf[8192]; } DIR;
DIR *fdopendir(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
{
DIR *d = malloc(sizeof *d);
if (!d) return NULL;
d->fd = fd; d->pos = d->len = 0;
return d;
}
+#endif
DIR *opendir(const char *path)
+#ifdef C_DECLS_ONLY
+;
+#else
{
int fd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
DIR *d;
@@ -1098,7 +2229,11 @@ DIR *opendir(const char *path)
if (!(d = fdopendir(fd))) { close(fd); return NULL; }
return d;
}
+#endif
struct dirent *readdir(DIR *d)
+#ifdef C_DECLS_ONLY
+;
+#else
{
if (d->pos >= d->len) {
long r = sys3(SYS_getdents64, d->fd, d->buf, sizeof d->buf);
@@ -1109,19 +2244,33 @@ struct dirent *readdir(DIR *d)
d->pos += e->d_reclen;
return e;
}
-int closedir(DIR *d) { int r = close(d->fd); free(d); return r; }
-int dirfd(DIR *d) { return d->fd; }
+#endif
+int closedir(DIR *d)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ int r = close(d->fd); free(d); return r; }
+#endif
+int dirfd(DIR *d)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return d->fd; }
+#endif
/* ---- users and groups [rifle] ----
* read straight from /etc/passwd and /etc/group each time. no NSS, no caches: what's in the files is the answer */
struct passwd { char *pw_name, *pw_passwd; uid_t pw_uid; gid_t pw_gid; char *pw_gecos, *pw_dir, *pw_shell; };
struct group { char *gr_name, *gr_passwd; gid_t gr_gid; char **gr_mem; };
+#ifndef C_DECLS_ONLY
static char *c_field(char **p)
{
char *r = *p, *q = strchr(*p, ':');
if (q) { *q = 0; *p = q + 1; } else *p += strlen(*p);
return r;
}
+#endif
+#ifndef C_DECLS_ONLY
static struct passwd *c_getpw(const char *name, long id)
{
static struct passwd pw; static char line[2048];
@@ -1139,8 +2288,20 @@ static struct passwd *c_getpw(const char *name, long id)
fclose(f);
return NULL;
}
-struct passwd *getpwnam(const char *name) { return c_getpw(name, 0); }
-struct passwd *getpwuid(uid_t uid) { return c_getpw(NULL, uid); }
+#endif
+struct passwd *getpwnam(const char *name)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c_getpw(name, 0); }
+#endif
+struct passwd *getpwuid(uid_t uid)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c_getpw(NULL, uid); }
+#endif
+#ifndef C_DECLS_ONLY
static struct group *c_getgr(const char *name, long id)
{
static struct group gr; static char line[8192]; static char *mem[512];
@@ -1160,11 +2321,23 @@ static struct group *c_getgr(const char *name, long id)
fclose(f);
return NULL;
}
-struct group *getgrnam(const char *name) { return c_getgr(name, 0); }
-struct group *getgrgid(gid_t gid) { return c_getgr(NULL, gid); }
+#endif
+struct group *getgrnam(const char *name)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c_getgr(name, 0); }
+#endif
+struct group *getgrgid(gid_t gid)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c_getgr(NULL, gid); }
+#endif
/* ---- time, written out [rifle] ----
* the days between 1970 and a date, counted the way everyone does it. TZ isn't read yet: the time is UTC */
+#ifndef C_DECLS_ONLY
static long c_days(long y, int m, int d) /* m 1..12 */
{
y -= m <= 2;
@@ -1172,17 +2345,34 @@ static long c_days(long y, int m, int d) /* m 1..12 */
long doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1, doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
return era * 146097 + doe - 719468;
}
+#endif
time_t timegm(struct tm *t)
+#ifdef C_DECLS_ONLY
+;
+#else
{
long y = t->tm_year + 1900 + t->tm_mon / 12; int m = t->tm_mon % 12;
if (m < 0) { m += 12; y--; }
return c_days(y, m + 1, 1) * 86400L + (t->tm_mday - 1) * 86400L + t->tm_hour * 3600L + t->tm_min * 60L + t->tm_sec;
}
-time_t mktime(struct tm *t) { return timegm(t); }
+#endif
+time_t mktime(struct tm *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return timegm(t); }
+#endif
+#ifndef C_DECLS_ONLY
static const char *c_wday[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
+#endif
+#ifndef C_DECLS_ONLY
static const char *c_month[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December" };
+#endif
size_t strftime(char *buf, size_t cap, const char *fmt, const struct tm *t)
+#ifdef C_DECLS_ONLY
+;
+#else
{
size_t n = 0;
char tmp[64];
@@ -1238,12 +2428,23 @@ size_t strftime(char *buf, size_t cap, const char *fmt, const struct tm *t)
buf[n] = 0;
return n;
}
-unsigned sleep(unsigned s) { struct timespec t = { s, 0 }; return nanosleep(&t, &t) < 0 ? t.tv_sec : 0; }
+#endif
+unsigned sleep(unsigned s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ struct timespec t = { s, 0 }; return nanosleep(&t, &t) < 0 ? t.tv_sec : 0; }
+#endif
/* ---- the environment, changed [rifle] ----
* the first change copies the list the kernel gave us onto the heap; after that it grows as needed */
+#ifndef C_DECLS_ONLY
static int c_env_own;
+#endif
+#ifndef C_DECLS_ONLY
static int c_env_len(void) { int n = 0; while (environ && environ[n]) n++; return n; }
+#endif
+#ifndef C_DECLS_ONLY
static int c_env_copy(void)
{
if (!c_env_own) {
@@ -1255,7 +2456,11 @@ static int c_env_copy(void)
}
return 0;
}
+#endif
int unsetenv(const char *name)
+#ifdef C_DECLS_ONLY
+;
+#else
{
size_t l = strlen(name); int i, j;
if (!l || strchr(name, '=')) { errno = EINVAL; return -1; }
@@ -1265,7 +2470,11 @@ int unsetenv(const char *name)
environ[j] = NULL;
return 0;
}
+#endif
int putenv(char *entry)
+#ifdef C_DECLS_ONLY
+;
+#else
{
char *eq = strchr(entry, '='); size_t l; int i, n;
if (!eq || eq == entry) { errno = EINVAL; return -1; }
@@ -1281,7 +2490,11 @@ int putenv(char *entry)
}
return 0;
}
+#endif
int setenv(const char *name, const char *value, int overwrite)
+#ifdef C_DECLS_ONLY
+;
+#else
{
size_t l = strlen(name), v = strlen(value); char *e;
if (!l || strchr(name, '=')) { errno = EINVAL; return -1; }
@@ -1290,29 +2503,63 @@ int setenv(const char *name, const char *value, int overwrite)
memcpy(e, name, l); e[l] = '='; memcpy(e + l + 1, value, v + 1);
return putenv(e);
}
-int clearenv(void) { if (c_env_copy()) return -1; environ[0] = NULL; return 0; }
+#endif
+int clearenv(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (c_env_copy()) return -1; environ[0] = NULL; return 0; }
+#endif
/* ---- start-up ----
* the kernel leaves argc, argv and the environment on the stack, then jumps to _start */
-int main(); /* no prototype: int main(void), main(argc, argv) and main(argc, argv, envp) all work */
+#ifdef C_DECLS_ONLY
+extern int main();
+#else
+int main();
+#endif /* no prototype: int main(void), main(argc, argv) and main(argc, argv, envp) all work */
void c_start(long *sp)
+#ifdef C_DECLS_ONLY
+;
+#else
{
int argc = (int)sp[0];
char **argv = (char **)(sp + 1);
environ = argv + argc + 1;
exit(main(argc, argv, environ));
}
+#endif
#ifndef C_NO_START
+#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
/* [rifle] priorities and getline */
-int strcoll(const char *a, const char *b) { return strcmp(a, b); }
+int strcoll(const char *a, const char *b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strcmp(a, b); }
+#endif
#define SYS_getpriority 140
#define SYS_setpriority 141
-int getpriority(int which, int who) { return fail(sys2(SYS_getpriority, which, who)); }
-int setpriority(int which, int who, int prio) { return fail(sys3(SYS_setpriority, which, who, prio)); }
+int getpriority(int which, int who)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_getpriority, which, who)); }
+#endif
+int setpriority(int which, int who, int prio)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_setpriority, which, who, prio)); }
+#endif
ssize_t getline(char **lp, size_t *cap, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
{
size_t n = 0;
int c;
@@ -1326,5 +2573,3186 @@ ssize_t getline(char **lp, size_t *cap, FILE *f)
(*lp)[n] = 0;
return n;
}
+#endif
+/* ---- 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)
+/* ---- system call numbers [chamesle] ---- */
+#ifndef SYS_read
+#define SYS_read 0
+#endif
+#ifndef SYS_write
+#define SYS_write 1
+#endif
+#ifndef SYS_open
+#define SYS_open 2
+#endif
+#ifndef SYS_close
+#define SYS_close 3
+#endif
+#ifndef SYS_stat
+#define SYS_stat 4
+#endif
+#ifndef SYS_fstat
+#define SYS_fstat 5
+#endif
+#ifndef SYS_lstat
+#define SYS_lstat 6
+#endif
+#ifndef SYS_poll
+#define SYS_poll 7
+#endif
+#ifndef SYS_lseek
+#define SYS_lseek 8
+#endif
+#ifndef SYS_mmap
+#define SYS_mmap 9
+#endif
+#ifndef SYS_mprotect
+#define SYS_mprotect 10
+#endif
+#ifndef SYS_munmap
+#define SYS_munmap 11
+#endif
+#ifndef SYS_brk
+#define SYS_brk 12
+#endif
+#ifndef SYS_rt_sigaction
+#define SYS_rt_sigaction 13
+#endif
+#ifndef SYS_rt_sigprocmask
+#define SYS_rt_sigprocmask 14
+#endif
+#ifndef SYS_rt_sigreturn
+#define SYS_rt_sigreturn 15
+#endif
+#ifndef SYS_ioctl
+#define SYS_ioctl 16
+#endif
+#ifndef SYS_pread64
+#define SYS_pread64 17
+#endif
+#ifndef SYS_pwrite64
+#define SYS_pwrite64 18
+#endif
+#ifndef SYS_readv
+#define SYS_readv 19
+#endif
+#ifndef SYS_writev
+#define SYS_writev 20
+#endif
+#ifndef SYS_access
+#define SYS_access 21
+#endif
+#ifndef SYS_pipe
+#define SYS_pipe 22
+#endif
+#ifndef SYS_select
+#define SYS_select 23
+#endif
+#ifndef SYS_sched_yield
+#define SYS_sched_yield 24
+#endif
+#ifndef SYS_mremap
+#define SYS_mremap 25
+#endif
+#ifndef SYS_msync
+#define SYS_msync 26
+#endif
+#ifndef SYS_madvise
+#define SYS_madvise 28
+#endif
+#ifndef SYS_dup
+#define SYS_dup 32
+#endif
+#ifndef SYS_dup2
+#define SYS_dup2 33
+#endif
+#ifndef SYS_pause
+#define SYS_pause 34
+#endif
+#ifndef SYS_nanosleep
+#define SYS_nanosleep 35
+#endif
+#ifndef SYS_getitimer
+#define SYS_getitimer 36
+#endif
+#ifndef SYS_alarm
+#define SYS_alarm 37
+#endif
+#ifndef SYS_setitimer
+#define SYS_setitimer 38
+#endif
+#ifndef SYS_getpid
+#define SYS_getpid 39
+#endif
+#ifndef SYS_sendfile
+#define SYS_sendfile 40
+#endif
+#ifndef SYS_socket
+#define SYS_socket 41
+#endif
+#ifndef SYS_connect
+#define SYS_connect 42
+#endif
+#ifndef SYS_accept
+#define SYS_accept 43
+#endif
+#ifndef SYS_sendto
+#define SYS_sendto 44
+#endif
+#ifndef SYS_recvfrom
+#define SYS_recvfrom 45
+#endif
+#ifndef SYS_sendmsg
+#define SYS_sendmsg 46
+#endif
+#ifndef SYS_recvmsg
+#define SYS_recvmsg 47
+#endif
+#ifndef SYS_shutdown
+#define SYS_shutdown 48
+#endif
+#ifndef SYS_bind
+#define SYS_bind 49
+#endif
+#ifndef SYS_listen
+#define SYS_listen 50
+#endif
+#ifndef SYS_getsockname
+#define SYS_getsockname 51
+#endif
+#ifndef SYS_getpeername
+#define SYS_getpeername 52
+#endif
+#ifndef SYS_socketpair
+#define SYS_socketpair 53
+#endif
+#ifndef SYS_setsockopt
+#define SYS_setsockopt 54
+#endif
+#ifndef SYS_getsockopt
+#define SYS_getsockopt 55
+#endif
+#ifndef SYS_clone
+#define SYS_clone 56
+#endif
+#ifndef SYS_fork
+#define SYS_fork 57
+#endif
+#ifndef SYS_vfork
+#define SYS_vfork 58
+#endif
+#ifndef SYS_execve
+#define SYS_execve 59
+#endif
+#ifndef SYS_exit
+#define SYS_exit 60
+#endif
+#ifndef SYS_wait4
+#define SYS_wait4 61
+#endif
+#ifndef SYS_kill
+#define SYS_kill 62
+#endif
+#ifndef SYS_uname
+#define SYS_uname 63
+#endif
+#ifndef SYS_fcntl
+#define SYS_fcntl 72
+#endif
+#ifndef SYS_flock
+#define SYS_flock 73
+#endif
+#ifndef SYS_fsync
+#define SYS_fsync 74
+#endif
+#ifndef SYS_fdatasync
+#define SYS_fdatasync 75
+#endif
+#ifndef SYS_truncate
+#define SYS_truncate 76
+#endif
+#ifndef SYS_ftruncate
+#define SYS_ftruncate 77
+#endif
+#ifndef SYS_getdents
+#define SYS_getdents 78
+#endif
+#ifndef SYS_getcwd
+#define SYS_getcwd 79
+#endif
+#ifndef SYS_chdir
+#define SYS_chdir 80
+#endif
+#ifndef SYS_fchdir
+#define SYS_fchdir 81
+#endif
+#ifndef SYS_rename
+#define SYS_rename 82
+#endif
+#ifndef SYS_mkdir
+#define SYS_mkdir 83
+#endif
+#ifndef SYS_rmdir
+#define SYS_rmdir 84
+#endif
+#ifndef SYS_creat
+#define SYS_creat 85
+#endif
+#ifndef SYS_link
+#define SYS_link 86
+#endif
+#ifndef SYS_unlink
+#define SYS_unlink 87
+#endif
+#ifndef SYS_symlink
+#define SYS_symlink 88
+#endif
+#ifndef SYS_readlink
+#define SYS_readlink 89
+#endif
+#ifndef SYS_chmod
+#define SYS_chmod 90
+#endif
+#ifndef SYS_fchmod
+#define SYS_fchmod 91
+#endif
+#ifndef SYS_chown
+#define SYS_chown 92
+#endif
+#ifndef SYS_fchown
+#define SYS_fchown 93
+#endif
+#ifndef SYS_lchown
+#define SYS_lchown 94
+#endif
+#ifndef SYS_umask
+#define SYS_umask 95
+#endif
+#ifndef SYS_gettimeofday
+#define SYS_gettimeofday 96
+#endif
+#ifndef SYS_getrlimit
+#define SYS_getrlimit 97
+#endif
+#ifndef SYS_getrusage
+#define SYS_getrusage 98
+#endif
+#ifndef SYS_sysinfo
+#define SYS_sysinfo 99
+#endif
+#ifndef SYS_times
+#define SYS_times 100
+#endif
+#ifndef SYS_getuid
+#define SYS_getuid 102
+#endif
+#ifndef SYS_getgid
+#define SYS_getgid 104
+#endif
+#ifndef SYS_setuid
+#define SYS_setuid 105
+#endif
+#ifndef SYS_setgid
+#define SYS_setgid 106
+#endif
+#ifndef SYS_geteuid
+#define SYS_geteuid 107
+#endif
+#ifndef SYS_getegid
+#define SYS_getegid 108
+#endif
+#ifndef SYS_setpgid
+#define SYS_setpgid 109
+#endif
+#ifndef SYS_getppid
+#define SYS_getppid 110
+#endif
+#ifndef SYS_getpgrp
+#define SYS_getpgrp 111
+#endif
+#ifndef SYS_setsid
+#define SYS_setsid 112
+#endif
+#ifndef SYS_setreuid
+#define SYS_setreuid 113
+#endif
+#ifndef SYS_setregid
+#define SYS_setregid 114
+#endif
+#ifndef SYS_getgroups
+#define SYS_getgroups 115
+#endif
+#ifndef SYS_setgroups
+#define SYS_setgroups 116
+#endif
+#ifndef SYS_setresuid
+#define SYS_setresuid 117
+#endif
+#ifndef SYS_setresgid
+#define SYS_setresgid 119
+#endif
+#ifndef SYS_getpgid
+#define SYS_getpgid 121
+#endif
+#ifndef SYS_getsid
+#define SYS_getsid 124
+#endif
+#ifndef SYS_rt_sigpending
+#define SYS_rt_sigpending 127
+#endif
+#ifndef SYS_rt_sigtimedwait
+#define SYS_rt_sigtimedwait 128
+#endif
+#ifndef SYS_rt_sigsuspend
+#define SYS_rt_sigsuspend 130
+#endif
+#ifndef SYS_sigaltstack
+#define SYS_sigaltstack 131
+#endif
+#ifndef SYS_utime
+#define SYS_utime 132
+#endif
+#ifndef SYS_mknod
+#define SYS_mknod 133
+#endif
+#ifndef SYS_statfs
+#define SYS_statfs 137
+#endif
+#ifndef SYS_fstatfs
+#define SYS_fstatfs 138
+#endif
+#ifndef SYS_getpriority
+#define SYS_getpriority 140
+#endif
+#ifndef SYS_setpriority
+#define SYS_setpriority 141
+#endif
+#ifndef SYS_prctl
+#define SYS_prctl 157
+#endif
+#ifndef SYS_arch_prctl
+#define SYS_arch_prctl 158
+#endif
+#ifndef SYS_setrlimit
+#define SYS_setrlimit 160
+#endif
+#ifndef SYS_chroot
+#define SYS_chroot 161
+#endif
+#ifndef SYS_sync
+#define SYS_sync 162
+#endif
+#ifndef SYS_mount
+#define SYS_mount 165
+#endif
+#ifndef SYS_umount2
+#define SYS_umount2 166
+#endif
+#ifndef SYS_gettid
+#define SYS_gettid 186
+#endif
+#ifndef SYS_time
+#define SYS_time 201
+#endif
+#ifndef SYS_futex
+#define SYS_futex 202
+#endif
+#ifndef SYS_getdents64
+#define SYS_getdents64 217
+#endif
+#ifndef SYS_set_tid_address
+#define SYS_set_tid_address 218
+#endif
+#ifndef SYS_clock_gettime
+#define SYS_clock_gettime 228
+#endif
+#ifndef SYS_clock_getres
+#define SYS_clock_getres 229
+#endif
+#ifndef SYS_clock_nanosleep
+#define SYS_clock_nanosleep 230
+#endif
+#ifndef SYS_exit_group
+#define SYS_exit_group 231
+#endif
+#ifndef SYS_tgkill
+#define SYS_tgkill 234
+#endif
+#ifndef SYS_utimes
+#define SYS_utimes 235
+#endif
+#ifndef SYS_openat
+#define SYS_openat 257
+#endif
+#ifndef SYS_mkdirat
+#define SYS_mkdirat 258
+#endif
+#ifndef SYS_mknodat
+#define SYS_mknodat 259
+#endif
+#ifndef SYS_fchownat
+#define SYS_fchownat 260
+#endif
+#ifndef SYS_newfstatat
+#define SYS_newfstatat 262
+#endif
+#ifndef SYS_unlinkat
+#define SYS_unlinkat 263
+#endif
+#ifndef SYS_renameat
+#define SYS_renameat 264
+#endif
+#ifndef SYS_linkat
+#define SYS_linkat 265
+#endif
+#ifndef SYS_symlinkat
+#define SYS_symlinkat 266
+#endif
+#ifndef SYS_readlinkat
+#define SYS_readlinkat 267
+#endif
+#ifndef SYS_fchmodat
+#define SYS_fchmodat 268
+#endif
+#ifndef SYS_faccessat
+#define SYS_faccessat 269
+#endif
+#ifndef SYS_ppoll
+#define SYS_ppoll 271
+#endif
+#ifndef SYS_utimensat
+#define SYS_utimensat 280
+#endif
+#ifndef SYS_accept4
+#define SYS_accept4 288
+#endif
+#ifndef SYS_dup3
+#define SYS_dup3 292
+#endif
+#ifndef SYS_pipe2
+#define SYS_pipe2 293
+#endif
+#ifndef SYS_getrandom
+#define SYS_getrandom 318
+#endif
+#ifndef SYS_copy_file_range
+#define SYS_copy_file_range 326
+#endif
+#ifndef SYS_setsid
+#define SYS_setsid 112
+#endif
+#ifndef SYS_fchmodat2
+#define SYS_fchmodat2 452
+#endif
+/* ---- more of unistd, fcntl, sys/*: the thin system calls [chamesle] ---- */
+
+#ifndef O_NONBLOCK
+#define O_NONBLOCK 04000
+#endif
+#ifndef O_NOCTTY
+#define O_NOCTTY 0400
+#endif
+#ifndef O_SYNC
+#define O_SYNC 04010000
+#endif
+#ifndef O_DSYNC
+#define O_DSYNC 010000
+#endif
+#ifndef O_NOFOLLOW
+#define O_NOFOLLOW 0400000
+#endif
+#ifndef O_DIRECTORY
+#define O_DIRECTORY 0200000
+#endif
+#ifndef O_NOATIME
+#define O_NOATIME 01000000
+#endif
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
+#endif
+#ifndef O_ACCMODE
+#define O_ACCMODE 3
+#endif
+#define O_NDELAY O_NONBLOCK
+#define F_DUPFD 0
+#define F_GETFD 1
+#define F_SETFD 2
+#define F_GETFL 3
+#define F_SETFL 4
+#define F_GETLK 5
+#define F_SETLK 6
+#define F_SETLKW 7
+#define F_DUPFD_CLOEXEC 1030
+#define FD_CLOEXEC 1
+#define F_RDLCK 0
+#define F_WRLCK 1
+#define F_UNLCK 2
+#define F_OK 0
+#define X_OK 1
+#define W_OK 2
+#define R_OK 4
+#define STDIN_FILENO 0
+#define STDOUT_FILENO 1
+#define STDERR_FILENO 2
+#define LOCK_SH 1
+#define LOCK_EX 2
+#define LOCK_NB 4
+#define LOCK_UN 8
+#define AT_EACCESS 0x200
+#define AT_SYMLINK_FOLLOW 0x400
+#ifndef AT_REMOVEDIR
+#define AT_REMOVEDIR 0x200
+#endif
+#ifndef AT_SYMLINK_NOFOLLOW
+#define AT_SYMLINK_NOFOLLOW 0x100
+#endif
+#define _SC_ARG_MAX 0
+#define _SC_CHILD_MAX 1
+#define _SC_CLK_TCK 2
+#define _SC_NGROUPS_MAX 3
+#define _SC_OPEN_MAX 4
+#define _SC_PAGESIZE 30
+#define _SC_PAGE_SIZE 30
+#define _SC_NPROCESSORS_CONF 83
+#define _SC_NPROCESSORS_ONLN 84
+#define _SC_PHYS_PAGES 85
+#define _SC_GETPW_R_SIZE_MAX 70
+#define _SC_GETGR_R_SIZE_MAX 69
+#define _SC_HOST_NAME_MAX 180
+#define _SC_LINE_MAX 43
+#define _POSIX_VERSION 200809L
+#define _POSIX_PATH_MAX 256
+#define NGROUPS_MAX 65536
+#define HOST_NAME_MAX 64
+#define LOGIN_NAME_MAX 256
+#define NAME_MAX 255
+#define PIPE_BUF 4096
+#define IOV_MAX 1024
+#define SSIZE_MAX LONG_MAX
+#define CLOCKS_PER_SEC 1000000
+
+struct iovec { void *iov_base; size_t iov_len; };
+ssize_t readv(int fd, const struct iovec *v, int n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_readv, fd, v, n)); }
+#endif
+ssize_t writev(int fd, const struct iovec *v, int n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_writev, fd, v, n)); }
+#endif
+
+int dup3(int a, int b, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_dup3, a, b, flags)); }
+#endif
+int pipe2(int fd[2], int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_pipe2, fd, flags)); }
+#endif
+int flock(int fd, int op)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_flock, fd, op)); }
+#endif
+pid_t vfork(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fork(); }
+#endif
+pid_t getpgrp(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sys0(SYS_getpgrp); }
+#endif
+pid_t getpgid(pid_t p)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_getpgid, p)); }
+#endif
+int setpgid(pid_t p, pid_t g)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_setpgid, p, g)); }
+#endif
+int setpgrp(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return setpgid(0, 0); }
+#endif
+pid_t getsid(pid_t p)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_getsid, p)); }
+#endif
+pid_t setsid(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys0(SYS_setsid)); }
+#endif
+int setreuid(uid_t r, uid_t e)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_setreuid, r, e)); }
+#endif
+int setregid(gid_t r, gid_t e)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_setregid, r, e)); }
+#endif
+int setresuid(uid_t r, uid_t e, uid_t s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_setresuid, r, e, s)); }
+#endif
+int setresgid(gid_t r, gid_t e, gid_t s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_setresgid, r, e, s)); }
+#endif
+int seteuid(uid_t e)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return setresuid(-1, e, -1); }
+#endif
+int setegid(gid_t e)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return setresgid(-1, e, -1); }
+#endif
+int setgroups(size_t n, const gid_t *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_setgroups, n, l)); }
+#endif
+pid_t gettid(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sys0(SYS_gettid); }
+#endif
+unsigned alarm(unsigned s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ struct { long ti[2], tv[2]; } nw = { { 0, 0 }, { s, 0 } }, old = { { 0, 0 }, { 0, 0 } };
+ if (fail(sys3(SYS_setitimer, 0, &nw, &old)) < 0) return 0;
+ return old.tv[0] + (old.tv[1] ? 1 : 0);
+}
+#endif
+int pause(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys0(SYS_pause)); }
+#endif
+unsigned usleep(unsigned us)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ struct timespec t = { us / 1000000, (us % 1000000) * 1000L }; return nanosleep(&t, NULL); }
+#endif
+int clock_getres(int clk, struct timespec *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_clock_getres, clk, t)); }
+#endif
+int clock_nanosleep(int clk, int flags, const struct timespec *t, struct timespec *rem)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ long r = sys4(SYS_clock_nanosleep, clk, flags, t, rem); return r < 0 && r > -4096 ? -r : 0; }
+#endif
+int getpagesize(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return 4096; }
+#endif
+int nice(int inc)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ int cur = getpriority(0, 0); if (cur < 0) return -1; return setpriority(0, 0, 20 - cur + inc) < 0 ? -1 : 20 - getpriority(0, 0); }
+#endif
+int fchmodat(int dirfd, const char *p, mode_t m, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)flags; return fail(sys3(SYS_fchmodat, dirfd, p, m)); }
+#endif
+int fchownat(int dirfd, const char *p, uid_t u, gid_t g, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_fchownat, dirfd, (long)p, u, g, flags, 0)); }
+#endif
+int faccessat(int dirfd, const char *p, int mode, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)flags; return fail(sys3(SYS_faccessat, dirfd, p, mode)); }
+#endif
+int renameat(int a, const char *p, int b, const char *q)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys4(SYS_renameat, a, p, b, q)); }
+#endif
+int linkat(int a, const char *p, int b, const char *q, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_linkat, a, (long)p, b, (long)q, flags, 0)); }
+#endif
+int symlinkat(const char *t, int dirfd, const char *p)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_symlinkat, t, dirfd, p)); }
+#endif
+ssize_t readlinkat(int dirfd, const char *p, char *buf, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys4(SYS_readlinkat, dirfd, p, buf, n)); }
+#endif
+int mknodat(int dirfd, const char *p, mode_t m, dev_t d)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys4(SYS_mknodat, dirfd, p, m, d)); }
+#endif
+int mkfifoat(int dirfd, const char *p, mode_t m)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return mknodat(dirfd, p, (m & 07777) | S_IFIFO, 0); }
+#endif
+int fstatfs(int fd, struct statfs *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_fstatfs, fd, s)); }
+#endif
+int utimes(const char *path, const struct timeval tv[2])
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ struct timespec t[2];
+ if (!tv) return utimensat(AT_FDCWD, path, NULL, 0);
+ t[0].tv_sec = tv[0].tv_sec; t[0].tv_nsec = tv[0].tv_usec * 1000;
+ t[1].tv_sec = tv[1].tv_sec; t[1].tv_nsec = tv[1].tv_usec * 1000;
+ return utimensat(AT_FDCWD, path, t, 0);
+}
+#endif
+int futimens(int fd, const struct timespec t[2])
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_utimensat, fd, 0, (long)t, 0, 0, 0)); }
+#endif
+struct utimbuf { time_t actime, modtime; };
+int utime(const char *path, const struct utimbuf *u)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ struct timespec t[2];
+ if (!u) return utimensat(AT_FDCWD, path, NULL, 0);
+ t[0].tv_sec = u->actime; t[0].tv_nsec = 0; t[1].tv_sec = u->modtime; t[1].tv_nsec = 0;
+ return utimensat(AT_FDCWD, path, t, 0);
+}
+#endif
+int lockf(int fd, int cmd, off_t len)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ (void)len;
+ return flock(fd, cmd == 0 ? LOCK_UN : cmd == 1 || cmd == 2 ? LOCK_EX : cmd == 3 ? LOCK_EX | LOCK_NB : LOCK_UN);
+}
+#endif
+int posix_fadvise(int fd, off_t o, off_t l, int a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)fd; (void)o; (void)l; (void)a; return 0; }
+#endif
+int posix_fallocate(int fd, off_t o, off_t l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys4(285, fd, 0, o, l)) < 0 ? errno : 0; }
+#endif
+ssize_t sendfile(int out, int in, off_t *off, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys4(SYS_sendfile, out, in, off, n)); }
+#endif
+ssize_t copy_file_range(int in, off_t *io, int out, off_t *oo, size_t n, unsigned flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_copy_file_range, in, (long)io, out, (long)oo, n, flags)); }
+#endif
+
+int gethostname(char *buf, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ struct utsname u;
+ if (uname(&u) < 0) return -1;
+ if (strlen(u.nodename) >= n) { errno = ENAMETOOLONG; return -1; }
+ strcpy(buf, u.nodename);
+ return 0;
+}
+#endif
+int getdomainname(char *buf, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ struct utsname u; if (uname(&u) < 0) return -1; strncpy(buf, u.domainname, n); return 0; }
+#endif
+char *getlogin(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ struct passwd *p = getpwuid(getuid()); return p ? p->pw_name : getenv("LOGNAME"); }
+#endif
+int getlogin_r(char *buf, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ char *l = getlogin(); if (!l) return ENOENT; strncpy(buf, l, n); return 0; }
+#endif
+char *ttyname(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ static char buf[64];
+ char p[32];
+ ssize_t n;
+ snprintf(p, sizeof p, "/proc/self/fd/%d", fd);
+ n = readlink(p, buf, sizeof buf - 1);
+ if (n < 0) return NULL;
+ buf[n] = 0;
+ return buf;
+}
+#endif
+int ttyname_r(int fd, char *buf, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ char *t = ttyname(fd); if (!t) return errno; strncpy(buf, t, n); return 0; }
+#endif
+char *ctermid(char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ static char d[] = "/dev/tty"; if (!s) return d; strcpy(s, d); return s; }
+#endif
+pid_t tcgetpgrp(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ int p; if (ioctl(fd, 0x540F, &p) < 0) return -1; return p; }
+#endif
+int tcsetpgrp(int fd, pid_t p)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return ioctl(fd, 0x5410, &p); }
+#endif
+pid_t tcgetsid(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ int p; if (ioctl(fd, 0x5429, &p) < 0) return -1; return p; }
+#endif
+long pathconf(const char *p, int name)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)p; (void)name; return name == 3 ? 255 : 4096; }
+#endif
+long fpathconf(int fd, int name)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)fd; return pathconf("/", name); }
+#endif
+char *getpass(const char *prompt)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ static char buf[128]; fputs(prompt, stderr); if (!fgets(buf, sizeof buf, stdin)) return NULL; buf[strcspn(buf, "\n")] = 0; return buf; }
+#endif
+int daemon(int nochdir, int noclose)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ pid_t p = fork();
+ if (p < 0) return -1;
+ if (p) _exit(0);
+ if (setsid() < 0) return -1;
+ if (!nochdir) chdir("/");
+ 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); } }
+ return 0;
+}
+#endif
+
+/* exec and spawn */
+extern char **environ;
+int execv(const char *path, char *const argv[])
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return execve(path, argv, environ); }
+#endif
+int execvpe(const char *file, char *const argv[], char *const envp[])
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char **save = environ;
+ int r;
+ environ = (char **)envp;
+ r = execvp(file, argv);
+ environ = save;
+ return r;
+}
+#endif
+int execle(const char *path, const char *arg, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *av[256];
+ char **envp;
+ int n = 0;
+ va_list ap;
+ va_start(ap, arg);
+ av[n++] = (char *)arg;
+ while (n < 255 && (av[n] = va_arg(ap, char *))) n++;
+ envp = va_arg(ap, char **);
+ va_end(ap);
+ return execve(path, av, envp);
+}
+#endif
+int execlp(const char *file, const char *arg, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *av[256];
+ int n = 0;
+ va_list ap;
+ va_start(ap, arg);
+ av[n++] = (char *)arg;
+ while (n < 255 && (av[n] = va_arg(ap, char *))) n++;
+ va_end(ap);
+ return execvp(file, av);
+}
+#endif
+int system(const char *cmd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ pid_t pid;
+ int st;
+ if (!cmd) return 1;
+ fflush(NULL);
+ pid = fork();
+ if (pid < 0) return -1;
+ if (!pid) {
+ char *av[] = { "sh", "-c", (char *)cmd, NULL };
+ execve("/bin/sh", av, environ);
+ _exit(127);
+ }
+ while (waitpid(pid, &st, 0) < 0) if (errno != EINTR) return -1;
+ return st;
+}
+#endif
+FILE *popen(const char *cmd, const char *mode)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ int p[2], parent_end, child_end;
+ pid_t pid;
+ FILE *f;
+ int rd = mode[0] == 'r';
+ if (pipe(p) < 0) return NULL;
+ parent_end = rd ? p[0] : p[1]; child_end = rd ? p[1] : p[0];
+ fflush(NULL);
+ pid = fork();
+ if (pid < 0) { close(p[0]); close(p[1]); return NULL; }
+ if (!pid) {
+ char *av[] = { "sh", "-c", (char *)cmd, NULL };
+ close(parent_end);
+ if (child_end != (rd ? 1 : 0)) { dup2(child_end, rd ? 1 : 0); close(child_end); }
+ execve("/bin/sh", av, environ);
+ _exit(127);
+ }
+ close(child_end);
+ f = fdopen(parent_end, rd ? "r" : "w");
+ if (!f) { close(parent_end); return NULL; }
+ f->pid = pid;
+ return f;
+}
+#endif
+int pclose(FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ int st;
+ pid_t pid = f->pid;
+ fclose(f);
+ while (waitpid(pid, &st, 0) < 0) if (errno != EINTR) return -1;
+ return st;
+}
+#endif
+
+/* wait */
+#define WNOHANG 1
+#define WUNTRACED 2
+#define WCONTINUED 8
+#define WEXITSTATUS(s) (((s) >> 8) & 0xff)
+#define WTERMSIG(s) ((s) & 0x7f)
+#define WSTOPSIG(s) WEXITSTATUS(s)
+#define WIFEXITED(s) (!WTERMSIG(s))
+#define WIFSIGNALED(s) ((signed char)(((s) & 0x7f) + 1) >> 1 > 0)
+#define WIFSTOPPED(s) (((s) & 0xff) == 0x7f)
+#define WIFCONTINUED(s) ((s) == 0xffff)
+#define WCOREDUMP(s) ((s) & 0x80)
+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; };
+#define RUSAGE_SELF 0
+#define RUSAGE_CHILDREN (-1)
+pid_t wait3(int *st, int options, struct rusage *ru)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys4(SYS_wait4, -1, st, options, ru)); }
+#endif
+pid_t wait4(pid_t pid, int *st, int options, struct rusage *ru)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys4(SYS_wait4, pid, st, options, ru)); }
+#endif
+int getrusage(int who, struct rusage *ru)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_getrusage, who, ru)); }
+#endif
+struct rlimit { unsigned long rlim_cur, rlim_max; };
+typedef unsigned long rlim_t;
+#define RLIM_INFINITY (~0UL)
+#define RLIMIT_CPU 0
+#define RLIMIT_FSIZE 1
+#define RLIMIT_DATA 2
+#define RLIMIT_STACK 3
+#define RLIMIT_CORE 4
+#define RLIMIT_NOFILE 7
+#define RLIMIT_AS 9
+int getrlimit(int r, struct rlimit *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_getrlimit, r, l)); }
+#endif
+int setrlimit(int r, const struct rlimit *l)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_setrlimit, r, l)); }
+#endif
+struct tms { long tms_utime, tms_stime, tms_cutime, tms_cstime; };
+clock_t times(struct tms *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_times, t)); }
+#endif
+clock_t clock(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ struct timespec t; if (clock_gettime(2, &t) < 0) return -1; return t.tv_sec * 1000000L + t.tv_nsec / 1000; }
+#endif
+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]; };
+int sysinfo(struct sysinfo *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys1(SYS_sysinfo, s)); }
+#endif
+int get_nprocs(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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; }
+#endif
+int prctl(int op, unsigned long a, unsigned long b, unsigned long c, unsigned long d)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys6(SYS_prctl, op, a, b, c, d, 0)); }
+#endif
+
+/* memory */
+#ifndef MAP_PRIVATE
+#define MAP_SHARED 1
+#define MAP_PRIVATE 2
+#endif
+#ifndef MAP_ANONYMOUS
+#define MAP_FIXED 0x10
+#define MAP_ANONYMOUS 0x20
+#define MAP_ANON MAP_ANONYMOUS
+#define MAP_NORESERVE 0x4000
+#define MAP_POPULATE 0x8000
+#endif
+#define MAP_FAILED ((void *)-1)
+#define PROT_NONE 0
+#define MS_ASYNC 1
+#define MS_INVALIDATE 2
+#define MS_SYNC 4
+#define MADV_NORMAL 0
+#define MADV_RANDOM 1
+#define MADV_SEQUENTIAL 2
+#define MADV_WILLNEED 3
+#define MADV_DONTNEED 4
+void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ long r = sys6(SYS_mmap, (long)addr, len, prot, flags, fd, off);
+ if (r < 0 && r > -4096) { errno = -r; return MAP_FAILED; }
+ return (void *)r;
+}
+#endif
+int munmap(void *a, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_munmap, a, n)); }
+#endif
+int msync(void *a, size_t n, int f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_msync, a, n, f)); }
+#endif
+int madvise(void *a, size_t n, int adv)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_madvise, a, n, adv)); }
+#endif
+int posix_madvise(void *a, size_t n, int adv)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return madvise(a, n, adv); }
+#endif
+int mlock(const void *a, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; (void)n; return 0; }
+#endif
+int munlock(const void *a, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)a; (void)n; return 0; }
+#endif
+
+/* the ioctl / termios corner */
+#define TIOCGWINSZ 0x5413
+#define TIOCSWINSZ 0x5414
+#define FIONREAD 0x541B
+#define FIONBIO 0x5421
+#define TIOCSCTTY 0x540E
+struct winsize { unsigned short ws_row, ws_col, ws_xpixel, ws_ypixel; };
+#define B0 0
+#define B9600 015
+#define B19200 016
+#define B38400 017
+#define B57600 010001
+#define B115200 010002
+#define TCSANOW 0
+#define TCSADRAIN 1
+#define TCSAFLUSH 2
+#define TCIFLUSH 0
+#define TCOFLUSH 1
+#define TCIOFLUSH 2
+#define TCOOFF 0
+#define TCOON 1
+typedef unsigned int speed_t;
+typedef unsigned char cc_t;
+typedef unsigned int tcflag_t;
+#define VINTR 0
+#define VQUIT 1
+#define VERASE 2
+#define VKILL 3
+#define VEOF 4
+#define VTIME 5
+#define VMIN 6
+#define VSTART 8
+#define VSTOP 9
+#define VSUSP 10
+#define NCCS 32
+#define IGNBRK 1
+#define BRKINT 2
+#define IGNPAR 4
+#define PARMRK 8
+#define INPCK 16
+#define ISTRIP 32
+#define INLCR 64
+#define IGNCR 128
+#define ECHONL 0100
+#define IXON 02000
+#define IXANY 04000
+#define IXOFF 010000
+#define CSIZE 060
+#define CS5 0
+#define CS6 020
+#define CS7 040
+#define CS8 060
+#define CSTOPB 0100
+#define CREAD 0200
+#define PARENB 0400
+#define PARODD 01000
+#define HUPCL 02000
+#define CLOCAL 04000
+#define CBAUD 010017
+int tcflush(int fd, int q)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return ioctl(fd, 0x540B, (void *)(long)q); }
+#endif
+int tcdrain(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return ioctl(fd, 0x5409, (void *)1L); }
+#endif
+int tcflow(int fd, int a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return ioctl(fd, 0x540A, (void *)(long)a); }
+#endif
+int tcsendbreak(int fd, int d)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)d; return ioctl(fd, 0x5409, (void *)0L); }
+#endif
+speed_t cfgetospeed(const struct termios *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return t->c_cflag & CBAUD; }
+#endif
+speed_t cfgetispeed(const struct termios *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return t->c_cflag & CBAUD; }
+#endif
+int cfsetospeed(struct termios *t, speed_t s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ t->c_cflag = (t->c_cflag & ~CBAUD) | s; return 0; }
+#endif
+int cfsetispeed(struct termios *t, speed_t s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)t; (void)s; return 0; }
+#endif
+int cfsetspeed(struct termios *t, speed_t s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return cfsetospeed(t, s); }
+#endif
+void cfmakeraw(struct termios *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
+ t->c_oflag &= ~OPOST;
+ t->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
+ t->c_cflag &= ~(CSIZE | PARENB);
+ t->c_cflag |= CS8;
+ t->c_cc[VMIN] = 1; t->c_cc[VTIME] = 0;
+}
+#endif
+int posix_openpt(int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return open("/dev/ptmx", flags); }
+#endif
+int grantpt(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)fd; return 0; }
+#endif
+int unlockpt(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ int z = 0; return ioctl(fd, 0x40045431, &z); }
+#endif
+char *ptsname(int fd)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ static char buf[32];
+ int n;
+ if (ioctl(fd, 0x80045430, &n) < 0) return NULL;
+ snprintf(buf, sizeof buf, "/dev/pts/%d", n);
+ return buf;
+}
+#endif
+
+/* getopt */
+#ifdef C_DECLS_ONLY
+extern char *optarg;
+#else
+char *optarg;
+#endif
+#ifdef C_DECLS_ONLY
+extern int optind;
+#else
+int optind = 1, opterr = 1, optopt;
+#endif
+#ifndef C_DECLS_ONLY
+static int c_optpos;
+#endif
+int getopt(int argc, char *const argv[], const char *shorts)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ const char *p;
+ char *a;
+ int c;
+ if (optind == 0) { optind = 1; c_optpos = 0; }
+ if (!c_optpos) {
+ if (optind >= argc) return -1;
+ a = argv[optind];
+ if (a[0] != '-' || !a[1]) return -1;
+ if (a[1] == '-' && !a[2]) { optind++; return -1; }
+ c_optpos = 1;
+ }
+ a = argv[optind];
+ c = a[c_optpos++];
+ p = strchr(shorts[0] == '+' || shorts[0] == ':' ? shorts + 1 : shorts, c);
+ if (!a[c_optpos]) { optind++; c_optpos = 0; }
+ if (!p || c == ':') {
+ optopt = c;
+ if (opterr && shorts[0] != ':') fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], c);
+ return '?';
+ }
+ if (p[1] == ':') {
+ if (c_optpos) { optarg = argv[optind] + c_optpos; optind++; c_optpos = 0; }
+ else if (p[2] == ':') optarg = NULL;
+ else if (optind >= argc) {
+ optopt = c;
+ if (opterr && shorts[0] != ':') fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], c);
+ return shorts[0] == ':' ? ':' : '?';
+ } else optarg = argv[optind++];
+ } else optarg = NULL;
+ return c;
+}
+#endif
+struct option { const char *name; int has_arg; int *flag; int val; };
+#define no_argument 0
+#define required_argument 1
+#define optional_argument 2
+int getopt_long(int argc, char *const argv[], const char *shorts, const struct option *longs, int *idx)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *a;
+ const struct option *o, *hit = NULL;
+ size_t l;
+ char *eq;
+ if (optind == 0) { optind = 1; c_optpos = 0; }
+ if (c_optpos || optind >= argc) return getopt(argc, argv, shorts);
+ a = argv[optind];
+ if (a[0] != '-' || a[1] != '-') return getopt(argc, argv, shorts);
+ if (!a[2]) { optind++; return -1; }
+ eq = strchr(a + 2, '=');
+ l = eq ? (size_t)(eq - a - 2) : strlen(a + 2);
+ for (o = longs; o->name; o++) {
+ if (!strncmp(o->name, a + 2, l)) {
+ if (strlen(o->name) == l) { hit = o; break; }
+ if (!hit) hit = o;
+ 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 '?'; }
+ }
+ }
+ if (!hit) { if (opterr) fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], a); optind++; return '?'; }
+ optind++;
+ if (idx) *idx = hit - longs;
+ if (hit->has_arg == no_argument) {
+ if (eq) { if (opterr) fprintf(stderr, "%s: option '--%s' doesn't allow an argument\n", argv[0], hit->name); return '?'; }
+ optarg = NULL;
+ } else if (eq) optarg = eq + 1;
+ else if (hit->has_arg == required_argument) {
+ if (optind >= argc) { if (opterr) fprintf(stderr, "%s: option '--%s' requires an argument\n", argv[0], hit->name); return shorts[0] == ':' ? ':' : '?'; }
+ optarg = argv[optind++];
+ } else optarg = NULL;
+ if (hit->flag) { *hit->flag = hit->val; return 0; }
+ return hit->val;
+}
+#endif
+int getopt_long_only(int argc, char *const argv[], const char *shorts, const struct option *longs, int *idx)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return getopt_long(argc, argv, shorts, longs, idx); }
+#endif
+/* ---- string, ctype, stdlib, time: what real programs reach for [chamesle] ---- */
+
+/* strings */
+char *strsep(char **sp, const char *delim)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *s = *sp, *e;
+ if (!s) return NULL;
+ e = s + strcspn(s, delim);
+ if (*e) *e++ = 0; else e = NULL;
+ *sp = e;
+ return s;
+}
+#endif
+char *strtok_r(char *s, const char *delim, char **save)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *e;
+ if (!s) s = *save;
+ if (!s) return NULL;
+ s += strspn(s, delim);
+ if (!*s) { *save = NULL; return NULL; }
+ e = s + strcspn(s, delim);
+ if (*e) *e++ = 0; else e = NULL;
+ *save = e;
+ return s;
+}
+#endif
+char *strtok(char *s, const char *delim)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ static char *save; return strtok_r(s, delim, &save); }
+#endif
+size_t strnlen(const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ size_t i = 0; while (i < n && s[i]) i++; return i; }
+#endif
+size_t strlcpy(char *d, const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ size_t l = strlen(s);
+ if (n) { size_t c = l < n - 1 ? l : n - 1; memcpy(d, s, c); d[c] = 0; }
+ return l;
+}
+#endif
+size_t strlcat(char *d, const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ size_t dl = strnlen(d, n), sl = strlen(s);
+ if (dl == n) return n + sl;
+ strlcpy(d + dl, s, n - dl);
+ return dl + sl;
+}
+#endif
+char *strncat(char *d, const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ char *r = d; d += strlen(d); while (n-- && *s) *d++ = *s++; *d = 0; return r; }
+#endif
+char *stpncpy(char *d, const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ size_t l = strnlen(s, n); memcpy(d, s, l); memset(d + l, 0, n - l); return d + l; }
+#endif
+char *strchrnul(const char *s, int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ while (*s && *s != (char)c) s++; return (char *)s; }
+#endif
+void *mempcpy(void *d, const void *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (char *)memcpy(d, s, n) + n; }
+#endif
+void *memccpy(void *d, const void *s, int c, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ unsigned char *dd = d; const unsigned char *ss = s;
+ while (n--) { if ((*dd++ = *ss++) == (unsigned char)c) return dd; }
+ return NULL;
+}
+#endif
+void *memmem(const void *h, size_t hl, const void *n, size_t nl)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ const char *p = h;
+ if (!nl) return (void *)h;
+ while (hl >= nl) {
+ const char *q = memchr(p, *(const char *)n, hl - nl + 1);
+ if (!q) return NULL;
+ if (!memcmp(q, n, nl)) return (void *)q;
+ hl -= q + 1 - p; p = q + 1;
+ }
+ return NULL;
+}
+#endif
+char *strcasestr(const char *h, const char *n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ size_t nl = strlen(n);
+ for (; *h; h++) if (!strncasecmp(h, n, nl)) return (char *)h;
+ return nl ? NULL : (char *)h;
+}
+#endif
+size_t strxfrm(char *d, const char *s, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ size_t l = strlen(s); if (n) strlcpy(d, s, n); return l; }
+#endif
+int strverscmp(const char *a, const char *b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ while (*a && *b) {
+ if (isdigit((unsigned char)*a) && isdigit((unsigned char)*b)) {
+ const char *ea = a, *eb = b;
+ while (*a == '0') a++;
+ while (*b == '0') b++;
+ ea = a; eb = b;
+ while (isdigit((unsigned char)*ea)) ea++;
+ while (isdigit((unsigned char)*eb)) eb++;
+ if (ea - a != eb - b) return ea - a < eb - b ? -1 : 1;
+ while (a < ea) { if (*a != *b) return (unsigned char)*a - (unsigned char)*b; a++; b++; }
+ } else {
+ if (*a != *b) return (unsigned char)*a - (unsigned char)*b;
+ a++; b++;
+ }
+ }
+ return (unsigned char)*a - (unsigned char)*b;
+}
+#endif
+int ffs(int i)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return __builtin_ffs(i); }
+#endif
+int ffsl(long i)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return __builtin_ffsl(i); }
+#endif
+int ffsll(long long i)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return __builtin_ffsll(i); }
+#endif
+void bcopy(const void *s, void *d, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ memmove(d, s, n); }
+#endif
+void bzero(void *d, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ memset(d, 0, n); }
+#endif
+int bcmp(const void *a, const void *b, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return memcmp(a, b, n); }
+#endif
+char *index(const char *s, int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strchr(s, c); }
+#endif
+char *rindex(const char *s, int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strrchr(s, c); }
+#endif
+void explicit_bzero(void *d, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ volatile char *p = d; while (n--) *p++ = 0; }
+#endif
+#ifndef C_DECLS_ONLY
+static const char *const c_signames[] = { "Unknown signal 0", "Hangup", "Interrupt", "Quit", "Illegal instruction", "Trace/breakpoint trap", "Aborted",
+ "Bus error", "Floating point exception", "Killed", "User defined signal 1", "Segmentation fault", "User defined signal 2", "Broken pipe",
+ "Alarm clock", "Terminated", "Stack fault", "Child exited", "Continued", "Stopped (signal)", "Stopped", "Stopped (tty input)",
+ "Stopped (tty output)", "Urgent I/O condition", "CPU time limit exceeded", "File size limit exceeded", "Virtual timer expired",
+ "Profiling timer expired", "Window changed", "I/O possible", "Power failure", "Bad system call" };
+#endif
+char *strsignal(int s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (char *)(s > 0 && s < 32 ? c_signames[s] : "Unknown signal"); }
+#endif
+
+/* ctype */
+int isblank(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c == ' ' || c == '\t'; }
+#endif
+int iscntrl(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (c >= 0 && c < 32) || c == 127; }
+#endif
+int isgraph(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c > 32 && c < 127; }
+#endif
+int isascii(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c >= 0 && c < 128; }
+#endif
+int toascii(int c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c & 127; }
+#endif
+
+/* numbers */
+typedef struct { int quot, rem; } div_t;
+typedef struct { long quot, rem; } ldiv_t;
+typedef struct { long long quot, rem; } lldiv_t;
+div_t div(int a, int b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ div_t r = { a / b, a % b }; return r; }
+#endif
+ldiv_t ldiv(long a, long b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ ldiv_t r = { a / b, a % b }; return r; }
+#endif
+lldiv_t lldiv(long long a, long long b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ lldiv_t r = { a / b, a % b }; return r; }
+#endif
+long long llabs(long long a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return a < 0 ? -a : a; }
+#endif
+intmax_t imaxabs(intmax_t a)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return a < 0 ? -a : a; }
+#endif
+intmax_t strtoimax(const char *s, char **e, int base)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strtoll(s, e, base); }
+#endif
+uintmax_t strtoumax(const char *s, char **e, int base)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return strtoull(s, e, base); }
+#endif
+#define RAND_MAX 2147483647
+#ifndef C_DECLS_ONLY
+static unsigned long long c_rand_state = 1;
+#endif
+void srandom(unsigned seed)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ c_rand_state = seed ? seed : 1; }
+#endif
+long random(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ c_rand_state = c_rand_state * 6364136223846793005ULL + 1442695040888963407ULL;
+ return (long)(c_rand_state >> 33);
+}
+#endif
+void srand(unsigned seed)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ srandom(seed); }
+#endif
+int rand(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (int)random(); }
+#endif
+int rand_r(unsigned *seed)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ unsigned long long s = *seed * 6364136223846793005ULL + 1442695040888963407ULL; *seed = s >> 32; return (int)((s >> 33) & RAND_MAX); }
+#endif
+void srand48(long seed)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ srandom(seed); }
+#endif
+long lrand48(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return random(); }
+#endif
+long mrand48(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (long)(int)random() * 2; }
+#endif
+double drand48(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (random() >> 7) / 16777216.0 + (random() % 128) / (16777216.0 * 128); }
+#endif
+unsigned arc4random(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ unsigned v; if (getrandom(&v, sizeof v, 0) != sizeof v) v = random(); return v; }
+#endif
+void arc4random_buf(void *b, size_t n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (getrandom(b, n, 0) != (ssize_t)n) { unsigned char *p = b; while (n--) *p++ = random(); } }
+#endif
+unsigned arc4random_uniform(unsigned n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ unsigned lim, v;
+ if (n < 2) return 0;
+ lim = -n % n;
+ do v = arc4random(); while (v < lim);
+ return v % n;
+}
+#endif
+
+void *bsearch(const void *key, const void *base, size_t n, size_t size, int (*cmp)(const void *, const void *))
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ size_t lo = 0, hi = n;
+ while (lo < hi) {
+ size_t mid = lo + (hi - lo) / 2;
+ const char *p = (const char *)base + mid * size;
+ int c = cmp(key, p);
+ if (!c) return (void *)p;
+ if (c < 0) hi = mid; else lo = mid + 1;
+ }
+ return NULL;
+}
+#endif
+#ifndef C_DECLS_ONLY
+static void c_sort_r(char *base, size_t n, size_t size, int (*cmp)(const void *, const void *, void *), void *arg, char *tmp)
+{
+ size_t mid, i, j, k;
+ if (n < 2) return;
+ mid = n / 2;
+ c_sort_r(base, mid, size, cmp, arg, tmp);
+ c_sort_r(base + mid * size, n - mid, size, cmp, arg, tmp);
+ i = 0; j = mid; k = 0;
+ while (i < mid && j < n) {
+ if (cmp(base + j * size, base + i * size, arg) < 0) memcpy(tmp + k++ * size, base + j++ * size, size);
+ else memcpy(tmp + k++ * size, base + i++ * size, size);
+ }
+ while (i < mid) memcpy(tmp + k++ * size, base + i++ * size, size);
+ while (j < n) memcpy(tmp + k++ * size, base + j++ * size, size);
+ memcpy(base, tmp, n * size);
+}
+#endif
+void qsort_r(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *, void *), void *arg)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *tmp;
+ if (n < 2 || !size) return;
+ tmp = malloc(n * size);
+ if (!tmp) return;
+ c_sort_r(base, n, size, cmp, arg, tmp);
+ free(tmp);
+}
+#endif
+
+/* atexit: exit runs them backwards, then flushes everything */
+#ifndef C_DECLS_ONLY
+static void (*c_atexit_fns[64])(void);
+#endif
+#ifndef C_DECLS_ONLY
+static int c_atexit_n;
+#endif
+int atexit(void (*f)(void))
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (c_atexit_n == 64) return -1; c_atexit_fns[c_atexit_n++] = f; return 0; }
+#endif
+void c_run_exit(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ while (c_atexit_n) c_atexit_fns[--c_atexit_n](); fflush(NULL); }
+#endif
+__attribute__((noreturn)) void _Exit(int code)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ _exit(code); }
+#endif
+int at_quick_exit(void (*f)(void))
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return atexit(f); }
+#endif
+__attribute__((noreturn)) void quick_exit(int code)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ exit(code); }
+#endif
+
+/* memory */
+int posix_memalign(void **out, size_t align, size_t size)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *p;
+ if (align < sizeof(void *) || (align & (align - 1))) return EINVAL;
+ p = malloc(size + align + sizeof(void *));
+ if (!p) return ENOMEM;
+ {
+ char *a = (char *)(((unsigned long)p + sizeof(void *) + align - 1) & ~(align - 1));
+ ((void **)a)[-1] = p;
+ *out = a;
+ }
+ return 0;
+}
+#endif
+void *aligned_alloc(size_t align, size_t size)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ void *p; return posix_memalign(&p, align, size) ? NULL : p; }
+#endif
+void *memalign(size_t align, size_t size)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return aligned_alloc(align, size); }
+#endif
+void *valloc(size_t size)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return aligned_alloc(4096, size); }
+#endif
+void *reallocarray(void *p, size_t n, size_t size)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (size && n > (size_t)-1 / size) { errno = ENOMEM; return NULL; } return realloc(p, n * size); }
+#endif
+#define alloca(n) __builtin_alloca(n)
+
+/* temporary files */
+#ifndef C_DECLS_ONLY
+static void c_fill_x(char *x)
+{
+ static const char a[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ unsigned char r[8];
+ int i;
+ arc4random_buf(r, sizeof r);
+ for (i = 0; i < 6; i++) x[i] = a[r[i] % 62];
+}
+#endif
+int mkostemps(char *tpl, int suffix, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ size_t l = strlen(tpl);
+ int tries;
+ if (l < 6 + (size_t)suffix || strncmp(tpl + l - 6 - suffix, "XXXXXX", 6)) { errno = EINVAL; return -1; }
+ for (tries = 0; tries < 100; tries++) {
+ int fd;
+ c_fill_x(tpl + l - 6 - suffix);
+ fd = open(tpl, O_RDWR | O_CREAT | O_EXCL | flags, 0600);
+ if (fd >= 0 || errno != EEXIST) return fd;
+ }
+ return -1;
+}
+#endif
+int mkstemp(char *tpl)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return mkostemps(tpl, 0, 0); }
+#endif
+int mkstemps(char *tpl, int suffix)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return mkostemps(tpl, suffix, 0); }
+#endif
+int mkostemp(char *tpl, int flags)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return mkostemps(tpl, 0, flags); }
+#endif
+char *mkdtemp(char *tpl)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ size_t l = strlen(tpl);
+ int tries;
+ if (l < 6 || strcmp(tpl + l - 6, "XXXXXX")) { errno = EINVAL; return NULL; }
+ for (tries = 0; tries < 100; tries++) {
+ c_fill_x(tpl + l - 6);
+ if (mkdir(tpl, 0700) == 0) return tpl;
+ if (errno != EEXIST) return NULL;
+ }
+ return NULL;
+}
+#endif
+char *mktemp(char *tpl)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ size_t l = strlen(tpl);
+ struct stat st;
+ if (l < 6 || strcmp(tpl + l - 6, "XXXXXX")) { errno = EINVAL; tpl[0] = 0; return tpl; }
+ do c_fill_x(tpl + l - 6); while (lstat(tpl, &st) == 0);
+ return tpl;
+}
+#endif
+char *tmpnam(char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ static char buf[L_tmpnam];
+ char *d = s ? s : buf;
+ strcpy(d, "/tmp/tmpXXXXXX");
+ mktemp(d);
+ return d;
+}
+#endif
+char *tempnam(const char *dir, const char *prefix)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *d = malloc(strlen(dir ? dir : "/tmp") + (prefix ? strlen(prefix) : 0) + 16);
+ if (!d) return NULL;
+ sprintf(d, "%s/%sXXXXXX", dir ? dir : "/tmp", prefix ? prefix : "");
+ mktemp(d);
+ return d;
+}
+#endif
+FILE *tmpfile(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char name[] = "/tmp/tmpfileXXXXXX";
+ int fd = mkstemp(name);
+ FILE *f;
+ if (fd < 0) return NULL;
+ unlink(name);
+ f = fdopen(fd, "w+");
+ if (!f) close(fd);
+ return f;
+}
+#endif
+
+/* printf pieces built on the core ones */
+int vasprintf(char **out, const char *fmt, va_list ap)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ va_list ap2;
+ int n;
+ va_copy(ap2, ap);
+ n = vsnprintf(NULL, 0, fmt, ap2);
+ va_end(ap2);
+ if (n < 0) return -1;
+ *out = malloc(n + 1);
+ if (!*out) return -1;
+ return vsnprintf(*out, n + 1, fmt, ap);
+}
+#endif
+int asprintf(char **out, const char *fmt, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ va_list ap; int n; va_start(ap, fmt); n = vasprintf(out, fmt, ap); va_end(ap); return n; }
+#endif
+int vdprintf(int fd, const char *fmt, va_list ap)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ char *s;
+ int n = vasprintf(&s, fmt, ap);
+ if (n < 0) return -1;
+ if (write(fd, s, n) < 0) n = -1;
+ free(s);
+ return n;
+}
+#endif
+int dprintf(int fd, const char *fmt, ...)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ va_list ap; int n; va_start(ap, fmt); n = vdprintf(fd, fmt, ap); va_end(ap); return n; }
+#endif
+ssize_t getdelim(char **lp, size_t *cap, int delim, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ size_t n = 0;
+ int c;
+ if (!*lp || !*cap) { *cap = 128; *lp = malloc(*cap); if (!*lp) return -1; }
+ while ((c = fgetc(f)) != EOF) {
+ if (n + 2 > *cap) { char *p = realloc(*lp, *cap * 2); if (!p) return -1; *lp = p; *cap *= 2; }
+ (*lp)[n++] = c;
+ if (c == delim) break;
+ }
+ if (!n) return -1;
+ (*lp)[n] = 0;
+ return n;
+}
+#endif
+int fputs_unlocked(const char *s, FILE *f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fputs(s, f); }
+#endif
+
+/* time */
+#define CLOCK_REALTIME 0
+#define CLOCK_MONOTONIC 1
+#define CLOCK_PROCESS_CPUTIME_ID 2
+#define CLOCK_THREAD_CPUTIME_ID 3
+#define CLOCK_MONOTONIC_RAW 4
+#define CLOCK_REALTIME_COARSE 5
+#define CLOCK_MONOTONIC_COARSE 6
+#define CLOCK_BOOTTIME 7
+#define TIMER_ABSTIME 1
+#ifdef C_DECLS_ONLY
+extern long timezone;
+#else
+long timezone;
+#endif
+#ifdef C_DECLS_ONLY
+extern int daylight;
+#else
+int daylight;
+#endif
+#ifdef C_DECLS_ONLY
+extern char *tzname[2];
+#else
+char *tzname[2] = { "UTC", "UTC" };
+#endif
+void tzset(void)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ }
+#endif
+struct tm *gmtime_r(const time_t *t, struct tm *out)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ *out = *gmtime(t); return out; }
+#endif
+struct tm *localtime_r(const time_t *t, struct tm *out)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ *out = *localtime(t); return out; }
+#endif
+double difftime(time_t a, time_t b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (double)(a - b); }
+#endif
+char *asctime_r(const struct tm *t, char *buf)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ static const char wd[] = "SunMonTueWedThuFriSat", mo[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
+ 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);
+ return buf;
+}
+#endif
+char *asctime(const struct tm *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ static char buf[32]; return asctime_r(t, buf); }
+#endif
+char *ctime_r(const time_t *t, char *buf)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return asctime_r(localtime(t), buf); }
+#endif
+char *ctime(const time_t *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ static char buf[32]; return ctime_r(t, buf); }
+#endif
+int clock_settime(int clk, const struct timespec *t)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(227, clk, t)); }
+#endif
+int settimeofday(const struct timeval *tv, const void *tz)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(164, tv, tz)); }
+#endif
+int timespec_get(struct timespec *t, int base)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (base != 1) return 0; return clock_gettime(CLOCK_REALTIME, t) < 0 ? 0 : base; }
+#endif
+struct itimerval { struct timeval it_interval, it_value; };
+#define ITIMER_REAL 0
+#define ITIMER_VIRTUAL 1
+#define ITIMER_PROF 2
+int setitimer(int which, const struct itimerval *nw, struct itimerval *old)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_setitimer, which, nw, old)); }
+#endif
+int getitimer(int which, struct itimerval *cur)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_getitimer, which, cur)); }
+#endif
+#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)
+#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)
+#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))
+#define timerclear(t) ((t)->tv_sec = (t)->tv_usec = 0)
+#define timerisset(t) ((t)->tv_sec || (t)->tv_usec)
+
+char *strptime(const char *s, const char *fmt, struct tm *tm)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ static const char *const mon[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
+ static const char *const day[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
+ for (; *fmt; fmt++) {
+ char *e;
+ long v;
+ int i;
+ if (isspace((unsigned char)*fmt)) { while (isspace((unsigned char)*s)) s++; continue; }
+ if (*fmt != '%') { if (*s++ != *fmt) return NULL; continue; }
+ fmt++;
+ if (*fmt == 'E' || *fmt == 'O') fmt++;
+ switch (*fmt) {
+ case 'Y': v = strtol(s, &e, 10); if (e == s) return NULL; tm->tm_year = v - 1900; s = e; break;
+ case 'y': v = strtol(s, &e, 10); if (e == s) return NULL; tm->tm_year = v < 69 ? v + 100 : v; s = e; break;
+ case 'm': v = strtol(s, &e, 10); if (e == s || v < 1 || v > 12) return NULL; tm->tm_mon = v - 1; s = e; break;
+ 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;
+ case 'H': v = strtol(s, &e, 10); if (e == s || v > 23) return NULL; tm->tm_hour = v; s = e; break;
+ case 'M': v = strtol(s, &e, 10); if (e == s || v > 59) return NULL; tm->tm_min = v; s = e; break;
+ case 'S': v = strtol(s, &e, 10); if (e == s || v > 61) return NULL; tm->tm_sec = v; s = e; break;
+ case 'j': v = strtol(s, &e, 10); if (e == s) return NULL; tm->tm_yday = v - 1; s = e; break;
+ case 'b': case 'B': case 'h':
+ for (i = 0; i < 12; i++) if (!strncasecmp(s, mon[i], 3)) break;
+ if (i == 12) return NULL;
+ tm->tm_mon = i; s += 3;
+ while (isalpha((unsigned char)*s)) s++;
+ break;
+ case 'a': case 'A':
+ for (i = 0; i < 7; i++) if (!strncasecmp(s, day[i], 3)) break;
+ if (i == 7) return NULL;
+ tm->tm_wday = i; s += 3;
+ while (isalpha((unsigned char)*s)) s++;
+ break;
+ case 'T': s = strptime(s, "%H:%M:%S", tm); if (!s) return NULL; break;
+ case 'R': s = strptime(s, "%H:%M", tm); if (!s) return NULL; break;
+ case 'F': s = strptime(s, "%Y-%m-%d", tm); if (!s) return NULL; break;
+ case 'D': s = strptime(s, "%m/%d/%y", tm); if (!s) return NULL; break;
+ case 'n': case 't': while (isspace((unsigned char)*s)) s++; break;
+ case 'z': if (*s == 'Z') { s++; break; } if (*s != '+' && *s != '-') return NULL; s++; while (isdigit((unsigned char)*s) || *s == ':') s++; break;
+ case 'Z': while (isalpha((unsigned char)*s)) s++; break;
+ case '%': if (*s++ != '%') return NULL; break;
+ default: return NULL;
+ }
+ }
+ return (char *)s;
+}
+#endif
+/* ---- signals the POSIX way [chamesle] ---- */
+#define SIGILL 4
+#define SIGTRAP 5
+#define SIGABRT 6
+#define SIGIOT 6
+#define SIGBUS 7
+#define SIGFPE 8
+#define SIGUSR1 10
+#define SIGSEGV 11
+#define SIGUSR2 12
+#define SIGPIPE 13
+#define SIGALRM 14
+#define SIGSTKFLT 16
+#define SIGCONT 18
+#define SIGSTOP 19
+#define SIGTSTP 20
+#define SIGTTIN 21
+#define SIGTTOU 22
+#define SIGURG 23
+#define SIGXCPU 24
+#define SIGXFSZ 25
+#define SIGVTALRM 26
+#define SIGPROF 27
+#define SIGWINCH 28
+#define SIGIO 29
+#define SIGPOLL 29
+#define SIGPWR 30
+#define SIGSYS 31
+#define NSIG 65
+#define SA_NOCLDSTOP 1
+#define SA_NOCLDWAIT 2
+#define SA_SIGINFO 4
+#define SA_ONSTACK 0x08000000
+#define SA_RESTART 0x10000000
+#define SA_NODEFER 0x40000000
+#define SA_RESETHAND 0x80000000
+#define SIG_BLOCK 0
+#define SIG_UNBLOCK 1
+#define SIG_SETMASK 2
+typedef int sig_atomic_t;
+typedef struct { unsigned long __bits[16]; } sigset_t;
+typedef struct {
+ int si_signo, si_errno, si_code, __pad0;
+ union {
+ struct { pid_t si_pid; uid_t si_uid; int si_status; };
+ void *si_addr;
+ char __pad[112];
+ };
+} siginfo_t;
+struct sigaction {
+ union { sighandler_t sa_handler; void (*sa_sigaction)(int, siginfo_t *, void *); };
+ sigset_t sa_mask;
+ int sa_flags;
+ void (*sa_restorer)(void);
+};
+typedef struct { void *ss_sp; int ss_flags; size_t ss_size; } stack_t;
+#define SS_ONSTACK 1
+#define SS_DISABLE 2
+#define MINSIGSTKSZ 2048
+#define SIGSTKSZ 8192
+
+int sigemptyset(sigset_t *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ memset(s, 0, sizeof *s); return 0; }
+#endif
+int sigfillset(sigset_t *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ memset(s, 0xff, sizeof *s); return 0; }
+#endif
+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; }
+#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; }
+#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; }
+#endif
+int sigisemptyset(const sigset_t *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return !s->__bits[0]; }
+#endif
+int sigaction(int n, const struct sigaction *act, struct sigaction *old)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ 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]; }
+ 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; }
+ return 0;
+}
+#endif
+int sigprocmask(int how, const sigset_t *set, sigset_t *old)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ unsigned 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; }
+ return 0;
+}
+#endif
+int pthread_sigmask(int how, const sigset_t *set, sigset_t *old)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sigprocmask(how, set, old) < 0 ? errno : 0; }
+#endif
+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; }
+#endif
+int sigsuspend(const sigset_t *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_rt_sigsuspend, s->__bits, 8)); }
+#endif
+int sigaltstack(const stack_t *ss, stack_t *old)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys2(SYS_sigaltstack, ss, old)); }
+#endif
+int raise(int sig)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return fail(sys3(SYS_tgkill, getpid(), gettid(), sig)); }
+#endif
+int killpg(int pg, int sig)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return kill(-pg, sig); }
+#endif
+int sigignore(int n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return signal(n, SIG_IGN) == SIG_ERR ? -1 : 0; }
+#endif
+sighandler_t sysv_signal(int n, sighandler_t f)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return signal(n, f); }
+#endif
+void psignal(int sig, const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ fprintf(stderr, "%s%s%s\n", s ? s : "", s && *s ? ": " : "", strsignal(sig)); }
+#endif
+typedef struct { jmp_buf b; } sigjmp_buf[1];
+#define sigsetjmp(env, save) setjmp((env)->b)
+#define siglongjmp(env, v) longjmp((env)->b, v)
+#define _setjmp(b) setjmp(b)
+#define _longjmp(b, v) longjmp(b, v)
+/* ---- libm: sqrt and the roundings by hand, the transcendental ones on the x87 [chamesle] ---- */
+#define M_E 2.7182818284590452354
+#define M_LOG2E 1.4426950408889634074
+#define M_LOG10E 0.43429448190325182765
+#define M_LN2 0.69314718055994530942
+#define M_LN10 2.30258509299404568402
+#define M_PI 3.14159265358979323846
+#define M_PI_2 1.57079632679489661923
+#define M_PI_4 0.78539816339744830962
+#define M_1_PI 0.31830988618379067154
+#define M_2_PI 0.63661977236758134308
+#define M_2_SQRTPI 1.12837916709551257390
+#define M_SQRT2 1.41421356237309504880
+#define M_SQRT1_2 0.70710678118654752440
+#define HUGE_VAL (__builtin_huge_val())
+#define HUGE_VALF (__builtin_huge_valf())
+#define INFINITY (__builtin_inff())
+#define NAN (__builtin_nanf(""))
+#define FP_NAN 0
+#define FP_INFINITE 1
+#define FP_ZERO 2
+#define FP_SUBNORMAL 3
+#define FP_NORMAL 4
+#define FP_ILOGB0 (-2147483647 - 1)
+#define FP_ILOGBNAN (-2147483647 - 1)
+#define MATH_ERRNO 1
+#define math_errhandling 1
+typedef float float_t;
+typedef double double_t;
+#define isnan(x) __builtin_isnan(x)
+#define isinf(x) __builtin_isinf(x)
+#define isfinite(x) __builtin_isfinite(x)
+#define isnormal(x) __builtin_isnormal(x)
+#define signbit(x) __builtin_signbit(x)
+#define fpclassify(x) __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
+#define isgreater(a, b) __builtin_isgreater(a, b)
+#define isgreaterequal(a, b) __builtin_isgreaterequal(a, b)
+#define isless(a, b) __builtin_isless(a, b)
+#define islessequal(a, b) __builtin_islessequal(a, b)
+#define islessgreater(a, b) __builtin_islessgreater(a, b)
+#define isunordered(a, b) __builtin_isunordered(a, b)
+
+#ifndef C_DECLS_ONLY
+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; }
+#endif
+#ifndef C_DECLS_ONLY
+static long double c_f2xm1(long double x) { long double r; __asm__("f2xm1" : "=t"(r) : "0"(x)); return r; }
+#endif
+#ifndef C_DECLS_ONLY
+static long double c_fscale(long double x, long double n) { long double r; __asm__("fscale" : "=t"(r) : "0"(x), "u"(n)); return r; }
+#endif
+#ifndef C_DECLS_ONLY
+static long double c_frndint(long double x) { long double r; __asm__("frndint" : "=t"(r) : "0"(x)); return r; }
+#endif
+#ifndef C_DECLS_ONLY
+static long double c_exp2(long double x)
+{
+ long double n, f;
+ if (x > 16400) return __builtin_infl();
+ if (x < -16400) return 0;
+ n = c_frndint(x); f = x - n;
+ return c_fscale(c_f2xm1(f) + 1, n);
+}
+#endif
+
+double fabs(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ union { double d; unsigned long u; } v = { x }; v.u &= ~(1UL << 63); return v.d; }
+#endif
+float fabsf(float x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)fabs(x); }
+#endif
+long double fabsl(long double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return x < 0 ? -x : x; }
+#endif
+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; }
+#endif
+float copysignf(float x, float y)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)copysign(x, y); }
+#endif
+double sqrt(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ double r; __asm__("sqrtsd %1, %0" : "=x"(r) : "x"(x)); return r; }
+#endif
+float sqrtf(float x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ float r; __asm__("sqrtss %1, %0" : "=x"(r) : "x"(x)); return r; }
+#endif
+long double sqrtl(long double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ long double r; __asm__("fsqrt" : "=t"(r) : "0"(x)); return r; }
+#endif
+double trunc(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ union { double d; unsigned 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);
+ return v.d;
+}
+#endif
+double floor(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ double t = trunc(x); return t > x ? t - 1 : t; }
+#endif
+double ceil(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ double t = trunc(x); return t < x ? t + 1 : t; }
+#endif
+double round(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ double t = trunc(x); if (fabs(x - t) >= 0.5) t += copysign(1.0, x); return t; }
+#endif
+double rint(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (double)c_frndint(x); }
+#endif
+double nearbyint(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return rint(x); }
+#endif
+float floorf(float x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)floor(x); }
+#endif
+float ceilf(float x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)ceil(x); }
+#endif
+float truncf(float x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)trunc(x); }
+#endif
+float roundf(float x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)round(x); }
+#endif
+float rintf(float x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)rint(x); }
+#endif
+long double floorl(long double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ long double t = (long double)(long long)x; return t > x ? t - 1 : t; }
+#endif
+long double ceill(long double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ long double t = (long double)(long long)x; return t < x ? t + 1 : t; }
+#endif
+long double truncl(long double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (long double)(long long)x; }
+#endif
+long lround(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (long)round(x); }
+#endif
+long long llround(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (long long)round(x); }
+#endif
+long lrint(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (long)rint(x); }
+#endif
+long long llrint(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (long long)rint(x); }
+#endif
+double fmod(double x, double y)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ long double r = x, d = y;
+ if (y == 0 || isinf(x) || isnan(x) || isnan(y)) return __builtin_nan("");
+ if (isinf(y)) return x;
+ __asm__ __volatile__("1: fprem\n fnstsw %%ax\n testb $4, %%ah\n jnz 1b" : "+t"(r) : "u"(d) : "ax", "cc");
+ return (double)r;
+}
+#endif
+float fmodf(float x, float y)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)fmod(x, y); }
+#endif
+double remainder(double x, double y)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ double n = rint(x / y); return x - n * y; }
+#endif
+double modf(double x, double *ip)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ double t = trunc(x); *ip = t; return isinf(x) ? copysign(0.0, x) : x - t; }
+#endif
+double frexp(double x, int *e)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ union { double d; unsigned long u; } v = { x };
+ int ex = (int)((v.u >> 52) & 0x7ff);
+ if (!ex) {
+ if (x == 0) { *e = 0; return x; }
+ x *= 18014398509481984.0;
+ 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);
+ return v.d;
+}
+#endif
+double scalbn(double x, int n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return ldexp(x, n); }
+#endif
+double scalbln(double x, long n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return ldexp(x, n > 100000 ? 100000 : n < -100000 ? -100000 : (int)n); }
+#endif
+int ilogb(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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; }
+#endif
+double logb(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (double)ilogb(x); }
+#endif
+double fmin(double a, double b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isnan(a) ? b : isnan(b) ? a : a < b ? a : b; }
+#endif
+double fmax(double a, double b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return isnan(a) ? b : isnan(b) ? a : a > b ? a : b; }
+#endif
+float fminf(float a, float b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)fmin(a, b); }
+#endif
+float fmaxf(float a, float b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)fmax(a, b); }
+#endif
+double fdim(double a, double b)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return a > b ? a - b : 0; }
+#endif
+double fma(double a, double b, double c)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (double)((long double)a * b + c); }
+#endif
+double nan(const char *s)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ (void)s; return __builtin_nan(""); }
+#endif
+double nextafter(double x, double y)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ union { double d; unsigned 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; }
+ if ((x < y) == (x > 0)) v.u++; else v.u--;
+ return v.d;
+}
+#endif
+
+double exp2(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (double)c_exp2(x); }
+#endif
+double exp(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (double)c_exp2((long double)x * 1.4426950408889634073599246810018921L); }
+#endif
+double expm1(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ long double t = (long double)x * 1.4426950408889634073599246810018921L;
+ if (fabs(x) < 0.3) return (double)(c_f2xm1(t));
+ return (double)(c_exp2(t) - 1);
+}
+#endif
+double log(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (x < 0) return __builtin_nan(""); return (double)c_fyl2x(0.693147180559945309417232121458176568L, x); }
+#endif
+double log2(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (x < 0) return __builtin_nan(""); return (double)c_fyl2x(1, x); }
+#endif
+double log10(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (x < 0) return __builtin_nan(""); return (double)c_fyl2x(0.301029995663981195213738894724493027L, x); }
+#endif
+double log1p(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ long double r;
+ if (x < -1) return __builtin_nan("");
+ if (fabs(x) < 0.29) { __asm__("fyl2xp1" : "=t"(r) : "0"((long double)x), "u"(0.693147180559945309417232121458176568L) : "st(1)"); return (double)r; }
+ return log(1 + x);
+}
+#endif
+double pow(double x, double y)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ long double r;
+ int yint;
+ if (y == 0 || x == 1) return 1;
+ if (isnan(x) || isnan(y)) return __builtin_nan("");
+ yint = fabs(y) < 9007199254740992.0 && y == trunc(y);
+ if (x == 0) {
+ if (y < 0) return yint && fmod(y, 2) != 0 ? copysign(HUGE_VAL, x) : HUGE_VAL;
+ return yint && fmod(y, 2) != 0 ? x : 0;
+ }
+ if (isinf(y)) { double a = fabs(x); return a == 1 ? 1 : (a > 1) == (y > 0) ? HUGE_VAL : 0; }
+ 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); }
+ if (x < 0) {
+ if (!yint) return __builtin_nan("");
+ r = c_exp2((long double)y * c_fyl2x(1, -(long double)x));
+ return fmod(y, 2) != 0 ? -(double)r : (double)r;
+ }
+ return (double)c_exp2((long double)y * c_fyl2x(1, (long double)x));
+}
+#endif
+double sin(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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; }
+#endif
+double cos(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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; }
+#endif
+double tan(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ long double r = x, one; if (isinf(x) || isnan(x)) return __builtin_nan(""); __asm__("fptan" : "+t"(r), "=u"(one)); return (double)one; }
+#endif
+double atan2(double y, double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ long double r; __asm__("fpatan" : "=t"(r) : "0"((long double)x), "u"((long double)y) : "st(1)"); return (double)r; }
+#endif
+double atan(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return atan2(x, 1); }
+#endif
+double asin(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (fabs(x) > 1) return __builtin_nan(""); return atan2(x, sqrt((1 - x) * (1 + x))); }
+#endif
+double acos(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (fabs(x) > 1) return __builtin_nan(""); return atan2(sqrt((1 - x) * (1 + x)), x); }
+#endif
+double sinh(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ double e = expm1(fabs(x)); double r = e < 1e300 ? (e + e / (e + 1)) / 2 : exp(fabs(x)) / 2; return copysign(r, x); }
+#endif
+double cosh(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ double e = exp(fabs(x)); return (e + 1 / e) / 2; }
+#endif
+double tanh(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ double a = fabs(x), e; if (a > 20) return copysign(1.0, x); e = expm1(2 * a); return copysign(e / (e + 2), x); }
+#endif
+double asinh(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ double a = fabs(x); return copysign(log(a + sqrt(a * a + 1)), x); }
+#endif
+double acosh(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (x < 1) return __builtin_nan(""); return log(x + sqrt(x * x - 1)); }
+#endif
+double atanh(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return 0.5 * log1p(2 * x / (1 - x)); }
+#endif
+double hypot(double x, double y)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ double a = fabs(x), b = fabs(y), t;
+ if (isinf(a) || isinf(b)) return HUGE_VAL;
+ if (a < b) { t = a; a = b; b = t; }
+ if (a == 0) return 0;
+ t = b / a;
+ return a * sqrt(1 + t * t);
+}
+#endif
+double cbrt(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ double r;
+ if (x == 0 || isinf(x) || isnan(x)) return x;
+ r = copysign(pow(fabs(x), 1.0 / 3.0), x);
+ return r - (r * r * r - x) / (3 * r * r);
+}
+#endif
+double c_erf_series(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ 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; }
+#endif
+#ifdef C_DECLS_ONLY
+extern double erfc(double x);
+#else
+double erfc(double x);
+#endif
+double erf(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ if (fabs(x) < 2.5) return c_erf_series(x); return x > 0 ? 1 - erfc(x) : erfc(-x) - 1; }
+#endif
+double erfc(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ double f = 0;
+ int n;
+ if (x < 2.5) return 1 - erf(x);
+ for (n = 60; n >= 1; n--) f = n / 2.0 / (x + f);
+ return exp(-x * x) / (1.7724538509055160 * (x + f));
+}
+#endif
+double lgamma(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ static const double c[] = { 76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5 };
+ double y, tmp, ser;
+ int j;
+ if (x < 0.5) return log(M_PI / fabs(sin(M_PI * x))) - lgamma(1 - x);
+ y = x; tmp = x + 5.5; tmp -= (x + 0.5) * log(tmp); ser = 1.000000000190015;
+ for (j = 0; j < 6; j++) ser += c[j] / ++y;
+ return -tmp + log(2.5066282746310005 * ser / x);
+}
+#endif
+double tgamma(double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{
+ if (x == trunc(x) && x > 0 && x < 24) { double r = 1; int i; for (i = 2; i < (int)x; i++) r *= i; return r; }
+ if (x < 0.5) return M_PI / (sin(M_PI * x) * tgamma(1 - x));
+ return exp(lgamma(x));
+}
+#endif
+#define c_float_fn(name) float name##f(float x) { return (float)name(x); }
+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)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)pow(x, y); }
+#endif
+float atan2f(float y, float x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)atan2(y, x); }
+#endif
+float hypotf(float x, float y)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)hypot(x, y); }
+#endif
+float ldexpf(float x, int n)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)ldexp(x, n); }
+#endif
+float frexpf(float x, int *e)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (float)frexp(x, e); }
+#endif
+long double expl(long double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c_exp2(x * 1.4426950408889634073599246810018921L); }
+#endif
+long double logl(long double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return c_fyl2x(0.693147180559945309417232121458176568L, x); }
+#endif
+long double powl(long double x, long double y)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return (long double)pow((double)x, (double)y); }
+#endif
+long double sinl(long double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return sin((double)x); }
+#endif
+long double cosl(long double x)
+#ifdef C_DECLS_ONLY
+;
+#else
+{ return cos((double)x); }
+#endif
#endif
diff --git a/rifle.c b/rifle.c
@@ -73,7 +73,6 @@ static int finish_stdout(void)
struct opt { const char *name; int arg; int val; };
static char **ops;
static int nops;
-static char *optarg;
static int opt_i = 1, opt_c, opt_stop;
static void opts_reset(void) { opt_i = 1; opt_c = 0; opt_stop = 0; nops = 0; optarg = NULL; }
@@ -9092,6 +9091,179 @@ static int t_find(int argc, char **argv)
return finish_stdout() ? 1 : f_status;
}
+/* ---- xargs ---- */
+
+struct xitem { char *s; int eol; };
+
+/* the input as items. by default blanks and newlines separate them, quotes and backslashes group, and a line that ends
+ * in a blank carries on into the next one. with -I only newlines separate. -0 and -d split at one byte and nothing else */
+static size_t x_items(char *d, size_t len, int delim, int onlynl, struct xitem **out, int *badquote)
+{
+ struct xitem *v = NULL;
+ size_t n = 0, cap = 0, i = 0;
+ char *b = xmalloc(len + 1);
+ while (i < len) {
+ size_t bl = 0;
+ int quote = 0, eol = 0;
+ if (delim >= 0) {
+ while (i < len && d[i] != delim) b[bl++] = d[i++];
+ i++;
+ eol = 1;
+ } else {
+ while (i < len && (d[i] == '\n' || d[i] == ' ' || d[i] == '\t')) i++;
+ if (i >= len) break;
+ while (i < len) {
+ char c = d[i];
+ if (quote) {
+ if (c == '\n') break;
+ if (c == quote) quote = 0; else b[bl++] = c;
+ i++;
+ continue;
+ }
+ if (c == '\n') { eol = 1; i++; break; }
+ if (!onlynl && (c == ' ' || c == '\t')) {
+ while (i < len && (d[i] == ' ' || d[i] == '\t')) i++;
+ if (i < len && d[i] == '\n') i++;
+ break;
+ }
+ if (c == '\'' || c == '"') { quote = c; i++; continue; }
+ if (c == '\\' && i + 1 < len) { b[bl++] = d[i + 1]; i += 2; continue; }
+ b[bl++] = c; i++;
+ }
+ if (quote) { *badquote = quote; break; }
+ if (i >= len) eol = 1;
+ }
+ if (*badquote) break;
+ if (n == cap) { cap = cap ? cap * 2 : 64; v = xrealloc(v, cap * sizeof *v); }
+ b[bl] = 0;
+ v[n].s = xstrdup(b); v[n].eol = eol; n++;
+ }
+ free(b);
+ *out = v;
+ return n;
+}
+
+static void xq_die(int quote)
+{
+ die(1, "unmatched %s quote; by default quotes are special to xargs unless you use the -0 option", quote == '"' ? "double" : "single");
+}
+
+static int x_exec(char **argv, int verbose)
+{
+ int st, i;
+ pid_t pid;
+ if (verbose) { for (i = 0; argv[i]; i++) fprintf(stderr, "%s%s", i ? " " : "", argv[i]); fputc('\n', stderr); }
+ fflush(stdout);
+ pid = fork();
+ if (pid < 0) die(1, "cannot fork: %s", strerror(errno));
+ if (!pid) {
+ execvp(argv[0], argv);
+ fprintf(stderr, "%s: failed to run command '%s': %s\n", prog, argv[0], strerror(errno));
+ _exit(errno == ENOENT ? 127 : 126);
+ }
+ while (waitpid(pid, &st, 0) < 0) if (errno != EINTR) die(1, "waitpid: %s", strerror(errno));
+ if (st & 0x7f) die(125, "%s: terminated by signal %d", argv[0], st & 0x7f);
+ st = (st >> 8) & 255;
+ if (st == 126 || st == 127) exit(st);
+ if (st == 255) die(124, "%s: exited with status 255; aborting", argv[0]);
+ return st;
+}
+
+static int t_xargs(int argc, char **argv)
+{
+ static const struct opt lo[] = {
+ { "null", 0, '0' }, { "arg-file", 1, 'a' }, { "delimiter", 1, 'd' }, { "eof", 2, 'e' }, { "replace", 2, 'i' }, { "max-lines", 2, 'l' },
+ { "max-args", 1, 'n' }, { "max-procs", 1, 'P' }, { "no-run-if-empty", 0, 'r' }, { "max-chars", 1, 's' }, { "verbose", 0, 't' },
+ { "exit", 0, 'x' }, { NULL, 0, 0 } };
+ int c, delim = -1, norun = 0, verbose = 0, exitbig = 0, fd = 0, status = 0, ncmd, i, badquote = 0;
+ const char *repl = NULL, *eofs = NULL;
+ unsigned long maxargs = 0, maxlines = 0, maxchars = 131072, v;
+ char **cmd, *d;
+ size_t len, n, k, cmdsize = 0;
+ struct xitem *it;
+ usage_status = 1;
+ opts_reset();
+ while ((c = getopts(argc, argv, "+0a:d:E:e::I:i::L:l::n:P:rs:tx", lo)))
+ switch (c) {
+ case '0': delim = 0; break;
+ case 'a': fd = open(optarg, O_RDONLY); if (fd < 0) die(1, "Cannot open input file '%s': %s", optarg, strerror(errno)); break;
+ case 'd':
+ if (!strcmp(optarg, "\\n")) delim = '\n'; else if (!strcmp(optarg, "\\t")) delim = '\t'; else if (!strcmp(optarg, "\\0")) delim = 0;
+ else if (!strcmp(optarg, "\\\\")) delim = '\\'; else if (strlen(optarg) == 1) delim = (unsigned char)optarg[0];
+ else die(1, "Invalid input delimiter specification %s: the delimiter must be either a single character or an escape sequence starting with \\", optarg);
+ break;
+ case 'E': eofs = optarg; break;
+ case 'e': eofs = optarg && *optarg ? optarg : NULL; break;
+ case 'I': repl = optarg; maxlines = 1; maxargs = 0; break;
+ case 'i': repl = optarg ? optarg : "{}"; maxlines = 1; maxargs = 0; break;
+ case 'L': case 'l':
+ if (optarg) { if (!parse_ulong(optarg, &v) || !v) die(1, "invalid number for -%c option", c); maxlines = v; } else maxlines = 1;
+ maxargs = 0; repl = NULL;
+ break;
+ case 'n': if (!parse_ulong(optarg, &v) || !v) die(1, "invalid number for -n option"); maxargs = v; maxlines = 0; repl = NULL; break;
+ case 's': if (!parse_ulong(optarg, &maxchars)) die(1, "invalid number for -s option"); break;
+ case 'r': norun = 1; break;
+ case 't': verbose = 1; break;
+ case 'x': exitbig = 1; break;
+ case 'P': break;
+ }
+ ncmd = nops ? nops : 1;
+ cmd = xmalloc((ncmd + 2) * sizeof *cmd);
+ if (nops) memcpy(cmd, ops, nops * sizeof *cmd); else cmd[0] = "echo";
+ for (i = 0; i < ncmd; i++) cmdsize += strlen(cmd[i]) + 1;
+ if (read_all(fd, &d, &len) < 0) die(1, "%s", strerror(errno));
+ n = x_items(d, len, delim, repl != NULL, &it, &badquote);
+ if (eofs && delim < 0) for (k = 0; k < n; k++) if (!strcmp(it[k].s, eofs)) { n = k; break; }
+ if (repl) {
+ for (k = 0; k < n; k++) {
+ char **av = xmalloc((ncmd + 1) * sizeof *av);
+ for (i = 0; i < ncmd; i++) {
+ const char *p = cmd[i], *hit;
+ struct sbuf b = { 0 };
+ size_t rl = strlen(repl);
+ while (*repl && (hit = strstr(p, repl))) { sb_add(&b, p, hit - p); sb_add(&b, it[k].s, strlen(it[k].s)); p = hit + rl; }
+ sb_add(&b, p, strlen(p));
+ sb_add(&b, "", 1);
+ av[i] = b.p;
+ }
+ av[ncmd] = NULL;
+ if (x_exec(av, verbose)) status = 123;
+ }
+ if (badquote) xq_die(badquote);
+ return status;
+ }
+ {
+ char **batch = xmalloc((ncmd + n + 1) * sizeof *batch);
+ size_t nb = 0, size = cmdsize, lines = 0;
+ memcpy(batch, cmd, ncmd * sizeof *batch);
+ if (cmdsize > maxchars) die(1, "can not fit single argument within argument list size limit");
+ for (k = 0; k < n; k++) {
+ size_t l = strlen(it[k].s) + 1;
+ if (nb && size + l > maxchars) {
+ if (exitbig && (maxargs || maxlines)) die(1, "argument line too long");
+ batch[ncmd + nb] = NULL;
+ if (x_exec(batch, verbose)) status = 123;
+ nb = 0; size = cmdsize; lines = 0;
+ }
+ if (size + l > maxchars) die(1, "can not fit single argument within argument list size limit");
+ batch[ncmd + nb++] = it[k].s;
+ size += l;
+ if (it[k].eol) lines++;
+ if ((maxargs && nb >= maxargs) || (maxlines && lines >= maxlines)) {
+ batch[ncmd + nb] = NULL;
+ if (x_exec(batch, verbose)) status = 123;
+ nb = 0; size = cmdsize; lines = 0;
+ }
+ }
+ if (nb || (!n && !norun && !badquote)) {
+ batch[ncmd + nb] = NULL;
+ if (x_exec(batch, verbose)) status = 123;
+ }
+ }
+ if (badquote) xq_die(badquote);
+ return status;
+}
+
/* ---- the table and main ---- */
struct applet { const char *name; int (*fn)(int, char **); const char *usage; const char *about; };
@@ -9188,6 +9360,7 @@ static const struct applet applets[] = {
{ "unlink", t_unlink, "FILE", "Remove FILE with the unlink call." },
{ "wc", t_wc, "[OPTION]... [FILE]...", "Print newline, word, and byte counts for each FILE." },
{ "whoami", t_whoami, "", "Print the user name for the current effective user ID." },
+ { "xargs", t_xargs, "[OPTION]... COMMAND [INITIAL-ARGS]...", "Build and execute command lines from standard input." },
{ "yes", t_yes, "[STRING]...", "Repeatedly output a line with all specified STRING(s), or 'y'." },
};
#define NAPPLETS ((int)(sizeof applets / sizeof applets[0]))
diff --git a/tests/cases b/tests/cases
@@ -1650,3 +1650,148 @@ mkdir -p d; touch d/a; $X find d -type d -a ! -name d
mkdir -p d; touch d/a; $X find d ! ! -type f
mkdir -p d; touch d/a; $X find d \( ! -type d \)
mkdir -p d; touch d/a; $X find d -type f -a \( -name a -o -name b \)
+# xargs
+printf 'a b c\n' | $X xargs echo
+printf 'a b c\n' | $X xargs
+printf 'a\nb\nc\n' | $X xargs echo
+printf 'a b\nc d\n' | $X xargs -n1 echo
+printf 'a b\nc d\n' | $X xargs -n 2 echo
+printf 'a b\nc d\ne\n' | $X xargs -n3 echo
+printf 'a b\nc d\ne\n' | $X xargs --max-args=3 echo
+printf 'a b\nc d\ne\n' | $X xargs -n0 echo; echo $?
+printf 'a b\nc d\ne\n' | $X xargs -nx echo; echo $?
+printf 'a b\nc d\n' | $X xargs echo pre
+printf 'a b\nc d\n' | $X xargs -n1 echo pre
+printf 'a b\nc d\n' | $X xargs -- echo
+printf 'a b\nc d\n' | $X xargs echo -n
+printf 'a b\nc d\n' | $X xargs -- echo -n
+printf 'a b\nc d\n' | $X xargs -n1 -- echo --
+printf '' | $X xargs echo
+printf '' | $X xargs -r echo
+printf '' | $X xargs --no-run-if-empty echo
+printf '\n\n' | $X xargs echo hi
+printf '\n \n' | $X xargs -r echo hi
+printf '' | $X xargs -n1 echo hi
+printf '' | $X xargs -I{} echo [{}]
+printf '' | $X xargs -L1 echo hi
+printf 'a b\n' | $X xargs -I{} echo [{}]
+printf 'a\nb\n' | $X xargs -I{} echo [{}] {}
+printf 'a\n\nb\n' | $X xargs -I{} echo [{}]
+printf ' a b \nc\n' | $X xargs -I{} echo '[{}]'
+printf 'a\nb\n' | $X xargs -I X echo X-X
+printf 'a\nb\n' | $X xargs -i echo [{}]
+printf 'a\nb\n' | $X xargs -iZ echo Z.Z
+printf 'a\nb\n' | $X xargs --replace echo {}!
+printf 'a\nb\n' | $X xargs --replace=% echo %%
+printf 'a\nb\n' | $X xargs -I{} echo x
+printf 'a\nb\n' | $X xargs -I{} echo {} {} {}
+printf 'a\nb\n' | $X xargs -I{} -n2 echo {}
+printf 'a b\nc\n' | $X xargs -L1 echo
+printf 'a b\nc\n' | $X xargs -L 2 echo
+printf 'a b\nc \nd\n' | $X xargs -L1 echo X
+printf 'a b\nc \nd\n' | $X xargs -L2 echo X
+printf 'a b\nc d\ne f\n' | $X xargs -l echo
+printf 'a b\nc d\ne f\n' | $X xargs -l2 echo
+printf 'a b\nc d\ne f\n' | $X xargs --max-lines=2 echo
+printf 'a b\nc d\ne f\n' | $X xargs --max-lines echo
+printf 'a b\nc d\ne f\n' | $X xargs -L0 echo; echo $?
+printf 'a b\nc d\ne f\n' | $X xargs -L1 -n5 echo
+printf 'a b\nc d\ne f\n' | $X xargs -n5 -L1 echo
+printf "a 'b c' \"d e\" f\\\\ g\n" | $X xargs -n1 echo
+printf "a 'b c' \"d e\" f\\\\ g\n" | $X xargs -n1 printf '[%s]\n'
+printf "a'b\n" | $X xargs echo; echo $?
+printf 'a"b\n' | $X xargs echo; echo $?
+printf "a 'b\n" | $X xargs echo; echo $?
+printf 'a\\ b\n' | $X xargs -n1 echo
+printf "''\n" | $X xargs -n1 printf '[%s]\n'
+printf '""\n' | $X xargs printf '[%s]\n'
+printf "a''b\n" | $X xargs printf '[%s]\n'
+printf 'a\\\nb\n' | $X xargs printf '[%s]\n'
+printf "a 'b\\\\'c' d\n" | $X xargs printf '[%s]\n'
+printf '"a\\"b"\n' | $X xargs printf '[%s]\n'
+printf 'a\tb\n' | $X xargs -n1 echo
+printf 'a b' | $X xargs -n1 echo
+printf 'a b c' | $X xargs -s 8 echo
+printf 'a b c' | $X xargs -s 9 echo
+printf 'a b c' | $X xargs -s 7 echo
+printf 'a b c' | $X xargs -s 20 echo
+printf 'a b c' | $X xargs --max-chars=11 echo
+printf 'a b c' | $X xargs -x -s 4 echo; echo $?
+printf 'a b c' | $X xargs -x -s 9 echo; echo $?
+printf 'a b c' | $X xargs -x -n1 echo; echo $?
+printf 'aaaa bbbb cc d\n' | $X xargs -s 14 echo
+printf 'aaaa bbbb cc d\n' | $X xargs -s 12 printf '[%s]'; echo
+printf 'aaaa bbbb cc d\n' | $X xargs -s 10 echo; echo $?
+printf 'a\nSTOP\nb\n' | $X xargs -E STOP echo
+printf 'a\nSTOP\nb\n' | $X xargs -ESTOP echo
+printf 'a\nSTOP\nb\n' | $X xargs -e STOP echo
+printf 'a\nSTOP\nb\n' | $X xargs -eSTOP echo
+printf 'a\nSTOP\nb\n' | $X xargs echo
+printf 'a b STOP c\n' | $X xargs -E STOP echo
+printf 'a b STOP c\n' | $X xargs -E STOP -n1 echo
+printf 'a\nSTOP\nb\n' | $X xargs -0 -E STOP echo | od -c | head -2
+printf 'a,b,c' | $X xargs -d, echo
+printf 'a,b,c\n' | $X xargs -d, echo
+printf 'a,b,,c' | $X xargs -d, -n1 printf '[%s]\n'
+printf 'a b,c d' | $X xargs -d, -n1 printf '[%s]\n'
+printf "a'b,c" | $X xargs -d, -n1 printf '[%s]\n'
+printf 'a\nb\n' | $X xargs -d '\n' -n1 printf '[%s]\n'
+printf 'a b\nc d\n' | $X xargs -d '\n' printf '[%s]\n'
+printf 'a\tb\tc' | $X xargs -d '\t' printf '[%s]\n'
+printf 'a\\b\\c' | $X xargs -d '\\' printf '[%s]\n'
+printf 'a,b' | $X xargs -d ,, echo; echo $?
+printf 'a,b' | $X xargs -d '' echo; echo $?
+printf 'a\0b\0' | $X xargs -0 echo | od -c | head -2
+printf 'a\0b\0' | $X xargs -0 -n1 echo
+printf 'a\0b' | $X xargs -0 -n1 echo
+printf 'a\0\0b\0' | $X xargs -0 -n1 printf '[%s]\n'
+printf 'a b\0c d\0' | $X xargs -0 printf '[%s]\n'
+printf 'a\nb\0c\n' | $X xargs -0 printf '[%s]\n'
+printf "a'b\0" | $X xargs -0 printf '[%s]\n'
+printf 'a\0b\0' | $X xargs --null -n1 echo
+printf 'a\0' | $X xargs -0 -I{} echo [{}]
+printf 'a b\0c d\0' | $X xargs -0 -I{} echo [{}]
+printf 'a b\0c d\0' | $X xargs -0 -L1 echo
+printf 'a\nb\n' > in; $X xargs -a in echo
+printf 'a\nb\n' > in; $X xargs --arg-file=in -n1 echo
+printf 'a\nb\n' > in; $X xargs -a nope echo; echo $?
+printf 'a\nb\n' > in; $X xargs -a in -a in2 echo; echo $?
+printf 'a\nb\n' | $X xargs -P 2 -n1 echo | sort
+printf 'a\nb\n' | $X xargs -P1 -n1 echo
+printf 'a\nb\n' | $X xargs -n1 sh -c 'exit 1'; echo $?
+printf 'a\nb\n' | $X xargs -n1 false; echo $?
+printf 'a\nb\n' | $X xargs -n1 true; echo $?
+printf 'a\nb\n' | $X xargs false; echo $?
+printf 'a\nb\n' | $X xargs nosuchcommand; echo $?
+printf 'a\nb\n' | $X xargs -n1 nosuchcommand; echo $?
+printf 'a\nb\n' | $X xargs /; echo $?
+printf 'a\nb\n' | $X xargs -n1 sh -c 'exit 255'; echo $?
+printf 'a\nb\n' | $X xargs -n1 sh -c 'kill -9 $$'; echo $?
+printf 'a\nb\n' | $X xargs -n1 sh -c 'echo $1; test $1 = a' x; echo $?
+printf 'a\nb\n' | $X xargs -n1 -t echo 2>&1
+printf 'a\nb\n' | $X xargs -t echo 2>/dev/null
+printf 'a\nb\n' | $X xargs --verbose -n1 echo 2>&1
+printf 'a\nb\n' | $X xargs --bogus echo; echo $?
+printf 'a\nb\n' | $X xargs -z echo; echo $?
+printf 'a\nb\n' | $X xargs -n; echo $?
+printf 'x y\n' | $X xargs -I{} sh -c 'echo "{}"'
+printf 'a b\n' | $X xargs echo '' x
+printf 'a b\n' | $X xargs echo '{}'
+printf 'a b\n' | $X xargs printf '[%s]\n' 'with space'
+printf 'a b\n' | $X xargs -n1 printf '[%s][%s]\n' pre
+printf 'a b\n' | $X xargs -L1 -I{} echo {}
+printf 'a b\nc d\n' | $X xargs -L1 -I{} echo [{}]
+printf 'one\ntwo\n' | $X xargs -n1 echo | $X xargs echo
+mkdir d; touch d/a d/b; find d -type f -print0 | $X xargs -0 echo | tr ' ' '\n' | sort
+mkdir d; touch d/a d/b; find d -type f | $X xargs -n1 echo | sort
+mkdir d; touch 'd/a b' d/c; find d -type f -print0 | $X xargs -0 -n1 echo | sort
+mkdir d; touch 'd/a b' d/c; find d -type f -print0 | $X xargs -0 -I{} echo '<{}>' | sort
+mkdir d; touch d/a d/b; echo hi > d/a; find d -type f | $X xargs grep -l hi
+mkdir d; touch d/a d/b; echo hi > d/a; find d -type f | $X xargs grep -c hi | sort
+mkdir d; touch d/a d/b; find d -type f | $X xargs rm; find d
+mkdir d; touch d/a d/b; find d -type f | $X xargs -I{} mv {} {}.bak; find d | sort
+mkdir d; touch d/a d/b; find d -type f | $X xargs ls d/a; echo $?
+printf '1\n2\n3\n4\n5\n6\n7\n' | $X xargs -n3 echo
+seq 1 5000 | $X echo | wc -l
+seq 1 5000 | $X echo | wc -c
+seq 1 40000 | $X echo | wc -l
diff --git a/tests/run.sh b/tests/run.sh
@@ -21,7 +21,7 @@ while IFS= read -r c; do
esac
rm -rf "$T/g" "$T/r"; mkdir "$T/g" "$T/r"
(cd "$T/g" && X="/usr/bin/env --" PATH=/usr/bin:/bin $ro sh -c "$c" </dev/null >"$T/g.out" 2>"$T/g.err"; echo "exit $?" >>"$T/g.out")
- (cd "$T/r" && X="/usr/bin/env PATH=$BIN" PATH=/usr/bin:/bin $ro sh -c "$c" </dev/null >"$T/r.out" 2>"$T/r.err"; echo "exit $?" >>"$T/r.out")
+ (cd "$T/r" && X="/usr/bin/env PATH=$BIN:/usr/bin:/bin" PATH=/usr/bin:/bin $ro sh -c "$c" </dev/null >"$T/r.out" 2>"$T/r.err"; echo "exit $?" >>"$T/r.out")
if cmp -s "$T/g.out" "$T/r.out"; then
pass=$((pass+1))
else