home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / lxlt121s.zip / lxLite_src / unLock.pas < prev   
Pascal/Delphi Source File  |  1997-05-09  |  5KB  |  209 lines

  1. {&AlignCode-,AlignData-,AlignRec-,G3+,Speed-,Frame-}
  2. {$M 262144}
  3. uses os2base, miscUtil, SysLib, CmdLine, Collect,
  4.      strOp, Strings, Crt, Dos, lxlite_Global;
  5. const
  6.  Recurse : boolean = FALSE;
  7.  Pause   : boolean = FALSE;
  8.  Verbose : boolean = FALSE;
  9. var
  10.  OldExit : Procedure;
  11.  fNames  : pStringCollection;
  12.  allDone : boolean;
  13.  
  14. Procedure Stop(eCode : Byte);
  15. begin
  16.  case eCode of
  17.   1,2 : begin
  18.          if eCode = 2
  19.           then begin
  20.                 TextAttr := $0C;
  21.                 Writeln('├ Invalid switch - see help below for details');
  22.                end;
  23.          TextAttr := $07;
  24.          Writeln('├ Usage: unLock [FileMask( FileMask)] {[?|-]Options}');
  25.          Writeln('├ /P{+|-} Enable (+) or disable (-) pause before each file');
  26.          Writeln('├ /R{+|-} [R]ecursive (+) file search through subdirectories');
  27.          Writeln('├ /V{+|-} Verbose (show additional information)');
  28.          Writeln('├ /?,/H   Show this help screen');
  29.          Writeln('├┤Default: /P- /R- /V-');
  30.          TextAttr := $08;
  31.          Writeln('└┤Example: unLock d:\*.exe d:\*.dll /r');
  32.         end;
  33.  end;
  34.  Halt(eCode);
  35. end;
  36.  
  37. Function ParmHandler(var S : string) : Byte;
  38. var
  39.  I : Longint;
  40.  
  41. Function Enabled : boolean;
  42. begin
  43.  Enabled := TRUE;
  44.  if length(S) = 1
  45.   then exit
  46.   else
  47.  if (S[2] in ['+','-'])
  48.   then ParmHandler := 2
  49.   else
  50.  if (S[2] in [' ','/'])
  51.   then exit
  52.   else Stop(2);
  53.  if S[2] = '-' then Enabled := FALSE;
  54. end;
  55.  
  56. begin
  57.  ParmHandler := 1;
  58.  case upCase(S[1]) of
  59.   '?',
  60.   'H' : Stop(1);
  61.   'P' : Pause := Enabled;
  62.   'R' : Recurse := Enabled;
  63.   'V' : Verbose := Enabled;
  64.   else Stop(2);
  65.  end;
  66. end;
  67.  
  68. Function NameHandler(var S : string) : Byte;
  69. var
  70.  fN : string;
  71. begin
  72.  NameHandler := ParseName(S, 1, fN);
  73.  if fN <> '' then fNames^.AtInsert(fNames^.Count, NewStr(fN));
  74. end;
  75.  
  76. Procedure MyExitProc;
  77. begin
  78.  Write(#13);
  79.  TextAttr := $07; ClrEOL;
  80.  OldExit;
  81. end;
  82.  
  83. Function Ask(const Q,A : string) : byte;
  84. var ch  : char;
  85. begin
  86.  TextAttr := $02;
  87.  Write('└ ', Q, ' ');
  88.  repeat
  89.   ch := upCase(ReadKey);
  90.   if First(ch, A) <> 0
  91.    then begin
  92.          Ask := First(ch, A);
  93.          break;
  94.         end;
  95.  until FALSE;
  96.  Writeln(Ch,#13'├');
  97. end;
  98.  
  99. Procedure ProcessFile(fName : string);
  100. var
  101.  F  : File;
  102.  _n : string;
  103.  sz : array[0..255] of Char absolute _n;
  104.  
  105. Procedure NotLocked;
  106. begin
  107.  if Verbose
  108.   then begin Write(' not locked'); textAttr := $0B; Writeln(#13'├'); end
  109.   else begin Write(#13); ClrEOL; end;
  110. end;
  111.  
  112. begin
  113.  if length(fName) >= 255 then exit;
  114.  _n := extractName(fName);
  115.  textAttr := $0B;
  116.  Write(#13); ClrEOL;
  117.  Write('└ Processing file ', Copy(_n, 1, 28));
  118.  FileMode := open_share_DenyReadWrite or open_access_ReadOnly;
  119.  Assign(F, fName); Reset(F, 1);
  120.  if ioResult = 0
  121.   then begin
  122.         Close(F); NotLocked;
  123.         Exit;
  124.        end;
  125.  case DosReplaceModule(strPCopy(sz, fName), nil, nil) of
  126.   0 : begin
  127.        textAttr := $0A; Write(' unlocked');
  128.        textAttr := $0B; Writeln(#13'├');
  129.       end;
  130.   2 : NotLocked;
  131.  else begin
  132.        textAttr := $0C; Write(' sharing violation');
  133.        textAttr := $0B; Writeln(#13'├');
  134.       end
  135.  end;
  136. end;
  137.  
  138. Procedure ProcessFiles(const fN : string; Level : Longint);
  139. var
  140.  sr : SearchRec;
  141.  nf : Longint;
  142.  _d : DirStr;
  143.  _n : NameStr;
  144. begin
  145.  _d := extractDir(fN);
  146.  _n := extractName(fN);
  147.  FindFirst(fN, Archive or Hidden or SysFile, sr);
  148.  nf := 0;
  149.  if (DosError <> 0) and (Level = 0) and (not Recurse)
  150.   then begin
  151.         textAttr := $0C;
  152.         Writeln('├ Cannot find such files: ', fN);
  153.        end
  154.   else
  155.  While (DosError = 0) and (not allDone) do
  156.   begin
  157.    Inc(nf);
  158.    if (length(_d) + length(sr.Name) <= 255)
  159.     then begin
  160.           if Pause
  161.            then case Ask('File ' + sr.Name + ': [P]rocess, [S]kip or [A]bort?', 'PSA') of
  162.                  2 : sr.Name := '';
  163.                  3 : begin allDone := TRUE; break; end;
  164.                 end;
  165.           if (sr.Name <> '') then ProcessFile(_d + sr.Name);
  166.          end;
  167.    FindNext(sr);
  168.   end;
  169.  FindClose(sr);
  170.  if allDone or not Recurse then Exit;
  171.  if nf = 0
  172.   then begin
  173.         textAttr := $0B; Write('└ ', Short(_d, 77));
  174.         ClrEOL; Write(#13);
  175.        end;
  176.  FindFirst(_d + '*', Archive or Hidden or SysFile or Directory, sr);
  177.  While (dosError = 0) and (not allDone) do
  178.   begin
  179.    if (sr.Attr and Directory <> 0) and (sr.Name[1] <> '.') and
  180.       (length(_d) + length(sr.Name) + length(_n) + 1 <= 255)
  181.     then ProcessFiles(_d + sr.Name + '\' + _n, succ(Level));
  182.    FindNext(sr);
  183.   end;
  184.  FindClose(sr);
  185. end;
  186.  
  187. var
  188.  I : Longint;
  189.  
  190. begin
  191.  TextAttr := $0F;
  192.  Writeln('┌[ unLock ]──────────────────────────────[ Version '+Version+']┐');
  193.  Writeln('├ Copyright 1996 by FRIENDS software ─ No rights reserved ┘');
  194.  TextAttr := $07;
  195.  @OldExit := ExitProc; ExitProc := @MyExitProc;
  196.  New(fNames, Create(8, 8));
  197.  ParseCommandLine(#0, ParmHandler, NameHandler);
  198.  if (fNames^.Count = 0) then Stop(1);
  199.  
  200.  For I := 0 to pred(fNames^.Count) do
  201.   begin
  202.    ProcessFiles(pString(fNames^.At(I))^, 0);
  203.    if allDone then break;
  204.   end;
  205.  
  206.  TextAttr := $01; ClrEOL;
  207.  Writeln('└┤Done');
  208. end.
  209.