揮発性のメモ2

http://d.hatena.ne.jp/iww/

ファイル名一覧取得

// printf用
#include <stdio.h>
// opendir用
#include <sys/types.h>
#include <dirent.h>
// perror用
#include <stdlib.h>

int main( int argc, char **argv )
{
    DIR *dir;
    struct dirent *dp;
    char *d_type;
    
    dir = opendir( argv[1] );
    if( dir==NULL ){
        perror("opendir");
        exit(1);
    }

    while( (dp = readdir(dir)) != NULL ){
        switch(dp->d_type){
          case DT_BLK:  d_type="ブロックデバイス";     break;
          case DT_CHR:  d_type="キャラクターデバイス"; break;
          case DT_DIR:  d_type="ディレクトリ";         break;
          case DT_FIFO: d_type="名前付きパイプ";       break;
          case DT_LNK:  d_type="シンボリックリンク";   break;
          case DT_REG:  d_type="普通のファイル";       break;
          case DT_SOCK: d_type="UNIXドメインソケット"; break;
          default:      d_type="正体不明";
        }
        printf("%s\t%s\n",dp->d_name,d_type);
    }

    closedir(dir);
    
    return 0;
}

Man page of OPENDIR
Man page of READDIR