Blob
1 #define _GNU_SOURCE /* for Linux O_DIRECT */2 #include <u.h>3 #define NOPLAN9DEFINES4 #include <sys/file.h>5 #include <libc.h>6 #ifndef O_DIRECT7 #define O_DIRECT 08 #endif10 int11 p9open(char *name, int mode)12 {13 int cexec, rclose;14 int fd, umode, lock, rdwr;16 rdwr = mode&3;17 umode = rdwr;18 cexec = mode&OCEXEC;19 rclose = mode&ORCLOSE;20 lock = mode&OLOCK;21 mode &= ~(3|OCEXEC|ORCLOSE|OLOCK);22 if(mode&OTRUNC){23 umode |= O_TRUNC;24 mode ^= OTRUNC;25 }26 if(mode&ODIRECT){27 umode |= O_DIRECT;28 mode ^= ODIRECT;29 }30 if(mode){31 werrstr("mode 0x%x not supported", mode);32 return -1;33 }34 fd = open(name, umode);35 if(fd >= 0){36 if(lock){37 if(flock(fd, (rdwr==OREAD) ? LOCK_SH : LOCK_EX) < 0){38 close(fd);39 return -1;40 }41 }42 if(cexec)43 fcntl(fd, F_SETFL, FD_CLOEXEC);44 if(rclose)45 remove(name);46 }47 return fd;48 }