• C

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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jshapiroCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.