home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / CLIPPER / DIRSTR / DIRSTRUC.C next >
Text File  |  1992-12-26  |  5KB  |  148 lines

  1. // dirstruc.c
  2. // Author.....: Michael J. Primeaux
  3. // Date.......: 25dec92
  4. // Syntax.....: dirStruct( <starting directory> )
  5. // Returns....: character array
  6. // Sample.....: aDirectory := dirStruct( "\" )
  7. // Compile....: bcc -c -ml -f- -a -r -B -O -Z dirstruc.c
  8. // Note.......: compiled with Borland C++ v3.1
  9.  
  10. #include <alloc.h>
  11. #include <ctype.h>
  12. #include <dir.h>
  13. #include <dos.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "extend.h"  // Clipper Extend Include File
  18.  
  19. #define MAX_DIR   512      // Maximum directories supported
  20.  
  21. // Directory sizing structure
  22. typedef struct {
  23.    char *name;             // Name of the directory
  24.    long size;              // Cummulative size of all files in dir
  25.    int files;              // Number of files in dir
  26. } DOSDIR;
  27.  
  28. int sizedir(char *dname,int *dn,DOSDIR **direc,int slots,int *nlen, int recurse)
  29. {
  30.    // Directory search attriubte
  31.    static const char srchattr = (FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_DIREC|FA_ARCH);
  32.  
  33.    char fspec[MAXPATH];    // Wildcard file spec for search
  34.    struct ffblk fb;        // used in findfirst/next functions
  35.    int done;               // end of directory check flag
  36.    DOSDIR *dir;            // Alias of direc[dn] for code readability
  37.  
  38.    // Check to see that number of directories hasn't outgrown our array
  39.    // of pointers.
  40.    if (*dn > slots) return(-1);
  41.  
  42.    // Check and make sure the array of pointers has been allocated
  43.    if (!direc) return(-1);
  44.  
  45.    // Allocate the totals directory
  46.    if (*dn == 0) {
  47.       dir = direc[0] = (DOSDIR *) malloc(sizeof(DOSDIR));
  48.       if (!dir) return(-1);
  49.       strcpy(fspec, "Totals");
  50.       dir->name = (char *) malloc(strlen(fspec)+1);
  51.       if (!dir->name) return(-1);
  52.       strcpy(dir->name, fspec);
  53.       direc[0]->files = 0;
  54.       direc[0]->size = 0;
  55.    }
  56.  
  57.    // Allocate the space for this directory
  58.    (*dn)++;       // Add this directory to the directory counter
  59.  
  60.    dir = direc[*dn] = (DOSDIR *) malloc(sizeof(DOSDIR));
  61.    if (!dir) return(-1);
  62.    dir->name = (char *) malloc(strlen(dname)+1);
  63.    if (!dir->name) return(-1);
  64.  
  65.    // Initialize the directory record
  66.    strcpy(dir->name, dname);
  67.    dir->size = 0;
  68.    dir->files = 0;
  69.  
  70.    // Update maximum directory name length for output
  71.    *nlen = max(*nlen, strlen(dir->name));
  72.  
  73.    // Add wildcard spec to the directory name
  74.    strcpy(fspec, dir->name);
  75.    strcat(fspec, "*.*");      // Look for all files
  76.  
  77.    // Scan the entire directory structure
  78.    done = findfirst(fspec, &fb, srchattr);
  79.    while (!done) {
  80.       do {
  81.          if (!strcmp(fb.ff_name,"."))  break;   // Skip curr/parent specs
  82.          if (!strcmp(fb.ff_name,"..")) break;
  83.          if ((fb.ff_attrib & FA_DIREC) && recurse) {
  84.             strcpy(fspec, dname);
  85.             strcat(fspec, fb.ff_name);
  86.             strcat(fspec, "\\");                // RECURSE
  87.             sizedir(fspec, dn, direc, slots, nlen, recurse);
  88.          }
  89.          else {                                 // File
  90.             (dir->files)++;
  91.             dir->size += fb.ff_fsize;
  92.  
  93.             (direc[0]->files)++;
  94.             (direc[0]->size) += fb.ff_fsize;
  95.          }
  96.       } while (0);
  97.       done = findnext(&fb);
  98.    }
  99.    return(*dn);
  100. }
  101.  
  102. CLIPPER dirStruct( void )
  103. {
  104.    char startdir[MAXPATH]="\\";
  105.    int numdirs=0;
  106.    DOSDIR **direc=NULL;          // The directories array
  107.    int nlen=63;                  // Maximum name length
  108.    int recurse = 1;              // recurse by default
  109.  
  110.    char *path;
  111.    register quant i;
  112.    path = _parc(1);              // recieve path parameter
  113.  
  114.    strcpy(startdir, path);
  115.    strupr(startdir);
  116.    if (startdir[strlen(startdir)-1] != '\\')
  117.       strcat(startdir, "\\");
  118.  
  119.    // Allocate the array of pointers
  120.    direc = (DOSDIR **) calloc(MAX_DIR, sizeof(DOSDIR *));
  121.  
  122.    // Calculate the size of the directories
  123.    sizedir(startdir, &numdirs, direc, MAX_DIR, &nlen, recurse);
  124.    if (numdirs == -1) {
  125.       _ret();
  126.    }
  127.  
  128. #ifdef DEBUG
  129.    // Print header
  130.    printf("%-*s %5s %9s\n\n", nlen, "Dir Name", "Files", "Size");
  131.  
  132.    // Loop for each directory in the array
  133.    for (i=1; i <= numdirs; i++) {
  134.       printf("%-*s %5d %9ld\n", nlen, direc[i]->name, direc[i]->files,direc[i]->size);
  135.    }
  136.    // Display summary totals
  137.    printf("\nA total of %d directories containing ", numdirs);
  138.    printf("%d files, ", direc[0]->files);
  139.    printf("Total bytes used %ld\n", direc[0]->size);
  140. #endif
  141.    _reta( numdirs );
  142.    for (i=1; i <= numdirs; i++) {
  143.       _storc( direc[i]->name, -1, i ); 
  144.    }
  145. }
  146.  
  147. // eof dirstruc.c
  148.