简体   繁体   中英

How can I check to see if user has execute permissions?

I wish to know how to check if the "user" (other than who is running this program) has execute permissions on a file ? [C api]

I have looked at "access" it gives information with respect to the caller.

I am looking for something like :-

"<cmd> <user_name> <file_name>"

here I am trying to get if <user_name> has execute permissions for <file_name> ?

I am looking C api ?

Possible solution :- I am using the following algo to get this information

boolean_t
is_user_has_execute_permissions(char *run_as_user)
{
        /* Check world execute permission */
        if ((cmd_stat.st_mode & S_IXOTH) == S_IXOTH) {
                return (TRUE);
        }

        /* group id for run_as_user */
        getpwnam_r(run_as_user, &pw, buf, passwd_len);

        /* Check group execute permission */
        if ((cmd_stat.st_mode & S_IXGRP) == S_IXGRP) {
                if (pw->pw_gid == cmd_stat.st_gid)
                        return (TRUE);
        }

        return (FALSE);
}

Did anyone see any error in this one ?

You need the call stat(2), which returns the permission bits, for the owner of the file, the owner group, and the others. Than you have to find out the id of the user you're interested in and ids its groups: see getpwent(3) and getgrouplist(3). The first which match, will give the resulting permissions.

From the command line, you can use an Awk program easily enough. Something like

ls -l filename | awk -F '{ if (substring($1,3,1) == "x" exit(0); exit(1)}'

will set the return code to 0 if it's found, 1 if it's not.

From a C program, you want to use fstat . Basically you open the file

int fd = fopen("filename", "r");

then get the file stat block with fstat

fstat(fd, &bufr)

and look at bufr.st_mode .

Here's a description of fstat .

Update

I'll note crankily that when the OP originally posted, it wasn't clear the C API was what was desired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM