I'm using the open system call to both create and open a file in the following manner:
fileID = open("aFile", O_CREAT|O_RDWR|O_TRUNC);
I expect the file to be created with read and write permissions, and sometimes it is, but only times when I run ls -l
I see just w, or just r, or just x, or ws, or rwx, etc. If I run my program with the system call 3 times, I may get 3 different file permissions set for aFile
each time. I don't know what could be causing the problem since it seems to be being set randomly. Any ideas as to what may be the cause?
open()
takes a third argument which takes effect when creating a file. This argument is a set of flags that modify the access mode of the new file. If you don't set a value for this argument, the open()
function will be provided with whatever happens to be in the register or stack position that applies to this argument -- depending on the specific system and compiler. In any case, it's unlikely to be what you intend, and will depend in an unpredictable way on the preceding code.
On Unix-like systems, man 2 open
should give you all the details.