#include <stdio.h>
#include <unistd.h>
int main(void)
{
/**
*
* unistd.h
* int access(const char * pathname, int mode);
* access()会检查是否可以读/写某一已存在的文件。
*
* pathname:
* 待检测文件或文件夹
* mode: (unistd.h库文件内描述如下)
* define F_OK 0 test for existence of file
* define X_OK (1<<0) test for execute or search permission
* define W_OK (1<<1) test for write permission
* define R_OK (1<<2) test for read permission
*
* return:
* 0 sucess
* -1 fail
*
* mode可以使用数字并叠加进行多权限检测;
*/
// 检测文件是否存在
if(access("/tmp/test.log", F_OK) == 0)
printf("/tmp/test.log exists\n");
// 检测文件夹是否存在
if(access("/tmp", F_OK) == 0)
{
printf("/tmp exists\n");
}
// 检测文件是否拥有读权限
if(access("/tmp/test.log", F_OK) == 0)
{
printf("/tmp/test.log can be read\n");
}
// F_OK 0
// X_OK 1
// W_OK 2
// R_OK 4
// 使用数字检测文件是否存在
if(access("/tmp/test.log", 0) == 0)
printf("/tmp/test.log exists\n");
// 数字合并检测 6 = 4 + 2,检测是否拥有读写权限
if(access("/tmp/test.log", 6) == 0)
printf("/tmp/test.log can be read and write\n");
// 检测不具有权限
if(access("/etc/passwd", 6) == -1)
printf("/etc/passwd can not be read and write\n");
// 检测执行权限
if(access("./access", 1) == 0)
printf("./access can be execute\n");
}
c语言:access函数
最新推荐文章于 2023-07-22 16:32:43 发布