Blob


1 #define _GNU_SOURCE /* for Linux O_DIRECT */
2 #include <u.h>
3 #define NOPLAN9DEFINES
4 #include <sys/file.h>
5 #include <libc.h>
6 #ifndef O_DIRECT
7 #define O_DIRECT 0
8 #endif
10 int
11 p9open(char *name, int mode)
12 {
13 int cexec, rclose;
14 int fd, umode, lock, rdwr;
15 struct flock fl;
17 rdwr = mode&3;
18 umode = rdwr;
19 cexec = mode&OCEXEC;
20 rclose = mode&ORCLOSE;
21 lock = mode&OLOCK;
22 mode &= ~(3|OCEXEC|ORCLOSE|OLOCK);
23 if(mode&OTRUNC){
24 umode |= O_TRUNC;
25 mode ^= OTRUNC;
26 }
27 if(mode&ODIRECT){
28 umode |= O_DIRECT;
29 mode ^= ODIRECT;
30 }
31 if(mode&ONONBLOCK){
32 umode |= O_NONBLOCK;
33 mode ^= ONONBLOCK;
34 }
35 if(mode){
36 werrstr("mode 0x%x not supported", mode);
37 return -1;
38 }
39 fd = open(name, umode);
40 if(fd >= 0){
41 if(lock){
42 fl.l_type = (rdwr==OREAD) ? F_RDLCK : F_WRLCK;
43 fl.l_whence = SEEK_SET;
44 fl.l_start = 0;
45 fl.l_len = 0;
46 if(fcntl(fd, F_SETLK, &fl) < 0){
47 close(fd);
48 return -1;
49 }
50 }
51 if(cexec)
52 fcntl(fd, F_SETFL, FD_CLOEXEC);
53 if(rclose)
54 remove(name);
55 }
56 return fd;
57 }