chmod

my_chmod.c

#include 
#include 
#include 

int main(int argc, char ** argv)
{
    int mode; 
    int mode_u;
    int mode_g;
    int mode_o;
    char* path;

    if(argc < 3) {
        printf("%s  \n", argv[0]);
        exit(0);
    }

    // 将字符串转换成整型
    mode = atoi(argv[1]);
    printf("%d\n", mode);

    if(mode > 777 || mode < 0) {
        printf("mode num error!\n");
        exit(0);
    }

        // 传进来的数当作八进制数,转换成十进制
    mode_u = mode / 100;
    mode_g = (mode - (mode_u*100)) / 10;
    mode_o = mode - (mode_u*100) - (mode_g*10);
    mode = (mode_u * 8 * 8) + (mode_g * 8) + mode_o;


    printf("The result mode we get-->%d\n", mode);

    path = argv[2];

    if(chmod(path, mode) == 1) {
        perror("chmod error");
        exit(1);
    } else {
        puts("Modify Success!");
    }

    return 0;
}

测试 查看结果

./my_chmod 776 my_chmod.c
ll
./my_chmod 777 my_chomd.c
ll

你可能感兴趣的:(chmod)