第三章_文件和目录 : 函数chmod、fchmod和fchmodat

函数chmod、fchmod和fchmodat

#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
int fchmodat(int fd, const char *pathname, mode_t mode, int flag);

chmod函数
用于改变现有文件的存取许可权
函数原型:
int chmod(const char* pathname, mode_t mode);
参数和返回值
第一个参数pathname:要改变权限的文件
第二个参数mode:新的存取权限位组合
返回值:成功则为0,出错则为-1

在这里插入图片描述
fchmod函数
用于改变现有文件的存取许可权
函数原型:
int fchmod(int filedes, mode_t mode);
参数和返回值
第一个参数filedes:要改变权限的文件描述符
第二个参数mode:新的存取权限位组合
返回值:成功则为0,出错则为-1

测试程序

#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
        chmod("demo.txt",S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
        exit(EXIT_SUCCESS);
}

进程的有效用户ID等于文件的所有者,或者进程具有超级用户权限,才能改变文件的许可权位。

#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
        chmod("demo.txt",S_IRUSR|S_IWGRP|S_IXOTH);
        exit(EXIT_SUCCESS);
}

在这里插入图片描述
chmod在下列条件下自动清除两个许可权位

如果试图设置普通文件的粘住位(S_ISVTX),而且又没有超级用户权限,则mode中的粘住位自动被关闭。这意味着只有超级用户才能设置普通文件的粘住位。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值