diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/ape/lib/ap/plan9/open.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/ape/lib/ap/plan9/open.c')
-rwxr-xr-x | sys/src/ape/lib/ap/plan9/open.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/sys/src/ape/lib/ap/plan9/open.c b/sys/src/ape/lib/ap/plan9/open.c new file mode 100755 index 000000000..1cfe0d6b5 --- /dev/null +++ b/sys/src/ape/lib/ap/plan9/open.c @@ -0,0 +1,62 @@ +#include <errno.h> +#include <stdarg.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include "lib.h" +#include <sys/stat.h> +#include "sys9.h" + +/* + * O_NOCTTY has no effect + */ +int +open(const char *path, int flags, ...) +{ + int n; + long f; + int mode; + Fdinfo *fi; + va_list va; + struct stat sbuf; + + f = flags&O_ACCMODE; + if(flags&O_CREAT){ + if(access(path, 0) >= 0){ + if(flags&O_EXCL){ + errno = EEXIST; + return -1; + }else{ + if((flags&O_TRUNC)&&(flags&(O_RDWR|O_WRONLY))) + f |= 16; + n = _OPEN(path, f); + } + }else{ + va_start(va, flags); + mode = va_arg(va, int); + va_end(va); + n = _CREATE(path, f, mode&0777); + } + if(n < 0) + _syserrno(); + }else{ + if((flags&O_TRUNC)&&(flags&(O_RDWR|O_WRONLY))) + f |= 16; + n = _OPEN(path, f); + if(n < 0) + _syserrno(); + } + if(n >= 0){ + fi = &_fdinfo[n]; + fi->flags = FD_ISOPEN; + fi->oflags = flags&(O_ACCMODE|O_NONBLOCK|O_APPEND); + fi->uid = -2; + fi->gid = -2; + fi->name = malloc(strlen(path)+1); + if(fi->name) + strcpy(fi->name, path); + if(fi->oflags&O_APPEND) + _SEEK(n, 0, 2); + } + return n; +} |