Unix学习笔记------实例练习---Day02_access函数文件访问权限测试
来源:互联网 发布:c语言初始化是什么意思 编辑:程序博客网 时间:2024/06/05 00:16
Day02_access函数文件访问权限测试
::::::::::
/*
access()函数,文件访问权限测试,
能测试这个进程是否有权限访问这个文件。
access函数原型:
::::::::::::
access - check real user鈥檚permissions for a file
SYNOPSIS
#include <unistd.h>
int access(const char *pathname, int mode);
DESCRIPTION
access() checks whether the calling process can access the filepathname. If pathname is a symboliclink, it is dereferenced.
The mode specifies the accessibility check(s) to beperformed, and is either the value F_OK, or a mask consisting of the bitwise ORof one or more of R_OK, W_OK, and
X_OK. F_OK tests for theexistence of the file. R_OK, W_OK, andX_OK test whether the file exists and grants read, write, and executepermissions, respectively.
The check is done using the calling process鈥檚 real UIDand GID, rather than the effective IDs as is done when actually attempting anoperation (e.g., open(2)) on the
file. This allows set-user-IDprograms to easily determine the invoking user鈥檚 authority.
If the calling process is privileged (i.e., its real UID is zero), then an X_OK checkis successful for a regular file if execute permission is enabled for any ofthe
file owner, group, or other.
RETURN VALUE
On success (all requested permissions granted), zero is returned. On error (at least one bit in mode askedfor a permission that is denied, or some other error
occurred), -1 is returned, and errno is set appropriately.
ERRORS
access() shall fail if:
EACCES The requested access wouldbe denied to the file, or search permission is denied for one of thedirectories in the path prefix of pathname. (See also path_reso-
lution(7).)
ELOOP Too many symbolic linkswere encountered in resolving pathname.
ENAMETOOLONG
pathname is too long.
ENOENT A component of pathname does not exist or is a dangling symboliclink.
ENOTDIR
A component used as a directoryin pathname is not, in fact, a directory.
EROFS Write permission wasrequested for a file on a read-only file system.
access() may fail if:
EFAULT pathname points outside your accessible address space.
EINVAL mode was incorrectly specified.
EIO An I/O error occurred.
ENOMEM Insufficient kernel memory was available
:::::::::::::::::::::
*/
测试实例源代码:
用该进程创建一个my.txt 文件,然后用access()函数测试。
程序源代码:
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<error.h>
#include<errno.h>
int main()
{
int access_returnvalue ;
access_returnvalue=access("/home/code/file1/my.txt",R_OK);
if(access_returnvalue!=-1)
printf("theaccessiblity of reading is not ok\n");
printf("theread accessibility is :%d\n",access_returnvalue);
access_returnvalue=access("/home/code/file1/my.txt",W_OK);
if(access_returnvalue!=-1)
printf("thewrite is allowed");
printf("THERETURN VALUE IS :%d \n",access_returnvalue);
return 0;
}
程序运行截图:
- Unix学习笔记------实例练习---Day02_access函数文件访问权限测试
- Unix学习笔记-------Unix编程实例练习
- 《unix环境编程》笔记-文件访问权限
- UNIX环境编程学习笔记(7)——文件I/O之文件访问权限与进程访问控制
- UNIX环境编程学习笔记(9)——文件I/O之文件访问权限的屏蔽和更改
- unix 学习笔记-----Unix编程练习实例----读目录的实例_1
- Unix学习笔记----编程实例-------Link函数的实例使用:
- Linux C编程学习笔记(1):chmod函数及文件的访问权限控制
- Unix学习笔记-----Unix编程练习实例-----lstat()&& stat 结构体详解
- [学习笔记]拥有权限的访问页面的实例
- UNIX文件和目录访问权限
- 《UNIX环境高级编程》笔记--文件访问权限和新文件、目录所有权
- 《UNIX环境高级编程》笔记9--文件访问权限和新文件、目录所有权
- Linux学习笔记02 文件权限管理练习
- UNIX环境编程学习笔记(8)——文件I/O之校验当前登录用户对文件的访问权限
- Androidx学习笔记(14)-- Android文件的访问权限
- Linux学习笔记4——文件访问权限
- APUE 笔记 文件访问权限
- 反调试技术揭秘
- oracle单行函数
- ActionBar/Toolbar左侧右侧空白处理
- 史上最全的常用iOS的第三方框架
- libvma状态机代码阅读
- Unix学习笔记------实例练习---Day02_access函数文件访问权限测试
- struts 通配符
- js函数细节
- 最近的事情(找工作+继续学习)
- LR录制脚本,录制完成后无法生成脚本
- ——黑马程序员——OC中Foundation框架NSString用法总结
- Android Preference 事件处理
- 使用Cocos2d-lua开发植物大战僵尸07-实现开始游戏按钮功能和主场景触摸监听
- Ajax传递二维数组到ThinkPHP控制器