diff --git a/include/parrot/io.h b/include/parrot/io.h index ec29cdd331..85daf13894 100644 --- a/include/parrot/io.h +++ b/include/parrot/io.h @@ -101,13 +101,6 @@ extern PIOOFF_T piooffsetzero; typedef struct _ParrotIOData ParrotIOData; -#ifdef _MSC_VER -/* Win32/MSVC has a deprecation warning about dup() in favor of _dup(). */ -# define Parrot_dup(x) (PIOHANDLE)_dup((int)(x)) -#else /* !_MSC_VER */ -# define Parrot_dup(x) (PIOHANDLE)dup((int)(x)) -#endif /* _MSC_VER */ - extern INTVAL pio_errno; /* io/core.c - interpreter initialization/destruction functions */ diff --git a/include/parrot/io_portable.h b/include/parrot/io_portable.h index db6c646257..8a03b2db76 100644 --- a/include/parrot/io_portable.h +++ b/include/parrot/io_portable.h @@ -145,6 +145,7 @@ size_t Parrot_io_write_portable(PARROT_INTERP, Parrot_io_fdopen_portable((interp), (pmc), (handle), (flags)) #define PIO_OPEN_PIPE(interp, pmc, file, flags) \ Parrot_io_open_pipe_portable((interp), (pmc), (file), (flags)) +#define PIO_DUP(interp, handle) (PIOHANDLE)dup((int)(handle)) #define PIO_CLOSE(interp, pmc) Parrot_io_close_portable((interp), (pmc)) #define PIO_IS_CLOSED(interp, pmc) Parrot_io_is_closed_portable((interp), (pmc)) #define PIO_READ(interp, pmc, buf) Parrot_io_read_portable((interp), (pmc), (buf)) diff --git a/include/parrot/io_unix.h b/include/parrot/io_unix.h index 9a288ed70c..ab04d1541b 100644 --- a/include/parrot/io_unix.h +++ b/include/parrot/io_unix.h @@ -281,6 +281,7 @@ INTVAL Parrot_io_socket_unix(PARROT_INTERP, Parrot_io_fdopen_unix((interp), (pmc), (handle), (flags)) #define PIO_OPEN_PIPE(interp, pmc, file, flags) \ Parrot_io_open_pipe_unix((interp), (pmc), (file), (flags)) +#define PIO_DUP(interp, handle) (PIOHANDLE)dup((int)(handle)) #define PIO_CLOSE(interp, pmc) Parrot_io_close_unix((interp), (pmc)) #define PIO_CLOSE_PIOHANDLE(interp, handle) Parrot_io_close_piohandle_unix((interp), (handle)) #define PIO_IS_CLOSED(interp, pmc) Parrot_io_is_closed_unix((interp), (pmc)) diff --git a/include/parrot/io_win32.h b/include/parrot/io_win32.h index 398455aabf..13779fc4b5 100644 --- a/include/parrot/io_win32.h +++ b/include/parrot/io_win32.h @@ -27,6 +27,10 @@ INTVAL Parrot_io_close_win32(PARROT_INTERP, ARGMOD(PMC *filehandle)) __attribute__nonnull__(2) FUNC_MODIFIES(*filehandle); +PARROT_WARN_UNUSED_RESULT +PIOHANDLE Parrot_io_dup_win32(PARROT_INTERP, PIOHANDLE handle) + __attribute__nonnull__(1); + PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL PMC * Parrot_io_fdopen_win32(PARROT_INTERP, @@ -119,6 +123,8 @@ size_t Parrot_io_write_win32(PARROT_INTERP, #define ASSERT_ARGS_Parrot_io_close_win32 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ PARROT_ASSERT_ARG(interp) \ , PARROT_ASSERT_ARG(filehandle)) +#define ASSERT_ARGS_Parrot_io_dup_win32 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ + PARROT_ASSERT_ARG(interp)) #define ASSERT_ARGS_Parrot_io_fdopen_win32 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ PARROT_ASSERT_ARG(interp)) #define ASSERT_ARGS_Parrot_io_flush_win32 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ @@ -269,6 +275,7 @@ INTVAL Parrot_io_socket_win32(PARROT_INTERP, Parrot_io_fdopen_win32((interp), (pmc), (handle), (flags)) #define PIO_OPEN_PIPE(interp, pmc, file, flags) \ Parrot_io_open_pipe_win32((interp), (pmc), (file), (flags)) +#define PIO_DUP(interp, handle) Parrot_io_dup_win32((interp), (handle)) #define PIO_CLOSE(interp, pmc) Parrot_io_close_win32((interp), (pmc)) #define PIO_CLOSE_PIOHANDLE(interp, handle) Parrot_io_close_piohandle_win32((interp), (handle)) #define PIO_IS_CLOSED(interp, pmc) Parrot_io_is_closed_win32((interp), (pmc)) diff --git a/src/io/unix.c b/src/io/unix.c index f88d357f2b..e97953ce89 100644 --- a/src/io/unix.c +++ b/src/io/unix.c @@ -738,7 +738,7 @@ Parrot_io_open_pipe_unix(PARROT_INTERP, ARGMOD(PMC *filehandle), close(STDIN_FILENO); close(fds[1]); - if (Parrot_dup(fds[0]) != STDIN_FILENO) + if (dup(fds[0]) != STDIN_FILENO) exit(EXIT_FAILURE); } else { @@ -747,9 +747,9 @@ Parrot_io_open_pipe_unix(PARROT_INTERP, ARGMOD(PMC *filehandle), close(STDERR_FILENO); close(fds[0]); - if (Parrot_dup(fds[1]) != STDOUT_FILENO) + if (dup(fds[1]) != STDOUT_FILENO) exit(EXIT_FAILURE); - if (Parrot_dup(fds[1]) != STDERR_FILENO) + if (dup(fds[1]) != STDERR_FILENO) exit(EXIT_FAILURE); } diff --git a/src/io/win32.c b/src/io/win32.c index f009a61a79..3b38203e55 100644 --- a/src/io/win32.c +++ b/src/io/win32.c @@ -291,6 +291,35 @@ Parrot_io_fdopen_win32(PARROT_INTERP, ARGMOD_NULLOK(PMC *filehandle), /* +=item C + +Duplicates file handle C. + +=cut + +*/ + +PARROT_WARN_UNUSED_RESULT +PIOHANDLE +Parrot_io_dup_win32(PARROT_INTERP, PIOHANDLE handle) +{ + ASSERT_ARGS(Parrot_io_dup_win32) + HANDLE current_process = GetCurrentProcess(); + HANDLE new_handle = INVALID_HANDLE_VALUE; + + DuplicateHandle(current_process, + (HANDLE)handle, + current_process, + &new_handle, + 0, + FALSE, + DUPLICATE_SAME_ACCESS); + + return new_handle; +} + +/* + =item C Calls C to close the given file descriptor. Returns 0 on diff --git a/src/pmc/filehandle.pmc b/src/pmc/filehandle.pmc index 58bd5ec552..5090b6aa34 100644 --- a/src/pmc/filehandle.pmc +++ b/src/pmc/filehandle.pmc @@ -127,23 +127,7 @@ Create a copy of the filehandle. new_attrs->last_pos = old_attrs->last_pos; /* Duplicate the file handle. */ -#ifdef PIO_OS_WIN32 - { - HANDLE current_process = GetCurrentProcess(); - HANDLE new_handle = INVALID_HANDLE_VALUE; - DuplicateHandle(current_process, - (HANDLE)old_attrs->os_handle, - current_process, - &new_handle, - 0, - FALSE, - DUPLICATE_SAME_ACCESS); - new_attrs->os_handle = new_handle; - } -#else - new_attrs->os_handle = (PIOHANDLE)Parrot_dup(old_attrs->os_handle); -#endif - + new_attrs->os_handle = PIO_DUP(interp, old_attrs->os_handle); /* We need to correctly allocate a buffer, so we check for the buffering mode. */ if (new_attrs->flags & PIO_F_LINEBUF)