All pastes #1536784 Raw Edit

richard

public text v1 · immutable
#1536784 ·published 2009-08-20 19:54 UTC
rendered paste body
/*	This program tests the functionality of the pipes.	It opens a pipe, forks and executes cat after dup-ing	the output end to stdin.	Afterwards the parent spews a pile of data into the input end,	closes the pipe and waits for cat to exit.*/#include "api.h"#include "lib.h"// the array indicies that pipe() returns.#define READ 0#define WRITE 1// the data to send over the pipe.const char* teststr="This is a drill, this is a drill\n";int main(int argc,char**argv) {	int len=strlen(teststr)+1;	print("Pipe test 1\n");	int fds[2];	// open a pipe	if(sys_pipe(fds)==-1) {		printf("Error creating pipes: %d\n",errno);		return 1;	}	printf("Got FDs in:%d\tout=%d\n",fds[READ],fds[WRITE]);	if(sys_fork()==0) { // dup it and fork off to cat.		sys_close(fds[WRITE]);		sys_dup(fds[READ],stdin);		int fd=sys_open("bin/cat",0);		if(fd==-1) {			printf("Couldn't open cat file: %d.\n",errno);			return 1;		}		sys_execve(fd,argv);	} else {	// close the other end of the pipe		sys_close(fds[READ]);		for(int i=0;i<5;++i)	// spew some data down it.			sys_write(fds[WRITE],teststr,len);		sys_close(fds[WRITE]);		// close it and wait.		int c;		sys_wait(&c);		printf("Cat returned %d.\n",c);	}	return 0;}