@type script ############################################## # O_CREAT ############################################## # create without write flag is fine. It create # a file (if it does not exist before), but # we can only read from it open "f1.txt" [O_CREAT] write (FD 3) "ABCDE" 5 read (FD 3) 100 close (FD 3) open "f2.txt" [O_CREAT] write (FD 3) "ABCDE" 5 pread (FD 3) 100 0 close (FD 3) unlink "f2.txt" # if O_EXCL is given the file must not exist open "f1.txt" [O_RDWR;O_CREAT;O_EXCL] close (FD 3) open "f2.txt" [O_RDWR;O_CREAT;O_EXCL] write (FD 3) "ABCDE" 5 pread (FD 3) 100 0 close (FD 3) unlink "f2.txt" # if O_EXCL but not O_CREATE is set, the result is undefined open "f1.txt" [O_RDWR;O_EXCL] 0o777 close (FD 3) # to create a file, we need write access to the parent dir mkdir "d1" open_close /d1/f3.txt [O_CREAT;O_WRONLY] chmod /d1 open /d1/f3.txt [O_CREAT;O_RDWR] write (FD 3) "ABCDE" 5 pread (FD 3) 100 0 close (FD 3) unlink /d1/f3.txt # the file-permissions of a newly created file don't matter open /d1/f3.txt [O_CREAT;O_RDWR] write (FD 3) "ABCDE" 5 pread (FD 3) 100 0 close (FD 3) open /d1/f3.txt [O_RDWR] close (FD 3) open /d1/f3.txt [O_RDONLY] read (FD 3) 100 close (FD 3) # open does not change the permissions on existing files open /d1/f3.txt [O_CREAT;O_RDONLY] <---------> pread (FD 3) 100 0 close (FD 3) open /d1/f3.txt [O_RDONLY] read (FD 3) 100 close (FD 3) unlink /d1/f3.txt # not even read permission is needed on newly created files open /d1/f3.txt [O_CREAT;O_RDONLY] <---------> pread (FD 3) 100 0 close (FD 3) open /d1/f3.txt [O_RDONLY] read (FD 3) 100 close (FD 3) unlink /d1/f3.txt # O_CREAT flag only creates files not the dirs in path open /no-such-dir/ [O_CREAT;O_RDONLY] open /no-such-dir/file.txt [O_CREAT;O_RDONLY] close (FD 3)