home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / dskutl / reform16.arc / REFORMAT.PAS < prev    next >
Pascal/Delphi Source File  |  1990-03-03  |  24KB  |  708 lines

  1. {
  2. To:  INFO-IBMPC-REQUEST@USC-ISIB.ARPA
  3. From:  U001222%HNYKUN11.BITNET@WISCVM.WISC.EDU
  4. Subject: reformat.pas
  5. }
  6. PROGRAM reformat;
  7. {
  8.   Program to reformat any disk attached to an Olivetti PC or compatible.
  9.   The progam will work well on any MS/PC-DOS machine running under DOS
  10.   versions 2.00 up to and including 3.10. Fixed disks up to 32 Meg.
  11.  
  12.     V1.50 Extensions made for big FATs. WORD.INC included for handling of
  13.     word integers. MS-DOS function INT21 / 32H used for drive information.
  14.     Corrected the problem of DOS losing track of the current directory.
  15.  
  16.     The program has been tested under DOS 2.00 thru 3.10
  17.     It will probably work under DOS 3.20 too, but this version will not
  18.     run when you try to do so. If you want to test it with DOS 3.20, look
  19.     for the string DOS_Versions (it's somewhere near the end of the program)
  20.     in this program, and include that version number into the test.
  21.     Please let the author know whether you had success or not.
  22.  
  23.     Jos Wennmacker                                    July 86.
  24.     Universitair Rekencentrum
  25.     Geert Grooteplein Zuid 41
  26.     6525 GA Nijmegen
  27.     The Netherlands
  28.  
  29.     BITNET address: U015415 at HNYKUN22
  30.  
  31.     v1.6, 23 Nov 89  David Kirschbaum, Toad Hall
  32.     - Recoded for TP v5.0
  33.  
  34.  
  35.     V1.5  Jos added new 16-bit and word code, handling up to 30Meg
  36.     hard disks, etc.  I cleaned up the integer math inline procedures
  37.     a little.
  38.     David Kirschbaum, Toad Hall
  39.  
  40.     V1.23 Added FRAMER.INC, WRITEF.INC for framing, fast screen write,
  41.     fast screen line clear.  Tightened up displays a little.
  42.     This version is now VERY PC-specific.  Generic MS-DOS functions
  43.     for screen stuff to follow.
  44.     David Kirschbaum, Toad Hall.
  45.  
  46.     V1.22 Added inline code to replace the INT25.ASM and INT26.ASM
  47.     files.
  48.     David Kirschbaum, Toad Hall, 3 May 86
  49.  
  50.     V1.21(mod) not publicly released. Changes made by Rick Watson to let
  51.     the program recognize the big (16 bit) FATs. He made it run on 20 Meggers
  52.     under DOS 3.x. Martin Hobson made it run on 30 Meggers under DOS 3.x.
  53.     May 86.
  54.  
  55.     V1.21 Small corrections made to avoid Turbo integer overflow. (Hey,
  56.     Borland, what about a WORD type in Turbo, would be very usefull using
  57.     the MSDOS features!).
  58.  
  59.     V1.20 First release to the public domain.
  60.     What started as a favour to a friend grew into a nice program.
  61.     Do you like it? Let me know about it! If I know many people to use it,
  62.     I'll keep it up to date, and send you new releases.
  63.     Jos Wennmacker
  64.  
  65. }
  66. {$A+}         {word alignment to be nice}
  67. {$B-}         {short-circuit Boolean Eval}
  68. {$D-}         {no debug}
  69. {$F-}         {no far calls}
  70. {$L-}         {no local information}
  71. {$R-}         {no range checking}
  72. {$S-}         {no stack checking}
  73. {$V-}         {no string checking}
  74.  
  75. Uses  Dos,Crt;  {v1.6}
  76.  
  77. {-------------------------------- Global types -------------------------------}
  78.  
  79. TYPE
  80.  
  81. {------------------- types for word arithmetic  see REFORMAT.IN1 -------------}
  82.  
  83.   Relational_Operator =      (Eq, Gt, Lt, Ne, Ge, Le);
  84.  
  85.  
  86. {----------------------------- Bootrecord layout -----------------------------}
  87.  
  88.   Boot                =                { the layout of the DOS boot record}
  89.     RECORD
  90.       Jump:                  ARRAY[0..2] OF Byte; { Near jump to boot code}
  91.       OEM :                  ARRAY[0..7] OF CHAR; { eight character OEM name}
  92.       sectorSize:            word;     { number of bytes in one sector}
  93.       clusterSize:           Byte;     { sectors per cluster}
  94.       reservedSectors:       word;     { DOS boot code sector(s) typically 1}
  95.       numberOfFATs:          Byte;     { usually 2, but often 1 for RAM disks}
  96.       rootDirSize,                     { maximum entries in root directory}
  97.       totalSectors:          word;     { total sectors in the logical image}
  98. { A limited description of the drive
  99.   bit 0 = 1: twosided, 0: not two-sided
  100.   bit 1 = 1: 8-sector, 0: not 8-sector
  101.   bit 2 = 1: removable 0: not removable
  102.   bits 3-7 must be set to 1
  103. }
  104.       mediaDescriptor:       Byte;
  105.       fatSize,                         { number of sectors in each FAT copy}
  106.       trackSize,                       { number of sectors per track}
  107.       numberOfHeads,                   { number of heads}
  108. { sectors hidden from DOS, preceding the DOS partition: including the
  109.   master boot record and any non-DOS partitions
  110. }
  111.       hiddenSectors:         word;
  112.     END;
  113.  
  114. {---------------- Layout of DOS function 32h disk parameter block ------------}
  115.  
  116.   Parms_32            =     ^Parameter_table;
  117.  
  118.   Parameter_Table     =                { layout of the DOS function 32 table}
  119.     RECORD
  120.       assignedDisk,                    { 0 = A, 1 = B, ...}
  121.       altAD:                 Byte;     { same as above, but 0 for RAM disk}
  122.       sectorSize:            word;     { number of bytes in one sector}
  123.       clusterSize_1,                   { sectors per cluster minus 1}
  124.       numberOfHeads_1:       Byte;     { number of heads minus 1}
  125.       reservedSectors:       word;     { DOS boot code sector(s) typically 1}
  126.       numberOfFATs:          Byte;     { usually 2, but often 1 for RAM disks}
  127.       rootDirSize,                     { maximum entries in root directory}
  128.       firstDataSector,                 { first sector for data storage}
  129.       totalDataClusters_1:   word;     { total data clusters plus 1}
  130.       fatSize:               Byte;     { number of sectors in each FAT copy}
  131.       firstDirectorySector:  word;     { first sector of the root directory}
  132. (* v1.6
  133.       DeviceDriverAddress:  ^byte;     { Far (offset, segment) address of the
  134.                                          DOS device driver for the drive}
  135. *)
  136.       DeviceDriverAddress:   Pointer;  {v1.6}
  137. { A limited description of the drive
  138.   only the low order byte is used:
  139.   bit 0 = 1: twosided, 0: not two-sided
  140.   bit 1 = 1: 8-sector, 0: not 8-sector
  141.   bit 2 = 1: removable 0: not removable
  142.   bits 3-7 must be set to 1
  143. }
  144.       mediaDescriptor:       word;
  145.       NextParameterTable:    Parms_32; { far pointer to next disk table}
  146. { Starting cluster of current working directory.
  147.   According to Glenn Roberts in his May 86 PC Tech Journal article
  148.   "Finding Disk Parameters" this would only hold for PC DOS 2.00
  149. }
  150.       currentDirCluster:     word;
  151.       CurrentDirName:        ARRAY [0..63] OF CHAR
  152. { The name of the current working directory. This does not hold for
  153.   DOS 3.x versions. Only for 2.00
  154. }
  155.     END;
  156.  
  157. {-------- Long byte and integerarays for Disk Transfer Area, Fats etc. -------}
  158.  
  159.   intArray   = ARRAY[0..32766] OF Word;  {v1.6 INTEGER;}
  160.  
  161.   buffer     = ARRAY[0..32766] OF Byte;
  162.  
  163. {--------------------- Element of tree of directory entries ------------------}
  164.  
  165.   DirectoryPointer = ^DirectoryEntry;
  166.  
  167. (*v1.6  longInteger      = ARRAY[0..1] OF INTEGER; *)
  168.  
  169.   DirectoryEntry =
  170.     RECORD
  171. { name + extension w/o period
  172.   If the first char is $00, then this entry was never used.
  173.   Also the next entries in this directory have never been used.
  174.   If the first char is $E5, then this entry was erased.
  175. }
  176.       EntryName:          ARRAY[0..10] OF CHAR;
  177. { attribute of entry:
  178.   bit      if set means
  179.    0       read only file
  180.    1       hidden file
  181.    2       system file
  182.    3       volume label
  183.    4       subdirectory
  184.    5       archive bit
  185. }
  186.       attribute:          Byte;
  187.       reserved:           ARRAY[1..10] OF Byte; { for future use}
  188. { mapped in the bits:
  189.   <     hh     > <    mm    > <   xx  >
  190.   15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  191.   where:
  192.   hh  binary number of hours [0..23]
  193.   mm  binary number of minutes [0..59]
  194.   xx  binary number of two-second
  195.       increments [0..28]
  196. }
  197.       timeLastUpdated:    word;
  198. { mapped in the bits
  199.   <        yy       > < mm  > <   dd  >
  200.   15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  201.   where:
  202.   yy  binary number of years [0..119]
  203.       (1980-2099)
  204.   mm  binary number of month [1..129]
  205.   dd  binary number of day [1..31]
  206. }
  207.       dateLastUpdated:    word;
  208.       startingCluster:    word;        { index of the file's first cluster}
  209.       fileSize:           longint;     { filesize in bytes  v1.6}
  210.       newStartingCluster: word;
  211.       Next,
  212.       SubDirectory:       DirectoryPointer;
  213.     END;
  214.  
  215. {------------------------------- Helps for I/O -------------------------------}
  216.  
  217.   WorkString     = STRING[255];
  218.   Line           = STRING[78];
  219.   Str40          = STRING[40];
  220.  
  221.   Disk_Activity  = (reading,writing);
  222.  
  223. {-------------------------------- constants ----------------------------------}
  224.  
  225. CONST
  226.  
  227.   Header:          STRING[37] = ' REFORMAT: an original JOS disk tool ';
  228.   Version:         STRING[10] = 'Ver: 1.60 ';
  229.   PDS:             STRING[24] = ' Public Domain Software ';
  230.   EnterStr:        STRING[28] = 'Press Enter to return to DOS';
  231.   HiSpace:         STRING[01] = #160;  {WriteF blank line flag}
  232.  
  233. {--------------------------- FAT special values ------------------------------}
  234.  
  235.   unused:          word       = $0000; { we use typed constants, because}
  236.   reservedMinimum: word       = $0FF0; { we might have to change these values}
  237.   reservedMaximum: word       = $0FF6; { the values entered here are for}
  238.   badCluster:      word       = $0FF7; { the old, small FAT format of 12 bits}
  239.   lastMinimum:     word       = $0FF8; { per entry. The new FAT format uses}
  240.   lastMaximum:     word       = $0FFF; { 16 bit-entries. See the procedure}
  241.   lastNormal:      word       = $0FFF; { Getinformation.}
  242.  
  243. {---------------------- file attribute byte values----------------------------}
  244.  
  245.   READONLY         = $01;              { file attribute byte}
  246.   HIDDENFILE       = $02;
  247.   SYSTEMFILE       = $04;
  248.   VOLUMELABEL      = $08;
  249.   SUBDIRECTORY     = $10;
  250.   ARCHIVE          = $20;
  251.  
  252.   NEVERUSED        = $00;              { first byte of file's name}
  253.   ERASED           = $E5;
  254.  
  255.   FIXEDDISK        = $F8;              { some values of mediaDescriptor}
  256.   DUAL8SECTOR      = $FF;
  257.   SINGLE8SECTOR    = $FE;
  258.   DUAL9SECTOR      = $FD;
  259.   SINGLE9SECTOR    = $FC;
  260.  
  261. {---------- constants for the screen layout, for FRAMER and WRITEF procedures}
  262.  
  263.   MIDDLEFIELDY     = 18;               { little window in the middle}
  264.   INPUTFIELDX      = 22;               { User Input Field}
  265.   INPUTFIELDY      = 20;
  266.   LOGFIELDX        = 22;               { Activity Logging}
  267.   LOGFIELDY        = 21;
  268.   WARNINGFIELDX    = 22;               { Warning Message Field}
  269.   WARNINGFIELDY    = 22;
  270.   ERRORFIELDX      = 22;               { Error Message Field}
  271.   ERRORFIELDY      = 23;
  272.   DISASTERFIELDX   = 22;               { Disaster Message Field}
  273.   DISASTERFIELDY   = 24;
  274.  
  275. {-------- Drive characteristics and constants communications block -----------}
  276.  
  277. VAR
  278.  
  279.   DriveLetter:          CHAR;
  280.   driveNumber:          Byte;
  281.   defaultDrive:         Byte;
  282.  
  283.   totalSectors,
  284.   sectorSize:           word;
  285.   media:                Byte;
  286.   trackSize,
  287.   numberOfHeads,
  288.   hiddenSectors,
  289.   reservedSectors,
  290.   rootDirSize:          word;
  291.   numberOfFATs:         Byte;
  292.   fatSize:              word;
  293.   clusterSize:          Byte;
  294.   firstDataSector,
  295.   firstDirectorySector,
  296.   totalDataClusters,
  297.   freeClusters,
  298.   badClusters,
  299.   usedClusters:         word;
  300.   DeviceDriverAddress:  Pointer;  {v1.6 ^byte;}
  301.   CurrentDirectory:     ARRAY[0..63] OF CHAR;
  302.   DiskLabel:            STRING[11];
  303.   diskCreationDate,
  304.   diskCreationTime:     word;
  305.   OEM:                  ARRAY[0..7] OF CHAR;
  306.   assignedDisk,
  307.   altAD:                Byte;
  308.  
  309.   BigFAT:               BOOLEAN;
  310.  
  311.   firstFATsector,
  312.   directorySectors:     word;
  313.  
  314. {----------------------------- Global variables ------------------------------}
  315.  
  316.   Register:             Registers;  {v1.6 Regpack;}
  317.  
  318.   oldFATindex,
  319.   newFATindex:          word;
  320.   totalFiles,
  321.   hiddenFiles,
  322.   hiddenDirectories,
  323.   inRootDirectory,
  324.   inSubdirectories,
  325.   nonContiguousFiles,
  326.   subdirectories,
  327.   movedClusters,
  328.   clustersToMove,
  329.   lostClusters,
  330.   errors,
  331.   count:                Word;  {v1.6 INTEGER;}
  332.  
  333.   SAVEaddress,
  334.   DTAddress:           ^buffer;
  335.  
  336.   PermutationAddress,
  337.   NewFATAddress,
  338.   OldFATAddress:       ^intArray;
  339.  
  340.   RootDir:              DirectoryPointer;
  341.  
  342. {---------------------------------- miscellaneous vars -----------------------}
  343.  
  344.   hour,
  345.   min,
  346.   sec,
  347.   frac:                 Word;  {v1.6 INTEGER;}
  348.   time:                 REAL;
  349.  
  350.   movedFieldX,
  351.   movedFieldY:          Word;  {v1.6 INTEGER;}
  352.  
  353.   Instr:                CHAR;
  354.   AlreadyWritten:       BOOLEAN;
  355.   TmpStr:               STRING[2];
  356.   NrStr,
  357.   S40:                  Str40;
  358.   Legals:               STRING[3];
  359.   Parms:                Line;
  360.  
  361. {-----------------------------------------------------------------------------}
  362. { I REFORMAT.IN1}  {Nijmegen .. word arithmetic v1.6 no longer needed}
  363. {$I REFORMAT.IN2}  {Toad Hall.. InLine code FOR Int25 AND Int26}
  364. {$I REFORMAT.IN3}  {Toad Hall.. fast screen framer, FRAMER.INC}
  365. {$I REFORMAT.IN4}  {Toad Hall.. better screen writing AND clearing, WRITEF.INC}
  366. {-----------------------------------------------------------------------------}
  367.  
  368. {----------------------- general screen I/O routines -------------------------}
  369.  
  370. PROCEDURE Beep;
  371.   BEGIN
  372.     WRITE(CHR(7));
  373.   END;  { of Beep}
  374.  
  375. PROCEDURE WriteLog(Text: Line);
  376.   BEGIN
  377.     WriteF(LOGFIELDX, LOGFIELDY, HiSpace);
  378.     WriteF(LOGFIELDX, LOGFIELDY, Text);
  379.   END;  { of WriteLog}
  380.  
  381. PROCEDURE WriteWarning(Text: Line);
  382.   BEGIN
  383.      WriteF(WARNINGFIELDX, WARNINGFIELDY, HiSpace);
  384.      WriteF(WARNINGFIELDX, WARNINGFIELDY, Text);
  385.   END;  { of WriteWarning}
  386.  
  387. PROCEDURE WriteError(Text: Line);
  388.   BEGIN
  389.     WriteF(ERRORFIELDX, ERRORFIELDY, HiSpace);
  390.     WriteF(ERRORFIELDX, ERRORFIELDY, Text);
  391.   END;  { of WriteError}
  392.  
  393. PROCEDURE WriteDisaster(Text: Line);
  394.   BEGIN
  395.     WriteF(DISASTERFIELDX, DISASTERFIELDY, HiSpace);
  396.     WriteF(DISASTERFIELDX, DISASTERFIELDY, Text);
  397.   END;  { of WriteDisaster}
  398.  
  399. PROCEDURE BlankFields;
  400.   BEGIN
  401.     WriteError(' ');
  402.     WriteWarning(' ');
  403.     WriteDisaster(' ');
  404.     IF Instr = 'I' THEN Register.flags := 0;
  405.   END;  { of BlankFields}
  406.  
  407. PROCEDURE GetInput(Prompt: Str40; VAR Instr: CHAR);
  408.   BEGIN
  409.     WriteF(INPUTFIELDX, INPUTFIELDY, HiSpace);        { really only needs 1}
  410.     WriteF(INPUTFIELDX, INPUTFIELDY, Prompt + ': ');
  411.     Beep;
  412. (* v1.6
  413.     REPEAT UNTIL KeyPressed;
  414.     READ(Kbd, Instr);
  415.     Instr := UpCase(Instr);
  416. *)
  417.     Instr := UpCase(ReadKey);  {v1.6}
  418.     WRITE(Instr);
  419.   END;  { of GetInput}
  420.  
  421. PROCEDURE Exeunt(Prompt: Str40);       { we do this a lot}
  422.   BEGIN
  423.     GetInput(Prompt, Instr);
  424.     Height := 25;
  425.     WriteF(1, 24, HiSpace);            { clear bottom line and leave screen
  426.                                          for user to contemplate}
  427.     HALT;
  428.   END;  { of Exeunt}
  429.  
  430. {---------------------------- timer control routines -------------------------}
  431.  
  432. PROCEDURE StartTimer;
  433. {
  434.   We want to let the user know how long REFORMAT took to do the job.
  435. }
  436.   BEGIN
  437. (* In Turbo:
  438.     with Register do begin
  439.          ah := $2C;
  440.          msdos(Register);
  441.          hour := hi(cx);
  442.          min  := lo(cx);
  443.          sec  := hi(dx);
  444.          frac := lo(dx);
  445.     end;
  446. *)
  447. {Toad Hall note:  tighter, quicker inline}
  448.   InLine(
  449.   $B4/$2C       {  mov  ah,$2C}
  450.   /$CD/$21      {  int  $21}
  451.   /$31/$C0      {  xor  ax,ax       ;clear msb}
  452.   /$86/$E8      {  xchg al,ch       ;the hour}
  453.   /$A3/>HOUR    {  mov  [>hour],ax}
  454.   /$86/$C8      {  xchg al,cl       ;the minute}
  455.   /$A3/>MIN     {  mov  [>min],ax}
  456.   /$86/$F0      {  xchg al,dh       ;the second}
  457.   /$A3/>SEC     {  mov [>sec],ax}
  458.   /$86/$D0      {  xchg al,dl       ;the fraction}
  459.   /$A3/>FRAC    {  mov [>frac],ax}
  460. );
  461.   END;  { of StartTimer}
  462.  
  463. PROCEDURE StopTimer;
  464. {
  465.   We want to let the user know how long REFORMAT took to do the job.
  466. }
  467.   BEGIN
  468. (* In Turbo:
  469.     with Register do begin
  470.       ah := $2C;
  471.       msdos(Register);
  472.       frac := lo(dx) - frac;
  473.       if frac < 0 then begin
  474.          frac := frac + 100;
  475.          sec  := succ(sec);
  476.        end;
  477.  
  478.        sec  := hi(dx) - sec;
  479.        if sec < 0 then begin
  480.          sec  := sec + 60;
  481.          min  := succ(min);
  482.        end
  483.        else if sec >= 60 then begin
  484.          sec := sec - 60;
  485.          min := pred(min);
  486.        end;
  487.  
  488.        min  := lo(cx) - min;
  489.        if min < 0 then begin
  490.          min  := min + 60;
  491.          hour := succ(hour);
  492.        end
  493.        else if min >= 60 then begin
  494.          min  := min - 60;
  495.          hour := pred(hour);
  496.        end;
  497.        hour := hi(cx) - hour;
  498.     end;
  499. *)
  500. {Toad Hall Note:  Tighter, faster inline.  You won't BELIEVE
  501.  what a kludge Turbo makes of the above routine.
  502. }
  503. InLine(
  504.   $B4/$2C        {  mov  ah,$2C}
  505.   /$CD/$21       {  int  $21}
  506.   /$BB/>FRAC     {  mov  bx,>frac    ;fraction offset}
  507.   /$BE/>SEC      {  mov  si,>sec     ;seconds offset}
  508.   /$31/$C0       {  xor  ax,ax       ;clear msb}
  509.   /$86/$D0       {  xchg al,dl       ;get fraction}
  510.   /$2B/$07       {  sub  ax,[bx]     ;frac = lo(dx)-frac}
  511.   /$73/$05       {  jnc  S1          ;no carry, so not <0}
  512.   /$05/$64/$00   {  add  ax,100      ;<0, adjust}
  513.   /$FF/$04       {  inc  word [si]   ;bump seconds}
  514.                  {S1:}
  515.   /$89/$07       {  mov  [bx],ax     ;save fraction}
  516.   /$BB/>MIN      {  mov  bx,>min     ;get Minute offset}
  517.   /$31/$C0       {  xor  ax,ax       ;clear msb}
  518.   /$BF/$3C/$00   {  mov  di,60       ;constant}
  519.   /$86/$F0       {  xchg al,dh       ;get seconds}
  520.   /$2B/$04       {  sub  ax,[si]     ;sec = hi(dx)-sec}
  521.   /$73/$06       {  jnc  S2          ;not <0}
  522.   /$01/$F8       {  add  ax,di       ;<0, adjust}
  523.   /$FF/$07       {  inc  word [bx]   ;min=min+1}
  524.   /$EB/$08       {  jmp short S3}
  525.                  {S2:}
  526.   /$39/$F8       {  cmp  ax,di       ;>=60?}
  527.   /$72/$04       {  jb   S3          ; nope}
  528.   /$29/$F8       {  sub  ax,di       ; sec=sec-60}
  529.   /$FF/$0F       {  dec  word [bx]   ;min=min-1}
  530.                  {S3:}
  531.   /$89/$04       {  mov  [si],ax     ;save seconds}
  532.   /$BE/>HOUR     {  mov  si,>hour    ;hour offset}
  533.   /$31/$C0       {  xor  ax,ax       ;clear msb}
  534.   /$86/$C8       {  xchg al,cl       ;get minutes}
  535.   /$2B/$07       {  sub  ax,[bx]     ;min=lo(cx)-min}
  536.   /$73/$06       {  jnc  S4          ; not below 0}
  537.   /$01/$F8       {  add  ax,di       ;min=min+60}
  538.   /$FF/$04       {  inc  word [si]   ;hour=hour+1}
  539.   /$EB/$08       {  jmp short S5}
  540.                  {S4:}
  541.   /$39/$F8       {  cmp  ax,di       ;>=60?}
  542.   /$72/$04       {  jb   S5          ; nope}
  543.   /$29/$F8       {  sub  ax,di       ;min=min-60}
  544.   /$FF/$0C       {  dec  word [si]   ;hour=hour-1}
  545.                  {S5:}
  546.   /$89/$07       {  mov  [bx],ax     ;save minutes}
  547.   /$31/$C0       {  xor  ax,ax       ;clear msb}
  548.   /$86/$E8       {  xchg al,ch       ;get hour}
  549.   /$2B/$04       {  sub  ax,[si]     ;hour=hi(cx)-hour}
  550.   /$89/$04       {  mov  [si],ax}
  551. );
  552.   END;  { of StopTimer}
  553.  
  554. {$I REFORMAT.IN5}   {Nijmegen  .. disk, FAT, directory, misc. routines}
  555.  
  556.  
  557. {------------------------------ MAIN PROGRAM ---------------------------------}
  558.  
  559. BEGIN
  560. { v1.6 Permitting user override of precautionary abort
  561.    for DOS versions above 3.10
  562.  
  563.   Check_DOS_Version;
  564. }
  565.   color := LIGHTGRAY;                  { initialize global screen attribute   }
  566.  
  567.   IF paramcount = 0                    { exactly one parameter allowed }
  568.   THEN Parms := ''
  569.   ELSE IF paramcount = 1
  570.   THEN Parms := ParamStr(1)
  571.   ELSE Parms := 'in error';       { force error message }
  572.  
  573.   IF LENGTH(Parms) <> 0
  574.     THEN IF Parms[1] = '?'                { display explanation screens }
  575.     THEN BEGIN
  576.       WriteDoc;
  577.       HALT;
  578.     END
  579.     ELSE IF (  LENGTH(Parms) > 2 )   { we accept only d or d: }
  580.          OR ( (LENGTH(Parms) = 2) AND (Parms[2] <> ':') )
  581.     THEN BEGIN
  582.       WRITELN;
  583.       WRITELN('Invalid parameter: REFORMAT d: or ?');
  584.       HALT;
  585.     END
  586.     ELSE Instr := Parms[1]
  587.   ELSE Instr := ' ';
  588.  
  589.   InitCounters;
  590.   InitScreen;
  591.  
  592.   GetInformation;
  593. {
  594.   The TURBO procedures getmem, move and fillchar will behave the way we want
  595.   in case the integer parameters are greater than 32767. (i.e. negative
  596.   when interpreted as integers). Block allocation, block move and block
  597.   filling can handle as many as 65535 bytes. Allocation however should
  598.   never be done in quantities of more than 65521 bytes, because the
  599.   pointer values are normalized, which means that the offset will be
  600.   in the range 0 through 15. Segments must begin on a paragraph boundary.
  601. }
  602. (* v1.6
  603.   GetMem(  OldFATaddress,  W_mul(totalDataClusters + 2, 2));
  604.   GetMem(  DTAddress,      W_mul(sectorSize, fatSize));
  605. *)
  606.   GetMem(  OldFATaddress, (totalDataClusters + 2) ShL 1);  {v1.6}
  607.   GetMem(  DTAddress,     (sectorSize * fatSize) );        {v1.6}
  608.   ReadFat( OldFATaddress^, DTAddress^);
  609.  
  610. (* v1.6
  611.   GetMem( NewFATaddress,      W_mul(totalDataClusters + 2, 2));
  612.   GetMem( PermutationAddress, W_mul(totalDataClusters + 2, 2));
  613.   GetMem( SAVEaddress,        W_mul(sectorSize, clusterSize));
  614. *)
  615.   GetMem( NewFATaddress,      (totalDataClusters + 2) ShL 1);  {v1.6}
  616.   GetMem( PermutationAddress, (totalDataClusters + 2) ShL 1);  {v1.6}
  617.   GetMem( SAVEaddress,        sectorSize * clusterSize);       {v1.6}
  618.  
  619.   ReadDirectories( DTAddress^ );
  620.  
  621.   Move( OldFATaddress^,
  622. (* v1.6
  623.         NewFATaddress^, W_mul(totalDataClusters + 2, 2));
  624. *)
  625.         NewFATaddress^, (totalDataClusters + 2) ShL 1);  {v1.6}
  626.  
  627.   CheckDisk( NewFATaddress^,  RootDir);
  628. (* v1.6
  629.   FillChar( NewFATaddress^,   W_mul(totalDataClusters + 2, 2), 0);
  630. *)
  631.   FillChar( NewFATaddress^,   (totalDataClusters + 2) ShL 1, 0);  {v1.6}
  632.   FOR count := 0 TO SUCC(totalDataClusters) DO
  633.     PermutationAddress^[count] := count;
  634.   Move(OldFATaddress^, NewFATaddress^, 4);
  635.   RemakeFAT(OldFATaddress^, NewFATaddress^,
  636.             PermutationAddress^, RootDir, 0, 0);
  637.  
  638.   LinkFreeClusters(OldFATaddress^, NewFATaddress^);
  639.   CountClustersToMove(PermutationAddress^);
  640.  
  641.   WriteStatistics;
  642.  
  643. {v1.6 Up to now, we've done nothing destructive and maybe have shown
  644.  the user something informative (like will this sucker work with his
  645.  DOS 4.01?) or whatever.
  646.  NOW we ask him to take his life in his own hands...
  647. }
  648.  
  649.   Check_DOS_Version;             {v1.6 moved down here.
  650.                                   may abort and die.}
  651.  
  652.   IF nonContiguousFiles = 0
  653.   THEN IF clustersToMove <> 0
  654.   THEN BEGIN
  655.     color := $70;              { inverse }
  656.     WriteF(0, middleFieldY,
  657.            'All files are contiguous, continue anyway?');
  658.     color := LIGHTGRAY;
  659.     REPEAT
  660.       GetInput('Continue? (Y/N)',Instr);
  661.     UNTIL Instr IN ['Y','N'];
  662.     IF Instr = 'N'
  663.     THEN Exeunt(EnterStr);
  664.   END
  665.   ELSE BEGIN
  666.     WriteLog('Cannot make your disk more optimized!');
  667.     Exeunt(EnterStr);
  668.   END
  669.   ELSE IF clustersToMove = 0
  670.   THEN BEGIN
  671.     WriteLog('Bad space makes file(s) noncontiguous.');
  672.     Exeunt(EnterStr);
  673.   END;
  674.  
  675.   IF media = FIXEDDISK
  676.   THEN BEGIN
  677.     color := $70;                   { inverse }
  678.     WriteF(0, middleFieldY,
  679.            'Fixed disk: did you uninstall all protected software?');
  680.     color := LIGHTGRAY;
  681.     REPEAT
  682.       GetInput('Continue? (Y/N)',Instr);
  683.     UNTIL Instr IN ['Y','N'];
  684.     IF Instr = 'N'
  685.     THEN Exeunt(EnterStr);
  686.   END;
  687.  
  688.   ResetDisk;
  689.   StartTimer;
  690.   WriteFAT(NewFATaddress^, DTAddress^);
  691.   WriteDirectories(DTAddress^);
  692.   DoIt(PermutationAddress^, DTAddress^, SAVEaddress^);
  693.   ResetDisk;
  694.   ResetSubdirectory;
  695.   StopTimer;
  696.   STR(hour:2, TmpStr);
  697.   Nrstr := TmpStr + ' h ';
  698.   STR(min:2, TmpStr);
  699.   NrStr := NrStr + TmpStr + ' m ';
  700.   STR(sec:2, TmpStr);
  701.   NrStr := NrStr + TmpStr + '.';
  702.   STR(frac:2, TmpStr);
  703.   NrStr := NrStr + TmpStr + ' s';
  704.   WriteLog('Done in: '+ NrStr + ' !');
  705.  
  706.   Exeunt(EnterStr);
  707. END.
  708.