This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: pipe data form windows program to cygwin program


hi again,

i join to this mail an example. This must be compiled with mingw
compiler.

the program is going great but at the end gzip( or you can try with cat
to see that data is in output file) stay open.

bertrand



Le mer 27/10/2004 Ã 21:37, Bob Byrnes a Ãcrit :
> > I'm writing a program which need to send data to a cygwin program using
> > a pipe on the stdin (actually data to compress using gzip).
> > 
> > All is working well but at the end of the program when i close the pipe
> > it seems that gzip doesn't see that the pipe has been closed and so it
> > stay open.
> 
> Are you *sure* that you have closed *all* of the write handles to the pipe?
> If any write handles remain open, then EOF won't be delivered to the
> read side of the pipe.
> 
> > I kind of think there must be something with windows<->cygwin EOF but i
> > can't find out what.
> > 
> > Is anyone has an idea ?
> 
> If you can provide a (very) simple test case that exhibits allegedly
> incorrect behavior, that would be helpful.
> 
> --
> Bob
> 
> --
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> Problem reports:       http://cygwin.com/problems.html
> Documentation:         http://cygwin.com/docs.html
> FAQ:                   http://cygwin.com/faq/
> 
#include <stdio.h>
#include <windows.h>
#include <fcntl.h>
#include <process.h>

main()
{

	HANDLE newstdout,newstdin,parent,handles[2];
	int phandles[2],compressor_pid,archivefd;
	
	/*open a file to put data on*/
	
	archivefd = open("test.gz", O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,0666 & ~umask(0));
	
	/*save current handles*/
	parent=GetCurrentProcess ();
	handles[0] = GetStdHandle (STD_INPUT_HANDLE);
	handles[1] = GetStdHandle (STD_OUTPUT_HANDLE);
	
	/*create a new pipe*/
	_pipe(phandles,0,O_BINARY);
	
	/*set pipe of stdin of process form our stdout*/
	DuplicateHandle (parent,
		       (HANDLE) _get_osfhandle (archivefd),
		       parent,
		       &newstdout,
		       0,
		       TRUE,
		       DUPLICATE_SAME_ACCESS);
	
	/*set pipe of stdout of process to open file*/
	DuplicateHandle (parent,
	       (HANDLE) _get_osfhandle (phandles[0]),
	       parent,
	       &newstdin,
	       0,
	       TRUE,
	       DUPLICATE_SAME_ACCESS);
	
	/*replace standard handles*/
	SetStdHandle (STD_INPUT_HANDLE, newstdin);
	SetStdHandle (STD_OUTPUT_HANDLE, newstdout);
	       
	/*start process*/
	compressor_pid= spawnlpe(_P_NOWAIT,"cat",NULL, NULL);
	
	/*restore standard handles*/
	CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
	CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
	
	SetStdHandle (STD_INPUT_HANDLE, handles[0]);
	SetStdHandle (STD_OUTPUT_HANDLE, handles[1]);
	
	/*close pipe unneeded and duplicate pipe input to archivefd*/
	dup2(phandles[1], archivefd);
	close(phandles[0]);
	close(phandles[1]);
	
	/*output data to gzip*/
	
	write(archivefd,"abcdefghijklmnopqrstuvwxyz",26);
	
	/*close our handles*/
	close(archivefd);
	
	/*exit*/
	exit(0);
}


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]