Skip to content

Commit

Permalink
[io] Put unknown++'s Windows dup fix in src/io/win32.c
Browse files Browse the repository at this point in the history
  • Loading branch information
nwellnhof committed Jan 2, 2011
1 parent b05bb32 commit 9eeccff
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 27 deletions.
7 changes: 0 additions & 7 deletions include/parrot/io.h
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions include/parrot/io_portable.h
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions include/parrot/io_unix.h
Expand Up @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions include/parrot/io_win32.h
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = (\
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions src/io/unix.c
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
29 changes: 29 additions & 0 deletions src/io/win32.c
Expand Up @@ -291,6 +291,35 @@ Parrot_io_fdopen_win32(PARROT_INTERP, ARGMOD_NULLOK(PMC *filehandle),

/*
=item C<PIOHANDLE Parrot_io_dup_win32(PARROT_INTERP, PIOHANDLE handle)>
Duplicates file handle C<handle>.
=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<INTVAL Parrot_io_close_piohandle_win32(PARROT_INTERP, PIOHANDLE handle)>
Calls C<CloseHandle()> to close the given file descriptor. Returns 0 on
Expand Down
18 changes: 1 addition & 17 deletions src/pmc/filehandle.pmc
Expand Up @@ -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)
Expand Down

0 comments on commit 9eeccff

Please sign in to comment.