Blob
1 #include <u.h>2 #define NOPLAN9DEFINES3 #include <libc.h>4 #include <sys/stat.h>6 int7 p9create(char *path, int mode, ulong perm)8 {9 int fd, cexec, umode, rclose;11 cexec = mode&OCEXEC;12 rclose = mode&ORCLOSE;13 mode &= ~(ORCLOSE|OCEXEC);15 /* XXX should get mode mask right? */16 fd = -1;17 if(perm&DMDIR){18 if(mode != OREAD){19 werrstr("bad mode in directory create");20 goto out;21 }22 if(mkdir(path, perm&0777) < 0)23 goto out;24 fd = open(path, O_RDONLY);25 }else{26 umode = (mode&3)|O_CREAT|O_TRUNC;27 mode &= ~(3|OTRUNC);28 if(mode&OEXCL){29 umode |= O_EXCL;30 mode &= ~OEXCL;31 }32 if(mode){33 werrstr("unsupported mode in create");34 goto out;35 }36 fd = open(path, umode, perm);37 }38 out:39 if(fd >= 0){40 if(cexec)41 fcntl(fd, F_SETFL, FD_CLOEXEC);42 if(rclose)43 remove(path);44 }45 return fd;46 }