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:

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.

I think I may have it figured out. Is this correct:

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...

if (!(perms->st_mode & S_IWOTH))
        return (error if file is not world-writeable);