自己動手寫shell命令之ls -R1fF

來源:互聯網
上載者:User

標籤:span   info   name   rcp   動手   實現   abs   效果   []   

    ls命令的R參數代表遞迴的列出全部子目錄中的全部檔案,1表示每一行僅僅顯示一個檔案或目錄,f表示不排序即輸出。F表示在每項的輸出的最後依據其檔案類型對應的加上*/=>@|字元。通過c語言實現ls -R1fF命令的效果,其源碼例如以下:

#include <stdio.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>#include <pwd.h>#include <grp.h>#include <string.h>void listdir(char *);char get_type(struct stat *);int main(int argc,char * argv[]){if(argc == 1)listdir(".");else{int index = 1;while(argc > 1){listdir(argv[index]);index++;argc--;}}return 0;}void listdir(char * dirname){DIR * dir;struct stat info;//char pointer[];struct dirent * direntp;if((dir = opendir(dirname)) != NULL){printf("%s:\n",dirname);while((direntp = readdir(dir)) != NULL){char absolute_pathname[255];strcpy(absolute_pathname,dirname);strcat(absolute_pathname,"/");strcat(absolute_pathname,direntp->d_name);lstat(absolute_pathname,&info);printf("%s",direntp->d_name);printf("%c\n",get_type(&info));}printf("\n");rewinddir(dir);while((direntp = readdir(dir)) != NULL){if(strcmp(direntp->d_name,".") == 0 || strcmp(direntp->d_name,"..") == 0)continue;char absolute_pathname[255];strcpy(absolute_pathname,dirname);strcat(absolute_pathname,"/");strcat(absolute_pathname,direntp->d_name);lstat(absolute_pathname,&info);if(S_ISDIR((&info)->st_mode))listdir(absolute_pathname);}}}char get_type(struct stat * info){if(S_ISCHR(info->st_mode))return ‘*‘;if(S_ISDIR(info->st_mode))return ‘/‘;if(S_ISSOCK(info->st_mode))return ‘=‘;if(S_ISBLK(info->st_mode))return ‘>‘;if(S_ISLNK(info->st_mode))return ‘@‘;if(S_ISFIFO(info->st_mode))return ‘|‘;return ‘ ‘;}



自己動手寫shell命令之ls -R1fF

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.