kernel/fcntl.h
Included by 14 files
kernel/sysfile.c, user/cat.c, user/dorphan.c, user/forphan.c, user/grep.c, user/grind.c, user/init.c, user/logstress.c, user/ls.c, user/sh.c, user/stressfs.c, user/ulib.c, user/usertests.c, user/wc.cAbout this file
The flags a program passes as the second argument of open, shared by the kernel
(sys_open in kernel/sysfile.c) and user programs, which include it as
kernel/fcntl.h. A call combines one access mode with any of the extra flags using |,
as in open("out", O_CREATE | O_WRONLY | O_TRUNC).
Read next: sys_open, which interprets them.
Access modes
Whether the new open file (struct file) may be read, written, or both. sys_open turns the
mode into the readable and writable fields of the struct file:
- readable unless the
O_WRONLYbit (0x001) is set; - writable if
O_WRONLYorO_RDWR(0x002) is set.
Because O_RDONLY is 0, it cannot be tested with &; “read-only” is simply the
absence of the other two bits. Passing O_WRONLY | O_RDWR gives a write-only file.
Opening a directory for anything but O_RDONLY is refused.
Extra flags
O_CREATE(0x200): create the file if it does not exist (throughcreate). If it exists and is a file or a device, it is opened as is.O_TRUNC(0x400): if the file exists and is an ordinary file, discard its contents (itrunc), so writing starts from an empty file.
Each flag is a separate bit, so flags can be combined freely and tested with &.
xv6 has no other flags: there is no O_APPEND and no O_EXCL.