相当前に作ったもの。タイムスタンプ確認したら2004年でした。
Windows7と以下のコンパイル環境でもエラー起きずに、コンパイルできたので今でも使えます。
プログラム勉強の参考にどうぞ。
gcc version 3.4.5 (mingw-vista special r3)
ファイル
root@root-PC ~/plog/viewdir9/viewdir_20120430
$ ls -1
list.c
list.h
test.c
viewdir.c
viewdir.h
コンパイル
root@root-PC ~/plog/viewdir9/viewdir_20120430
$ gcc test.c list.c viewdir.c
実行
root@root-PC ~/plog/viewdir9/viewdir_20120430
$ a.exe
>list.init
[6][0]: ./a.exe
[5][0]: ./list.c
[4][0]: ./list.h
[3][0]: ./test.c
[2][0]: ./test3.exe
[1][0]: ./viewdir.c
[0][0]: ./viewdir.h
-----
ソース
----test.c---------------------------------------------
//#include <stdio.h>
#include "viewdir.h"
//extern void viewdir(const char* path);
int main(int argc, char* argv[]){
char path[512];
if(argc<=1){
strcpy(path, ".");
}else{
strcpy(path, argv[1]);
}
//while(1){
viewdir_init();
viewdir_main(path);
//}
return 0;
}
----list.c---------------------------------------------
//list.c
//FIFO
#include "list.h"
LIST_FIFO *list_fifo_top =NULL;
LIST_FIFO *list_fifo_bottom=NULL;
int list_fifo_counter=0;
int list_getcunter(){
return list_fifo_counter;
}
int list_get(LIST_FIFO *q){
LIST_FIFO *p;
if(list_fifo_top == NULL)return -1;
strcpy(q->name, (const char*)list_fifo_top->name);
q->val1 = (int)list_fifo_top->val1;
p = (LIST_FIFO *)list_fifo_top;
list_fifo_top = (LIST_FIFO *)list_fifo_top->next;
if(list_fifo_top == NULL)list_fifo_bottom = NULL;
free(p); p=0;
list_fifo_counter--;
return 0;
}
LIST_FIFO *list_set(const char *name, int isdir){
LIST_FIFO *p=NULL;
p = (LIST_FIFO*)malloc(sizeof(LIST_FIFO));
if(p == 0)return NULL;
strcpy(p->name, name);
p->val1 = isdir;
p->next = NULL;
if(list_fifo_bottom == NULL){ //first
list_fifo_top = p;
list_fifo_bottom = p;
/*
strcpy(list_fifo_top->name,"-");
list_fifo_top->val1=0;
list_fifo_top->next=0;
strcpy(list_fifo_bottom->name,"-");
list_fifo_bottom->val1=0;
list_fifo_bottom->next=0;
*/
}else{
list_fifo_bottom->next = (struct _LIST_FIFO *) p;
list_fifo_bottom = (LIST_FIFO *)p;
}
list_fifo_counter++;
return (LIST_FIFO*)p;
}
void list_init(){
LIST_FIFO *p=NULL, *q=NULL;
printf(">list.init\n");
if(list_fifo_top != NULL){
for(p = list_fifo_top; p !=NULL; ){
//while()
q = p;
p = (LIST_FIFO *)p->next;
free(q);
q=0;
}
}
list_fifo_top = NULL;
list_fifo_bottom = NULL;
list_fifo_counter= 0;
//LIST_FIFO_INIT(&list_fifo_top);
//LIST_FIFO_INIT(&list_fifo_bottom);
return;
}
----list.h---------------------------------------------
//list.h
#ifndef LIST_H
#define LIST_H
#include <stdio.h>
typedef struct _LIST_FIFO{
char name[256]; //path name
int val1; //
struct _LIST_FIFO *next;
}LIST_FIFO;
//#define LIST_FIFO_INIT(v) (v)->next=0
LIST_FIFO *list_set(const char *name, int isdir);
//LIST_FIFO *list_get();
int list_get(LIST_FIFO *q);
int list_getcunter();
#endif
----viewdir.c---------------------------------------------
//viewdir.c
#include "viewdir.h"
int flag=1;
typedef struct _STR{
char buf[512];
}STR;
void viewdir_main(const char* pathn){
DIR *dir;
struct dirent *dp;
char path_name[512];
char path[512];
struct stat64 st;
int isdir =0; //0:file 1:dir
LIST_FIFO list;
int len=strlen(pathn);
int i=0;
STR *box, *b=NULL;
int cnt=0;
if((dir = opendir(pathn)) == NULL){
perror("opendir");
exit(-1);
}
strcpy(path_name, pathn);
if(flag){
if(path_name[len-1] == '/')path_name[len-1] = '\0';
flag=0;
}
for(dp = readdir(dir); dp !=NULL; dp=readdir(dir)){
sprintf((char*)path,"%s/%s",path_name, dp->d_name);
if (!strcmp("..",dp->d_name) || !strcmp(".",dp->d_name)) continue;
if (stat64((char*)path, &st) < 0)st.st_mode = 0;
isdir = 0;
isdir = ((st.st_mode & S_IFMT) == S_IFDIR);
list_set(path, isdir);
}
closedir(dir);
cnt = list_getcunter();
box = (STR*)malloc( sizeof(STR)*cnt );//box=NULL;
//---V---
cnt=0;
while(list_get(&list) != -1){
printf("[%d][%d]: %s\n", list_getcunter(), list.val1, list.name);
if(list.val1==1){
strcpy(box[cnt].buf,list.name);
printf("-[%d]%s\n", cnt, box[cnt].buf);
cnt++;
}
}
//---A---
//---V---
for(i=0; cnt>i; i++){
printf("box[%d]=%s\n", i, box[i].buf);
if(box[i].buf!=NULL)
viewdir_main((const char*)box[i].buf);
}
free(box); box=0;
//---A---
}
void viewdir_init(){
list_init();
}
----viewdir.h---------------------------------------------
//viewdir.h
#ifndef VIEWDIR_H
#define VIEWDIR_H
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include "list.h"
#define stat64 stat
void viewdir_int();
void viewdir_main(const char* path);
#endif
PR