zoukankan      html  css  js  c++  java
  • access()

    Linux access函数功能描述: 检查调用进程是否可以对指定的文件执行某种操作。
    Linux access函数用法: 
    #include 
    #include 
    int access(const char *pathname, int mode);
    Linux access函数参数: 
    pathname: 需要测试的文件路径名。 
    mode: 需要测试的操作模式,可能值是一个或多个R_OK(可读?), W_OK(可写?), X_OK(可执行?) 或 F_OK(文件存在?)组合体。
    Linux access函数返回说明: 
    成功执行时,返回0。失败返回-1,errno被设为以下的某个值 
    EINVAL: 模式值无效 
    EACCES: 文件或路径名中包含的目录不可访问 
    ELOOP : 解释路径名过程中存在太多的符号连接 
    ENAMETOOLONG:路径名太长 
    ENOENT:路径名中的目录不存在或是无效的符号连接 
    ENOTDIR: 路径名中当作目录的组件并非目录 
    EROFS: 文件系统只读 
    EFAULT: 路径名指向可访问的空间外 
    EIO:输入输出错误 
    ENOMEM: 不能获取足够的内核内存 
    ETXTBSY:对程序写入出错
    Linux access函数例子: 
    例子: 
     
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <unistd.h> 
    #include <fcntl.h>
    int main(int argc, char *argv[]) 

       if (argc < 2) { 
           printf("Usage: ./test filename "); 
           exit(1); 
       } 
       if (access(argv[1], F_OK) == -1) { 
           puts("File not exists!"); 
           exit(2); 
       } 
       if (access(argv[1], R_OK) == -1) 
           puts("You can't read the file!"); 
       else 
           if (access(argv[1], R_OK | W_OK) != -1) 
               puts("You can read and write the file"); 
           else 
               puts("You can read the file"); 
       
       exit(0); 

    #include <stdio.h> 
    #include <stdlib.h> 
    #include <unistd.h> 
    #include <fcntl.h>
    int main(int argc, char *argv[]) 

       if (argc < 2) { 
           printf("Usage: ./test filename "); 
           exit(1); 
       } 
       if (access(argv[1], F_OK) == -1) { 
           puts("File not exists!"); 
           exit(2); 
       } 
       if (access(argv[1], R_OK) == -1) 
           puts("You can't read the file!"); 
       else 
           if (access(argv[1], R_OK | W_OK) != -1) 
               puts("You can read and write the file"); 
           else 
               puts("You can read the file"); 
       
       exit(0); 
    }

    由于Linux Access函数只作权限的核查,并不理会文件形态或文件内容,因此,如果一目录表示为“可写入”,表示可以在该目录中建立新文件等操作,而非意味此目录可以被当做文件处理。例如,你会发现DOS的文件都具有“可执行”权限,但用execve执行时则会失败。
    Linux Access函数(判断是否具有存取文件的权限)
    相关函数
    stat,open,chmod,chown,setuid,setgid
    表头文件
    #include<unistd.h>
    定义函数
    int access;
    Linux Access函数说明
    access会检查是否可以读/写某一已存在的文件。参数mode有几种情况组合, R_OK,W_OK,X_OK 和F_OK。R_OK,W_OK与X_OK用来检查文件是否具有读取、写入和执行的权限。F_OK则是用来判断该文件是否存在。由于access只作权限的核查,并不理会文件形态或文件内容,因此,如果一目录表示为“可写入”,表示可以在该目录中建立新文件等操作,而非意味此目录可以被当做文件处理。例如,你会发现DOS的文件都具有“可执行”权限,但用execve执行时则会失败。
    Linux Access函数返回值
    若所有欲查核的权限都通过了检查则返回0值,表示成功,只要有一权限被禁止则返回-1。
    错误代码
    EACCESS 参数pathname 所指定的文件不符合所要求测试的权限。
    EROFS 欲测试写入权限的文件存在于只读文件系统内。
    EFAULT 参数pathname指针超出可存取内存空间。
    EINVAL 参数mode 不正确。
    ENAMETOOLONG 参数pathname太长。
    ENOTDIR 参数pathname为一目录。
    ENOMEM 核心内存不足
    ELOOP 参数pathname有过多符号连接问题。
    EIO I/O 存取错误。
    Linux Access函数附加说明
    使用access作用户认证方面的判断要特别小心,例如在access后再做open的空文件可能会造成系统安全上的问题。
    范例
    / 判断是否允许读取/etc/passwd /
    #include<unistd.h>
    int main

    执行
    /etc/passwd can be read

  • 相关阅读:
    nasm astrstr函数 x86
    nasm astrspn函数 x86
    nasm astrset_s函数 x86
    nasm astrrev函数 x86
    模板:最长公共子序列(LCS)
    模板:最小生成树(MST)
    模板:并查集
    模板:单源最短路径
    模板:最近公共祖先(LCA)
    模板:线段树(1)——加法,求和
  • 原文地址:https://www.cnblogs.com/banwhui/p/5051852.html
Copyright © 2011-2022 走看看