4412 chmod权限

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("
Please input file apth 
");
                return 1;
        }

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

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

        return 0;
}
无欲速,无见小利。欲速,则不达;见小利,则大事不成。
原文地址:https://www.cnblogs.com/ch122633/p/9401525.html