首页 > 其他 > 详细

4412 chmod权限

时间:2018-08-01 15:46:00      阅读:209      评论:0      收藏:0      [点我收藏+]

chmod权限

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

 

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

 

#include <sys/stat.h>

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

int main(int argc, char *argv[])
{
        int fd, ret;

        if(argc < 3) {
                printf("\nPlease input file apth \n");
                return 1;
        }

        //chmod test
        ret = chmod(argv[1], 0777);
        if(ret < 0) {
                printf("Please makes sure file path\n");
                return 1;
        }
        printf("chmod %s is success.\n", argv[1]);

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

        return 0;
}

 

4412 chmod权限

原文:https://www.cnblogs.com/ch122633/p/9401525.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!