home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1694 / fpipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  3.2 KB  |  127 lines

  1. /******************************************************************************
  2. *                                                                             *
  3. *                                  fpipe.c                                    *
  4. *                                                                             *
  5. ******************************************************************************/
  6.  
  7. /*--------------------------  INITIAL CODING DATE -----------------------------
  8. Thu Nov 10 10:30:22 EST 1988 by George M. Bogatko
  9.  
  10. --------------------------------  HEADER FILES  -----------------------------*/
  11. #include <stdio.h>
  12. #include <fpipe.h>
  13. #include <fcntl.h>
  14. #include <varargs.h>
  15.  
  16. /*------------------  TYPEDEF'S, DEFINES, STRUCTURE DEF'S  ------------------*/
  17.  
  18. /*----------------  IMPORTED GLOBAL VARIABLE/FUNCTION DEF'S  ----------------*/
  19.  
  20. /*----------------  EXPORTED GLOBAL VARIABLE/FUNCTION DEF'S  ----------------*/
  21.  
  22. /*----------------  INTERNAL GLOBAL VARIABLE/FUNCTION DEF'S  ----------------*/
  23. #ident "%W% %G% - George M. Bogatko -"
  24.  
  25. /*-----------------------------------------------------------------------------
  26.  
  27. SYNOPSIS:
  28.     int fpipe(flag, arg_array, program, arg1, arg2, ... argn, 0 );
  29.  
  30. DESCRIPTION:
  31.     FPIPE creates a two way pipe between a calling process and
  32.     a child process.  
  33.     That child process should expect to read from STANDARD IN
  34.     and write to STANDARD OUT.
  35.  
  36.     The arguments are:
  37.  
  38.         flag - one of STD_ERR_SCR or STD_ERR_NUL.  STD_ERR_SCR will
  39.             put STANDARD ERR to the screen,  STD_ERR_NUL will
  40.             put STANDARD ERR to "/dev/null"
  41.  
  42.         arg_array - an array of 2 'FILE *' type pointers
  43.             EX:  FILE *arg_array[2];
  44.             When the function returns,
  45.                 
  46.                 arg_array[0] will contain the READING end of the
  47.                     pipe connected to the process's
  48.                     standard output, and
  49.  
  50.                 arg_array[1] will contain the WRITING end of the
  51.                     pipe connected to the process's
  52.                     standard input.
  53.  
  54.         program - the full path of the program you want to invoke.
  55.  
  56.         arg1 ... argN - all the arguments to your program
  57.  
  58.         0 - the termination point of the arg1 ... argN sequence.
  59.  
  60. RETURN:
  61.     the PROCESS ID of the created child on success,
  62.     -1 on any error (use "perror()" to find out what happened).
  63.  
  64. CAVEATS:
  65.     The LAST argument MUST be 0
  66.  
  67. =============================================================================*/
  68.  
  69. int fpipe(flag, arg_array, va_alist)
  70. int flag;
  71. FILE *arg_array[];
  72. va_dcl
  73. {
  74.     char **args;
  75.     int in_pfd[2];
  76.     int out_pfd[2];
  77.     FILE *read_fp, *write_fp;
  78.     char *file;
  79.     int i;
  80.     int proc_id = 0;
  81.  
  82.     if( pipe(in_pfd) == (-1) )
  83.         return(-1);
  84.     if( pipe(out_pfd) == (-1) )
  85.         return(-1);
  86.  
  87.     args = (char **)&va_alist; /* won't work on Pyramid */
  88.  
  89.     switch( proc_id = fork() )
  90.     {
  91.     case -1:
  92.         return(-1);
  93.  
  94.     case 0:
  95.         close(0);
  96.         dup(out_pfd[0]);
  97.         close(1);
  98.         dup(in_pfd[1]);
  99.         if( flag == STD_ERR_NUL )
  100.         {
  101.             close(2);
  102.             if(open("/dev/null", O_WRONLY) == -1)
  103.                 return(-1);
  104.         }
  105.         close(in_pfd[0]);
  106.         close(in_pfd[1]);
  107.         close(out_pfd[0]);
  108.         close(out_pfd[1]);
  109.         execv(args[0], args);
  110.         return(-1);
  111.  
  112.     default:
  113.         close(in_pfd[1]);
  114.         close(out_pfd[0]);
  115.  
  116.         if( (read_fp = fdopen( in_pfd[0], "r" )) == (FILE *)NULL )
  117.             return(-1);
  118.         if( (write_fp = fdopen( out_pfd[1], "w" )) == (FILE *)NULL )
  119.             return(-1);
  120.  
  121.         arg_array[0] = read_fp;
  122.         arg_array[1] = write_fp;
  123.  
  124.         return(proc_id);
  125.     }
  126. }
  127.