Linux系统编程之修改目录与文件权限的chmod函数组

15 篇文章 2 订阅
14 篇文章 0 订阅

linux中的文件的权限修改可通过chmod命令来修改,也可以通过编程来实现文件权限的修改,我们可以通过man 2 chmod查看这个函数是如何使用的。

如下是一个修改两个文件的权限的demo:

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

int main(int argc,char *argv[])
{
    int fd, ret;
    if(argc < 3)//判断参数个数
    {
        printf("\nplease input the file path!\n");
        return 1;
    }
    
    //chmod test
    ret = chmod(argv[1], 0777);
    if(ret)
    {
        printf("\nplease make sure the file path!\n");
        return 1;
    }

//fchmod test
    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, 0777);
    if(ret)
    {
        printf("\nplease make sure the file path!\n");
        return 1;
    }
    return 0;
}

我们把编写好的deno放入ubuntu中,然后使用编译命令gcc chmod.c -o chmod生成chmod可执行文件:

我们在同一个文件夹下建立两个文件test1与test2:

通过ls -l可以看到这两个文件的权限为rw-r--r--,也即是644,我们运行可执行文件: ./chmod test1 test2,卡一看到权限改变了,变成了777:

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值