#include <u.h>
#include <libc.h>
#define NODEFINE
#include <9pm/u.h>
#include <9pm/libc.h>
#include <9pm/ns.h>

int
pm_pipe(int fd[2])
{
	int i, sysfd[2];
	char buf[30];
	Chan *c[2];
	Dir *d;
	PmQid q;

	if(pipe(sysfd) < 0){
		pm_oserror();
		return -1;
	}

	for(i=0; i<2; i++){
		if((d = dirfstat(sysfd[i])) != nil){
			q = *(PmQid*)&d->qid;
			free(d);
		}else
			memset(&q, 0, sizeof q);
		sprint(buf, "#|/pipe%d", i);
		c[i] = pm_sysfd2chan(sysfd[i], ORDWR, q, buf);
	}
	if(pm_newfd2(fd, c) < 0){
		pm_cclose(c[0]);
		pm_cclose(c[1]);
		return -1;
	}
	return 0;
}

