/* ECP: FILEname=fig13_28.c */ /* 1*/ int /* 2*/ RunCommand( FullCommand *Buf ) /* 3*/ { /* 4*/ pid_t Pid; /* 5*/ int Fd[ 2 ]; /* 6*/ int i; /* 7*/ if( ( Pid = fork( ) ) < 0 ) /* Error */ /* 8*/ return -1; /* 9*/ if( Pid == 0 ) /* Child */ /*10*/ { /*11*/ SetFiles( Buf ); /*12*/ if( !Buf->Background ) /*13*/ { /*14*/ signal( SIGINT, SIG_DFL ); /* Restore Signals */ /*15*/ signal( SIGQUIT, SIG_DFL ); /* Before Exec */ /*16*/ } /*17*/ /* For Each pipe, We Need An Additional Process */ /*18*/ for( i = Buf->NumPipes; i > 0 ; i-- ) /*19*/ { /*20*/ if( pipe( Fd ) < 0 ) /*21*/ { /*22*/ perror( Buf->Commands[ 0 ][ 0 ] ); /*23*/ _exit( 127 ); /*24*/ } /*25*/ if( ( Pid = fork( ) ) < 0 ) /*26*/ return -1; /*27*/ UsePipe( Fd, Pid ? STDIN_FILENO : STDOUT_FILENO ); /*28*/ if( Pid != 0 ) /* Parent */ /*29*/ break; /*30*/ } /*31*/ execvp( Buf->Commands[ i ][ 0 ], Buf->Commands[ i ] ); /*32*/ perror( Buf->Commands[ i ][ 0 ] ); /*33*/ _exit( 127 ); /* execl Failed; Give Up On Child */ /*34*/ } /*35*/ /* Shell Process: Note Pid Is Last Command In Pipeline */ /*36*/ if( !Buf->Background ) /*37*/ waitpid( Pid, NULL, 0 ); /*38*/ return 0; /*39*/ }