flags for st_mode of stat system call flags for st_mode of stat system call unix unix

flags for st_mode of stat system call


mode is a bitfield which is a common way to pack data. Think of each bit in the field as being a toggle switch which can be set to off or on. To check if the toggle is on, you check to see if the appropriate bit has been set using the & operator. You can set bits using | and clear them using ~ bitwise operations.


Well, the POSIX spec for <sys/stat.h> enumerates everything you can learn from the st_mode field of struct stat.

Is your question "What can this field tell me", or is it "How do I extract the information", or both?


It's common in operating system programing to use unsigned integers to hold a series of bits that describe something. In this case, mode has 9 bits of interest. One bit tells if the owner can read, one for owner write, one for owner execute, and similar bits for the group and others. The trick is in extracting whether a bit is set or not.

Here's an example. The S_IRUSR is a name for a bitmask that describes the "owner read" bit. It's defined in the specs as 0400 (octal) which translates to 100000000 in binary. When you take the binary and of this value with the mask, you get the value 100000000 (binary) if the 9th bit of the mask is set. Otherwise you get 0.

The if-statement evaluates to true if the large value is returned, and false otherwise. This way if the bit is set, 'r' is written into the string into position 1. The same technique applies to all of the other bits as well.