chmod权限

1、使用命令"man 2 chmod"学习chmod函数

2、int chmod(const char *path, mode_t mode);
      –参数*path:文件路径。
      –参数mode:直接使用数字即可。和前面命令中chmod 777 xxx 中的777 这个参数含义类似,也可以使用文档中的         组合值。
      –返回值:成功返回0,错误返回-1。

3、int fchmod(int fd, mode_t mode);
      –参数fd:文件描述符。
      –参数mode:直接使用数字即可。和前面命令中chmod 777 xxx 中的777 这
      个参数含义类似,也可以使用文档中的组合值。
      –返回值:成功返回0,错误返回-1。

#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
	int fd,ret;
	
	if(argc <3){
		printf("\nPlease input file path\n");
		return 1;
	}
	
//chmod函数测试	
	ret = chmod(argv[1],0777);
	if(ret<0){
		printf("Please make sure file path\n");
		return 1;
	}
	printf("chmod %s 0777 is success!\n",argv[1]);

//fchmod函数测试		
	fd = open(argv[2],O_RDWR|O_NOCTTY|O_NDELAY);
	if(fd<0)
	{
		printf("Please make sure file path\n");
		return 1;
	}
	ret = fchmod(fd,0555);
	if(ret<0){
		printf("Please make sure file path\n");
		return 1;
	}
	printf("fchmod %s 0555 is success!\n",argv[1]);
	
	return 0;

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值