delp.pp 6.1 KB

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