Issue
I'm writing a program that needs to be able to set file permissions, but for whatever reason, chmod is not behaving in the way I would expect it to. For a couple tests, I attempted to create two different files (fileOne.txt, and fileTwo.txt). fileOne.txt should have permissions set to 600, while fileTwo.txt should have permissions set to 777.
Running my program results in the following:
fileOne.txt having permissions set to ---x-wx--T
fileTwo.txt having permissions set to -r----x--t
?? WHAT?
Below is my code. The results of my printf are as anticipated, (600, 777), so why would chmod not like this?
printf("chmod = %d\n", (int)getHeader.p_owner * 100 + (int)getHeader.p_group * 10 + (int)getHeader.p_world);
chmod(getHeader.file_name, (int)getHeader.p_owner * 100 + (int)getHeader.p_group * 10 + (int)getHeader.p_world);
Solution
Permissions are reported in octal so 600
is in fact 0600
in C (or 384 in decimal).
Hence code should be:
printf("chmod = %d\n", (int)getHeader.p_owner * 100 + (int)getHeader.p_group * 10 + (int)getHeader.p_world);
chmod(getHeader.file_name, (int)getHeader.p_owner * 0100 + (int)getHeader.p_group * 010 + (int)getHeader.p_world);
Answered By - Maciej Piechotka Answer Checked By - Marie Seifert (WPSolving Admin)