自己動手寫shell之chgrp,chown,chmod

來源:互聯網
上載者:User

標籤:unix環境進階編程   shell   linux   

1.chgrp實現

#include <grp.h>#include <unistd.h>void chgrp(char * groupname,char * filename){    struct group * groupinfo = NULL;    if((groupinfo = getgrnam(groupname)) == NULL)    {        printf("groupname does not exist\n");        return;    }    if(access(filename,F_OK) != 0)    {        printf("file %s does not exist\n",filename);        return;    }    int gid = groupinfo->gr_gid;    chown(filename,-1,gid);    printf("the group id of the file has been changed successfully\n");}

2.chown實現

void chowner(char * ownername,char * filename){    struct passwd *password_info = NULL;    if((password_info = getpwnam(ownername)) == NULL)    {        printf("username does not exist\n");        return;    }    if(access(filename,F_OK) != 0)    {        printf("file %s does not exist\n",filename);        return;    }    int uid = password_info->pw_uid;    chown(filename,uid,-1);    printf("the user id of the file has been changed successfully\n");}

3.chmod實現

void _chmod(char * mod,char * filename){    if(access(filename,F_OK) != 0)        {            printf("the file %s does not exist",filename);            return;        }    int len = strlen(mod);    switch(len)        {            case 1:            {                int a;                a = mod[0] - '0';                mode_t mode = 0;                if(a < 0 || a > 7)                    {                        printf("octal number out of range\n");                        return ;                    }                if(a & 0x04)                    mode = mode | S_IROTH;                if(a & 0x02)                    mode = mode | S_IWOTH;                if(a & 0x01)                    mode = mode | S_IXOTH;                chmod(filename,mode);                printf("the mode has been changed successfully\n");                break;            }            case 2:            {                int a,b;                mode_t mode = 0;                a = mod[0] - '0';                b = mod[1] - '0';                if(a < 0 || a > 7 || b < 0 || b > 7)                    {                        printf("octal number out of range\n");                        return ;                    }                if(b & 0x04)                    mode = mode | S_IROTH;                if(b & 0x02)                    mode = mode | S_IWOTH;                if(b & 0x01)                    mode = mode | S_IXOTH;                if(a & 0x04)                    mode = mode | S_IRGRP;                if(a & 0x02)                    mode = mode | S_IWGRP;                if(a & 0x01)                    mode = mode | S_IXGRP;                chmod(filename,mode);                printf("the mode has been changed successfully\n");                break;            }            case 3:            {                int a,b,c;                mode_t mode = 0;                a = mod[0] - '0';                b = mod[1] - '0';                c = mod[2] - '0';                if(a < 0 || a > 7 || b < 0 || b > 7 || c < 0 || c > 7)                    {                        printf("octal number out of range\n");                        return ;                    }                if(a & 0x04)                    mode = mode | S_IRUSR;                if(a & 0x02)                    mode = mode | S_IWUSR;                if(a & 0x01)                    mode = mode | S_IXUSR;                if(b & 0x04)                    mode = mode | S_IRGRP;                if(b & 0x02)                    mode = mode | S_IWGRP;                if(b & 0x01)                    mode = mode | S_IXGRP;                if(c & 0x04)                    mode = mode | S_IROTH;                if(c & 0x02)                    mode = mode | S_IWOTH;                if(c & 0x01)                    mode = mode | S_IXOTH;                chmod(filename,mode);                printf("the mode has been changed successfully\n");                break;            }            default:            {                int a,b,c,d;                mode_t mode = 0;                a = mod[len-4] - '0';                b = mod[len-3] - '0';                c = mod[len-2] - '0';                d = mod[len-1] - '0';                if(a != 0 || b < 0 || b > 7 || c < 0 || c > 7 || d < 0 || d >7)                    {                        printf("octal number out of range\n");                        return ;                    }                if(b & 0x04)                    mode = mode | S_IRUSR;                if(b & 0x02)                    mode = mode | S_IWUSR;                if(b & 0x01)                    mode = mode | S_IXUSR;                if(c & 0x04)                    mode = mode | S_IRGRP;                if(c & 0x02)                    mode = mode | S_IWGRP;                if(c & 0x01)                    mode = mode | S_IXGRP;                if(d & 0x04)                    mode = mode | S_IROTH;                if(d & 0x02)                    mode = mode | S_IWOTH;                if(d & 0x01)                    mode = mode | S_IXOTH;                chmod(filename,mode);                printf("the mode has been changed successfully\n");                break;            }        }}


自己動手寫shell之chgrp,chown,chmod

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.