delp.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. {
  2. Copyright (c) 1999-2000 by Peter Vreman
  3. Deletes all files generated for Pascal (*.exe,units,objects,libs)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. program Delp;
  17. uses
  18. dos,getopts;
  19. const
  20. Version = 'Version 1.1';
  21. Title = 'DelPascal';
  22. Copyright = 'Copyright (c) 1999-2002 by the Free Pascal Development Team';
  23. function DStr(l:longint):string;
  24. var
  25. TmpStr : string[32];
  26. i : byte;
  27. begin
  28. Str(l,tmpstr);
  29. i:=Length(TmpStr);
  30. while (i>3) do
  31. begin
  32. i:=i-3;
  33. if TmpStr[i]<>'-' then
  34. Insert('.',TmpStr,i+1);
  35. end;
  36. DStr:=TmpStr;
  37. end;
  38. Procedure EraseFile(Const HStr:String);
  39. var
  40. f : file;
  41. begin
  42. Assign(f,Hstr);
  43. {$I-}
  44. Erase(f);
  45. {$I+}
  46. if ioresult<>0 then;
  47. end;
  48. function MatchesMask(What, Mask: string): boolean;
  49. function upper(const s : string) : string;
  50. var
  51. i : longint;
  52. begin
  53. for i:=1 to length(s) do
  54. if s[i] in ['a'..'z'] then
  55. upper[i]:=char(byte(s[i])-32)
  56. else
  57. upper[i]:=s[i];
  58. upper[0]:=s[0];
  59. end;
  60. Function CmpStr(const hstr1,hstr2:string):boolean;
  61. var
  62. found : boolean;
  63. i1,i2 : longint;
  64. begin
  65. i1:=0;
  66. i2:=0;
  67. found:=true;
  68. while found and (i1<length(hstr1)) and (i2<length(hstr2)) do
  69. begin
  70. inc(i2);
  71. inc(i1);
  72. case hstr1[i1] of
  73. '?' :
  74. found:=true;
  75. '*' :
  76. begin
  77. found:=true;
  78. if (i1=length(hstr1)) then
  79. i2:=length(hstr2)
  80. else
  81. if (i1<length(hstr1)) and (hstr1[i1+1]<>hstr2[i2]) then
  82. begin
  83. if i2<length(hstr2) then
  84. dec(i1)
  85. end
  86. else
  87. if i2>1 then
  88. dec(i2);
  89. end;
  90. else
  91. found:=(hstr1[i1]=hstr2[i2]) or (hstr2[i2]='?');
  92. end;
  93. end;
  94. if found then
  95. begin
  96. { allow 'p*' matching 'p' }
  97. if (i1<length(hstr1)) and (hstr1[i1+1]='*') then
  98. inc(i1);
  99. found:=(i1>=length(hstr1)) and (i2>=length(hstr2));
  100. end;
  101. CmpStr:=found;
  102. end;
  103. var
  104. D1,D2 : DirStr;
  105. N1,N2 : NameStr;
  106. E1,E2 : Extstr;
  107. begin
  108. {$ifdef Unix}
  109. FSplit(What,D1,N1,E1);
  110. FSplit(Mask,D2,N2,E2);
  111. {$else}
  112. FSplit(Upper(What),D1,N1,E1);
  113. FSplit(Upper(Mask),D2,N2,E2);
  114. {$endif}
  115. MatchesMask:=CmpStr(N2,N1) and CmpStr(E2,E1);
  116. end;
  117. type
  118. PMaskItem=^TMaskItem;
  119. TMaskItem=record
  120. Mask : string[15];
  121. Files : longint;
  122. Size : longint;
  123. Next : PMaskItem;
  124. end;
  125. var
  126. masklist : pmaskitem;
  127. procedure AddMask(s:string);
  128. var
  129. maskitem : PMaskItem;
  130. i : longint;
  131. begin
  132. repeat
  133. i:=pos(' ',s);
  134. if i=0 then
  135. i:=length(s)+1;
  136. New(maskitem);
  137. fillchar(maskitem^,sizeof(tmaskitem),0);
  138. maskitem^.mask:=Copy(s,1,i-1);
  139. maskitem^.next:=masklist;
  140. masklist:=maskitem;
  141. Delete(s,1,i);
  142. until s='';
  143. end;
  144. Var quiet: boolean;
  145. procedure usage;
  146. begin
  147. Writeln('Delp [options] <directory>');
  148. Writeln('Where options is one of:');
  149. writeln(' -e Delete executables also (Not on Unix)');
  150. writeln(' -h Display (this) help message.');
  151. writeln(' -q Quietly perfoms deleting.');
  152. Halt(1);
  153. end;
  154. procedure processoptions;
  155. Var c : char;
  156. begin
  157. quiet:=false;
  158. Repeat
  159. C:=Getopt('ehq');
  160. Case C of
  161. 'e' : AddMAsk('*.exe *.so *.dll');
  162. 'h' : Usage;
  163. 'q' : Quiet:=True;
  164. EndOfOptions : ;
  165. end;
  166. Until C=EndOfOptions;
  167. end;
  168. var
  169. Dir : Searchrec;
  170. Total : longint;
  171. hp : pmaskitem;
  172. found : boolean;
  173. basedir : string;
  174. begin
  175. ProcessOptions;
  176. if Optind<>ParamCount then
  177. Usage;
  178. BaseDir:=Paramstr(OptInd);
  179. If BaseDir[Length(BaseDir)]<>DirectorySeparator then
  180. BaseDir:=BaseDir+DirectorySeparator;
  181. { Win32 target }
  182. AddMask('*.ppw *.ow *.aw *.sw');
  183. AddMask('ppas.bat ppas.sh link.res fpcmaked fpcmade fpcmade.*');
  184. AddMask('*.tpu *.tpp *.tpw *.tr');
  185. AddMask('*.dcu *.dcp *.bpl');
  186. AddMask('*.log *.bak *.~pas *.~pp *.*~');
  187. AddMask('*.ppu *.o *.a *.s');
  188. AddMask('*.pp1 *.o1 *.a1 *.s1');
  189. AddMask('*.ppo *.oo *.ao *.so');
  190. AddMask('*.rst');
  191. { OS/2 target }
  192. AddMask('*.oo2 *.so2 *.ppo');
  193. { Amiga target }
  194. AddMask('*.ppa *.asm');
  195. if not quiet then
  196. begin
  197. writeln(Title+' '+Version);
  198. writeln(Copyright);
  199. Writeln;
  200. end;
  201. FindFirst(basedir+'*.*',anyfile,Dir);
  202. Total:=0;
  203. while (doserror=0) do
  204. begin
  205. hp:=masklist;
  206. while assigned(hp) do
  207. begin
  208. if MatchesMask(Dir.Name,hp^.mask) then
  209. begin
  210. EraseFile(BaseDir+Dir.Name);
  211. inc(hp^.Files);
  212. inc(hp^.Size,Dir.Size);
  213. break;
  214. end;
  215. hp:=hp^.next;
  216. end;
  217. FindNext(Dir);
  218. end;
  219. {Write Results}
  220. found:=false;
  221. hp:=masklist;
  222. while assigned(hp) do
  223. begin
  224. if hp^.Files>0 then
  225. begin
  226. if not quiet then
  227. WriteLn(' - Removed ',hp^.Files:2,' ',hp^.Mask,' (',DStr(hp^.Size)+' Bytes)');
  228. inc(Total,hp^.Size);
  229. found:=true;
  230. end;
  231. hp:=hp^.next;
  232. end;
  233. if not quiet then
  234. if not found then
  235. WriteLn(' - No Redundant Files Found!')
  236. else
  237. WriteLn(' - Total ',DStr(Total),' Bytes Freed');
  238. end.