1. 程式人生 > >[領卓教育]用C語言實現ls以及ls-功能

[領卓教育]用C語言實現ls以及ls-功能

各位程式設計師在自己的虛擬機器裡一定沒少執行過“ls”這個功能吧,這個程式碼就是實現了ls和ls-l功能,話不多說,上程式碼。

實現程式碼

int process_ls(char * path)
{
    DIR * dirp;
    struct dirent *direntp ;
    dirp = opendir(path);
    if(dirp == NULL)
    {
        perror("opendir"); 
        exit(-1);
    }
    while(  (direntp=readdir(dirp)) != NULL)
    {
        if(strncmp(direntp->d_name,".",1) == 0) continue;
        printf("%s  ",direntp->d_name);
    }
    printf("\n");
    closedir(dirp);

    return 0 ;
}

這部分程式碼就已經實現了ls的功能。

int process_ls_l(char *path)
{
    DIR * dirp;
    struct dirent *direntp ;
    char filename[100] ={0};
    struct stat st;
    int ret; 
    char buf[100] ={0};
    char tmp[100] ={0};
    dirp = opendir(path);
    if(dirp == NULL)
    {
        perror("opendir"); 
        exit(-1);
    }
    while(  (direntp=readdir(dirp)) != NULL)
    {
        if(strncmp(direntp->d_name,".",1) == 0) continue;
        strcpy(filename,path);
        if(filename[strlen(filename) -1] !='/')
        {
            strcat(filename,"/");
        }
        strcat(filename,direntp->d_name);
       // printf("filename:%s\n",filename);
        ret = stat(filename,&st);
        if(ret < 0)
        {
            perror("stat"); 
            exit(-1);
        }
        memset(buf,'-',10);
        if(S_ISREG(st.st_mode))
        {
            buf[0] = '-';
        }
        else if(S_ISDIR(st.st_mode))
        {
            buf[0] = 'd';
        }
        else if(S_ISCHR(st.st_mode))
        {
            buf[0] = 'c';
        }
        else if(S_ISBLK(st.st_mode))
        {
            buf[0] = 'b';
        }
        else if(S_ISFIFO(st.st_mode))
        {
            buf[0] = 'p';
        }
        else if(S_ISLNK(st.st_mode))
        {
            buf[0] = 'l';
        }
        else if(S_ISSOCK(st.st_mode))
        {
            buf[0] = 's';
        }
        else 
        {
            printf("error\n");
            exit(-1);
        }
        if(st.st_mode&S_IRUSR)
        {
            buf[1] = 'r';
        }
        if(st.st_mode&S_IWUSR)
        {
            buf[2] = 'w';
        }
        if(st.st_mode&S_IXUSR)
        {
            buf[3] = 'x';
        }
/************************************************/
        if(st.st_mode&S_IRGRP)
        {
            buf[4] = 'r';
        }
        if(st.st_mode&S_IWGRP)
        {
            buf[5] = 'w';
        }
        if(st.st_mode&S_IXGRP)
        {
            buf[6] = 'x';
        }
/***********************************************/
        if(st.st_mode&S_IROTH)
        {
            buf[7] = 'r';
        }
        if(st.st_mode&S_IWOTH)
        {
            buf[8] = 'w';
        }
        if(st.st_mode&S_IXOTH)
        {
            buf[9] = 'x';
        }
        buf[10] = ' ';
        sprintf(&buf[11],"%ld",st.st_nlink);
        strcat(buf," ");
        struct passwd *pw;
        pw = getpwuid(st.st_uid);
        sprintf(tmp,"%s",pw->pw_name);
        strcat(buf,tmp);
        strcat(buf," ");
        struct group *gr;
        gr = getgrgid(st.st_gid);
        sprintf(tmp,"%s",gr->gr_name);
        strcat(buf,tmp);
        strcat(buf," ");
        sprintf(tmp,"%5ld",st.st_size);
        strcat(buf,tmp);
        strcat(buf," ");
        sprintf(tmp,"%s",ctime(&st.st_mtime));
        tmp[strlen(tmp) -1 ] = '\0' ;
        strcat(buf,tmp);
        strcat(buf," ");
        strcat(buf,direntp->d_name);
        printf("%s\n",buf);
    }
    closedir(dirp);
    return 0 ;
}

這段程式碼就是實現了ls-l的功能了。

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

   if(argc == 2 )
   {
       process_ls(argv[1]); // myls path
   }
   else if(argc == 3) // ./myls -l path
   {
       process_ls_l(argv[2]);
   }
   else 
   {
       printf("error\n");
       exit(-1);
   }
   return 0;
}

再加上主函式的判斷,在執行檔案後加上想要ls的那個檔案路徑,就可以想實現哪個就實現哪個了。

效果預覽

這就是ls的實現效果。 這就是ls-l的實現效果。

到這裡就結束了,祝大家天天開心,心想事成(不脫髮)!嘿嘿!