使用access函數函數
功能:
檢查調用進程是否能夠對指定的文件執行某種操做。測試
用法:code
#include <unistd.h> #include <fcntl.h> int access(const char *pathname, int mode); pathname: 須要測試的文件路徑名。 mode: 須要測試的操做模式,可能值是一個或多個R_OK(可讀?), W_OK(可寫?), X_OK(可執行?) 或 F_OK(文件存在?)組合體。 返回說明: 成功執行時,返回0。失敗返回-1,errno被設爲如下的某個值 EINVAL: 模式值無效 EACCES: 文件或路徑名中包含的目錄不可訪問 ELOOP : 解釋路徑名過程當中存在太多的符號鏈接 ENAMETOOLONG:路徑名太長 ENOENT: 路徑名中的目錄不存在或是無效的符號鏈接 ENOTDIR: 路徑名中看成目錄的組件並不是目錄 EROFS: 文件系統只讀 EFAULT: 路徑名指向可訪問的空間外 EIO: 輸入輸出錯誤 ENOMEM: 不能獲取足夠的內核內存 ETXTBSY:對程序寫入出錯
程序實例:進程
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main() { if((access("mytest.c",F_OK))!=-1) { printf("file mytest.c exist.\n"); } else { printf("file mytest.c not exist\n"); } if(access("mytest.c",R_OK)!=-1) { printf("file test.c have read permission\n"); } else { printf("mytest.c cann't read.\n"); } if(access("mytest.c",W_OK)!=-1) { printf("mytest.c have write permission\n"); } else { printf("mytest.c can't wirte.\n"); } if(access("mytest.c",X_OK)!=-1) { printf("file mytest.c have executable permissions\n"); } else { printf("file mytest.c Not executable.\n"); } return 0; }