#include <sys/types.h>
#include <dirent.h>
#include <sys/time.h> 
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>

#define NODEFINE
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include "fd.h"

int
pm_pipe(int *p)
{
	int up[2];
	File *f[2];
	
/*
 *	This would preserve message boundaries,
 *	but seems to interact badly with programs that
 * 	expect a real file descriptor that you can read from.
 *	Oh well.
 *
	if(socketpair(PF_LOCAL, SOCK_DGRAM, 0, up) < 0){
		pm_oserror();
		return -1;
	}
 *
 */
	if(pipe(up) < 0){
		pm_oserror();
		return -1;
	}

	if((f[0] = pm_allocfile(up[0])) == nil){
		close(up[0]);
		close(up[1]);
		return -1;
	}
	if((f[1] = pm_allocfile(up[1])) == nil){
		close(up[0]);
		close(up[1]);
		pm_closefile(f[0]);
		return -1;
	}
	f[0]->type = Tpipe;
	f[0]->ufd = up[0];
	f[0]->path = nil;
	f[1]->type = Tpipe;
	f[1]->ufd = up[1];
	f[1]->path = nil;
	pm_qunlock(&f[0]->lk);
	pm_qunlock(&f[1]->lk);
	p[0] = f[0]->pmfd;
	p[1] = f[1]->pmfd;
	return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1