home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / open / dbdir8.lzh / DBDIR.C next >
Text File  |  1989-04-03  |  6KB  |  160 lines

  1. /*
  2.  
  3. +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  4. *
  5. *               Program..: DBDIR.c   Version 8
  6. *               Author...: Vernon E. Davis
  7. *               Date.....: 12/21/86
  8. *               Notes....: a utility for DOS to display
  9. *                          database file information like dBASE.
  10. *
  11. *               Compiled with Turbo C  v2.0
  12. *
  13. *               References:
  14. *                  Advanced Programmers Guide(dBASE) - Ashton Tate
  15. *                  C Language User's Guide - Weber Systems
  16. *                  Programmer's Guide to the IBM PC - Peter Norton
  17. *
  18. *               dBASE is a registered trademark of Ashton Tate
  19. *               Turbo C is a trademark of Borland International
  20. *
  21. *
  22. *
  23. *  Revision History
  24. *  ~~~~~~~~~~~~~~~~
  25. *  V    Date    Description
  26. *  ~  ~~~~~~~~  ~~~~~~~~~~~
  27. *  1  12/15/86  Preliminary release( Lattice C v3.1 )
  28. *  2  12/21/86  Corrected pointer problem ( *buffer was not reinitialized )
  29. *  3  02/09/87  Added III,III+ distinction; corrected problem in II
  30. *               ( *buffer was not reinitialized )
  31. *  4  08/08/87  Recoded and recompiled under Turbo C v1.0; II info removed
  32. *  5  02/20/88  Recompiled under Turbo C v1.5
  33. *  6  05/04/88  Fixed printf() when memo field is present
  34. *  7  02/04/89  Recompiled under Turbo C v2.0; added IV distinction;
  35. *               added title message; source code included with program.
  36. *  8  04/03/89  Fix to filter DBF type.
  37. *
  38. *
  39. *
  40. +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  41.  
  42. */
  43.  
  44. #include <DIR.h>
  45. #include <DOS.h>
  46. #include <FCNTL.h>
  47. #include <STDIO.h>
  48. #include <STDLIB.h>
  49. #include <STRING.h>
  50.  
  51.    typedef struct
  52.    {  unsigned char dbtype; /* .DBF type (0x03 = .DBF)(0x83 = .DBF + .DBT) */
  53.       char lupy;                                     /* last update - year */
  54.       char lupm;                                    /* last update - month */
  55.       char lupd;                                     /* last update - date */
  56.       unsigned long reccnt;                     /* total number of records */
  57.       unsigned int headlen;                               /* header length */
  58.       unsigned int reclen;    /* record length ( including delete marker ) */
  59.       char reserved[20];                          /* reserved ( not used ) */
  60.    }  headdef;                   /* .... dBASE III header definitions .... */
  61.  
  62.    /* The field type is contained in the twelfth byte of the field info.   */
  63.  
  64.  
  65. void main(void)
  66. {  headdef dirhead,*dhdr=&dirhead;
  67.    struct ffblk ffblk;
  68.    struct dfree dfree;
  69.    int h,i=0,j,filecnt=0,fh,fldcnt;
  70.    long sizecnt=0L,diskfree=0L;
  71.    char dbfver[5],*vptr=&dbfver[0],ismemo[4],*iptr=&ismemo[0],*mptr,*tptr;
  72.  
  73.    printf("dBDIR.com  version 8 (04/03/89)  Vernon E. Davis\n");
  74.    if((h=findfirst("*.DBF",&ffblk,FA_ARCH))==-1)
  75.       printf("\nNone\n");
  76.    else
  77.    {  printf("\nDatabase Files    # Records    Last Update     Size  ");
  78.       printf("Memo  RecLen  # Flds  Ver.\n");
  79.       while(!h)
  80.       {  /* *** 1. Attempt to open DBF *** */
  81.          if((fh=open(ffblk.ff_name,O_RDONLY|O_BINARY))==-1)
  82.          {  printf("%-12s  Cannot open\n",ffblk.ff_name);
  83.             h=findnext(&ffblk);
  84.             continue;
  85.          }
  86.          filecnt++;
  87.          sizecnt+=ffblk.ff_fsize;
  88.  
  89.          /* *** 2. Read the header information *** */
  90.          _read(fh,dhdr,32);
  91.          dhdr->dbtype&=0x83; /* filter DBF type */
  92.          if(dhdr->dbtype!=3&&dhdr->dbtype!=0x83) /* if not .DBF, continue */
  93.          {  close(fh);
  94.             printf("%-12s  Not a database file        %10ld\n",
  95.              ffblk.ff_name,ffblk.ff_fsize);
  96.             h=findnext(&ffblk);
  97.             continue;
  98.          }
  99.  
  100.          /* *** 3. Determine the field count *** */
  101.          fldcnt=(dhdr->headlen-32)/32;
  102.  
  103.          /* *** 4. Determine the DBF version and if MEMOs exist *** */
  104.          lseek(fh,dhdr->headlen-1,0);        /* point to end of header - 1 */
  105.          _read(fh,&i,1);
  106.          if(!i)                              /* if zero, force III */
  107.          {  close(fh);
  108.             strcpy(vptr,"III");
  109.          }
  110.          else                                /* ... else */
  111.          {  if(fldcnt<=128)
  112.             {  tptr=mptr=malloc(fldcnt*32);  /* allocate memory: field info */
  113.                if(mptr==NULL)
  114.                {  close(fh);
  115.                   printf("\n... Memory Allocation Error.\n");
  116.                   exit(1);
  117.                }
  118.                lseek(fh,33,0);               /* point to start of field info */
  119.                _read(fh,tptr,fldcnt*32);     /* read field info in memory */
  120.                close(fh);                    /* close database */
  121.                h=0;                          /* flag for III+ */
  122.                for(i=0;i<10;i++) *tptr++;    /* point to first field type */
  123.                for(i=0;i<fldcnt;i++)
  124.                {  if(*tptr=='F')             /* if field type = FLOAT */
  125.                   {  h=1;                    /* flag for IV */
  126.                      break;                  /* exit loop */
  127.                   }
  128.                   for(h=0;h<32;h++) *tptr++; /* else loop to next field type */
  129.                   h=0;                       /* reset flag for III+ */
  130.                }
  131.                free(mptr);                   /* unallocate memory */
  132.             }
  133.             else
  134.             {  close(fh);
  135.                h=1;                          /* force IV */
  136.             }
  137.             !h?strcpy(vptr,"III+"):strcpy(vptr,"IV");
  138.          }
  139.          dhdr->dbtype==0x83?strcpy(iptr,"Yes"):strcpy(iptr," No");
  140.  
  141.          /* *** 5. Display DBF information *** */
  142.          printf("%-12s    %10ld     %02d/%02d/%02d  %10ld   %s"
  143.           ,ffblk.ff_name,dhdr->reccnt,dhdr->lupm,dhdr->lupd,dhdr->lupy
  144.           ,ffblk.ff_fsize,iptr);
  145.          printf("    %4d     %3d  %s\n",dhdr->reclen,fldcnt,vptr);
  146.  
  147.          h=findnext(&ffblk);
  148.       }
  149.    }
  150.  
  151.    /* *** 6. Obtain and display disk bytes free *** */
  152.    printf("\n%10ld bytes in %d files.\n",sizecnt,filecnt);
  153.    getdfree(0,&dfree);
  154.    diskfree=dfree.df_bsec*dfree.df_sclus;
  155.    diskfree*=dfree.df_avail;
  156.    printf("%10ld bytes remaining on drive.\n",diskfree);
  157.    exit(0);
  158. }
  159.  
  160.