From 6a82920046979ff373ec55b1ace6e542c8cb13f5 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 3 Apr 2012 15:41:32 +0100 Subject: Make fake fcntl(F_DUPFD) for WIN32 --- .gitignore | 1 + include/mingw.h | 2 ++ shell/ash.c | 18 +++--------------- win32/mingw.c | 52 ++++++++++++++++++++++++++++++++++++++++------------ 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 0a0c65bc3..54369de10 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ Config.in # Normal output # /busybox +/busybox.exe /busybox_old /busybox_unstripped* diff --git a/include/mingw.h b/include/mingw.h index f89297df9..9c9767b3e 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -139,6 +139,8 @@ int mingw_rename(const char*, const char*); #define fopen mingw_fopen #define rename mingw_rename +#define setlinebuf(fd) setvbuf(fd, (char *) NULL, _IOLBF, 0) + /* * ANSI emulation wrappers */ diff --git a/shell/ash.c b/shell/ash.c index 68d39c1f3..4766af34a 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -950,7 +950,7 @@ static void opentrace(void) { char s[100]; -#if defined(O_APPEND) && !ENABLE_PLATFORM_MINGW32 +#ifdef O_APPEND int flags; #endif @@ -975,7 +975,7 @@ opentrace(void) return; } } -#if defined(O_APPEND) && !ENABLE_PLATFORM_MINGW32 +#ifdef O_APPEND flags = fcntl(fileno(tracefile), F_GETFL); if (flags >= 0) fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); @@ -3904,7 +3904,7 @@ setjobctl(int on) if (--fd < 0) goto out; } - fd = copyfd(fd, 10); + fd = fcntl(fd, F_DUPFD, 10); if (ofd >= 0) close(ofd); if (fd < 0) @@ -5506,18 +5506,6 @@ copyfd(int from, int to) /*if (from != to)*/ newfd = dup2(from, to); } else { - if (ENABLE_PLATFORM_MINGW32) { - char* fds = ckmalloc(to); - int i,fd; - memset(fds,0,to); - while ((fd = dup(from)) < to && fd >= 0) - fds[fd] = 1; - for (i = 0;i < to;i ++) - if (fds[i]) - close(i); - free(fds); - return fd; - } newfd = fcntl(from, F_DUPFD, to); } if (newfd < 0) { diff --git a/win32/mingw.c b/win32/mingw.c index 219ef96b8..c9b00bbcd 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -693,18 +693,46 @@ int mingw_mkdir(const char *path, int mode UNUSED_PARAM) return mkdir(path); } -int fcntl(int fd UNUSED_PARAM, int cmd, ...) -{ - /* - * F_GETFL needs to be dealt at higher level - * Usually it does not matter if the call is - * fcntl(fd, F_SETFL, fcntl(fd, F_GETFD) | something) - * because F_SETFL is not supported - */ - if (cmd == F_GETFD || cmd == F_SETFD || cmd == F_GETFL) - return 0; - errno = ENOSYS; - return -1; +int fcntl(int fd, int cmd, ...) +{ + va_list arg; + int result = -1; + char *fds; + int target, i, newfd; + + va_start(arg, cmd); + + switch (cmd) { + case F_GETFD: + case F_SETFD: + case F_GETFL: + /* + * Our fake F_GETFL won't matter if the return value is used as + * fcntl(fd, F_SETFL, ret|something); + * because F_SETFL isn't supported either. + */ + result = 0; + break; + case F_DUPFD: + target = va_arg(arg, int); + fds = xzalloc(target); + while ((newfd = dup(fd)) < target && newfd >= 0) { + fds[newfd] = 1; + } + for (i = 0; i < target; ++i) { + if (fds[i]) { + close(i); + } + } + free(fds); + result = newfd; + default: + errno = ENOSYS; + break; + } + + va_end(arg); + return result; } #undef unlink -- cgit v1.2.3-55-g6feb