Blob


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