By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,636 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Finding access permissions in UNIX C

schmals
P: 8
Hello,

I am currently writing a simple shell program for UNIX and I've run into a bit of an issue. So, every file has 9 access permissions: read, write, and execute for user, group, and others. I know the stat struct contains mode_t st_mode which has the file type and access permissions encoded within it. It is easy to recover the file type from the variable because there are macro functions which will return true if the file is of that type. I need to decode the 9 access permissions from this variable though and I can't figure out how. I know there are functions such as umask() and chmod() which will set the access permissions for a current process or given file, but that doesn't really help me in my case. Basically, I just need to be educated on whether there is a function that will tell me the access permissions given the st_mode variable, or any tips to decode the access permissions from the variable itself.

Thank you very much!!!
May 12 '08 #1
Share this Question
Share on Google+
3 Replies


gpraghuram
Expert 100+
P: 1,275
Hello,

I am currently writing a simple shell program for UNIX and I've run into a bit of an issue. So, every file has 9 access permissions: read, write, and execute for user, group, and others. I know the stat struct contains mode_t st_mode which has the file type and access permissions encoded within it. It is easy to recover the file type from the variable because there are macro functions which will return true if the file is of that type. I need to decode the 9 access permissions from this variable though and I can't figure out how. I know there are functions such as umask() and chmod() which will set the access permissions for a current process or given file, but that doesn't really help me in my case. Basically, I just need to be educated on whether there is a function that will tell me the access permissions given the st_mode variable, or any tips to decode the access permissions from the variable itself.

Thank you very much!!!
Check this link
This provide some info about the mode

raghuram
May 12 '08 #2

RRick
Expert 100+
P: 463
Mode_t is part of the C unix API and is not directly accessed via a script. If you want to use mode_t, you need to make a C program with it and then compile and run the program.

Shell scripts have a more direct method for retrieving this information. Try using the following shell stat command.
Expand|Select|Wrap|Line Numbers
  1. stat -c "%A" .../fileName
This returns a string (i.e. "-rwxr-xr--") that you can parse for permissions info. There are a host of other options you can use with stat, too. On a unix box, type in man stat for more info.
May 14 '08 #3

gpraghuram
Expert 100+
P: 1,275
Sorry,
I missed the pint that he is writing a shell script.

Raghuram
May 14 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.