Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

pipe.c

Go to the documentation of this file.
00001 
00002 /*
00003  *   THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE
00004  *   CHECKED IT OUT USING THE COMMAND CHECKOUT.
00005  *
00006  *    $Id: pipe_8c-source.html 2161 2006-05-19 16:55:03Z paulf $
00007  *
00008  *    Revision history:
00009  *     $Log$
00009  *     Revision 1.1  2006/05/19 16:55:02  paulf
00009  *     first inclusion
00009  *
00010  *     Revision 1.1  2000/02/14 18:53:30  lucky
00011  *     Initial revision
00012  *
00013  *
00014  */
00015 
00016 /***************************************************************
00017  *                            pipe.c                           *
00018  *                                                             *
00019  *      Routines for writing to and reading from a pipe        *
00020  *      under Windows NT.                                      *
00021  ***************************************************************/
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 static FILE  *Pipe;  /* "write-handle" to pipe to "child" process */
00028 
00029 
00030 /*****************************************************************/
00031 /* pipe_init() starts a process and opens a pipe to it.  The     */
00032 /*             pipe replaces stdin of the new process.           */
00033 /*    Returns:   0 on success                                    */
00034 /*              -1 on failure                                    */
00035 /*****************************************************************/
00036 int pipe_init( char  *nextproc,  /* command to start new process */
00037       unsigned long   pipesize ) /* how big (bytes) to make pipe */
00038                                  /* NOT USED under Solaris       */
00039 {
00040    Pipe = _popen( nextproc, "w" );
00041    if ( Pipe == (FILE *) NULL ) return (-1);
00042    return ( 0 );
00043 }
00044 
00045 
00046 /*****************************************************************/
00047 /* pipe_put() writes a msg to a pipe, terminating with null byte */
00048 /* Returns 0 if there were no errors,                            */
00049 /*        some number if there were errors                       */
00050 /*****************************************************************/
00051 #define MAXWRITE 500
00052 
00053 int pipe_put( char *msg,        /* null-terminated char string  */
00054               int   msgtype )   /* type of message (0-255)      */
00055 {
00056    char     str[4];
00057    char     *m;
00058    unsigned n;
00059    unsigned nwrite;
00060    unsigned nwritten;    /* Number of chars written to pipe    */
00061 
00062 /* Write message type to pipe
00063    **************************/
00064    if ( (msgtype > 255) || (msgtype < 0) )
00065    {
00066       fprintf( stderr, "msgtype out of range.  msgtype: %d\n", msgtype );
00067       return( -1 );
00068    }
00069    sprintf( str, "%3d", msgtype );
00070    nwritten = fwrite( str, sizeof(char), (size_t)3, Pipe );
00071 /* printf("pipe_put:  type:%3d.   msg:\n%s\n", msgtype, msg ); */
00072    if ( nwritten < 3 )
00073    {
00074       fprintf( stderr, "Write error. nwritten: %d  Should be 3.\n", nwritten );
00075       return( -1 );
00076    }
00077 
00078 /* Write message string to pipe, including the null character
00079    **********************************************************/
00080    m        = msg;
00081    nwrite   = strlen( msg ) + 1;
00082    nwritten = 0;
00083 
00084    while ( nwrite > 0 )
00085    {
00086       n = fwrite( m, sizeof(char), ( nwrite > MAXWRITE ) ?
00087                   (size_t)MAXWRITE : (size_t)nwrite, Pipe );
00088       if ( n == 0 )
00089       {
00090          fprintf( stderr, "pipe_put(): Write error!\n" );
00091          return( -1 );
00092       }
00093       nwritten += n;
00094       nwrite -= n;
00095       m += n;
00096       fflush( Pipe );
00097    }
00098 
00099    if ( nwritten != strlen( msg ) + 1 )
00100    {
00101       fprintf( stderr, "Pipe write error. nwritten != strlen(msg)+1\n" );
00102       return( -1 );
00103    }
00104 
00105    return( 0 );
00106 }
00107 
00108 /*****************************************************************
00109  * pipe_get() reads a msg from a pipe (stdin) and writes it as a *
00110  *            null-terminated char string to the given address   *
00111  * Returns  # of chars written to msg (not including null-byte)  *
00112  *         -1 if the message was longer than maxlen              *
00113  *         -2 if EOF encountered reading message type            *
00114  *****************************************************************/
00115 
00116 int pipe_get( char *msg,        /* address to copy msg to       */
00117               int   maxlen,     /* size of msg buffer           */
00118               int  *type )      /* type of message returned     */
00119 {
00120    char  typestr[4];
00121    char *m;
00122    char  ch;
00123    int   i;
00124 
00125 /* Start by reading message type from pipe
00126  *****************************************/
00127    m = typestr;     /* Use a working copy of the target address */
00128 
00129    for ( i = 0; i < 3; i++ )
00130    {
00131       *m = fgetc( stdin );
00132       if ( *m == '\0' )
00133       {
00134          *m = '\0';
00135          return ( -2 );
00136       }
00137       if ( *m == (char)EOF )
00138       {
00139          *m = '\0';
00140          return ( -3 );
00141       }
00142       m++;
00143    }
00144    typestr[i] = '\0';
00145   *type = atoi(typestr);
00146 
00147 /* Now read the message (terminated by null byte)
00148  ************************************************/
00149    m = msg;     /* Use a working copy of the target address */
00150 
00151    for ( i = 0; i < maxlen; i++ )
00152    {
00153       *m = fgetc( stdin );
00154       if ( *m == '\0' || *m == (char)EOF )
00155       {
00156          *m = '\0';
00157          return ( i );
00158       }
00159       m++;
00160    }
00161 
00162 /* If you got here, the message was too long!  Skip to the end of it
00163  *******************************************************************/
00164    while( 1 )
00165    {
00166       ch = fgetc( stdin );
00167       if ( ch == '\0' || ch == (char)EOF )
00168          break;
00169    }
00170    return( -1 );
00171 }
00172 
00173 
00174 /*****************************************************************/
00175 /* pipe_close()  Closes the pipe                                 */
00176 /*****************************************************************/
00177 void pipe_close( void )
00178 {
00179    _pclose( Pipe );
00180 }

Generated on Tue May 6 09:16:07 2003 for Earthworm Libs by doxygen1.3-rc3