home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 225_01 / secread.c < prev    next >
Text File  |  1987-06-09  |  8KB  |  241 lines

  1. /*-----------------------------------------------------------------*/
  2. /*  PROGRAMME  ==>  SECREAD
  3.  
  4.     This will read a nominated sector on a disk and display the
  5.     information in either ASCII or Hex, depending on which is
  6.     appropriate.
  7.  
  8.     Written:  21/01/86
  9.     -------
  10.     Updated:  15/11/86
  11.     -------
  12.     Version:  1.1
  13.     -------
  14.     Copyright 1986 - Cogar Computer Services Pty. Ltd.             */
  15. /*-----------------------------------------------------------------*/
  16. #include <bdscio.h>    /* Note: Programme written for BDS C       */
  17. #include <pec.h>
  18.  
  19. main(argc, argv)
  20. int argc;
  21. char *argv[];
  22. {
  23. /*******************************************************************/
  24. /*  Space reserved for variables used in programme                 */
  25. /*******************************************************************/
  26.  
  27.     int i, j, k, FD;
  28.     char c;
  29.     int TRACK, SECTOR, PHYS_SEC;
  30.     int LAST_TRACK, LAST_SECTOR;
  31.     char dma_buf[128];
  32.     char DRIVE;        /* The active drive                */
  33.     char OLD_DRIVE;        /* The starting drive No.          */
  34.     struct fcb *FCB1;    /* The active File Control Block   */
  35.     struct dpb *THIS;    /* The Disk Parameter Block        */
  36.     char **skew_table;    /* Pointer to a pointer decl.      */
  37. /*******************************************************************/
  38. /*  Start of programme - check for the active drive                */
  39. /*******************************************************************/
  40.  
  41.     pec_clear();
  42.     header();
  43.     printf("-----------------------------------------------------\n");
  44.     printf("This programme will read any Track/Sector of a disk in\n");
  45.     printf("both hex and ASCII format.   To use it just follow the\n");
  46.     printf("prompt messages which follow.\n");
  47.     printf("-----------------------------------------------------\n");
  48.     line();
  49.  
  50.     if(argc != 2)
  51.     {
  52.         printf("Enter the DRIVE you wish to check A, B, C....");
  53.         DRIVE = toupper(getchar());
  54.     }
  55.     else if(argc == 2)
  56.         DRIVE = toupper(argv[1][0]);
  57.     lines(2);
  58. /*-----------------------------------------------------------------*/
  59. /*  Save the starting Drive No.                                    */
  60. /*-----------------------------------------------------------------*/
  61.     OLD_DRIVE = get_default() + 0x41;
  62.  
  63. /*-----------------------------------------------------------------*/
  64. /*  Select the nominated drive for all disk I/O.                   */
  65. /*-----------------------------------------------------------------*/
  66.     if(!(skew_table = seldsk(DRIVE)) )
  67.         exit();        /* Invalid disk, so quit           */
  68.     if(select_dsk(DRIVE) != 0)
  69.         exit();
  70. /*-----------------------------------------------------------------*/
  71. /*  Get the disk parameters needed in programme.                   */
  72. /*-----------------------------------------------------------------*/
  73.     THIS = dpb_adr();    /* Point to disk parameter block   */
  74.     LAST_SECTOR = THIS->DSM;    /* Starting value          */
  75.     for(i = 0; i < THIS->BSH; i++)
  76.     {
  77.         LAST_SECTOR = LAST_SECTOR + LAST_SECTOR;
  78.     }
  79.     LAST_TRACK = (LAST_SECTOR/THIS->SPT) + THIS->OFF;
  80.  
  81.     printf("Now enter the TRACK to check.\n");
  82.     printf("The value must lie in the range 0 to %d\n", LAST_TRACK);
  83. MARK1:    scanf("%d", &TRACK);
  84.     lines(2);
  85.     if(TRACK < 0 || TRACK > LAST_TRACK)
  86.     {
  87.         printf("This TRACK is out of range...try again.\n");
  88.         goto MARK1;
  89.     }
  90.     printf("Now enter the SECTOR to read.\n");
  91.     printf("The value must lie in the range 0 to %d\n", THIS->SPT -1);
  92. MARK2:    scanf("%d", &SECTOR);
  93.         lines(2);
  94.         if(SECTOR < 0 || SECTOR > THIS->SPT - 1)
  95.         {
  96.             printf("This SECTOR is out of range...try again.\n");
  97.         goto MARK2;
  98.         }
  99.  
  100. MARK3:    set_dma(&dma_buf[0]);        /* The DMA buffer in use   */
  101.  
  102.     set_trk(TRACK);                     /* The track to read      */
  103. /*-----------------------------------------------------------------*/
  104. /*  Now get the physical sector to read.                           */
  105. /*-----------------------------------------------------------------*/
  106.     PHYS_SEC = biosh(16, SECTOR, *skew_table);
  107.     set_sec(PHYS_SEC);        /* The sector to read      */
  108.     printf("For Drive No. - %c\n", DRIVE);
  109.  
  110.     if(read_sec() == 1)    /* Read sector into DMA buffer     */
  111.     {
  112.     printf("Unspecified read error, perhaps track damaged.\n");
  113. printf("Reading Track No. %d Sector - %d Physical Sec - %d\n", TRACK, SECTOR, PHYS_SEC);
  114.     line();
  115.     showsec(&dma_buf[0]);
  116.     lines(2);
  117.     }
  118. else if(read_sec() == 0)
  119. {
  120. printf("Reading Track No. %d Sector - %d Physical Sec - %d\n", TRACK, SECTOR, PHYS_SEC);
  121.     lines(2);
  122.     showsec(&dma_buf[0]);
  123.     lines(2);
  124. }
  125. /*-----------------------------------------------------------------*/
  126. /*  Now ask if there are any more sectors to display.              */
  127. /*-----------------------------------------------------------------*/
  128.     printf("If you want to see either the next sector or the previous\n");
  129.     printf("sector, enter either '+' (for next) or '-' (for last).\n");
  130.     printf("Else just enter <RETURN>\n\n");
  131.     if((c = getchar()) == '+')
  132.     {
  133.         SECTOR++;
  134.         if(SECTOR > THIS->SPT -1)
  135.         {
  136.             TRACK++;
  137.             SECTOR = 0;
  138.             if(TRACK > LAST_TRACK)
  139.                 TRACK = 0;
  140.         }
  141.         pec_clear();
  142.         header();
  143.         goto MARK3;
  144.     }
  145.     else if(c == '-')
  146.     {
  147.         SECTOR--;
  148.         if(SECTOR < 0)
  149.         {
  150.             TRACK--;
  151.             SECTOR = THIS->SPT - 1;
  152.             if(TRACK < 0)
  153.                 TRACK = LAST_TRACK;
  154.         }
  155.         pec_clear();
  156.         header();
  157.         goto MARK3;
  158.     }
  159.     else if(c == CR)
  160.         putchar('\n');
  161.     else if(c == LF)
  162.         putchar('\n');
  163.     else printf("\nUnknown response.\n\n");
  164.     printf("Do you want to change track/sector - 'Y/N' :- ");
  165.     if(toupper(getchar()) == 'Y')
  166.         main(argc, argv);
  167. /*-----------------------------------------------------------------*/
  168. /*  Restore the original Drive No.                                 */
  169. /*-----------------------------------------------------------------*/
  170.     if(select_dsk(OLD_DRIVE) != 0)
  171.     {
  172.         printf("\nUnable to return to starting drive.");
  173.         exit();
  174.     }
  175.     printf("\n\nDo you want to run another utility - Y/N.");
  176. if((FD = open("UTIL.COM", 0)) != -1 && (c = toupper(getchar())) == 'Y')
  177. {
  178.     close(FD);
  179.     exec("UTIL");
  180. }
  181. else if(c == 'N' || c == CR)
  182. printf("\n\nReturning to CP/M.");
  183. }
  184. /*******************************************************************/
  185. /*  The subroutines used in programme.                             */
  186. /*******************************************************************/
  187.  
  188. void header()
  189. {
  190.     printf("SECREAD - Version 1.1 of November, 1986\n");
  191.     printf("Copyright 1986 - Cogar Computer Services Pty. Ltd.\n\n");
  192. }
  193. /*-----------------------------------------------------------------*/
  194.  
  195. showsec( record)
  196. char *record;
  197. {
  198.     int i, j;
  199.  
  200.     for(j = 0; j < 4; j++)
  201.     {
  202. /*-----------------------------------------------------------------*/
  203. /*  Do the first 16 characters in Hex and ASCII.                   */
  204. /*-----------------------------------------------------------------*/
  205.     for(i = j*32; i < j*32 + 16; i++)
  206.     {
  207.         prt_hex( record[i]);
  208.         if((i + 1)%8 == 0)
  209.             putchar(SPACE);    /* Every 8 characters      */
  210.     }
  211.     printf("  ");        /* Two spaces                      */
  212.     for(i = j*32; i < j*32 + 16; i++)
  213.     {
  214.         if(record[i] > 31 && record[i] < 127)
  215.             putchar(record[i]);
  216.         else putchar('.');
  217.     }
  218. /*-----------------------------------------------------------------*/
  219. /*  Now do the next 16 characters in the 32-character sequence.    */
  220. /*-----------------------------------------------------------------*/
  221.     line();
  222.     for(i = j*32 + 16; i < j*32 + 32; i++)
  223.     {
  224.         prt_hex( record[i]);
  225.         if((i + 1)%8 == 0)
  226.             putchar(SPACE);    /* Every 8 characters      */
  227.     }
  228.     printf("  ");        /* Two spaces                      */
  229.     for(i = j*32 + 16; i < j*32 + 32; i++)
  230.     {
  231.         if(record[i] > 31 && record[i] < 127)
  232.             putchar(record[i]);
  233.         else putchar('.');
  234.     }
  235.     line();
  236.     }
  237. }
  238. /*-----------------------------------------------------------------*/---------*/
  239.     if(select_dsk(OLD_DRIVE) != 0)
  240.     {
  241.         printf("\nUnable to return to starting drive."