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

int
open(char *file, int mode)
{
	char *fullpath, *path;
	volatile int ret;
	volatile Chan *c;
	Mnt *mnt;

	if((path = nsassign(file, &mnt, &fullpath)) == nil)
		return -1;
	ret = -1;
	checkerrstack();
	c = nil;
	if(!waserror()){
		c = newchan(fullpath, mnt);
		(*mnt->dev->_open)(c, mnt, path, mode);
		ret = newfd(c);
		poperror();
	}
	if(ret >= 0){
		c->omode = mode;
		c = nil;	/* ref goes into fd table */
	}
	if(c)
		cclose(c);
	free(path);
	mntclose(mnt);
	checkerrstack();
	return ret;
}
