home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / forum5.lzh / SPRACHEN / C / TREE / tree.c < prev    next >
C/C++ Source or Header  |  1988-04-06  |  4KB  |  205 lines

  1. /* tree.c 
  2.  *
  3.  * Print out the directory-tree
  4.  * 
  5.  * Uwe Simon      April 1988
  6.  *
  7.  * Dies Programm ist PD-Software
  8.  *
  9.  * Es ist aus dem Program 'du' von Tom Leitner, Graz entstanden
  10.  *
  11.  */
  12.  
  13. /* system includes */
  14.  
  15. #include <stdio.h>
  16. #include <dir.h>
  17. #include <modes.h>
  18. #include <direct.h>
  19. #include <time.h>
  20.  
  21. /* constants & macros */
  22.  
  23. #define RBF (1)                 /* random block file found opened       */
  24. #define PIPE (2)                /* pipe file opened                     */
  25. #define NET (4)                 /* network file opened                  */
  26. #define TRUE 1                  /* logical true                         */
  27. #define FALSE 0                 /* logical false                        */
  28.  
  29. /* global variables */
  30.  
  31. int files = 0;                  /* # of files found                     */
  32. int dirs = 0;/* # of directories found       
  33.         */
  34. int max_level = 1000;
  35. char only_dirs = FALSE;
  36. char root[256];                     /* root directory to start from         */
  37.  
  38.  
  39. usage()
  40. {
  41.     fprintf(stderr,"Syntax: tree {<opts>} [<path>]\n");    
  42.     fprintf(stderr,"Function: prints the directory-tree\n");
  43.     fprintf(stderr,"Options:\n");
  44.     fprintf(stderr,"\t-d        print only the directories\n");
  45.     fprintf(stderr,"\t-l=<num>  print to level <num>\n");
  46.     exit(1);
  47. }
  48.  
  49.  
  50. /*--------------------------------- M A I N ----------------------------------*/
  51.  
  52. main(argc, argv)
  53. int argc;
  54. char *argv[];
  55. {
  56.     time_t t;
  57.     char tstr[27];
  58.     get_parms(argc, argv);
  59.     time(&t);
  60.     strcpy(tstr,ctime(&t));
  61.     tstr[19]='\0';
  62.     printf("\t\tDirectory-tree of %s %s\n", root,&tstr[11]);
  63.     printfile(0,0,root);
  64. }
  65.  
  66. /*-------------------- parses command line for parameters --------------------*/
  67.  
  68. get_parms(argc,argv)
  69. int argc;
  70. char *argv[];
  71. {
  72.     char *s;
  73.     int pc = 0;
  74.     while (--argc>0) {
  75.         if ((*++argv)[0]=='-' ) {
  76.             for (s = argv[0]+1;*s != '\0';s++)
  77.                 switch(toupper(*s)) {
  78.                 case 'D':
  79.                     only_dirs = TRUE;
  80.                     break;
  81.                 case 'L':
  82.                     if(s[1]=='=') max_level=atoi(&s[2]);
  83.                     else max_level=atoi(&s[1]);
  84.                     s[1]='\0';
  85.                     break;
  86.                 case '?':
  87.                     usage();
  88.                     exit(0);
  89.                 default:
  90.                     fprintf(stderr,
  91.                     "%s: unknown option '%c'\n",
  92.                     _prgname(),*s);
  93.                     exit(1);
  94.                 }
  95.         }
  96.         else if (pc == 0) {
  97.             strcpy(root,argv[0]);
  98.             pc++;
  99.         } 
  100.         else {
  101.             fprintf(stderr,"%s: only one parameter allowed\n",
  102.             _prgname());
  103.             exit(1);
  104.         }
  105.     }
  106.     if (pc == 0) strcpy(root,".");
  107. }
  108.  
  109. /*------------------------------ main algorithm ------------------------------*/
  110.  
  111. int printfile(anz_files,level,filename )  /* returns number of files printet */
  112. int level;
  113. char *filename;
  114. {
  115.     int fd, pl, dfiles;
  116.     DIR *dirp;
  117.     struct direct *dirrec;
  118.     static struct fildes stat;
  119.  
  120.  
  121.     if(level>=max_level) return;
  122.  
  123.  
  124.  
  125.     /* try to open the file */
  126.  
  127.     if( ((fd=open(filename,S_IFDIR|S_IREAD)) < 0) &&              /* regular file ? */
  128.     ((fd=open(filename,S_IREAD)) < 0) )       /* directory ?    */
  129.         fprintf(stderr,"Cannot open '%s'\n", filename);
  130.  
  131.     /* successfully opened. check if it's on the right device */
  132.  
  133.     else {
  134.         if(_gs_gfd(fd, &stat,sizeof(struct fildes))==-1)
  135.             fprintf(stderr,"wrong type: %s\n",filename);
  136.         else {
  137.  
  138.             /* ok. we're on a valid device : now check if we hit a directory file */
  139.  
  140.             if( stat.fd_att & S_IFDIR ) {
  141.  
  142.                 /* yes. we hit a directory file. close the FD and CHD into it. */
  143.  
  144.                 close(fd);
  145.                 if( (dirp = opendir( filename )) && !chdir( filename) ) {
  146.  
  147.                     strcat(filename," ---------------------");
  148.                     printline(anz_files?level:0,filename);
  149.                     anz_files=0;
  150.                     if(level<max_level-1) {
  151.                         /* skip the "." and the ".." entries */
  152.  
  153.                         readdir(dirp); 
  154.                         readdir( dirp );
  155.  
  156.                         /* get file sizes of all files in this directory */
  157.  
  158.                         dfiles = 0;
  159.                         while (dirrec = readdir (dirp)) 
  160.                             if (dirrec->d_addr) 
  161.                                 printfile(anz_files++,level+1,dirrec->d_name);
  162.  
  163.                         /* end of directory : close it and print the results */
  164.                     }
  165.  
  166.                     closedir( dirp );
  167.                     chdir("..");
  168.                     if(!only_dirs) {
  169.                         printline(anz_files++?level+1:0,"");
  170.                         printf("\n");
  171.                     }
  172.                 }
  173.  
  174.                 /* ERROR occurred : can't CHD into this directory. */
  175.  
  176.                 else
  177.                     fprintf(stderr, "Cannot chdir '%s'\n", filename);
  178.  
  179.             }   
  180.             else {
  181.                 if(!only_dirs) {
  182.                     printline(anz_files?level:0,filename);
  183.                     printf("\n");
  184.                     anz_files++;
  185.                 }
  186.                 close( fd );
  187.             }
  188.         }
  189.     }
  190.     if(!anz_files) printf("\n");
  191. }
  192.  
  193.  
  194. printline(level,path)
  195. int level;
  196. char *path;
  197. {
  198.     int i;
  199.     for(i=0;i<level;i++) printf("   |           ");
  200.     printf("%-15.14s",path);
  201. }
  202.  
  203. /* E O F */
  204.  
  205.