#include #include #include #include void main() { long os_handle; int fh1, fh2, rc; fh1 = open( "file", O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); if( fh1 == -1 ) { printf( "Could not open output file\n" ); exit( EXIT_FAILURE ); } printf( "First POSIX handle %d\n", fh1 ); #if defined(USE_DUP2) fh2 = 6; if( dup2( fh1, fh2 ) == -1 ) fh2 = -1; #else os_handle = _get_osfhandle( fh1 ); printf( "OS Handle %ld\n", os_handle ); fh2 = _open_osfhandle( os_handle, O_WRONLY | O_BINARY ); #endif if( fh2 == -1 ) { printf( "Could not open with second handle\n" ); exit( EXIT_FAILURE ); } printf( "Second POSIX handle %d\n", fh2 ); rc = write( fh2, "trash\x0d\x0a", 7 ); printf( "Write file using second handle %d\n", rc ); rc = close( fh2 ); printf( "Closing second handle %d\n", rc ); rc = close( fh1 ); printf( "Closing first handle %d\n", rc ); }