Blame


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