All pastes #1184429 Raw Edit

Untitled

public c v1 · immutable
#1184429 ·published 2008-08-25 16:57 UTC
rendered paste body
#include <stdio.h>#include <unistd.h>#include <sys/types.h>int main(int argc,char **argv) {	int p_stdin[2], p_stdout[2];	pid_t pid, pid2;	pipe(p_stdin);	pipe(p_stdout);	if ((pid = fork()) == -1) {		perror("fork");		exit(1);	}	if (pid == 0) {		// Controlled process		close(p_stdin[1]);		close(p_stdout[0]);		dup2(0,p_stdin[0]);		dup2(1,p_stdout[1]);		dup2(2,p_stdout[1]);		execlp(argv[1],argv[1], NULL);	} else {		close(p_stdin[0]);		close(p_stdout[1]);		if ((pid2 = fork()) == -1) {			perror("fork");			exit(1);		}		if (pid2 == 0) {			char buffer[80];			int readlength;			//close(p_stdin[1]);			while ((readlength = read(p_stdout[0],buffer,sizeof(buffer)-1)) != 0) {				buffer[readlength+1] = 0;				printf("Read: %s",buffer);			}			close(p_stdout[0]);			exit(0);		} else {			//close(p_stdout[0]);			FILE *infile = fopen(argv[2],"r");			if (!infile) {				perror("fopen");				exit(1);			}			char buffer[80];			int readlength;			while ((readlength = fread(buffer,sizeof(buffer),1,infile)) != 0) {				write(p_stdin[1],buffer,readlength);			}			fclose(infile);			close(p_stdin[1]);		}	}	return 0;}