home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- * *
- * fpipe.c *
- * *
- ******************************************************************************/
-
- /*-------------------------- INITIAL CODING DATE -----------------------------
- Thu Nov 10 10:30:22 EST 1988 by George M. Bogatko
-
- -------------------------------- HEADER FILES -----------------------------*/
- #include <stdio.h>
- #include <fpipe.h>
- #include <fcntl.h>
- #include <varargs.h>
-
- /*------------------ TYPEDEF'S, DEFINES, STRUCTURE DEF'S ------------------*/
-
- /*---------------- IMPORTED GLOBAL VARIABLE/FUNCTION DEF'S ----------------*/
-
- /*---------------- EXPORTED GLOBAL VARIABLE/FUNCTION DEF'S ----------------*/
-
- /*---------------- INTERNAL GLOBAL VARIABLE/FUNCTION DEF'S ----------------*/
- #ident "%W% %G% - George M. Bogatko -"
-
- /*-----------------------------------------------------------------------------
-
- SYNOPSIS:
- int fpipe(flag, arg_array, program, arg1, arg2, ... argn, 0 );
-
- DESCRIPTION:
- FPIPE creates a two way pipe between a calling process and
- a child process.
- That child process should expect to read from STANDARD IN
- and write to STANDARD OUT.
-
- The arguments are:
-
- flag - one of STD_ERR_SCR or STD_ERR_NUL. STD_ERR_SCR will
- put STANDARD ERR to the screen, STD_ERR_NUL will
- put STANDARD ERR to "/dev/null"
-
- arg_array - an array of 2 'FILE *' type pointers
- EX: FILE *arg_array[2];
- When the function returns,
-
- arg_array[0] will contain the READING end of the
- pipe connected to the process's
- standard output, and
-
- arg_array[1] will contain the WRITING end of the
- pipe connected to the process's
- standard input.
-
- program - the full path of the program you want to invoke.
-
- arg1 ... argN - all the arguments to your program
-
- 0 - the termination point of the arg1 ... argN sequence.
-
- RETURN:
- the PROCESS ID of the created child on success,
- -1 on any error (use "perror()" to find out what happened).
-
- CAVEATS:
- The LAST argument MUST be 0
-
- =============================================================================*/
-
- int fpipe(flag, arg_array, va_alist)
- int flag;
- FILE *arg_array[];
- va_dcl
- {
- char **args;
- int in_pfd[2];
- int out_pfd[2];
- FILE *read_fp, *write_fp;
- char *file;
- int i;
- int proc_id = 0;
-
- if( pipe(in_pfd) == (-1) )
- return(-1);
- if( pipe(out_pfd) == (-1) )
- return(-1);
-
- args = (char **)&va_alist; /* won't work on Pyramid */
-
- switch( proc_id = fork() )
- {
- case -1:
- return(-1);
-
- case 0:
- close(0);
- dup(out_pfd[0]);
- close(1);
- dup(in_pfd[1]);
- if( flag == STD_ERR_NUL )
- {
- close(2);
- if(open("/dev/null", O_WRONLY) == -1)
- return(-1);
- }
- close(in_pfd[0]);
- close(in_pfd[1]);
- close(out_pfd[0]);
- close(out_pfd[1]);
- execv(args[0], args);
- return(-1);
-
- default:
- close(in_pfd[1]);
- close(out_pfd[0]);
-
- if( (read_fp = fdopen( in_pfd[0], "r" )) == (FILE *)NULL )
- return(-1);
- if( (write_fp = fdopen( out_pfd[1], "w" )) == (FILE *)NULL )
- return(-1);
-
- arg_array[0] = read_fp;
- arg_array[1] = write_fp;
-
- return(proc_id);
- }
- }
-