home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-11  |  4.7 KB  |  205 lines

  1. #include <libraries/dos.h>
  2. #include <libraries/dosextens.h>
  3. #include <proto/dos.h>
  4. #include <exec/memory.h>
  5. #include <string.h>
  6.  
  7. #define MAX_NAME_LEN 30
  8.  
  9. struct FILETREE
  10. {
  11.     UBYTE name[MAX_NAME_LEN];
  12.     struct FILETREE *parent;
  13.     struct FILETREE *next_sib;
  14.     struct FILETREE *prev_sib;
  15.     struct FILETREE *child;
  16. };
  17.  
  18. int memused=0;
  19. char target[100];
  20. int ppath[20];
  21.  
  22. struct FILETREE *GetFileTree(BPTR,struct FILETREE *);
  23. void free_filetree(struct FILETREE *);
  24. int print_ftree(struct FILETREE *,int);
  25.  
  26.  
  27. void CalcPath(dir_path,ftree)
  28. char dir_path[];
  29. struct FILETREE *ftree;
  30. {
  31. int length;
  32.  
  33.     if(ftree->parent != NULL)
  34.     {
  35.         CalcPath(dir_path,ftree->parent);
  36.         strcat(dir_path,"/");
  37.         strcat(dir_path,ftree->name);
  38.     }
  39.     else
  40.     {
  41.         length=strlen(target);
  42.         if(length>0)
  43.         {
  44.             strcpy(dir_path,target);
  45.             if((target[length-1] != ':')&&(target[length-1]!='/'))
  46.                 strcat(dir_path,"/");
  47.             strcat(dir_path,ftree->name);
  48.         }
  49.         else
  50.             strcpy(dir_path,ftree->name);
  51.     }
  52. }
  53.  
  54. struct FILETREE *GetFileTree(lock,par)
  55. BPTR lock;
  56. struct FILETREE *par;
  57. {
  58. struct FILETREE *ftree;
  59. struct FileInfoBlock *FileIB;
  60. BPTR lock2;
  61. char dir_path[200];
  62.  
  63.     FileIB = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_FAST);
  64.     if (Examine(lock,FileIB) == FALSE)
  65.     {
  66.         return(NULL);
  67.     }
  68.  
  69.     ftree = (struct FILETREE *)malloc(sizeof(struct FILETREE));
  70.  
  71.     strcpy(ftree->name, FileIB->fib_FileName);
  72.     ftree->parent = par;
  73.     ftree->prev_sib = NULL;
  74.     ftree->next_sib = NULL;
  75.     ftree->child = NULL;
  76.  
  77.     while(ExNext(lock, FileIB))
  78.     {
  79.         if(FileIB->fib_DirEntryType>0)
  80.         {
  81.             ftree->next_sib = (struct FILETREE *)malloc(sizeof(struct FILETREE));
  82.  
  83.             ftree->next_sib->prev_sib = ftree;
  84.             ftree = ftree->next_sib;
  85.  
  86.             strcpy(ftree->name, FileIB->fib_FileName);
  87.             ftree->parent = par;
  88.             ftree->next_sib=NULL;
  89.  
  90.             CalcPath(dir_path,ftree);
  91.             lock2 = Lock(dir_path, ACCESS_READ);
  92.             ftree->child = GetFileTree(lock2,ftree);
  93.  
  94.             UnLock(lock2);
  95.         }
  96.     }
  97.  
  98.     while(ftree->prev_sib != NULL)
  99.         ftree = ftree->prev_sib;
  100.     FreeMem(FileIB,sizeof(struct FileInfoBlock));
  101.     return(ftree);
  102. }
  103.  
  104.  
  105.  
  106. void free_filetree(struct FILETREE *ftree)
  107. {
  108.     if (ftree->child != NULL)
  109.     {
  110.         free_filetree(ftree->child);
  111.     }
  112.     if (ftree->next_sib != NULL) free_filetree(ftree->next_sib);
  113.     if (ftree != NULL)
  114.         free(ftree);
  115.     return;
  116. }
  117.  
  118.  
  119. int print_ftree(ftree,depth)
  120. struct FILETREE *ftree;
  121. int depth;
  122. {
  123. int loop;
  124. int blank;
  125.  
  126.     blank=0;
  127.     ftree=ftree->next_sib;
  128.     while(ftree != NULL)
  129.     {
  130.         if(ftree->next_sib == NULL)
  131.             ppath[depth]=0;
  132.         else
  133.             ppath[depth]=1;
  134.         for(loop=0;loop<depth;loop++)
  135.             if(ppath[loop]==0)
  136.                 printf("   ");
  137.             else
  138.                 printf("|  ");
  139.         printf("+--%s\n",ftree->name);
  140.         blank=print_ftree(ftree->child,depth+1);
  141.         if((ftree->next_sib==NULL)&&(blank==0))
  142.         {
  143.             blank=1;
  144.             for(loop=0;loop<depth;loop++)
  145.                 if(ppath[loop]==0)
  146.                     printf("   ");
  147.                 else
  148.                     printf("|  ");
  149.             printf("\n");
  150.         }
  151.         ftree=ftree->next_sib;
  152.     }
  153.     return(blank);
  154. }
  155.  
  156.  
  157.  
  158. int main(int argc,char *argv[],char *env)
  159. {
  160. struct FILETREE *ftree = NULL;
  161. BPTR lock;
  162. int  doflag;
  163.  
  164.     doflag=1;
  165.     if(argc>0)
  166.         if(*argv[1]=='?')
  167.         {
  168.             printf("Displays directory tree structure of current or selected directory\n");
  169.             printf("\n   tree <directory name>  lists tree of <directory name>\n");
  170.             printf("   tree                   lists tree of current directory\n");
  171.             printf("   tree :                 lists tree of current device\n");
  172.             printf("   tree /                 lists tree of previous directory\n");
  173.             doflag=0;
  174.         }
  175.         else
  176.             strcpy(target,argv[1]);
  177.     else
  178.         strcpy(target,"");
  179.  
  180.     if (doflag=1)
  181.     {
  182.         lock = Lock(target, ACCESS_READ);
  183.         if(lock == 0)
  184.             printf("Error - Can't lock selected directory\n");
  185.         else
  186.         {
  187.             ftree = GetFileTree(lock,NULL);
  188.             if (ftree->next_sib==NULL)
  189.                 printf("Directory %s has no subdirectories.\n");
  190.             else
  191.             {
  192.                 if (ftree==NULL)
  193.                     printf("Can't Examine current directory\n");
  194.                 else
  195.                 {
  196.                     printf("Directory tree of %s\n",ftree->name);
  197.                     print_ftree(ftree,0);
  198.                     free_filetree(ftree);
  199.                 }
  200.             }
  201.             UnLock(lock);
  202.         }
  203.     }
  204. }
  205.