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("\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;
}

 

posted @ 2018-08-01 15:30  习惯就好233  阅读(153)  评论(0编辑  收藏  举报