This is actually a single patch (put 18/18 by mistake).

Also, I ended up explicitly setting --include for each out of 6 files. 
Doing this globally for all musl files yielded some errors for some *.c 
files which I gave up trying to solve.

On Saturday, August 15, 2020 at 12:28:38 AM UTC-4 Waldek Kozaczuk wrote:

> Signed-off-by: Waldemar Kozaczuk <[email protected]>
> ---
> Makefile | 18 ++++++++-----
> libc/aliases.ld | 3 +++
> libc/stdio/__stdio_close.c | 7 -----
> libc/stdio/__stdio_seek.c | 7 -----
> libc/stdio/fopen.c | 32 ----------------------
> libc/stdio/freopen.c | 55 --------------------------------------
> libc/stdio/tmpfile.c | 27 -------------------
> libc/stdio/tmpnam.c | 30 ---------------------
> libc/syscall_to_function.h | 31 +++++++++++++++++++++
> 9 files changed, 46 insertions(+), 164 deletions(-)
> delete mode 100644 libc/stdio/__stdio_close.c
> delete mode 100644 libc/stdio/__stdio_seek.c
> delete mode 100644 libc/stdio/fopen.c
> delete mode 100644 libc/stdio/freopen.c
> delete mode 100644 libc/stdio/tmpfile.c
> delete mode 100644 libc/stdio/tmpnam.c
> create mode 100644 libc/syscall_to_function.h
>
> diff --git a/Makefile b/Makefile
> index 93c4e189..9a490211 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1413,10 +1413,12 @@ libc += stdio/__fopen_rb_ca.o
> libc += stdio/__fprintf_chk.o
> libc += stdio/__lockfile.o
> musl += stdio/__overflow.o
> -libc += stdio/__stdio_close.o
> +musl += stdio/__stdio_close.o
> +$(out)/musl/src/stdio/__stdio_close.o: CFLAGS += --include 
> libc/syscall_to_function.h
> musl += stdio/__stdio_exit.o
> libc += stdio/__stdio_read.o
> -libc += stdio/__stdio_seek.o
> +musl += stdio/__stdio_seek.o
> +$(out)/musl/src/stdio/__stdio_seek.o: CFLAGS += --include 
> libc/syscall_to_function.h
> libc += stdio/__stdio_write.o
> libc += stdio/__stdout_write.o
> musl += stdio/__string_read.o
> @@ -1442,7 +1444,8 @@ musl += stdio/fgetws.o
> musl += stdio/fileno.o
> libc += stdio/flockfile.o
> libc += stdio/fmemopen.o
> -libc += stdio/fopen.o
> +musl += stdio/fopen.o
> +$(out)/musl/src/stdio/fopen.o: CFLAGS += --include 
> libc/syscall_to_function.h
> musl += stdio/fprintf.o
> libc += stdio/fputc.o
> musl += stdio/fputs.o
> @@ -1450,7 +1453,8 @@ musl += stdio/fputwc.o
> musl += stdio/fputws.o
> musl += stdio/fread.o
> libc += stdio/__fread_chk.o
> -libc += stdio/freopen.o
> +musl += stdio/freopen.o
> +$(out)/musl/src/stdio/freopen.o: CFLAGS += --include 
> libc/syscall_to_function.h
> musl += stdio/fscanf.o
> musl += stdio/fseek.o
> musl += stdio/fsetpos.o
> @@ -1499,8 +1503,10 @@ libc += stdio/stdout.o
> musl += stdio/swprintf.o
> musl += stdio/swscanf.o
> musl += stdio/tempnam.o
> -libc += stdio/tmpfile.o
> -libc += stdio/tmpnam.o
> +musl += stdio/tmpfile.o
> +$(out)/musl/src/stdio/tmpfile.o: CFLAGS += --include 
> libc/syscall_to_function.h
> +musl += stdio/tmpnam.o
> +$(out)/musl/src/stdio/tmpnam.o: CFLAGS += --include 
> libc/syscall_to_function.h
> musl += stdio/ungetc.o
> musl += stdio/ungetwc.o
> musl += stdio/vasprintf.o
> diff --git a/libc/aliases.ld b/libc/aliases.ld
> index c1ad406a..0bc91597 100644
> --- a/libc/aliases.ld
> +++ b/libc/aliases.ld
> @@ -31,6 +31,9 @@ __setlocale = setlocale;
> /* multibyte */
> __mbrlen = mbrlen;
>
> +/* stdio */
> +__dup3 = dup3;
> +
> /* string */
> __strtok_r = strtok_r;
> __strndup = strndup;
> diff --git a/libc/stdio/__stdio_close.c b/libc/stdio/__stdio_close.c
> deleted file mode 100644
> index 976e34ed..00000000
> --- a/libc/stdio/__stdio_close.c
> +++ /dev/null
> @@ -1,7 +0,0 @@
> -#include <unistd.h>
> -#include "stdio_impl.h"
> -
> -int __stdio_close(FILE *f)
> -{
> - return close(f->fd);
> -}
> diff --git a/libc/stdio/__stdio_seek.c b/libc/stdio/__stdio_seek.c
> deleted file mode 100644
> index e51f4a12..00000000
> --- a/libc/stdio/__stdio_seek.c
> +++ /dev/null
> @@ -1,7 +0,0 @@
> -#include <unistd.h>
> -#include "stdio_impl.h"
> -
> -off_t __stdio_seek(FILE *f, off_t off, int whence)
> -{
> - return lseek(f->fd, off, whence);
> -}
> diff --git a/libc/stdio/fopen.c b/libc/stdio/fopen.c
> deleted file mode 100644
> index 83452407..00000000
> --- a/libc/stdio/fopen.c
> +++ /dev/null
> @@ -1,32 +0,0 @@
> -#include "stdio_impl.h"
> -#include <fcntl.h>
> -#include <string.h>
> -#include <unistd.h>
> -#include <errno.h>
> -
> -FILE *fopen(const char *restrict filename, const char *restrict mode)
> -{
> - FILE *f;
> - int fd;
> - int flags;
> -
> - /* Check for valid initial mode character */
> - if (!strchr("rwa", *mode)) {
> - errno = EINVAL;
> - return 0;
> - }
> -
> - /* Compute the flags to pass to open() */
> - flags = __fmodeflags(mode);
> -
> - fd = open(filename, flags|O_LARGEFILE, 0666);
> - if (fd < 0) return 0;
> -
> - f = __fdopen(fd, mode);
> - if (f) return f;
> -
> - close(fd);
> - return 0;
> -}
> -
> -LFS64(fopen);
> diff --git a/libc/stdio/freopen.c b/libc/stdio/freopen.c
> deleted file mode 100644
> index 8ae1e992..00000000
> --- a/libc/stdio/freopen.c
> +++ /dev/null
> @@ -1,55 +0,0 @@
> -#include "stdio_impl.h"
> -#include <stdlib.h>
> -#include <fcntl.h>
> -
> -/* The basic idea of this implementation is to open a new FILE,
> - * hack the necessary parts of the new FILE into the old one, then
> - * close the new FILE. */
> -
> -/* Locking IS necessary because another thread may provably hold the
> - * lock, via flockfile or otherwise, when freopen is called, and in that
> - * case, freopen cannot act until the lock is released. */
> -
> -int dup3(int, int, int);
> -
> -FILE *freopen(const char *restrict filename, const char *restrict mode, 
> FILE *restrict f)
> -{
> - int fl = __fmodeflags(mode);
> - FILE *f2;
> -
> - FLOCK(f);
> -
> - fflush(f);
> -
> - if (!filename) {
> - if (fl&O_CLOEXEC)
> - fcntl(f->fd, F_SETFD, FD_CLOEXEC);
> - fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC);
> - if (fcntl(f->fd, F_SETFL, fl) < 0)
> - goto fail;
> - } else {
> - f2 = fopen(filename, mode);
> - if (!f2) goto fail;
> - if (f2->fd == f->fd) f2->fd = -1; /* avoid closing in fclose */
> - else if (dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) goto fail2;
> -
> - f->flags = (f->flags & F_PERM) | f2->flags;
> - f->read = f2->read;
> - f->write = f2->write;
> - f->seek = f2->seek;
> - f->close = f2->close;
> -
> - fclose(f2);
> - }
> -
> - FUNLOCK(f);
> - return f;
> -
> -fail2:
> - fclose(f2);
> -fail:
> - fclose(f);
> - return NULL;
> -}
> -
> -LFS64(freopen);
> diff --git a/libc/stdio/tmpfile.c b/libc/stdio/tmpfile.c
> deleted file mode 100644
> index 195ba191..00000000
> --- a/libc/stdio/tmpfile.c
> +++ /dev/null
> @@ -1,27 +0,0 @@
> -#include <stdio.h>
> -#include <fcntl.h>
> -#include <unistd.h>
> -#include "stdio_impl.h"
> -
> -#define MAXTRIES 100
> -
> -FILE *tmpfile(void)
> -{
> - char buf[L_tmpnam], *s;
> - int fd;
> - FILE *f;
> - int try;
> - for (try=0; try<MAXTRIES; try++) {
> - s = tmpnam(buf);
> - if (!s) return 0;
> - fd = open(s, O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600);
> - if (fd >= 0) {
> - f = __fdopen(fd, "w+");
> - unlink(s);
> - return f;
> - }
> - }
> - return 0;
> -}
> -
> -LFS64(tmpfile);
> diff --git a/libc/stdio/tmpnam.c b/libc/stdio/tmpnam.c
> deleted file mode 100644
> index 8377d1ad..00000000
> --- a/libc/stdio/tmpnam.c
> +++ /dev/null
> @@ -1,30 +0,0 @@
> -#include <stdio.h>
> -#include <stdlib.h>
> -#include <stdint.h>
> -#include <unistd.h>
> -#include <time.h>
> -#include "libc.h"
> -#include "atomic.h"
> -
> -#define MAXTRIES 100
> -
> -char *tmpnam(char *s)
> -{
> - static int index;
> - static char s2[L_tmpnam];
> - struct timespec ts;
> - int try = 0;
> - unsigned n;
> -
> - if (!s) s = s2;
> -
> - if (access(P_tmpdir, R_OK|W_OK|X_OK) != 0)
> - return NULL;
> -
> - do {
> - clock_gettime(CLOCK_REALTIME, &ts);
> - n = ts.tv_nsec ^ (uintptr_t)&s ^ (uintptr_t)s;
> - snprintf(s, L_tmpnam, "/tmp/t%x-%x", a_fetch_add(&index, 1), n);
> - } while (!access(s, F_OK) && try++<MAXTRIES);
> - return try>=MAXTRIES ? 0 : s;
> -}
> diff --git a/libc/syscall_to_function.h b/libc/syscall_to_function.h
> new file mode 100644
> index 00000000..08d7ce93
> --- /dev/null
> +++ b/libc/syscall_to_function.h
> @@ -0,0 +1,31 @@
> +#include <bits/syscall.h>
> +#include <unistd.h>
> +
> +#define __OSV_TO_FUNCTION_SYS_open(filename, flags, perm) (open(filename, 
> flags, perm))
> +
> +#define __OSV_TO_FUNCTION_SYS_close(fd) (close(fd))
> +
> +#define __OSV_TO_FUNCTION_SYS_lseek(file, off, whence) (lseek(file, off, 
> whence))
> +
> +#define __OSV_TO_FUNCTION_SYS_fcntl(fd, cmd, ...) (fcntl(fd, cmd 
> __VA_OPT__(,) __VA_ARGS__))
> +
> +#define __OSV_TO_FUNCTION_SYS_clock_gettime(c, t, x) (clock_gettime(c, t))
> +
> +#define __OSV_TO_FUNCTION_SYS_access(p, i) (access(p, i))
> +
> +#define __OSV_TO_FUNCTION_SYS_ioctl(fd, cmd, args) (ioctl(fd, cmd, args))
> +
> +#define __OSV_TO_FUNCTION_SYS_unlink(path) (unlink(path))
> +
> +#define __OSV_TO_FUNCTION_SYS_rmdir(path) (rmdir(path))
> +
> +#define __OSV_TO_FUNCTION_SYS_readv(fd, cmd, args) (readv(fd, cmd, args))
> +
> +#define __OSV_TO_FUNCTION_SYS_writev(fd, cmd, args) (writev(fd, cmd, 
> args))
> +
> +#undef __syscall
> +#define __syscall(sys_number, ...) 
> (__OSV_TO_FUNCTION_##sys_number(__VA_ARGS__))
> +#undef syscall
> +#define syscall(sys_number, ...) 
> (__OSV_TO_FUNCTION_##sys_number(__VA_ARGS__))
> +#undef syscall_cp
> +#define syscall_cp(sys_number, ...) 
> (__OSV_TO_FUNCTION_##sys_number(__VA_ARGS__))
> -- 
> 2.26.2
>
>

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/ad097ff8-416a-46b8-aab8-c38a61194becn%40googlegroups.com.

Reply via email to