home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MAGAZINE / MISC / MAY86.ZIP / INFO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-03-24  |  7.5 KB  |  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 will be
  18. **          assumed.
  19. **          Compiler:    Microsoft C V3.0
  20. **          Options :    /Zp    (pack arrays)
  21. **                       /Ze    (support "far" extension)
  22. **
  23. **          External modules:
  24. **                       absread()
  25. **          Version 1.53     February 6, 1986
  26. **          Glenn F. Roberts
  27. */
  28.  
  29. #define TRUE 1
  30. #define ENTRY_LENGTH 32
  31. #define MIN_VERSION 200        /* DOS 2.0 */
  32. #define MAX_VERSION 310        /* DOS 3.1 */
  33.  
  34. #include "structs.h"
  35. #include "dosfns.h"
  36.  
  37. main(argc, argv)
  38. int argc;
  39. char *argv[];
  40. {
  41.   int drive, ver;
  42.   struct disk_table far *get_table(), far *tbl;
  43.   struct boot *bpb;
  44.   static struct ext_fcb fcb = {
  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.     0xFF,0,0,0,0,0,VOL_ENTRY,0,
  96.     '?','?','?','?','?','?','?','?','?','?','?',
  97.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  98.   };
  99.   int i;
  100.   char cre_date[15], cre_time[15];
  101.  
  102.   printf("*** Information for Disk %c:", (drive + 'A'));
  103.   if (tbl->designator != drive)
  104.     printf(" (Assigned to drive %c:)", (tbl->designator + 'A'));
  105.   printf(" ***\n\n  Volume ");
  106.   setdta(&dir_entry);
  107.   vol_fcb.drive_id = drive+1;
  108.   if (search_first(&vol_fcb) != 255) {
  109.     dtoa(dir_entry.body.create_date, cre_date);
  110.     ttoa(dir_entry.body.create_time, cre_time);
  111.     for (i=0; i<11; i++)
  112.       putchar(dir_entry.body.filname[i]);
  113.     printf(" created  %s  %s\n", cre_date, cre_time);
  114.   }
  115.   else
  116.     printf("has no label\n");
  117. }
  118.  
  119. /*  print_info -- Print general information.
  120. */
  121. print_info(drive, bpb, tbl)
  122. int drive;
  123. struct boot *bpb;
  124. struct disk_table far *tbl;
  125. {
  126.   int i, valid_boot;
  127.   unsigned ncyl;
  128.  
  129.   /* If OEM name is printable then boot data are OK */
  130.   for (i=0, valid_boot = TRUE; (i<8) && (valid_boot); i++)
  131.     valid_boot = isprint(bpb->oem_name[i]);
  132.   if (valid_boot) {
  133.     ncyl = bpb->number_of_sectors/
  134.         (bpb->sectors_per_track*bpb->number_of_heads);
  135.     if ((bpb->number_of_sectors %
  136.         (bpb->sectors_per_track*bpb->number_of_heads)) != 0)
  137.       ncyl++;
  138.     printf("  OEM name : ");
  139.     for (i=0; i<8; i++)
  140.       putchar(bpb->oem_name[i]);
  141.     printf("     ");
  142.   }
  143.   printf("  Media descriptor (hex): %2x\n", tbl->media_type);
  144.   if (valid_boot) {
  145.     printf("  Volume has %d Surfaces, ", bpb->number_of_heads);
  146.     printf("%d Tracks with ", ncyl);
  147.     printf("%d Sectors/Track\n", bpb->sectors_per_track);
  148.   }
  149.   printf("  Sector size is %u bytes.", tbl->sector_size);
  150.   printf("   FAT entries are %d bits\n",
  151.       (tbl->last_cluster < MAX_12BIT) ? 12 : 16);
  152.   printf("  Cluster size is %u bytes ",
  153.       (tbl->cluster_size+1)*tbl->sector_size);
  154.   printf("(%u sectors)\n\n", tbl->cluster_size+1);
  155. }
  156. /*  print_tbl -- Print table showing disk usage.
  157. */
  158. print_tbl(drive, bpb, tbl)
  159. int drive;
  160. struct boot *bpb;
  161. struct disk_table far *tbl;
  162. {
  163.   int i, nhidden, valid_boot, twelve_bit_fat;
  164.   unsigned avail, locked, used, ntotal;
  165.   unsigned char *fat;
  166.  
  167.   printf("Usage:                    ");
  168.   printf("Sectors        Bytes     Clusters\n");
  169.   for (i=0; i<61; i++)
  170.     putchar('=');
  171.   putchar('\n');
  172.  
  173.   ntotal = tbl->fat_start + tbl->fat_copies*tbl->fat_size +
  174.       (tbl->max_entries*ENTRY_LENGTH/tbl->sector_size) +
  175.       (tbl->last_cluster-1) * (tbl->cluster_size+1);
  176.  
  177.   /* If OEM name is printable then boot data are OK */
  178.   for (i=0, valid_boot = TRUE; (i<8) && (valid_boot); i++)
  179.     valid_boot = isprint(bpb->oem_name[i]);
  180.  
  181.   /* Print "hidden" information, if available */
  182.   if(valid_boot) {
  183.     nhidden = bpb->number_of_sectors - ntotal;
  184.     ntotal = bpb->number_of_sectors;
  185.     printf("Sectors Not Available        | %7d  | %12lu  |         |\n",
  186.         nhidden, (long) nhidden * (long) tbl->sector_size);
  187.   }
  188.   /* Print stat's on DOS boot, FAT, and root dir. */
  189.   printf("DOS Boot Area          | %7d  | %12lu  |         |\n",
  190.       tbl->fat_start, (long) tbl->fat_start *
  191.       (long) tbl->sector_size);
  192.   printf("File Allocation Table  | %7d  | %12lu  |         |\n",
  193.       (tbl->fat_copies)*(tbl->fat_size),
  194.       (long) (tbl->fat_copies)*(tbl->fat_size) *
  195.       (long) tbl->sector_size);
  196.   printf("Root Directory         | %7d  | %12lu  |         |\n",
  197.       (tbl->max_entries*ENTRY_LENGTH/tbl->sector_size),
  198.       (long) (tbl->max_entries*ENTRY_LENGTH));
  199.  
  200.   /* Read and analyze the FAT */
  201.   fat = (unsigned char *) malloc(tbl->fat_size*tbl->sector_size);
  202.   absread(drive, tbl->fat_size, tbl->fat_start, fat);
  203.   twelve_bit_fat = (tbl->last_cluster < MAX_12BIT);
  204.   scan_fat(fat, twelve_bit_fat, tbl->last_cluster,
  205.       &avail, &locked, &used);
  206.  
  207.   /* Print used, avail, locked out summary */
  208.   printf("Files & Subdirectories | %7lu  | %12lu  | %6d  |\n",
  209.       (long) used * (long) (tbl->cluster_size+1),
  210.       (long) used * (long) (tbl->cluster_size+1) *
  211.       (long) tbl->sector_size, used);
  212.   printf("Locked Out             | %7u  | %12lu  | %6u  |\n",
  213.       locked*(tbl->cluster_size+1),
  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.