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>
6 32f69c36 2003-12-11 devnull extern char *_p9translate(char*);
9 32f69c36 2003-12-11 devnull p9create(char *xpath, int mode, ulong perm)
11 32f69c36 2003-12-11 devnull int fd, cexec, umode, rclose;
12 32f69c36 2003-12-11 devnull char *path;
14 32f69c36 2003-12-11 devnull if((path = _p9translate(xpath)) == nil)
15 32f69c36 2003-12-11 devnull return -1;
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);
21 32f69c36 2003-12-11 devnull /* XXX should get mode mask right? */
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;
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);
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;
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;
42 32f69c36 2003-12-11 devnull fd = open(path, umode, perm);
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);
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;