www.digitalmars.com         C & C++   DMDScript  

c++.windows.32-bits - Bug in _open_osfhandle of _fdopen

reply "Steve Adams" <sadams3 columbus.rr.com> writes:
Below is a code fragment with a bug in either _open_osfhandle or _fdopen.
This fragement runs as expected under the Borland command line tools, but
generates a "Bad file descriptor" error when compiled using the latest sc.


#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
#include <fcntl.h>
#include <io.h>

int main()
{
  SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
  FILE *f = NULL;
  int fno, binary_mode;
  HANDLE child_out;
  HANDLE father_in;
  HANDLE father_in_dup;
  HANDLE current_pid;

  current_pid = GetCurrentProcess();
  binary_mode = _O_TEXT | _O_RDONLY;
  if (CreatePipe( &father_in, &child_out, &sa, 0) == FALSE) {
    fprintf(stderr, "popen: error CreatePipe\n");
    exit( 0 );
    }
  if (DuplicateHandle( current_pid, father_in, current_pid, &father_in_dup,
0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE) {
    fprintf(stderr, "popen: error DuplicateHandle father_in\n");
    exit( 0 );
    }
  CloseHandle( father_in );
  fno = _open_osfhandle( (long) father_in_dup, binary_mode );
// problem is here XXX
perror( "\nBefore\n" );fflush(stdout);
  f   = _fdopen( fno, "r" );
perror("\nAfter\n");printf( "fno: %ld f: %08lX\n", fno, f );fflush(stdout);
  return( 0 );
}
Jun 27 2002
parent "Walter" <walter digitalmars.com> writes:
I don't know what's going wrong. I'll add it to the bug list.
Thanks. -Walter

"Steve Adams" <sadams3 columbus.rr.com> wrote in message
news:aff6lc$1crd$1 digitaldaemon.com...
 Below is a code fragment with a bug in either _open_osfhandle or _fdopen.
 This fragement runs as expected under the Borland command line tools, but
 generates a "Bad file descriptor" error when compiled using the latest sc.


 #include <windows.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <direct.h>
 #include <fcntl.h>
 #include <io.h>

 int main()
 {
   SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
   FILE *f = NULL;
   int fno, binary_mode;
   HANDLE child_out;
   HANDLE father_in;
   HANDLE father_in_dup;
   HANDLE current_pid;

   current_pid = GetCurrentProcess();
   binary_mode = _O_TEXT | _O_RDONLY;
   if (CreatePipe( &father_in, &child_out, &sa, 0) == FALSE) {
     fprintf(stderr, "popen: error CreatePipe\n");
     exit( 0 );
     }
   if (DuplicateHandle( current_pid, father_in, current_pid,
&father_in_dup,
 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE) {
     fprintf(stderr, "popen: error DuplicateHandle father_in\n");
     exit( 0 );
     }
   CloseHandle( father_in );
   fno = _open_osfhandle( (long) father_in_dup, binary_mode );
 // problem is here XXX
 perror( "\nBefore\n" );fflush(stdout);
   f   = _fdopen( fno, "r" );
 perror("\nAfter\n");printf( "fno: %ld f: %08lX\n", fno,
f );fflush(stdout);
   return( 0 );
 }
Jul 01 2002