About file mode

I have questions about the st_mode in Unix
if I have a several statements like these:

1. STATS s;
2. int a;
3. a = s.st_mode&040777;
4. if (s.st_mode&0x1000)
5. {  ......  }
6. if (w&040000)
7. { ....... }

Q1. What does it mean to assign st_mode&.... to a/
Q2. On line 4 & 6, how do the conditional statements be   evaluated to be true or false?  
UTEKAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jshapiroConnect With a Mentor Commented:
The & is a bitwise AND.  I don't know much about st_mode but based on other similar examples the st_mode has a bunch of bits, each representing some flag like read-only, data, etc.  In line 3 you are setting a to contain the flags that are set for both s and the binary represenation of 40777.  In line 4 and 6 the bitwise and evaluates to 0 if there are no common flags set and TRUE if there is at least one
common flag set between the first variable and the binary
representation of the second variable.  I would need to
know more about st_mode to know exactly which flags.
0
 
UTEKAuthor Commented:
Thanks jshapiro.

I would give you those points if you can tell me about the st_mode representation. I understand the 777 but I am not quite sure about 040.
0
 
jshapiroCommented:
It's rare to see bit comparisons with hard coded values,
mostly because the actual values may vary while the type names are usually the same.  These are the types for
st_mode.  
Constant Meaning
_S_IFMT File type mask
_S_IFDIR Directory
_S_IFCHR Character special (indicates a device if set)
_S_IFREG Regular
_S_IREAD Read permission, owner
_S_IWRITE Write permission, owner
_S_IEXEC Execute/search permission, owner
The values are in your sys/stat.h file.  4000 means the
file is really a directory but I can't seem to find
40000.
0
 
UTEKAuthor Commented:
Thanks jshapiro.

I would give you those points if you can tell me about the st_mode representation. I understand the 777 but I am not quite sure about 040.
0
 
UTEKAuthor Commented:
Thanks a lot.
0
All Courses

From novice to tech pro — start learning today.