access函数 权限测试



#include <unistd.h>

int access(const char *pathname, int mode);

成功返回0

失败返回-1


R_OK(可读)、W_OK(可写)、X_OK(可执行)


具体示例如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
	if(argc < 2){
		printf("args error\n");
		exit(1);
	}

	int ret = access(argv[1], W_OK);
	if(ret==-1){
		perror("access");
		exit(1);
	}

	printf("你可以写这个文件\n");
	return 0;
	
}


测试执行结果:

yu@ubuntu:~/cplusplus/wjcz_文件操作$ ls -l
total 36
-rw-rw-r-- 1 yu yu  288 Dec 16 14:33 access.c
-rwxrwxr-x 1 yu yu 8760 Dec 16 14:33 a.out
-rw-rw-r-- 1 yu yu 1821 Dec 16 13:46 ls-l.c
-rwxrwxr-x 1 yu yu 9192 Dec 16 14:01 myls
-rw-rw-r-- 1 yu yu   65 Dec 16 11:06 stat.c
yu@ubuntu:~/cplusplus/wjcz_文件操作$ gcc access.c -o xaccess
yu@ubuntu:~/cplusplus/wjcz_文件操作$ ./xaccess access.c 
你可以写这个文件


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值