#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函數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.