home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / VOLMOD.ZIP / LABEL.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1989-07-11  |  7.7 KB  |  272 lines

  1. {
  2.    Created using Turbo Pascal, Copywrite (c) Borland International
  3.    1987, 1988.
  4.  
  5.    This is similar to the DOS 'LABEL' external command.  I modified code
  6.    which was written for Turbo Pascal 3.0 which was written by
  7.    David Kozinn (D.KOZINN on GEnie).  This code will delete volume
  8.    labels, however, as well as modify and create them.  In order to
  9.    accomplish the deletes FCBs must be used.  Although it is rumored DOS
  10.    will soon cease to support FCBs, this program works with DOS 4.01
  11.    running the SHARE.EXE command on my NORTHGATE 386-20Mhz PC without
  12.    any problems.  If you enter paramters, the 1st 2 bytes must be
  13.    the driver letter followed by a colon.
  14.  
  15.    WARNING: If you try to label a drive which doesn't exist, say Q: if
  16.    you don't have a drive Q:, this program will attempt to label it
  17.    anyway with unpredictable results. If you know of a way to determine
  18.    the maximum drive letter/number please send me a note on GEnie and
  19.    let me know.  John Gatewood Ham (J.HAM3 on GEnie)
  20. }
  21.  
  22.  
  23. PROGRAM vlabel(Input, Output);
  24.  
  25. USES DOS,CRT;
  26.  
  27. VAR
  28.     i                 : integer;
  29.     drivelet          : STRING[1];
  30.     labl              : STRING[11];
  31.     cmdline,
  32.     whichdrive        : string[13];
  33.  
  34.   procedure Getlegal;
  35.   begin
  36.     writeln('Created using Turbo Pascal, Copywrite (c) Borland International 1987, 1988.');
  37.   end;
  38.  
  39.   PROCEDURE volume(drive : Byte);
  40.     TYPE
  41.       extendfcb       = ARRAY[0..43] OF Char;
  42.     VAR
  43.       drivenam        : STRING[3];
  44.       i               : Integer;
  45.       reg             : registers;
  46.       c               : string[1];
  47.       haslabel        : Boolean;
  48.       dta, xfcb, sfcb : extendfcb;
  49.  
  50.     {initialize an extended fcb}
  51.     PROCEDURE initfcb(VAR x : extendfcb; namechar : Char);
  52.       VAR
  53.         i : Integer;
  54.       BEGIN
  55.         x[0] := Chr(255);     {flag for extended FCB}
  56.         FOR i := 1 TO 5 DO
  57.            x[i] := Chr(0);
  58.         x[6] := Chr(8);       {specifies that we want volume label}
  59.         x[7] := Chr(0);       {where drive number goes}
  60.         FOR i := 8 TO 18 DO   {This is where the filename (in our }
  61.            x[i] := namechar;  {case the volume label) goes        }
  62.         FOR i := 19 TO 43 DO
  63.            x[i] := Chr(0);
  64.       END;
  65.  
  66.       { Get current drive number }
  67.       FUNCTION Current_drive:byte;
  68.         BEGIN
  69.           { get current drive }
  70.           reg.ah := $19;
  71.           MsDos(reg);
  72.           current_drive:=reg.al+1;
  73.         END;
  74.  
  75.       { Set disk transfer address area to variable named DTA }
  76.       { for use in directory search for the volume label.    }
  77.       procedure set_dta;
  78.       begin
  79.         { set dta }
  80.         with reg do
  81.           begin
  82.             ah := $1A;
  83.             ds := Seg(dta[0]);
  84.             dx := Ofs(dta[0]);
  85.           end;
  86.         MsDos(reg);
  87.       end;
  88.  
  89.       { Search for label.  If found display else display    }
  90.       { message that no label exists                        }
  91.       procedure search_for_label;
  92.       begin
  93.         { find first using FCB method }
  94.         with reg do
  95.           begin
  96.             ah := $11;
  97.             dx := Ofs(sfcb[0]);
  98.           end;
  99.         MsDos(reg);
  100.         { on error }
  101.         IF (reg.al = $FF) THEN
  102.             haslabel := False
  103.         ELSE
  104.             haslabel := True;
  105.       end;
  106.  
  107.       { Display volume label  }
  108.       procedure show_label;
  109.       var i:integer;
  110.       begin
  111.         if haslabel then
  112.           begin
  113.             Write('Volume in drive ', whichdrive[1], ' is ');
  114.             FOR i := 1 TO 11 DO
  115.                Write(dta[7+i]);
  116.             WriteLn;
  117.           end
  118.         else
  119.            WriteLn('Volume in drive ', whichdrive[1], ' has no label');
  120.       end;
  121.  
  122.       { Delete a volume label }
  123.       PROCEDURE delete_file;
  124.       BEGIN
  125.         { delete a file specified by an unopened FCB }
  126.         WITH reg DO
  127.           begin
  128.             ah := $13;
  129.             ds := Seg(dta[0]);
  130.             dx := Ofs(dta[0]);
  131.           end;
  132.         MsDos(reg);
  133.         { if error }
  134.         if (reg.al = $FF) then
  135.           BEGIN
  136.             WriteLn('Unable to delete volume label');
  137.             halt;
  138.           END;
  139.       END;
  140.  
  141.       procedure rename_file;
  142.       var i:integer;
  143.       BEGIN
  144.         {modify dta filename}
  145.         FOR i := 1 TO 11 DO
  146.           dta[23+i] := xfcb[7+i];
  147.         { rename file using FCB method }
  148.         with reg do
  149.           begin
  150.             ds := Seg(dta[0]);
  151.             dx := Ofs(dta[0]);
  152.             ah := $17;
  153.           end;
  154.         MsDos(reg);
  155.         { if error }
  156.         IF (reg.al = $FF) THEN
  157.           begin
  158.             WriteLn('Unable to change volume label');
  159.             halt;
  160.           end;
  161.       END;
  162.  
  163.       procedure create_file;
  164.       BEGIN
  165.          {create a file using FCB method}
  166.          with reg do
  167.            begin
  168.              ds := Seg(xfcb[0]);
  169.              dx := Ofs(xfcb[0]);
  170.              ah := $16;
  171.            end;
  172.          MsDos(reg);
  173.          { if error }
  174.          IF (reg.al = $FF) THEN
  175.           begin
  176.             WriteLn('Unable to create volume label');
  177.             halt;
  178.           end;
  179.       END;
  180.  
  181.     BEGIN
  182.       {initialize FCB buffers}
  183.       initfcb(sfcb, '?');
  184.       initfcb(xfcb, ' ');
  185.  
  186.       {if no drive letter entered used logged drive}
  187.       IF drive = 0 THEN
  188.         begin
  189.           drive:=current_drive;
  190.           whichdrive := chr(drive+64);
  191.         end;
  192.  
  193.       {set drive in FCB buffers to drive selected}
  194.       sfcb[7] := Chr(drive);
  195.       xfcb[7] := Chr(drive);
  196.  
  197.       {set up disk transfer area for directory search, etc.}
  198.       set_dta;
  199.       {directory search using FCB method, directory bit set}
  200.       search_for_label;
  201.       show_label;
  202.  
  203.       {if they didn't enter a label name on command line get}
  204.       {one now.                                             }
  205.       if length(cmdline) < 3 then
  206.         begin
  207.           {display label (or message that there is no label) }
  208.           Write('Enter new volume label:');
  209.           ReadLn(labl);
  210.         end;
  211.  
  212.       {if label is spaces}
  213.       if (labl = '') then
  214.         begin
  215.           {if label is spaces and there was a volume label found}
  216.           if haslabel then
  217.             begin
  218.               write('Delete current volume label (Y/N)?');
  219.               repeat
  220.                 c:=readkey;
  221.               until c[1] in ['y','n','Y','N'];
  222.               writeln(c[1]);
  223.               if c[1] in ['y','Y'] then
  224.                   delete_file;
  225.             end;
  226.         end
  227.       else {label is not spaces}
  228.         BEGIN
  229.           {insert label into xfcb}
  230.           FOR i := 1 TO Length(labl) DO
  231.              xfcb[7+i] := labl[i];
  232.           { rename file (change label) }
  233.           IF haslabel THEN
  234.              rename_file
  235.           ELSE { create file (make a new label) }
  236.              create_file;
  237.         END;
  238.       {directory search using FCB method, directory bit set}
  239.       search_for_label;
  240.       {display label (or message that there is no label) }
  241.       show_label;
  242.     END;                      {volume}
  243.  
  244. begin
  245.   getlegal;
  246.   labl:='';
  247.   if paramcount < 1 then
  248.      volume(0)
  249.   else
  250.     begin
  251.      cmdline:='';
  252.      whichdrive:='';
  253.      cmdline:=paramstr(1);
  254.      if length(cmdline) < 2 then
  255.        begin
  256.          writeln('Invalid drive specification');
  257.          halt;
  258.        end;
  259.      whichdrive:=copy(cmdline,1,2);
  260.      if (whichdrive[2] <> ':') or
  261.         (not (upcase(whichdrive[1]) in ['A'..'Z'])) then
  262.         begin
  263.           writeln('Invalid drive specification');
  264.           halt;
  265.         end;
  266.      for i:=2 to paramcount do
  267.        cmdline:=cmdline+' '+paramstr(i);
  268.      labl:=copy(cmdline,3,11);
  269.      whichdrive[1]:=upcase(whichdrive[1]);
  270.      volume(ord(whichdrive[1])-64);
  271.     end;
  272. end.