#include <unistd.h>

int makepipe(int *in,int *out,int *err)
{	
	int     o_r0w1[2]; 				/* Output from cmd */
	int     i_r0w1[2]; 				/* Input from cmd  */
	int     e_r0w1[2]; 				/* Error from cmd  */

	*in = 0;
	*out =0;
	*err =0;

	/* make the pipe */
	if (pipe(o_r0w1) < 0) return 0;
	if (pipe(i_r0w1) < 0) {
		close(o_r0w1[0]);
		close(o_r0w1[1]);
                return 0;
                }
        if (pipe(e_r0w1) < 0) {
		close(o_r0w1[0]);
		close(o_r0w1[1]);
		close(i_r0w1[0]);
		close(i_r0w1[1]);
		return 0;
		}

	/* fork and exec */
        switch (fork()) {
          case -1:                                      /* error */
                return -1;

          case 0:                                       /* child */
                close(o_r0w1[0]);
                close(e_r0w1[0]);
                close(i_r0w1[1]);

                /* redirect stdin to go to the "read" end of the pipe */
		close(0);
		dup(i_r0w1[0]);
		close(i_r0w1[0]);

                /* redirect stdout to go to the "write" end of the pipe */
                close(1);
                dup(o_r0w1[1]);
                close(o_r0w1[1]);

                /* redirect stderr to go to the "write" end of the pipe */
                close(2);
                dup(e_r0w1[1]);
                close(e_r0w1[1]);

		return 0;

          default:                                      /* parent */
                /* close the "write" end of the pipe */ 
                close(o_r0w1[1]);
                close(e_r0w1[1]);
                close(i_r0w1[0]);

		*in  = i_r0w1[1];
                *out = o_r0w1[0];
		*err = e_r0w1[0];
          }
	return(1);
	}
