June 27, 2002
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 );
}



July 01, 2002
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 );
> }
>
>
>