home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 165 / DSKPARM.ZIP / INFO.C < prev    next >
Text File  |  1987-12-26  |  7KB  |  231 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <dos.h>
  5. #include <malloc.h>
  6.  
  7. /* info -- This program displays useful information
  8. **    about a DOS disk.  Output is in the form of
  9. **    a series of general information lines followed
  10. **    by a table showing disk space usage.  Space
  11. **    usage is shown in sectors, bytes and clusters.
  12. **    The program should work with all disk formats
  13. **    including "RAM" disks.
  14. **
  15. **    Usage: info [d:]
  16. **    Where d: is an optional drive specification.
  17. **    If d: is omitted, the default drive wil be
  18. **    assumed.
  19. **    Compiler:    Microsoft C V4.0
  20. **    Options:    /Zp  (pack arrays)  (V3.0 requires /Ze)
  21. **
  22. **    External modules:
  23. **            absread()
  24. **    Version 1.53    February 6, 1986
  25. **    Glenn F. Roberts
  26. */
  27.  
  28. #define TRUE 1
  29. #define ENTRY_LENGTH 32
  30. #define MIN_VERSION 200        /* DOS 2.0 */
  31. #define MAX_VERSION 330        /* DOS 3.3 */
  32.  
  33. #include "structs.h"
  34. #include "dosfns.h"
  35.  
  36. main(argc, argv)
  37. int argc;
  38. char *argv[];
  39. {
  40.   int drive, ver;
  41.   struct disk_table far *get_table(), far *tbl;
  42.   struct boot *bpb;
  43.   static struct ext_fcb fcb = {
  44.       
  45.     0xFF,0,0,0,0,0,VOL_ENTRY,0,
  46.     '?','?','?','?','?','?','?','?','?','?','?',
  47.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  48.   };
  49.  
  50. ver = _osmajor*100+_osminor;
  51. if ((ver < MIN_VERSION) || (ver > MAX_VERSION))
  52.   printf("Incorrect DOS version %d\n", ver);
  53. else if (!((--argc > 0) ?
  54.     (parse(*++argv, &fcb.drive_id, 1) != 255) : TRUE))
  55.   printf("Invalid drive specification\n");
  56. else {
  57.   drive = (fcb.drive_id == 0) ? current_drv() : fcb.drive_id-1;
  58.   tbl = get_table(drive);
  59.   bpb = (struct boot *) malloc(tbl->sector_size);
  60.   absread(drive, 1, 0, bpb);
  61.   print_vol(drive, tbl);
  62.   print_info(drive, bpb, tbl);
  63.   print_tbl(drive, bpb, tbl);
  64.  }
  65. }
  66. /*  scan_fat -- Analyze FAT, Calculate the number
  67. **    of clusters available, in use and
  68. **    "locked out" by DOS
  69. */
  70. scan_fat(fat, is12, fatlast, avail, locked, used)
  71. unsigned char *fat;
  72. int is12;
  73. unsigned fatlast, *avail, *locked, *used;
  74. {
  75.   int i, cn;
  76.  
  77.   *avail = *locked = *used = 0;
  78.   for (i=2; i<=fatlast; i++) {
  79.       cn = fatval(is12, i, fat);
  80.       if (cn == LOCKED_OUT(is12))    (*locked)++;
  81.       else if (cn == AVAILABLE)    (*avail)++;
  82.       else                (*used)++;
  83.   }
  84. }
  85. /*  print_vol --  Print volume name and creation time/
  86. **        date if it exists, else print that
  87. **        volume has no label.
  88. */
  89. print_vol(drive, tbl)
  90. int drive;
  91. struct disk_table far *tbl;
  92. {
  93.     struct extended_entry dir_entry;
  94.     static struct ext_fcb vol_fcb = {
  95.         
  96.           0xFF,0,0,0,0,0,VOL_ENTRY,0,
  97.           '?','?','?','?','?','?','?','?','?','?','?',
  98.           0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  99.     };
  100.     int i;
  101.     char cre_date[15], cre_time[15];
  102.     
  103.     printf("*** Information for Disk %c:", (drive + 'A'));
  104.     if (tbl->designator != drive)
  105.       printf(" (Assigned to drive %c:)",(tbl->designator + 'A'));
  106.     printf(" *** \n\n  Volume ");
  107.     setdta(&dir_entry);
  108.     vol_fcb.drive_id = drive+1;
  109.     if (search_first(&vol_fcb) != 255) {
  110.         dtoa(dir_entry.body.create_date, cre_date);
  111.         ttoa(dir_entry.body.create_time, cre_time);
  112.         for (i=0; i<11; i++)
  113.           putchar(dir_entry.body.filname[i]);
  114.         printf(" created %s %s\n", cre_date, cre_time);
  115.     }
  116.     else
  117.       printf("has no label\n");
  118. }
  119.  
  120. /*  print_info -- Print general information
  121. */
  122. print_info(drive, bpb, tbl)
  123. int drive;
  124. struct boot *bpb;
  125. struct disk_table far *tbl;
  126. {
  127.     int i, valid_boot;
  128.     unsigned ncyl;
  129.     
  130.     /* if OEM name is printable then boot data are OK */
  131.     for (i=0, valid_boot = TRUE; (i<8) && (valid_boot); i++)
  132.       valid_boot = isprint(bpb->oem_name[i]);
  133.     if (valid_boot) {
  134.         ncyl = bpb->number_of_sectors/
  135.           (bpb->sectors_per_track*bpb->number_of_heads);
  136.         if ((bpb->number_of_sectors%
  137.           (bpb->sectors_per_track*bpb->number_of_heads)) != 0)
  138.          ncyl++;
  139.         printf("  OEM name : ");
  140.         for (i=0; i<8; i++)
  141.           putchar(bpb->oem_name[i]);
  142.         printf("     ");
  143.     }
  144.     printf("  Media descriptor (hex): %2x\n", tbl->media_type);
  145.     if (valid_boot) {
  146.         printf("  Volume has %d Surfaces, ", bpb->number_of_heads);
  147.         printf("%d Tracks with ", ncyl);
  148.         printf("%d Sectors/Track\n", bpb->sectors_per_track);
  149.     }
  150.     printf("  Sector size is %u bytes.", tbl->sector_size);
  151.     printf("   FAT entries are %d bits\n",
  152.       (tbl->last_cluster < MAX_12BIT) ? 12 : 16);
  153.     printf("  Cluster size is %u bytes ",
  154.       (tbl->cluster_size+1)*tbl->sector_size);
  155.     printf("(%u sectors)\n\n", tbl->cluster_size+1);
  156. }
  157. /* print_tbl --  Print table showing disk usage.
  158. */
  159. print_tbl(drive, bpb, tbl)
  160. int drive;
  161. struct boot *bpb;
  162. struct disk_table far *tbl;
  163. {
  164.     int i, nhidden, valid_boot, twelve_bit_fat;
  165.     unsigned avail, locked, used, ntotal;
  166.     unsigned char *fat;
  167.     
  168.     printf("Usage:                 ");
  169.     printf("   Sectors     Bytes     Clusters\n");
  170.     for (i=0; i<61; i++)
  171.       putchar('=');
  172.     putchar('\n');
  173.     
  174.     ntotal = tbl->fat_start + tbl->fat_copies*tbl->fat_size +
  175.       (tbl->max_entries*ENTRY_LENGTH/tbl->sector_size) +
  176.       (tbl->last_cluster-1) * (tbl->cluster_size+1);
  177.       
  178.     /* If OEM name is printable then boot data are OK */
  179.     for (i=0, valid_boot=TRUE; (i<8) && (valid_boot); i++)
  180.       valid_boot = isprint(bpb->oem_name[i]);
  181.       
  182.     /* Print "hidden" information, if available */
  183.     if(valid_boot) {
  184.         nhidden = bpb->number_of_sectors - ntotal;
  185.         ntotal = bpb->number_of_sectors;
  186.         printf("Sectors Not Available  | %7d  | %12lu  |         |\n",
  187.           nhidden, (long) nhidden * (long) tbl->sector_size);
  188.     }
  189.     /* Print stat's on DOS boot, FAT, and root dir. */
  190.     printf("DOS Boot Area          | %7d  | %12lu  |         |\n",
  191.       tbl->fat_start, (long) tbl->fat_start *
  192.       (long) tbl->sector_size);
  193.     printf("File Allocation Table  | %7d  | %12lu  |         |\n",
  194.       (tbl->fat_copies)*(tbl->fat_size),
  195.       (long)(tbl->fat_copies)*(tbl->fat_size)*
  196.       (long) tbl->sector_size);
  197.     printf("Root Directory         | %7d  | %12lu  |         |\n",
  198.     (tbl->max_entries*ENTRY_LENGTH/tbl->sector_size),
  199.     (long)(tbl->max_entries*ENTRY_LENGTH));
  200.  
  201.     /* Read and analyze the FAT */
  202.     fat = (unsigned char *) malloc(tbl->fat_size*tbl->sector_size);
  203.     absread(drive, tbl->fat_size, tbl->fat_start, fat);
  204.     twelve_bit_fat=(tbl->last_cluster < MAX_12BIT);
  205.     scan_fat(fat, twelve_bit_fat, tbl->last_cluster,
  206.       &avail, &locked, &used);
  207.  
  208.     /* Print used, avail, locked out summary */
  209.     printf("Files & Subdirectories | %7lu  | %12lu  | %6d  |\n",
  210.       (long) used * (long) (tbl->cluster_size+1),
  211.       (long) used * (long) (tbl->cluster_size+1) *
  212.       (long) tbl->sector_size, used);
  213.     printf("Locked Out             | %7u  | %12lu  | %6u  |\n",
  214.       (long) locked * (long) (tbl->cluster_size+1) *
  215.       (long) tbl->sector_size, locked);
  216.     printf("Available              | %7lu  | %12lu  | %6u  |\n",
  217.       (long) avail * (long) (tbl->cluster_size+1),
  218.       (long) avail * (long) (tbl->cluster_size+1) *
  219.       (long) tbl->sector_size, avail);
  220.  
  221.     /* Print totals and percent disk used information */
  222.     for (i=0; i<61; i++)
  223.       putchar('=');
  224.     putchar('\n');
  225.     printf("TOTAL                  |  %7u ", ntotal);
  226.     printf("| %12lu  | %6u  |\n\n", (long) ntotal *
  227.       (long) tbl->sector_size, (tbl->last_cluster)-1);
  228.     printf("              The disk is %lu%% full\n",
  229.       (used+locked)*100L/((tbl->last_cluster)-1));
  230. }
  231.