From 9ba9333e913c617e995b2e1d37dc738b77304158 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 7 Jun 2021 09:03:01 +0100 Subject: win32: let virtual /dev/fd/ files be opened Add support for virtual /dev/fd files to represent file descriptors. --- include/platform.h | 2 ++ win32/mingw.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/platform.h b/include/platform.h index b2d918643..37e48dcc5 100644 --- a/include/platform.h +++ b/include/platform.h @@ -447,6 +447,8 @@ typedef unsigned smalluint; #define HAVE_SYS_STATFS_H 1 #define HAVE_PRINTF_PERCENTM 1 #define HAVE_WAIT3 1 +#define HAVE_DEV_FD 1 +#define DEV_FD_PREFIX "/dev/fd/" #if defined(__UCLIBC__) # if UCLIBC_VERSION < KERNEL_VERSION(0, 9, 32) diff --git a/win32/mingw.c b/win32/mingw.c index 11ae09043..3183cd78e 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -185,6 +185,19 @@ void update_special_fd(int dev, int fd) rand_fd = fd; } +#define PREFIX_LEN (sizeof(DEV_FD_PREFIX)-1) +static int get_dev_fd(const char *filename) +{ + int fd; + + if (filename && is_prefixed_with(filename, DEV_FD_PREFIX)) { + fd = bb_strtou(filename+PREFIX_LEN, NULL, 10); + if (errno == 0 && (HANDLE)_get_osfhandle(fd) != INVALID_HANDLE_VALUE) + return fd; + } + return -1; +} + #undef open int mingw_open (const char *filename, int oflags, ...) { @@ -199,6 +212,9 @@ int mingw_open (const char *filename, int oflags, ...) filename = "nul"; oflags = O_RDWR; } + else if ((fd=get_dev_fd(filename)) >= 0) { + return fd; + } va_start(args, oflags); mode = va_arg(args, int); @@ -240,8 +256,12 @@ ssize_t FAST_FUNC mingw_open_read_close(const char *fn, void *buf, size_t size) #undef fopen FILE *mingw_fopen (const char *filename, const char *otype) { + int fd; + if (get_dev_type(filename) == DEV_NULL) filename = "nul"; + else if ((fd=get_dev_fd(filename)) >= 0) + return fdopen(fd, otype); return fopen(filename, otype); } -- cgit v1.2.3-55-g6feb