Linux系统编程:简单实现ls -R 功能
生活随笔
收集整理的這篇文章主要介紹了
Linux系统编程:简单实现ls -R 功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
實現(xiàn)效果
這是系統(tǒng)提供的
這是自己實現(xiàn)的
實現(xiàn)思路
主要是目錄項的讀取和文件詳細(xì)信息的獲取以及文件類型的判斷。
打開目錄opendir->讀取目錄readdir->展示文件名->判斷是否是普通文件還是目錄文件是目錄文件保存目錄文件路徑->遞歸遍歷目錄文件。
這里主要用到函數(shù)原型是 opendir,closedir,readdir,lstat函數(shù)。還有就是文件類型的判斷,自己封裝簡單封裝了個文件類型的獲取的stat_info.c。這里保存目錄文件名個數(shù)是有限的,大家可以使用鏈表或者動態(tài)數(shù)組改進(jìn),簡單實現(xiàn)嘛,就不弄那么復(fù)雜了。
實現(xiàn)代碼
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) ;//路徑為 目錄,進(jìn)行遞歸遍歷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:缺少參數(shù)\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);#endifstat_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; //}總結(jié)
以上是生活随笔為你收集整理的Linux系统编程:简单实现ls -R 功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C/C++ 按行读取文件
- 下一篇: 使用redis数据库的目的?