[ACCEPTED]-Using the open() system call-unix

Accepted answer
Score: 18

You probably need the third argument. For 1 example:

Score: 14

Just use the optional third argument to 1 open:

int open(const char* pathname, int flags, mode_t mode);

so like this:

open("blahblah", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSER | S_IRGRP | S_IROTH);

See man open(2).

Score: 1

On Linux there's a third argument you can 6 use to pass permissions. S_IWUSR should 5 be the flag to give you write permissions, but 4 in practice you'll probably want to use 3 more flags than just that one (bitwise or'd 2 together). Check the manpage for a list 1 of the permission flags.

Score: 1

From the manual:


If the file exists, this 26 flag has no effect except as noted under 25 O_EXCL below. Otherwise, the file shall 24 be created; the user ID of the file shall be 23 set to the effective user ID of the process; the 22 group ID of the file shall be set to the 21 group ID of the file's parent directory 20 or to the effective group ID of the process; and the access permission bits (see ) of the file mode shall be set to the value of the third argument taken 19 as type mode_t modified as follows: a 18 bitwise AND is performed on the file-mode 17 bits and the corresponding bits in the 16 complement of the process' file mode creation mask. Thus, all 15 bits in the file mode whose corresponding 14 bit in the file mode creation mask is 13 set are cleared. When bits other than 12 the file permission bits are set, the 11 effect is unspecified. The third argument 10 does not affect whether the file is open for 9 reading, writing, or for both. Implementations 8 shall provide a way to initialize the 7 file's group ID to the group ID of the 6 parent directory. Implementations may, but 5 need not, provide an implementation-defined 4 way to initialize the file's group ID 3 to the effective group ID of the calling process.

So 2 it seems you need to pass a third argument 1 specifying the desired file permissions.

Score: 1

Note that under POSIX (Unix, Linux, MacOS, etc) you 4 can open and create a file with any permissions 3 you choose, including 0 (no permission for 2 anyone), and yet still write to the file 1 if opened for writing.

More Related questions