home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / directry / tc_dir01 / direct02.c next >
Encoding:
C/C++ Source or Header  |  1988-11-18  |  5.1 KB  |  189 lines

  1. #include <fcntl.h>
  2. #include <dir.h>
  3. #include <dos.h>
  4. #include <io.h>
  5. #include <stdio.h>
  6.  
  7. #define TRUE 1
  8. #define FALSE 0
  9. #define MAX_DIRS 500
  10.  
  11. typedef struct {
  12.    char name[80];
  13.    char status;
  14. } DIRECT_ORY;
  15.  
  16.   struct ffblk buf;
  17.   int done;
  18.   DIRECT_ORY directory[MAX_DIRS];
  19.  
  20.   char Path[80];
  21.   char Search_Path[80];
  22.   char Attributes[6];
  23.   char *directory_name = "\0";
  24.   char *complete_dir   = "\0";
  25.   char *complete_file  = "\0";
  26.  
  27.   FILE *fd;
  28.   int direct_ctr = 1;
  29.   int save_direct_ctr;
  30.   int array_ctr;
  31.   int print_ctr;
  32.   int ctl_state = TRUE;
  33.   int found_new = FALSE;
  34.   int directory_counter = 0;
  35.   int file_counter      = 0;
  36.   /* Declarations End */
  37.  
  38.   /* Main program flow begins here */
  39. main ()
  40. {
  41.  
  42.   if (!get_directories()) {
  43.     printf("Program aborted in get_directories function...\n");
  44.     exit(1);
  45.   }
  46.   print_directories();
  47.   if (!get_files()) {
  48.     printf("Program aborted in get_files function...\n");
  49.     exit(2);
  50.   }
  51.   printf("There are %d directories...\n",directory_counter);
  52.   printf("There are %d files...\n\n\n",file_counter);
  53.   printf("        ...Program completed successfully...\n");
  54.   /* END OF MAIN */
  55. }
  56.  
  57. int get_directories()
  58.   {
  59.     printf("Gathering directory names...\n\n");
  60.     directory[0].status = 'n';
  61.     strcpy(directory[0].name,"\\");
  62.     while (ctl_state) {
  63.       save_direct_ctr = direct_ctr;
  64.       for (array_ctr = 0;array_ctr < save_direct_ctr;array_ctr++) {
  65.         found_new = FALSE;
  66.         if (directory[array_ctr].status == 'n') {
  67.           found_new = TRUE;
  68.           if (array_ctr == 0) {
  69.             strcpy(Path,directory[array_ctr].name);
  70.             strcpy(Search_Path,Path);
  71.             strcat(Search_Path,"*.*");
  72.           }
  73.           else {
  74.             strcpy(Path,directory[array_ctr].name);
  75.             strcat(Path,"\\");
  76.             strcpy(Search_Path,Path);
  77.             strcat(Search_Path,"*.*");
  78.           }
  79.           done = findfirst(Search_Path,&buf,0x3f);
  80.           while (done == 0) {
  81.             if (buf.ff_attrib & FA_DIREC) {
  82.               if (direct_ctr > MAX_DIRS) {
  83.                 printf("Array Overflow...\n");
  84.                 return(FALSE);
  85.               }
  86.               if (real_dir(buf.ff_name)) {
  87.                 directory[direct_ctr].status = 'n';
  88.                 strcpy(directory[direct_ctr].name,Path);
  89.                 strcat(directory[direct_ctr].name,buf.ff_name);
  90.                 strcpy(directory_name,directory[direct_ctr].name);
  91.                 direct_ctr++;
  92.               }
  93.             }
  94.             done = findnext(&buf);
  95.           }
  96.           directory[array_ctr].status = 'o';
  97.         }
  98.       }
  99.       if (!found_new)
  100.         ctl_state = FALSE;
  101.     }
  102.     return(TRUE);
  103.   }
  104.  
  105. int print_directories()
  106.   {
  107.     printf("Printing directory names to file: dir.lst...\n\n");
  108.     if ((fd = fopen("dir.lst","w")) == NULL) {
  109.       printf("Error opening dir.lst for writing...\n");
  110.       return(FALSE);
  111.     }
  112.     for (print_ctr = 0;print_ctr < direct_ctr;print_ctr++) {
  113.       strcpy(complete_dir,directory[print_ctr].name);
  114.       strcat(complete_dir,"\n");
  115.       fputs(complete_dir,fd);
  116.       directory_counter++;
  117.     }
  118.     fclose(fd);
  119.     return(TRUE);
  120.   }
  121.  
  122. int real_dir(current_name)
  123.   char current_name[];
  124.   {
  125.     if (current_name[0] == '.' &&
  126.          (current_name[1] == '\0' || current_name[1] == '.'))
  127.       return(FALSE);
  128.     else
  129.       return(TRUE);
  130.   }
  131. int get_files()
  132.   {
  133.     printf("Gathering file names...\n");
  134.     printf("    and printing to file: file.lst...\n\n");
  135.     if ((fd = fopen("file.lst","w")) == NULL) {
  136.       printf("Error opening file.lst for writing...\n");
  137.       return(FALSE);
  138.     }
  139.     for (array_ctr = 0;array_ctr < direct_ctr;array_ctr++) {
  140.       if (array_ctr == 0) {
  141.         strcpy(Path,directory[array_ctr].name);
  142.         strcpy(Search_Path,Path);
  143.         strcat(Search_Path,"*.*");
  144.       }
  145.       else {
  146.         strcpy(Path,directory[array_ctr].name);
  147.         strcat(Path,"\\");
  148.         strcpy(Search_Path,Path);
  149.         strcat(Search_Path,"*.*");
  150.       }
  151.       done = findfirst(Search_Path,&buf,0x3f);
  152.       while (done == 0) {
  153.         if (buf.ff_attrib != 16) {
  154.           if (buf.ff_attrib == 0)
  155.             Attributes[0] = 'N';
  156.           else
  157.             Attributes[0] = ' ';
  158.           if (buf.ff_attrib & FA_RDONLY)
  159.             Attributes[1] = 'R';
  160.           else
  161.             Attributes[1] = ' ';
  162.           if (buf.ff_attrib & FA_HIDDEN)
  163.             Attributes[2] = 'H';
  164.           else
  165.             Attributes[2] = ' ';
  166.           if (buf.ff_attrib & FA_SYSTEM)
  167.             Attributes[3] = 'S';
  168.           else
  169.             Attributes[3] = ' ';
  170.           if (buf.ff_attrib & FA_ARCH)
  171.             Attributes[4] = 'A';
  172.           else
  173.             Attributes[4] = ' ';
  174.           strcpy(Attributes[5],'\0');
  175.           strcpy(complete_file,Attributes);
  176.           strcat(complete_file," - ");
  177.           strcat(complete_file,Path);
  178.           strcat(complete_file,buf.ff_name);
  179.           strcat(complete_file,"\n");
  180.           fputs(complete_file,fd);
  181.           file_counter++;
  182.         }
  183.         done = findnext(&buf);
  184.       }
  185.     }
  186.     fclose(fd);
  187.     return(TRUE);
  188.   }
  189.