[英]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]
[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>
? 如果
<user_name>
具有<file_name>
执行权限,我试图获取?
I am looking C api ? 我在寻找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. 您需要调用stat(2),它返回文件所有者,所有者组和其他人的权限位。 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).
您必须找出您感兴趣的用户的ID并且需要其组:请参阅getpwent(3)和getgrouplist(3)。 The first which match, will give the resulting permissions.
匹配的第一个将给出结果权限。
From the command line, you can use an Awk program easily enough. 从命令行,您可以轻松地使用Awk程序。 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. 如果找到则将返回代码设置为0,如果不是则将其设置为1。
From a C program, you want to use fstat
. 从C程序,您想要使用
fstat
。 Basically you open the file 基本上你打开文件
int fd = fopen("filename", "r");
then get the file stat block with fstat
然后使用
fstat
获取文件stat块
fstat(fd, &bufr)
and look at bufr.st_mode
. 并查看
bufr.st_mode
。
Here's a description of fstat . 这是fstat的描述。
Update 更新
I'll note crankily that when the OP originally posted, it wasn't clear the C API was what was desired. 我会注意到,当OP最初发布时,不清楚C API是否是所期望的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.