玩玩linux下的access函数---探测文件/目录权限
2015-05-06 22:53
489 查看
access函数用来探测文件/目录权限, 我们先来看程序:
[taoge@localhost learn_c]$ ls
test.c
[taoge@localhost learn_c]$ gcc test.c
[taoge@localhost learn_c]$ ./a.out
a.out test.c
e0: No such file or directory
e1: No such file or directory
e2: No such file or directory
e3: No such file or directory
file ok
e1: Permission denied
e2: Permission denied
e3: Permission denied
file ok
read ok
e2: Permission denied
e3: Permission denied
file ok
read ok
write ok
e3: Permission denied
file ok
read ok
write ok
exec ok
[taoge@localhost learn_c]$
我们再来看目录, 代码如下:
[taoge@localhost learn_c]$ ls
test.c
[taoge@localhost learn_c]$ gcc test.c
[taoge@localhost learn_c]$ ./a.out
a.out test.c
e0: No such file or directory
e1: No such file or directory
e2: No such file or directory
e3: No such file or directory
directory ok
e1: Permission denied
e2: Permission denied
e3: Permission denied
directory ok
read ok
e2: Permission denied
e3: Permission denied
directory ok
read ok
write ok
e3: Permission denied
directory ok
read ok
write ok
exec ok
[taoge@localhost learn_c]$
不消多说。
#include <stdio.h> #include <unistd.h> int main() { char szTest[][100] = { "ls", "touch test", // 此时, test是文件 "chmod u-rwx test", "chmod u+r test", "chmod u+w test", "chmod u+x test", }; int i = 0; int n = sizeof(szTest) / sizeof(szTest[0]); for(i = 0; i < n; i++) { system(szTest[i]); if(1 == i) { continue; } if(access("test", F_OK) < 0) { perror("e0"); } else { printf("file ok\n"); } if(access("test", R_OK) < 0) { perror("e1"); } else { printf("read ok\n"); } if(access("test", W_OK) < 0) { perror("e2"); } else { printf("write ok\n"); } if(access("test", X_OK) < 0) { perror("e3"); } else { printf("exec ok\n"); } } return 0; }结果为:
[taoge@localhost learn_c]$ ls
test.c
[taoge@localhost learn_c]$ gcc test.c
[taoge@localhost learn_c]$ ./a.out
a.out test.c
e0: No such file or directory
e1: No such file or directory
e2: No such file or directory
e3: No such file or directory
file ok
e1: Permission denied
e2: Permission denied
e3: Permission denied
file ok
read ok
e2: Permission denied
e3: Permission denied
file ok
read ok
write ok
e3: Permission denied
file ok
read ok
write ok
exec ok
[taoge@localhost learn_c]$
我们再来看目录, 代码如下:
#include <stdio.h> #include <unistd.h> int main() { char szTest[][100] = { "ls", "mkdir test", // 此时, test是目录, 不再是"touch test"生成的文件 "chmod u-rwx test", "chmod u+r test", "chmod u+w test", "chmod u+x test", }; int i = 0; int n = sizeof(szTest) / sizeof(szTest[0]); for(i = 0; i < n; i++) { system(szTest[i]); if(1 == i) { continue; } if(access("test", F_OK) < 0) { perror("e0"); } else { printf("directory ok\n"); } if(access("test", R_OK) < 0) { perror("e1"); } else { printf("read ok\n"); } if(access("test", W_OK) < 0) { perror("e2"); } else { printf("write ok\n"); } if(access("test", X_OK) < 0) { perror("e3"); } else { printf("exec ok\n"); } } return 0; }结果为:
[taoge@localhost learn_c]$ ls
test.c
[taoge@localhost learn_c]$ gcc test.c
[taoge@localhost learn_c]$ ./a.out
a.out test.c
e0: No such file or directory
e1: No such file or directory
e2: No such file or directory
e3: No such file or directory
directory ok
e1: Permission denied
e2: Permission denied
e3: Permission denied
directory ok
read ok
e2: Permission denied
e3: Permission denied
directory ok
read ok
write ok
e3: Permission denied
directory ok
read ok
write ok
exec ok
[taoge@localhost learn_c]$
不消多说。
相关文章推荐
- Linux 文件权限与目录配置(有机会可以挑战FHS英文官方文档来加深你对目录了解)
- Linux只修改目录或只修改文件权限
- 如何设置UNIX/Linux中新创建目录或文件的默认权限
- 【学习笔记——Linux】Linux 文件权限与目录配置
- Linux下的文件与目录权限
- linux chmod和fchmod设置 文件和目录权限设置
- linux常用命令笔记(一):目录切换、查找文件、权限设置
- Linux 文件和目录权限
- U6Linux的文件权限与目录配置
- linux 文件/目录权限、用户、组相关操作
- Linux 入门常用命令 — 改变文件或目录的访问权限
- 18.1.2-linux目录文件隐藏属性和特殊权限
- linux下的文件及目录权限详解
- Linux中修改文件或目录的用户组和访问权限
- Linux的文件/目录访问权限
- 攻城狮在路上(叁)Linux(十一)--- 用户与用户组、文件权限、目录配置
- Linux文件目录权限、隐藏属性、特殊权限
- 修改LINUX目录或文件的权限和所有权详解
- Linux-6-文件权限和目录
- Linux运维初级教程(三)文件及目录权限