plan9port/src/lib9/create.c
rsc 8ad517944e Today's changes.
More changes.
2004-03-25 23:03:57 +00:00

46 lines
805 B
C

#include <u.h>
#define NOPLAN9DEFINES
#include <libc.h>
#include <sys/stat.h>
int
p9create(char *path, int mode, ulong perm)
{
int fd, cexec, umode, rclose;
cexec = mode&OCEXEC;
rclose = mode&ORCLOSE;
mode &= ~(ORCLOSE|OCEXEC);
/* XXX should get mode mask right? */
fd = -1;
if(perm&DMDIR){
if(mode != OREAD){
werrstr("bad mode in directory create");
goto out;
}
if(mkdir(path, perm&0777) < 0)
goto out;
fd = open(path, O_RDONLY);
}else{
umode = (mode&3)|O_CREAT|O_TRUNC;
mode &= ~(3|OTRUNC);
if(mode&OEXCL){
umode |= O_EXCL;
mode &= ~OEXCL;
}
if(mode){
werrstr("unsupported mode in create");
goto out;
}
fd = open(path, umode, perm);
}
out:
if(fd >= 0){
if(cexec)
fcntl(fd, F_SETFL, FD_CLOEXEC);
if(rclose)
remove(path);
}
return fd;
}