1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
/************************************************************************/
/* */
/* sioPipe.[ch]: sio equivalents to popen/pclose. */
/* */
/************************************************************************/
# include "appUtilConfig.h"
# include <sioPipe.h>
# include <stdlib.h>
# include <stdarg.h>
# include <stdio.h>
# include <string.h>
# include <signal.h>
# include <errno.h>
# include <appDebugon.h>
/************************************************************************/
/* */
/* Open an output stream to a command using popen(). */
/* */
/************************************************************************/
static int sioOutPipeWriteBytes( void * voidf,
const unsigned char * buffer,
int count )
{
int rval;
void (*prevSigPipeHandler)( int );
prevSigPipeHandler= signal( SIGPIPE, SIG_IGN );
rval= fwrite( buffer, 1, count, (FILE *)voidf );
signal( SIGPIPE, prevSigPipeHandler );
return rval;
}
static int sioPipeClose( void * voidf )
{
int rval;
void (*prevSigPipeHandler)( int );
prevSigPipeHandler= signal( SIGPIPE, SIG_IGN );
rval= pclose( (FILE *)voidf );
signal( SIGPIPE, prevSigPipeHandler );
return rval;
}
SimpleOutputStream * sioOutPipeOpen( const char * command )
{
SimpleOutputStream * sos= (SimpleOutputStream *)0;
FILE * f;
void (*prevSigPipeHandler)( int );
prevSigPipeHandler= signal( SIGPIPE, SIG_IGN );
f= popen( command, "w" );
if ( ! f )
{ SXDEB(command,f); goto ready; }
sos= sioOutOpen( (void *)f, sioOutPipeWriteBytes,
(SIOoutSEEK)0, sioPipeClose );
if ( ! sos )
{ SXDEB(command,sos); pclose( f ); goto ready; }
ready:
signal( SIGPIPE, prevSigPipeHandler );
return sos;
}
|