How to read flags from mode_t type?


 
Thread Tools Search this Thread
Top Forums Programming How to read flags from mode_t type?
# 1  
Old 12-04-2008
How to read flags from mode_t type?

Hello,

After, I stat() a file, how can I read the various permissions flags from the st_mode variable which is of type mode_t? I would like to do something like:
Code:
struct stat *perms;
int res = stat(filename, perms);
if(perms->st.mode == S_IROTH)  do something;

but I know that is the wrong syntax. I would like to check the S_IROTH flag which determines if the file is world-readable. Man pages explain what the flags are; no necessarily how to access them. I'm not a C expert.

Thanks for your help.
# 2  
Old 12-04-2008
I think I may have it figured out. Is this correct:
Code:
if((perms->st_mode & S_IRWXO) && S_IROTH != 1) then return an error

to check if a file is world-readable and return an error if it's not? That's really the only permission I care about.
# 3  
Old 12-05-2008
Quote:
Originally Posted by pallak7
I think I may have it figured out. Is this correct:
Code:
if((perms->st_mode & S_IRWXO) && S_IROTH != 1) then return an error

to check if a file is world-readable and return an error if it's not? That's really the only permission I care about.
If you care only about world-writeable files then and'ing with S_IWOTH is all you need...
Code:
if (!(perms->st_mode & S_IWOTH))
        return (error if file is not world-writeable);

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to read file and check file type

Hi, I have a file with few values in it. I need script help to read file line by line and check: 1/if it's a file (with extension eg .java .css .jar etc ) or 2/if it's a file without extension and treat it as a directory and then check if the directory exists in working copy else create one... (6 Replies)
Discussion started by: iaav
6 Replies

2. UNIX for Dummies Questions & Answers

Ifconfig Flags

Hi there, I need your help in understanding the below Solaris 10 ifconfig output; athnetspns02>ifconfig -a lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 inet 127.0.0.1 netmask ff000000 e1000g0:... (2 Replies)
Discussion started by: wthomas
2 Replies

3. UNIX for Dummies Questions & Answers

WHat are flags?

Can anybody actually tell, what is flag? I know they are termed as permission flags and various others. Please explain (3 Replies)
Discussion started by: nixhead
3 Replies

4. UNIX for Advanced & Expert Users

Processes Communication Only with flags!

hello everybody and a happy new year! i am trying the client-server model...i have no problem with sockets etc... especially for server:there is a father who is listening for TCP connections from clients,the later send commands which parent shares to his children. then children execute... (1 Reply)
Discussion started by: vaggelakis
1 Replies

5. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

6. Programming

array type has incomplete element type

Dear colleagues, One of my friend have a problem with c code. While compiling a c program it displays a message like "array type has incomplete element type". Any body can provide a solution for it. Jaganadh.G (1 Reply)
Discussion started by: jaganadh
1 Replies

7. Shell Programming and Scripting

String type to date type

Can one string type variable changed into the date type variable. (1 Reply)
Discussion started by: rinku
1 Replies

8. Shell Programming and Scripting

makefile not taking -D flags

Hi, I found this strange behaviour while using one of the makefiles. Here is the snippet of the unix.mak that is necessary for this context SO = SvSocket.o SvStmt.o SvOdbcWrapper.o \ OdbcCallReader.o MgrCalls.o OdbcSvProxy.o \ OdbcSvApp.o... (4 Replies)
Discussion started by: vino
4 Replies

9. UNIX for Dummies Questions & Answers

if flags

Hi folks. I'm just starting to teach myself shell scripting and am having some trouble with an if statement. I am working with a directory where only one file will reside at a time and need to evaluate if this file is compressed to determine subsequent steps. I'm using echo for testing purposes.... (2 Replies)
Discussion started by: kristy
2 Replies
Login or Register to Ask a Question