tfexpand.pp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. program TFExpand;
  2. (* Test for possible bugs in Dos.FExpand *)
  3. { $DEFINE DEBUG}
  4. (* Defining DEBUG causes all the source and target strings *)
  5. (* to be written to the console to make debugging easier. *)
  6. uses
  7. {$ifdef FPC}
  8. {$ifdef win32}
  9. win32err,
  10. {$endif win32}
  11. {$endif FPC}
  12. Dos;
  13. {$IFDEF OS2}
  14. function _DosError (Error: longint): longint; cdecl;
  15. external 'DOSCALLS' index 212;
  16. {$ENDIF OS2}
  17. {$IFDEF LINUX}
  18. {$IFNDEF UNIX}
  19. {$DEFINE UNIX}
  20. {$ENDIF UNIX}
  21. {$ENDIF LINUX}
  22. {$IFDEF AMIGA}
  23. {$DEFINE VOLUMES}
  24. {$ENDIF AMIGA}
  25. {$IFDEF NETWARE}
  26. {$DEFINE VOLUMES}
  27. {$ENDIF NETWARE}
  28. const
  29. {$IFNDEF FPC}
  30. FileNameCaseSensitive = false;
  31. DirSep = '\';
  32. CDrive = 'C:';
  33. {$ELSE}
  34. DirSep = System.DirectorySeparator;
  35. {$IFDEF UNIX}
  36. CDrive = '';
  37. {$ELSE}
  38. CDrive = 'C:';
  39. {$ENDIF}
  40. {$ENDIF}
  41. HasErrors: boolean = false;
  42. var
  43. TestDir, OrigDir, OrigTstDir, CurDir, CDir, S: DirStr;
  44. TestDrive: string [2];
  45. I: byte;
  46. IOR: longint;
  47. function Translate (S: PathStr): PathStr;
  48. var
  49. I: byte;
  50. begin
  51. {$IFDEF UNIX}
  52. if (Length (S) > 1) and (S [2] = ':') then Delete (S, 1, 2);
  53. {$ELSE}
  54. for I := 1 to Length (S) do if S [I] = '/' then S [I] := DirSep;
  55. if (Length (S) > 0) and (S [1] in ['a'..'z']) then S [1] := UpCase (S [1]);
  56. {$ENDIF}
  57. if not (FileNameCaseSensitive) then
  58. for I := 1 to Length (S) do S [I] := UpCase (S [I]);
  59. Translate := S;
  60. end;
  61. procedure Check (Src, Rslt: PathStr);
  62. var
  63. Rslt2: PathStr;
  64. begin
  65. {$IFDEF DEBUG}
  66. WriteLn (Src, '=>', Rslt);
  67. {$ENDIF}
  68. Rslt := Translate (Rslt);
  69. Rslt2 := FExpand (Src);
  70. if Rslt <> Rslt2 then
  71. begin
  72. WriteLn ('Error: FExpand (', Src, ') should be "', Rslt, '", not "',
  73. Rslt2, '"');
  74. HasErrors := true;
  75. end;
  76. end;
  77. begin
  78. {$IFDEF OS2}
  79. (* Avoid OS/2 error messages. *)
  80. _DosError (0);
  81. {$ENDIF OS2}
  82. if ParamCount <> 1 then
  83. begin
  84. WriteLn ('Warning: Parameter missing!');
  85. WriteLn ('Full path to a directory with write access' +
  86. {$IFNDEF UNIX}
  87. #13#10'(preferably not on a C: drive)' +
  88. {$ENDIF}
  89. ' expected.');
  90. WriteLn ('Trying to use the current directory instead ' +
  91. {$IFDEF UNIX}
  92. '(not quite ideal).');
  93. {$ELSE UNIX}
  94. '(problems might arise).');
  95. {$ENDIF UNIX}
  96. GetDir (0, TestDir);
  97. end else TestDir := ParamStr (1);
  98. if TestDir [Length (TestDir)] <> DirSep then TestDir := TestDir + DirSep;
  99. GetDir (0, OrigDir);
  100. {$IFDEF UNIX}
  101. TestDrive := '';
  102. {$ELSE UNIX}
  103. TestDrive := Copy (TestDir, 1, 2);
  104. GetDir ((Ord (TestDir [1]) and not ($20)) - 64, OrigTstDir);
  105. {$ENDIF UNIX}
  106. {$I-}
  107. MkDir (TestDir + 'TESTDIR1');
  108. if IOResult <> 0 then ;
  109. MkDir (TestDir + 'TESTDIR1' + DirSep + 'TESTDIR2');
  110. if IOResult <> 0 then ;
  111. {$I+}
  112. ChDir (TestDir + 'TESTDIR1' + DirSep + 'TESTDIR2');
  113. GetDir (0, CurDir);
  114. {$IFDEF UNIX}
  115. CDir := CurDir;
  116. {$ELSE UNIX}
  117. GetDir (3, CDir);
  118. {$ENDIF UNIX}
  119. Check (' ', CurDir + DirSep + ' ');
  120. Check ('', CurDir + DirSep);
  121. Check ('.', CurDir);
  122. Check ('C:', CDir);
  123. Check ('C:.', CDir);
  124. if CDir [Length (CDir)] = DirSep then Check ('c:anything', CDir + 'anything')
  125. else Check ('c:anything', CDir + DirSep + 'anything');
  126. Check ('C:' + DirSep, CDrive + DirSep);
  127. Check ('C:' + DirSep + '.', CDrive + DirSep);
  128. Check ('C:' + DirSep + '..', CDrive + DirSep);
  129. Check ('C:' + DirSep + 'DOS', CDrive + DirSep + 'DOS');
  130. Check ('C:' + DirSep + '..' + DirSep + 'DOS', CDrive + DirSep + 'DOS');
  131. Check ('C:' + DirSep + 'DOS.', CDrive + DirSep + 'DOS.');
  132. Check ('C:' + DirSep + 'DOS' + DirSep, CDrive + DirSep + 'DOS' + DirSep);
  133. Check ('C:' + DirSep + 'DOS' + DirSep + '.', CDrive + DirSep + 'DOS');
  134. Check ('C:' + DirSep + 'DOS' + DirSep + '..', CDrive + DirSep);
  135. Check ('C:' + DirSep + 'DOS' + DirSep + '..' + DirSep, CDrive + DirSep);
  136. Check ('C:' + DirSep + 'DOS' + DirSep + 'TEST' + DirSep + '..', CDrive +
  137. DirSep + 'DOS');
  138. Check ('C:' + DirSep + 'DOS' + DirSep + 'TEST' + DirSep + '..' + DirSep,
  139. CDrive + DirSep + 'DOS' + DirSep);
  140. Check (DirSep, TestDrive + DirSep);
  141. Check (DirSep + '.', TestDrive + DirSep);
  142. Check (DirSep + '..', TestDrive + DirSep);
  143. Check (DirSep + 'DOS', TestDrive + DirSep + 'DOS');
  144. Check ('d', CurDir + DirSep + 'd');
  145. Check (' d', CurDir + DirSep + ' d');
  146. Check ('dd', CurDir + DirSep + 'dd');
  147. Check ('dd' + DirSep + 'dd', CurDir + DirSep + 'dd' + DirSep + 'dd');
  148. Check ('ddd', CurDir + DirSep + 'ddd');
  149. Check ('dddd' + DirSep + 'eeee.ffff', CurDir + DirSep + 'dddd' + DirSep
  150. + 'eeee.ffff');
  151. Check ('.special', CurDir + DirSep + '.special');
  152. Check ('..special', CurDir + DirSep + '..special');
  153. Check ('special..', CurDir + DirSep + 'special..');
  154. Check ('special.' + DirSep, CurDir + DirSep + 'special.' + DirSep);
  155. Check (DirSep + '.special', TestDrive + DirSep + '.special');
  156. Check ('..', TestDir + 'TESTDIR1');
  157. Check ('.' + DirSep + '..', TestDir + 'TESTDIR1');
  158. Check ('..' + DirSep + '.', TestDir + 'TESTDIR1');
  159. Check ('...', CurDir + DirSep + '...');
  160. {$IFDEF UNIX}
  161. S := GetEnv ('HOME');
  162. { On m68k netbsd at least, HOME contains a final slash
  163. remove it PM }
  164. if S[length(S)]=DirSep then
  165. S:=Copy(S,1,Length(S)-1);
  166. Check ('~', S);
  167. Check ('~' + DirSep + '.', S);
  168. if (Length (S) > 0) and (S [Length (S)] <> DirSep) then S := S + DirSep;
  169. Check ('~NobodyWithThisNameShouldEverExist.test/nothing', CurDir + DirSep +
  170. '~NobodyWithThisNameShouldEverExist.test/nothing');
  171. Check ('/tmp/~NoSuchUserAgain', '/tmp/~NoSuchUserAgain');
  172. Check ('~' + DirSep, S);
  173. Check ('~' + DirSep + '.' + DirSep, S);
  174. Check ('~' + DirSep + 'directory' + DirSep + 'another',
  175. S + 'directory' + DirSep + 'another');
  176. {$ELSE UNIX}
  177. Check (TestDrive + '..', TestDir + 'TESTDIR1');
  178. Check (TestDrive + '..' + DirSep, TestDir + 'TESTDIR1' + DirSep);
  179. Check (TestDrive + '.' + DirSep + '.', CurDir);
  180. Check (TestDrive + '.' + DirSep + '..', TestDir + 'TESTDIR1');
  181. {$I-}
  182. {$ifndef unix}
  183. { avoid a and b drives for
  184. no unix systems to reduce the
  185. probablility of getting an alert message box }
  186. I := 3;
  187. {$else unix}
  188. I := 1;
  189. {$endif unix}
  190. repeat
  191. S := '';
  192. GetDir (I, S);
  193. IOR := IOResult;
  194. if IOR = 0 then Inc (I);
  195. until (I > 26) or (IOR <> 0);
  196. if I <= 26 then
  197. begin
  198. S := Chr (I + 64) + ':ddd';
  199. Check (S, Chr (I + 64) + ':' + DirSep + 'ddd');
  200. end else
  201. WriteLn ('Sorry, cannot test FExpand behaviour for incorrect drives here.');
  202. {$I+}
  203. {$IFDEF FPC}
  204. Check ('d\d/d', CurDir + DirSep + 'd' + DirSep + 'd' + DirSep + 'd');
  205. Check ('\\server\share\directory', '\\server\share\directory');
  206. Check ('\\server\share\directory1\directory2\..',
  207. '\\server\share\directory1');
  208. Check ('\\', '\\');
  209. Check ('\\.', '\\.\');
  210. Check ('\\.\', '\\.\');
  211. Check ('\\.\.', '\\.\.');
  212. Check ('\\.\..', '\\.\..');
  213. Check ('\\.\...', '\\.\...');
  214. Check ('\\.\TEST', '\\.\TEST');
  215. Check ('\\..\', '\\..\');
  216. Check ('\\..\TEST', '\\..\TEST');
  217. Check ('\\..\TEST\.', '\\..\TEST');
  218. Check ('\\..\TEST1\TEST2\..', '\\..\TEST1');
  219. Check ('\\..\TEST\..', '\\..\TEST');
  220. Check ('\\..\TEST\..\..', '\\..\TEST');
  221. {$ENDIF FPC}
  222. ChDir (OrigTstDir);
  223. {$ENDIF UNIX}
  224. {$IFDEF VOLUMES}
  225. Check ('VolName:' + DirSep + 'DIR1', 'VolName:' + DirSep + 'DIR1');
  226. Check ('VolName:' + DirSep + 'DIR1' + DirSep + '..', 'VolName:' + DirSep);
  227. Check ('VolName:' + DirSep + 'DIR1' + DirSep + '..' + DirSep + '..',
  228. 'VolName:' + DirSep);
  229. Check ('VolName:' + DirSep + '.', 'VolName:' + DirSep);
  230. Check ('VolName:' + DirSep + '..', 'VolName:' + DirSep);
  231. Check ('VolName:' + DirSep + '..' + DirSep, 'VolName:' + DirSep);
  232. Check ('SrvName\VolName:' + DirSep + 'TEST', 'SrvName' + DirSep + 'VolName:' +
  233. DirSep + 'TEST');
  234. Check ('SrvName/VolName:' + DirSep + 'TEST', 'SrvName' + DirSep + 'VolName:' +
  235. DirSep + 'TEST');
  236. {$ENDIF VOLUMES}
  237. ChDir (OrigDir);
  238. RmDir (TestDir + 'TESTDIR1' + DirSep + 'TESTDIR2');
  239. RmDir (TestDir + 'TESTDIR1');
  240. if HasErrors then
  241. begin
  242. WriteLn ('FExpand doesn''t work correctly.');
  243. Halt (1);
  244. end;
  245. end.