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