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;
}