Below code and the quoted statement are from Linux System Programming by Robert Love
.
I don't understand how are the modes in the first and the second code examples the same.
From the first example, I calculated the mode values as
USR -> 2 1 = 3
GRP -> 2 1 = 3
OTH -> 1 = 1
So my expectation was 0331, but it the book says it's 0664.
What's wrong in my logic?
Copied from the book below:
int fd;
fd = open (file, O_WRONLY | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IROTH);
if(fd == -1)
/* error */
Trading portability (in theory at least) for readability, we could have written the following, to identical effect:
int fd;
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0664);
if(fd == -1)
/*error*/
CodePudding user response:
For whichever reason, you have chosen to use the wrong values for the constants.
- Instead of S_IRUSR being
0400
, you decided it was0200
. - Instead of S_IWUSR being
0200
, you decided it was0100
. - etc
If you use the correct values for the constants, you will get the same result.