[comp.sys.ibm.pc] fix to DU

kb5mu@pnet01.CTS.COM (Paul Williamson) (05/18/87)

In the program DU by Tom Vijlbrief, recently distributed on the net,
the function comp_cluster tries to read the partition boot record to
get the number of sectors in a cluster.  Unfortunately, the method it
uses to find the boot record is incorrect.  It simply searches the
first cylinder for a sector that looks like a boot sector, in that it
starts with the byte 0xEB.  This is not sufficient in the case of a
hard disk.  On mine, it finds an old boot sector left over from a
previous formatting with DOS 2.0, instead of the current DOS 3.1 boot
sector.  If you're going to look for the boot sector, the Master Boot
Record of the hard disk must be consulted to find out which sector
contains the boot record for the currect partition.

There is an easier and better way.  DOS function 1Ch returns the
needed information, plus the sector size so 512 needn't be hard-coded
into the program.  The following is a replacement for the function
comp_cluster() in the program du.c.  In addition, the routines
readsect() and max_head() and the associated #defines (BLOCK_SIZE, DISK,
READ_SECT, RESET_DISK, FLOP_RETRIES, GET_PARAM) can be deleted, along
with the declaration for sector[BLOCK_SIZE].

This modification may be distributed freely.

int comp_cluster(drive_nr)

int     drive_nr;
{
  int    nr_sectors;
  int    block_size;
  union  REGS  regs;
  struct SREGS sregs;

  segread(&sregs);                   /* set up input segment registers */
  regs.h.ah = 0x1C;                  /* Get allocation info for drive */
  regs.h.dl = drive_nr + 1;          /* zero is used for default here */
  intdosx(&regs, &regs, &sregs);     /* get info */
                /* intdosx is needed to restore DS after the call. */
  nr_sectors = regs.h.al;            /* this is what we wanted */
  block_size = regs.x.cx;

  return(nr_sectors * block_size);
}

        Paul Williamson
        ... !sdcsvax!macomw!williams
  or    ... !sdcsvax!crash!pnet01!kb5mu