home *** CD-ROM | disk | FTP | other *** search
- #include <libraries/dos.h>
- #include <libraries/dosextens.h>
- #include <proto/dos.h>
- #include <exec/memory.h>
- #include <string.h>
-
- #define MAX_NAME_LEN 30
-
- struct FILETREE
- {
- UBYTE name[MAX_NAME_LEN];
- struct FILETREE *parent;
- struct FILETREE *next_sib;
- struct FILETREE *prev_sib;
- struct FILETREE *child;
- };
-
- int memused=0;
- char target[100];
- int ppath[20];
-
- struct FILETREE *GetFileTree(BPTR,struct FILETREE *);
- void free_filetree(struct FILETREE *);
- int print_ftree(struct FILETREE *,int);
-
-
- void CalcPath(dir_path,ftree)
- char dir_path[];
- struct FILETREE *ftree;
- {
- int length;
-
- if(ftree->parent != NULL)
- {
- CalcPath(dir_path,ftree->parent);
- strcat(dir_path,"/");
- strcat(dir_path,ftree->name);
- }
- else
- {
- length=strlen(target);
- if(length>0)
- {
- strcpy(dir_path,target);
- if((target[length-1] != ':')&&(target[length-1]!='/'))
- strcat(dir_path,"/");
- strcat(dir_path,ftree->name);
- }
- else
- strcpy(dir_path,ftree->name);
- }
- }
-
- struct FILETREE *GetFileTree(lock,par)
- BPTR lock;
- struct FILETREE *par;
- {
- struct FILETREE *ftree;
- struct FileInfoBlock *FileIB;
- BPTR lock2;
- char dir_path[200];
-
- FileIB = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_FAST);
- if (Examine(lock,FileIB) == FALSE)
- {
- return(NULL);
- }
-
- ftree = (struct FILETREE *)malloc(sizeof(struct FILETREE));
-
- strcpy(ftree->name, FileIB->fib_FileName);
- ftree->parent = par;
- ftree->prev_sib = NULL;
- ftree->next_sib = NULL;
- ftree->child = NULL;
-
- while(ExNext(lock, FileIB))
- {
- if(FileIB->fib_DirEntryType>0)
- {
- ftree->next_sib = (struct FILETREE *)malloc(sizeof(struct FILETREE));
-
- ftree->next_sib->prev_sib = ftree;
- ftree = ftree->next_sib;
-
- strcpy(ftree->name, FileIB->fib_FileName);
- ftree->parent = par;
- ftree->next_sib=NULL;
-
- CalcPath(dir_path,ftree);
- lock2 = Lock(dir_path, ACCESS_READ);
- ftree->child = GetFileTree(lock2,ftree);
-
- UnLock(lock2);
- }
- }
-
- while(ftree->prev_sib != NULL)
- ftree = ftree->prev_sib;
- FreeMem(FileIB,sizeof(struct FileInfoBlock));
- return(ftree);
- }
-
-
-
- void free_filetree(struct FILETREE *ftree)
- {
- if (ftree->child != NULL)
- {
- free_filetree(ftree->child);
- }
- if (ftree->next_sib != NULL) free_filetree(ftree->next_sib);
- if (ftree != NULL)
- free(ftree);
- return;
- }
-
-
- int print_ftree(ftree,depth)
- struct FILETREE *ftree;
- int depth;
- {
- int loop;
- int blank;
-
- blank=0;
- ftree=ftree->next_sib;
- while(ftree != NULL)
- {
- if(ftree->next_sib == NULL)
- ppath[depth]=0;
- else
- ppath[depth]=1;
- for(loop=0;loop<depth;loop++)
- if(ppath[loop]==0)
- printf(" ");
- else
- printf("| ");
- printf("+--%s\n",ftree->name);
- blank=print_ftree(ftree->child,depth+1);
- if((ftree->next_sib==NULL)&&(blank==0))
- {
- blank=1;
- for(loop=0;loop<depth;loop++)
- if(ppath[loop]==0)
- printf(" ");
- else
- printf("| ");
- printf("\n");
- }
- ftree=ftree->next_sib;
- }
- return(blank);
- }
-
-
-
- int main(int argc,char *argv[],char *env)
- {
- struct FILETREE *ftree = NULL;
- BPTR lock;
- int doflag;
-
- doflag=1;
- if(argc>0)
- if(*argv[1]=='?')
- {
- printf("Displays directory tree structure of current or selected directory\n");
- printf("\n tree <directory name> lists tree of <directory name>\n");
- printf(" tree lists tree of current directory\n");
- printf(" tree : lists tree of current device\n");
- printf(" tree / lists tree of previous directory\n");
- doflag=0;
- }
- else
- strcpy(target,argv[1]);
- else
- strcpy(target,"");
-
- if (doflag=1)
- {
- lock = Lock(target, ACCESS_READ);
- if(lock == 0)
- printf("Error - Can't lock selected directory\n");
- else
- {
- ftree = GetFileTree(lock,NULL);
- if (ftree->next_sib==NULL)
- printf("Directory %s has no subdirectories.\n");
- else
- {
- if (ftree==NULL)
- printf("Can't Examine current directory\n");
- else
- {
- printf("Directory tree of %s\n",ftree->name);
- print_ftree(ftree,0);
- free_filetree(ftree);
- }
- }
- UnLock(lock);
- }
- }
- }
-