文件和目录:access函数

access函数是按照实际用户ID和实际组ID进行访问权限测试的:

#include <unistd.h>
int access( const char *pathname, int mode );

返回值:若成功则返回0,若出错则返回-1

mode是表下所列常量的按位或

access函数的mode常量,取自<unistd.h>

mode 说明
R_OK 测试读权限
W_OK 测试写权限
X_OK 测试执行权限
F_OK 测试文件是否存在

 

 

 

 

 

 

测试实例如下:

#include<fcntl.h>
#include "apue.h"

int main(int argc ,char *argv[])
{
  if (argc!=2)
    err_quit("usage: access <pathname>");
  if (access(argv[1],R_OK)<0)
    err_ret("access error for %s",argv[1]);
  else
    printf("read access OK ");
  if (open(argv[1],O_RDONLY)<0)
    err_ret("open error for %s",argv[1]);
  else
    printf("open for reading OK ");
  exit(0);

}

运行结果:

 

 

原文地址:https://www.cnblogs.com/hezhangyear/p/4026599.html