summaryrefslogtreecommitdiff
path: root/file_io/win32/filedup.c
diff options
context:
space:
mode:
authorWilliam A. Rowe Jr <wrowe@apache.org>2007-10-12 01:53:12 +0000
committerWilliam A. Rowe Jr <wrowe@apache.org>2007-10-12 01:53:12 +0000
commitae1e765a87cea074e946a611f559929ae4878064 (patch)
treea7e8cd65e95d46a4b4adca6e0ff8704aecda4448 /file_io/win32/filedup.c
parent5a188b7c0c9c378e818998d9be8253f37823e3ca (diff)
downloadapr-ae1e765a87cea074e946a611f559929ae4878064.tar.gz
Group of two changes; we must keep file->flags in sync when
we are apr_file_dup2()'ing into a standard handle (it has to remain marked as a standard handle), remove a redundant CloseFile() (win faux-posix _dup2 does this for us) and also close the msvcrt faux-posix file when we close std streams. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@584024 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'file_io/win32/filedup.c')
-rw-r--r--file_io/win32/filedup.c42
1 files changed, 17 insertions, 25 deletions
diff --git a/file_io/win32/filedup.c b/file_io/win32/filedup.c
index 70ed4379d..e96ef2bc5 100644
--- a/file_io/win32/filedup.c
+++ b/file_io/win32/filedup.c
@@ -40,7 +40,7 @@ APR_DECLARE(apr_status_t) apr_file_dup(apr_file_t **new_file,
(*new_file) = (apr_file_t *) apr_pcalloc(p, sizeof(apr_file_t));
(*new_file)->filehand = newhand;
- (*new_file)->flags = old_file->flags & ~APR_INHERIT;
+ (*new_file)->flags = old_file->flags & ~(APR_STD_FLAGS | APR_INHERIT);
(*new_file)->pool = p;
(*new_file)->fname = apr_pstrdup(p, old_file->fname);
(*new_file)->append = old_file->append;
@@ -77,21 +77,10 @@ APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
apr_int32_t newflags;
int fd;
- if (new_file->flags & APR_STD_FLAGS) {
- /* if (!DuplicateHandle(hproc, old_file->filehand,
- * hproc, &newhand, 0,
- * TRUE, DUPLICATE_SAME_ACCESS)) {
- * return apr_get_os_error();
- * }
- * if (((stdhandle & stderr_handle) && !SetStdHandle(STD_ERROR_HANDLE, newhand)) ||
- * ((stdhandle & stdout_handle) && !SetStdHandle(STD_OUTPUT_HANDLE, newhand)) ||
- * ((stdhandle & stdin_handle) && !SetStdHandle(STD_INPUT_HANDLE, newhand))) {
- * return apr_get_os_error();
- * }
- * newflags = old_file->flags | APR_INHERIT;
- */
-
- if ((new_file->flags & APR_STD_FLAGS) == APR_STDERR_FLAG) {
+ if (new_file->flags & APR_STD_FLAGS)
+ {
+ if ((new_file->flags & APR_STD_FLAGS) == APR_STDERR_FLAG)
+ {
/* Flush stderr and unset its buffer, then commit the fd-based buffer.
* This is typically a noop for Win2K/XP since services with NULL std
* handles [but valid FILE *'s, oddly enough], but is required
@@ -106,6 +95,7 @@ APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
* and close our temporary pseudo-fd once it's been duplicated.
* This will incidently keep the FILE-based stderr in sync.
* Note the apparently redundant _O_BINARY coersions are required.
+ * Note the _dup2 will close the previous std Win32 handle.
*/
if (!DuplicateHandle(hproc, old_file->filehand, hproc, &newhand,
0, FALSE, DUPLICATE_SAME_ACCESS)) {
@@ -124,8 +114,7 @@ APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
*/
newhand = (HANDLE)_get_osfhandle(2);
}
-
- if ((new_file->flags & APR_STD_FLAGS) == APR_STDOUT_FLAG) {
+ else if ((new_file->flags & APR_STD_FLAGS) == APR_STDOUT_FLAG) {
/* For the process flow see the stderr case above */
fflush(stdout);
setvbuf(stdout, NULL, _IONBF, 0);
@@ -141,8 +130,7 @@ APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
_setmode(1, _O_BINARY);
newhand = (HANDLE)_get_osfhandle(1);
}
-
- if ((new_file->flags & APR_STD_FLAGS) == APR_STDIN_FLAG) {
+ else if ((new_file->flags & APR_STD_FLAGS) == APR_STDIN_FLAG) {
/* For the process flow see the stderr case above */
fflush(stdin);
setvbuf(stdin, NULL, _IONBF, 0);
@@ -158,7 +146,10 @@ APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
_setmode(0, _O_BINARY);
newhand = (HANDLE)_get_osfhandle(0);
}
- newflags = old_file->flags | APR_INHERIT;
+ newflags = (new_file->flags & APR_STD_FLAGS)
+ | (old_file->flags & ~APR_STD_FLAGS) | APR_INHERIT;
+
+ /* No need to close the old file, _dup2() above did that for us */
}
else {
if (!DuplicateHandle(hproc, old_file->filehand,
@@ -166,11 +157,12 @@ APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
FALSE, DUPLICATE_SAME_ACCESS)) {
return apr_get_os_error();
}
- newflags = old_file->flags & ~APR_INHERIT;
- }
+ newflags = old_file->flags & ~(APR_STD_FLAGS | APR_INHERIT);
- if (new_file->filehand && (new_file->filehand != INVALID_HANDLE_VALUE)) {
- CloseHandle(new_file->filehand);
+ if (new_file->filehand
+ && (new_file->filehand != INVALID_HANDLE_VALUE)) {
+ CloseHandle(new_file->filehand);
+ }
}
new_file->flags = newflags;