日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux系统编程:简单实现ls -R 功能

發布時間:2025/3/15 linux 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux系统编程:简单实现ls -R 功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現效果

這是系統提供的


這是自己實現的


實現思路

主要是目錄項的讀取文件詳細信息的獲取以及文件類型的判斷。

打開目錄opendir->讀取目錄readdir->展示文件名->判斷是否是普通文件還是目錄文件是目錄文件保存目錄文件路徑->遞歸遍歷目錄文件

這里主要用到函數原型是 opendir,closedir,readdir,lstat函數。還有就是文件類型的判斷,自己封裝簡單封裝了個文件類型的獲取的stat_info.c。這里保存目錄文件名個數是有限的,大家可以使用鏈表或者動態數組改進,簡單實現嘛,就不弄那么復雜了。

實現代碼

ls_R.c

#include <stdio.h> #include <string.h> #include <fcntl.h> #include <dirent.h> #include <error.h> #include <stdlib.h> #include <sys/stat.h> #include "stat_info.h"void ListDir(char* path) {DIR* root;int ret,i,j;char c;struct dirent* dir;struct stat info;char dirList[100][256];root = opendir(path);if(root == NULL){perror("opendir error");exit(1);}//展示根目錄printf("%s:\n",path);i = 0;while( (dir=readdir(root))!=NULL ){if(dir->d_name[0]=='.'){continue;}char temp[256];char last = path[strlen(path)-1];if(last == '/'){sprintf(temp,"%s%s",path,dir->d_name);}else{sprintf(temp,"%s/%s",path,dir->d_name);}//printf("%s\n",temp);ret = lstat(temp,&info);if(ret < 0){perror("lstat error");exit(0);}c = GetFileTypeCh(info.st_mode);//printf("name:%20s,type:%c,size:%ld\n",dir->d_name,c,info.st_size) ;printf("%s\t",dir->d_name) ;//路徑為 目錄,進行遞歸遍歷if(c == 'd'){strcpy(dirList[i++],temp); if( i > 100 ){i--;break;}}}closedir(root);printf("\n\n");//遞歸展示目錄下的目錄項for(j = 0; j < i; j++){ListDir(dirList[j]);}return; } int main(int argc,char* argv[]) {if(argc < 2){printf("error:缺少參數\n");exit(1);}ListDir(argv[1]);return 0; }

stat_info.h

#pragma once #ifndef __STATINFO_H__ #define __STATINFO_H__ #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <error.h> #include <sys/errno.h> #include <sys/stat.h> #include <sys/types.h> #include <stdlib.h> #include <string.h>char GetFileTypeCh(mode_t mode);char* GetFileTypeStr(mode_t mode);int GetFileTypeInt(mode_t mode);#endif

stat_info.c

#include "stat_info.h" char GetFileTypeCh(mode_t mode) {char c;switch (mode & S_IFMT) {case S_IFBLK: c = 'b'; break;case S_IFCHR: c = 'c'; break;case S_IFDIR: c = 'd'; break;case S_IFIFO: c = 'p'; break;case S_IFLNK: c = 'l'; break;case S_IFREG: c = '-'; break;case S_IFSOCK: c = 's'; break;default: c = 'u'; break;}return c; } char* GetFileTypeStr(mode_t mode) {char* fileType = (char*)malloc(sizeof(char)*100);bzero(fileType,100);switch (mode & S_IFMT) {case S_IFBLK: strcpy(fileType,"block device"); break;case S_IFCHR: strcpy(fileType,"character device"); break;case S_IFDIR: strcpy(fileType,"directory"); break;case S_IFIFO: strcpy(fileType,"FIFO/pipe"); break;case S_IFLNK: strcpy(fileType,"symlink"); break;case S_IFREG: strcpy(fileType,"regular file"); break;case S_IFSOCK: strcpy(fileType,"socket"); break;default: strcpy(fileType,"unknown?"); break;}return fileType; } int GetFileTypeInt(mode_t mode) {if(S_ISREG(mode)){return S_IFREG; }if(S_ISDIR(mode)){return S_IFDIR; }if(S_ISCHR(mode)){return S_IFCHR;}if(S_ISBLK(mode)){return S_IFBLK; }if(S_ISFIFO(mode)){return S_IFIFO; }if(S_ISLNK(mode)){return S_IFLNK; }if(S_ISSOCK(mode)){return S_IFSOCK; }return -1; } //int main(int argc,char* argv[]) //{ // struct stat file_info; // //使用lseek 查看文件大小 // int ret = stat(argv[1],&file_info); // if(ret < 0) // { // perror("stat error"); // exit(1); // } // printf("stat -> %s size :%ld,fileType:%s\n",argv[1],file_info.st_size,GetFileTypeStr(file_info.st_mode)); // return 0; //}

總結

以上是生活随笔為你收集整理的Linux系统编程:简单实现ls -R 功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。