您的位置:首页 > 产品设计 > UI/UE

access and faccessat 按照实际用户Id和实际组ID进行文件权限测试

2015-09-14 14:51 507 查看
一、源代码:

<bldc:/home/tingbinz/apue.3e/SBSCODE/4>R*_*G:vim 4_8.c

  1 #include "apue.h"

  2 #include <fcntl.h>

  3

  4 int main(int argc, char *argv[])

  5 {

  6         if ( argc != 2 )

  7                 err_quit("Usage: access <pathname>");

  8         if ( access(argv[1], R_OK) < 0 )

  9                 err_ret("access error");

 10         else

 11                 printf("real user id has read permission of %s\n",argv[1]);

 12         if ( open(argv[1],O_RDONLY) < 0 )

 13                 err_ret("open error");

 14         else

 15                 printf("open file %s successfully\n", argv[1]);

 16

 17         exit(0);

 18

 19 }

 20

~

二、运行结果:

<bldc:/home/tingbinz/apue.3e/SBSCODE/4>R*_*G:access error.c

real user id has read permission of error.c

open file error.c successfully

<bldc:/home/tingbinz/apue.3e/SBSCODE/4>R*_*G:access /etc/passwd

real user id has read permission of /etc/passwd

open file /etc/passwd successfully

<bldc:/home/tingbinz/apue.3e/SBSCODE/4>R*_*G:access /etc/shadow

access error: Permission denied

open error: Permission denied

<bldc:/home/tingbinz/apue.3e/SBSCODE/4>R*_*G:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息