home *** CD-ROM | disk | FTP | other *** search
- /* ----------------------------------------------------------------------
- Copyright (c) 1988 by Edward V Dong, All Rights Reserved.
-
- This is a code fragment, complete unto itself, to determine the total
- number of sectors on a disk (any disk, hard or floppy), and do a simple
- verification of the disk by simply reading each and every sector on the
- disk.
-
- Compiler: Turbo C version 1.5. Should also work with version 1.0.
- Although I have not yet received TC ver 2.0,
- this should still work with ver 2.0.
-
- The key Turbo C routine used is "absread", summarized as follows.
- int absread(drive,nsects,sectno,buffer)
- int drive,int nsects,int sectno,void *buffer;
- - reads disk absolute logical sectors
- - drive = 0(A), 1(B), etc
- - nsects = # of sectors to read
- - sectno = beginning logical sector number
- - buffer = address of buffer
- - returns 0 if OK; -1 on error; 'errno' = DOS error number
- - needs dos.h
- Logical sectors start with 0. What the manual doesn't tell you is
- that DOS (and Turbo C) doesn't really manage the stacks very well,
- when doing "absread". The parameters passed to 'absread' should be
- global or static.
- ----------------------------------------------------------------------- */
-
- #include <stdio.h> /* standard I/O library */
- #include <dos.h> /* you need this to use 'absread' */
-
- typedef struct xyyx /* -- defines the structure of the boot sector -- */
- { char jump[3]; /* NEAR jump to start of boot code */
- char OEM[8]; /* OEM & version number */
- int SectSize; /* bytes per sector */
- char ClustSize; /* sectors per cluster */
- int ResSects; /* reserved sectors (sectors before 1st FAT) */
- char FatCnt; /* number of FATs */
- int RootSize; /* max no of root directory entries (32 bytes) */
- unsigned TotSecs; /* total number of sectors in media */
- char Media; /* media descriptor */
- int FatSize; /* number of sectors in one FAT */
- int TrkSecs; /* sectors per track/cylinder */
- int HeadCnt; /* number of read/write heads */
- int HidnSecs; /* number of hidden sectors */
- } BOOT, *BOOTPTR;
-
- char buffer[16384]; /* arbitrary buffer. 16K allows 1.2M disks */
- int error, count; /* parameters used with 'absread' */
- int nsects;
- unsigned NumSect; /* actual no. of sectors */
-
- main()
- {
- char string[80], ch;
- int drive;
- struct dfree dfree2;
- BOOTPTR bootptr;
- static char *bootinfo[12] =
- { " OEM & Version Number: ",
- " Bytes Per Sector: ",
- " Sectors Per Cluster: ",
- " Reserved Sectors: ",
- " FATs: ",
- " Max Root Dir Entries: ",
- " Total Sectors: ",
- " Media Descriptor: ",
- " Sectors Per FAT: ",
- " Sectors Per Track: ",
- " Read/Write Heads: ",
- " Hidden Sectors: "
- };
-
- /* setup */
- printf("Simple-Minded Diskette Test (c) 1988 by Edward V. Dong\n");
- printf("Drive to test? (A, B, C...Z): ");
- ch = getche();
- drive = toupper(ch) - 'A' + 1;
- if ((drive < 1) || (drive > 26)) /* simple sanity check */
- exit(1);
-
- /* get number of sectors on disk via 'getdfree' */
- getdfree(drive,&dfree2);
- NumSect = dfree2.df_total * dfree2.df_sclus;
- printf("\nNumber of sectors = %u\n",NumSect);
-
- /* convert string into drive number */
- drive -= 1;
- absread(drive,1,0,buffer);
- bootptr = (BOOTPTR)&buffer[0];
-
- /* The total number of sectors should be higher than the number reported by
- 'getdfree', because 'getdfree' doesn't include boot sector, FAT sectors,
- and DIR sectors. */
-
- /* We print out the disk information. Interesting, but not useful. */
- count = 0;
- setmem(string,sizeof(string),0);
- memcpy(string,bootptr->OEM,8);
- printf("%s%s\n",bootinfo[count++],string);
- printf("%s%d\n",bootinfo[count++],bootptr->SectSize);
- printf("%s%d\n",bootinfo[count++],bootptr->ClustSize);
- printf("%s%d\n",bootinfo[count++],bootptr->ResSects);
- printf("%s%d\n",bootinfo[count++],bootptr->FatCnt);
- printf("%s%d\n",bootinfo[count++],bootptr->RootSize);
- printf("%s%u\n",bootinfo[count++],bootptr->TotSecs);
- printf("%s%x\n",bootinfo[count++],bootptr->Media);
- printf("%s%d\n",bootinfo[count++],bootptr->FatSize);
- printf("%s%d\n",bootinfo[count++],bootptr->TrkSecs);
- printf("%s%d\n",bootinfo[count++],bootptr->HeadCnt);
- printf("%s%d\n",bootinfo[count++],bootptr->HidnSecs);
- printf("\n");
-
- NumSect = bootptr->TotSecs;
- count = 0;
-
- /* This is where we read the disk, sector by sector. The theory is that
- if there is no read error for a sector, then it's probably OK. A
- rigorous test would be to write to that sector, verify that it's been
- changed appropriately by reading it back, and restoring (if needed)
- the original contents of the sector. But this should get 90% of errors. */
-
- nsects = sizeof(buffer) / (bootptr->SectSize);
- printf("Verify disk? (y/n) [N] ");
- if (toupper(getch())!='Y') exit(0);
-
- ReadDisk:
- error = absread(drive,nsects,count,buffer);
- if (!error)
- printf("Reading sectors %05d-%05d...\r",count,count+nsects);
- else if (count < (NumSect)) printf("\nBad sector %d\n",count);
- if (count < (NumSect))
- { count += nsects;
- if (count > (unsigned)(NumSect))
- count = (unsigned)(NumSect) - 2;
- goto ReadDisk;
- }
- }
-