全ファイル表示

全ファイル表示例題list_25.c)
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>

enum EXE_OPT {
    LS_ALL    = 0, /* ファイルの全表示 */
    FIND_FILE = 1, /* ファイルの検索   */
};
int fschcnt;
int exeopt;
char ffile[256];
char flist[128][256];
/////////////////////////////////////////////////////////////////
// 階層の深さをスペースで表す
/////////////////////////////////////////////////////////////////
// void print_depth(int depth)
//   引数:int depth: 階層の深さ
// 返り値:無
/////////////////////////////////////////////////////////////////
void print_depth(int depth)
{
    int i;
    for(i=0;i<depth;i++) printf("   ");
}
/////////////////////////////////////////////////////////////////
// 全ファイルの表示 or ファイル名の検索
/////////////////////////////////////////////////////////////////
// void Find_file(int depth,char* basename,char* dname)
//   引数:
//        int depth: 階層の深さ
//        char* basename: 元の階層からのフォルダ名
//        char* dname: フォルダ名
// 返り値:無
/////////////////////////////////////////////////////////////////
void Find_file(int depth,char* basename,char* dname)
{
    char newdir[256];
    DIR *dir;
    struct dirent *dp;
    struct stat   buf;
    
    chdir(dname);
    if((dir=opendir("."))==NULL) return;
    while(dp = readdir(dir)){
        if(stat(dp->d_name, &buf) < 0)
            return;
        if(strcmp(dp->d_name,".")!=0 && strcmp(dp->d_name,"..")!=0){
            switch(buf.st_mode & S_IFMT){
            case S_IFDIR:
                if(exeopt == LS_ALL){
                    print_depth(depth+1);
                    printf("--> %s/\n",dp->d_name);
                }
                sprintf(newdir,"%s\\%s",basename,dp->d_name);
                Find_file(depth+1,newdir,dp->d_name);
                chdir("..");
                break;
            case S_IFREG:
                if(strcmp(dp->d_name,ffile) == 0){
                    sprintf(flist[fschcnt],basename);
                    fschcnt++;
                }
                if(exeopt == LS_ALL){
                    print_depth(depth+1);
                    printf("[f] %s\n",dp->d_name);
                }
                break;
            default:
                print_depth(depth+1);
                printf("\t[x] %s\n",dp->d_name);
                break;
            }
        }
    }
    closedir(dir);
}

int main(int argc,char* argv[])
{
    int i;
    fschcnt = 0;

    if(argc <= 1){/* 全ファイル表示 */
        exeopt = LS_ALL;
    } else {      /* ファイル検索   */
        exeopt = FIND_FILE;
        sprintf(ffile,"%s",argv[1]);
    }
    
    Find_file(0,".",".");
    
    /* file name display */
    if(exeopt == FIND_FILE){
        printf("\nfile [%s]:",ffile);
        printf(" %d file found\n\n",fschcnt);
        for(i=0;i<fschcnt;i++){
            printf("  %s\\\n",flist[i]);
        }
        printf("\n");
    }
    return 0;
}
実行結果
Gami[1351]% ls
dir1/  file1/  list_25.c  list_25.exe*  list_25.o  makefile  temp/
Gami[1352]% list_25
   --> dir1/
      --> directory/
         [f] file1.txt
      [f] file1.txt
      [f] temp.c
      [f] temp.cpp
      [f] temp.h
      [f] temp.txt
   --> file1/
   [f] list_25.c
   [f] list_25.exe
   [f] list_25.o
   [f] makefile
   --> temp/
Gami[1353]%
inserted by FC2 system