Home  >  Article  >  Operation and Maintenance  >  How to use linux c access function

How to use linux c access function

藏色散人
藏色散人Original
2023-03-27 10:46:491582browse

The function of the linux c access function is to determine the access permissions of the file. The syntax of this function is "int access(const char * pathname, int mode);", where the parameter "pathname" represents the file that needs to be detected. pathname.

How to use linux c access function

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

How to use linux c access function?

linux C access

【1】Function

##Determine file access permissions

【2】Header file

#include <unistd.h>

【3】Function definition

int access(const char * pathname,  int mode);

【4】Parameter description

Pathname: The file path name to be detected

Mode: When the parameter is 0, it means checking the existence of the file. If the file exists, 0 is returned. If it does not exist, -1 is returned. This function can also check other file attributes:

## 06 Check read and write permissions

 04 Check read permissions

02 Check write permission

01 Check execution permission

00 Check file existence

【5】Return value

If all the permissions to be checked pass the check, then A value of 0 is returned, indicating success; as long as one permission is prohibited, -1 is returned, indicating failure.

【6】Function description

Access() will check whether an existing file can be read/written.

There are several combinations of parameter mode, R_OK, W_OK, X_OK and F_OK.

R_OK, W_OK and X_OK are used to check whether the file has read, write and execute permissions respectively. F_OK is used to determine whether the file exists.

Since access() only checks permissions and does not pay attention to the file form or file content, therefore, if a directory is indicated as "writable", it means that the directory can be accessed Operations such as creating new files do not mean that this directory can be treated as a file.

For example, you will find that all DOS files have "executable" permissions, but they will fail when executed with execve().

【7】Error code

EACCESS The file specified by the parameter pathname does not meet the permissions required for the test.

EROFS The file to be tested for write permissions exists in a read-only file system.

EFAULT The parameter pathname pointer exceeds the accessible memory space.

EINVAL parameter mode is incorrect.

 ENAMETOOLONG The parameter pathname is too long.

ENOTDIR The parameter pathname is a directory.

ENOMEM Insufficient core memory

  ELOOP   参数pathname有过多符号连接问题。

  EIO I/O  存取错误。

【8】附加说明

    使用access()作用户认证方面的判断要特别小心,例如在access()后再做open()的空文件可能会造成系统安全上的问题。

【9】范例

// 范例1
#include <unistd.h>
int main()
{
    if (access("/etc/passwd", R_OK) == 0)
        printf("/etc/passwd can be read\n");
    return 0;
}
// 范例2
#include 
#include <unistd.h>
int file_exists(char *filename);
int main(void)
{
    printf("Does NOTEXIST.FIL exist: %s\n",
    file_exists("NOTEXISTS.FIL") ? "YES" : "NO");
    return 0;
}
int file_exists(char *filename)
{
    return (access(filename, 0) == 0);
}

【10】相关函数

stat,open,chmod,chown,setuid,setgid

推荐学习:《linux视频教程

The above is the detailed content of How to use linux c access function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is linux nginxNext article:what is linux nginx