Mail Archives: cygwin/1998/06/11/08:29:33
Hello fellow analysts,
Below is a sample program demonstrating a method for piping in a WIN32
environment. This was tested using the Mingw32 toolset.
Enjoy,
- \\||//
---o0O0--Earnie--0O0o----
--earnie_boyd AT yahoo DOT com--
------ooo0O--O0ooo-------
-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<--
/*
testpipe.c
Example of the use of a pipe in a WIN32 setting.
Author: Earnie Boyd, (c) 1998
No warranties are given, use at your own risk.
Permission is granted to use as you wish.
This program pipes the output of ls to the input of less. The
prerequesites
of using this program is that you have the executables for both of
these
somewhere on your PATH.
*/
#include <windows.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
//Setup and Initializations.
#define PIPESIZE 512
static HANDLE hProcesses[2];
static int StdIn;
static int StdOut;
int status;
int dupStdIn;
int dupStdOut;
int hPipe[2];
#define pSTDIN hPipe[0]
#define pSTDOUT hPipe[1]
int
main( void )
{
/*
Some functions use the file stream and some use the file handle.
Let's make it easy on ourselves and get the file handle for the
associated file stream.
*/
StdIn = _fileno ( stdin );
StdOut = _fileno ( stdout );
// We duplicate these so that we can later restore them.
dupStdIn = _dup ( StdIn );
dupStdOut = _dup ( StdOut );
/*
Create the pipe.
hPipe will contain the file handles for the pipe.
PIPESIZE is defined above.
File translation mode will be BINARY.
And we don't want the children inheriting the pipe handles.
*/
status = _pipe ( hPipe, PIPESIZE, _O_BINARY | _O_NOINHERIT );
if (status) perror ( "_pipe" );
/*
_dup2#1:
We want the first process will write to the pipe output; so, we
need to
make the stdout file handle the same as the pipe out file handle.
*/
status = _dup2 ( pSTDOUT, StdOut );
if ( status ) perror ( "_dup2#1" );
// Since we've duplicated this handle we no longer need the original.
close ( pSTDOUT );
hProcesses[0] = (HANDLE) _spawnlp ( _P_NOWAIT, "ls", NULL );
/*
_dup2#2:
We want the second process will read from the pipe input; so, we
need to
make the stdin file handle the same as the pipe in file handle.
*/
status = _dup2 ( pSTDIN, StdIn );
if ( status ) perror ( "_dup2#2" );
close ( pSTDIN );
/*
_dup2#3:
We need to restore the original stdout file handle as the second
process
will need to write to the screen. If we don't do this the second
process
will get into a "circular pipe" by reading what it has written.
Also, this is done for the sake of the parent. If the parent writes
to stdout after the _dup2#1 step, it would be written to the pipe.
The
parent would have to write to dupStdOut to output to the screen.
*/
status = _dup2 ( dupStdOut, StdOut );
if ( status ) perror ( "_dup2#3" );
close ( dupStdOut );
hProcesses[1] = (HANDLE) _spawnlp ( _P_NOWAIT, "less", NULL );
/*
_dup2#4:
Restore the stdin file handle for the parents use.
*/
status = _dup2( dupStdIn, StdIn );
if ( status ) perror ( "_dup2#4" );
close( dupStdIn );
/*
Wait for the second process to complete. There is no need to wait
for the
first process in this example. There may be reason to do a
WaitForMultipleObjects() function call for your application.
*/
WaitForSingleObject( hProcesses[1], INFINITE );
return 0;
}
_________________________________________________________
DO YOU YAHOO!?
Get your free @yahoo.com address at http://mail.yahoo.com
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".
- Raw text -