tfattr.pp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. {******************************************}
  2. { Used to check the DOS unit }
  3. {------------------------------------------}
  4. { SetFAttr / GetFAttr testing }
  5. {******************************************}
  6. Program tfattr;
  7. uses dos;
  8. {$IFDEF MSDOS}
  9. {$DEFINE EXTATTR}
  10. {$ENDIF}
  11. {$IFDEF DPMI}
  12. {$DEFINE EXTATTR}
  13. {$ENDIF}
  14. {$IFDEF GO32V1}
  15. {$DEFINE EXTATTR}
  16. {$ENDIF}
  17. {$IFDEF GO32V2}
  18. {$DEFINE EXTATTR}
  19. {$ENDIF}
  20. {$IFDEF OS2}
  21. {$DEFINE EXTATTR}
  22. {$ENDIF}
  23. {$IFDEF WIN32}
  24. {$DEFINE EXTATTR}
  25. {$ENDIF}
  26. {$IFDEF ATARI}
  27. {$DEFINE EXTATTR}
  28. {$ENDIF}
  29. {$IFDEF WINCE}
  30. {$DEFINE EXTATTR}
  31. {$ENDIF}
  32. CONST
  33. { what is the root path }
  34. {$ifdef UNIX}
  35. RootPath = '/';
  36. {$else UNIX}
  37. {$ifdef WINCE}
  38. RootPath = '\';
  39. {$else WINCE}
  40. RootPath = 'C:\';
  41. {$endif WINCE}
  42. {$ENDIF}
  43. Week:Array[0..6] of String =
  44. ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  45. TestFName = 'TESTDOS.DAT'; { CASE SENSITIVE DON'T TOUCH! }
  46. TestFName1 = 'TESTFILE'; { CASE SENSITIVE DON'T TOUCH! }
  47. TestDir = 'MYDIR'; { CASE SENSITIVE DON'T TOUCH! }
  48. TestExt = 'DAT';
  49. {$IFDEF TP}
  50. DirectorySeparator = '\';
  51. {$ENDIF}
  52. has_errors : boolean = false;
  53. { verifies that the DOSError variable is equal to }
  54. { the value requested. }
  55. Procedure CheckDosError(err: Integer);
  56. var
  57. x : integer;
  58. s :string;
  59. Begin
  60. x := DosError;
  61. case x of
  62. 0 : s := '(0): No Error.';
  63. 2 : s := '(2): File not found.';
  64. 3 : s := '(3): Path not found.';
  65. 5 : s := '(5): Access Denied.';
  66. 6 : s := '(6): Invalid File Handle.';
  67. 8 : s := '(8): Not enough memory.';
  68. 10 : s := '(10) : Invalid Environment.';
  69. 11 : s := '(11) : Invalid format.';
  70. 18 : s := '(18) : No more files.';
  71. else
  72. s := 'INVALID DOSERROR';
  73. end;
  74. if err <> x then
  75. Begin
  76. WriteLn('FAILURE. (Value of DOSError should be ',err,' '+s+')');
  77. has_errors:=true;
  78. end;
  79. end;
  80. procedure fail;
  81. Begin
  82. WriteLn('Failed!');
  83. has_errors:=true;
  84. End;
  85. Procedure TestFAttr1;
  86. Var
  87. F: File;
  88. Attr: Word;
  89. s: string;
  90. Begin
  91. WriteLn('Opening an invalid file...Success!');
  92. Assign(f,'');
  93. GetFAttr(f,Attr);
  94. CheckDosError(3);
  95. Assign(f,TestFName);
  96. WriteLn('Trying to open a valid file...Success!');
  97. GetFAttr(f,Attr);
  98. CheckDosError(0);
  99. {$ifndef wince}
  100. Write('Trying to open the current directory file...');
  101. Assign(f,'.');
  102. GetFAttr(f,Attr);
  103. if (attr and Directory) = 0 then
  104. fail
  105. else
  106. WriteLn('Success!');
  107. CheckDosError(0);
  108. Write('Trying to open the parent directory file...');
  109. Assign(f,'..');
  110. GetFAttr(f,Attr);
  111. if (attr and Directory) = 0 then
  112. fail
  113. else
  114. WriteLn('Success!');
  115. CheckDosError(0);
  116. {$endif wince}
  117. { This is completely platform dependent
  118. Write('Trying to open the parent directory file when in root...');
  119. Getdir(0,s);
  120. ChDir(RootPath);
  121. Assign(f,'..');
  122. GetFAttr(f,Attr);
  123. ChDir(s);
  124. CheckDosError(3);
  125. WriteLn('Success!');
  126. }
  127. {$ifdef go32v2}
  128. { Should normally fail, because of end directory separator. This is
  129. allowed under unixes so the test is go32v2 only }
  130. WriteLn('Trying to open a directory file...Success!');
  131. GetDir(0,s);
  132. Assign(f,s+DirectorySeparator);
  133. GetFAttr(f, Attr);
  134. CheckDosError(3);
  135. {$endif}
  136. Write('Trying to open a directory file...');
  137. {$ifdef wince}
  138. s:='\windows';
  139. {$else}
  140. GetDir(0,s);
  141. {$endif wince}
  142. Assign(f,s);
  143. GetFAttr(f, Attr);
  144. if (attr and Directory) = 0 then
  145. fail
  146. else
  147. WriteLn('Success!');
  148. CheckDosError(0);
  149. end;
  150. Procedure TestFAttr;
  151. Var
  152. F: File;
  153. Attr: Word;
  154. s: string;
  155. Begin
  156. Assign(f, TestFname);
  157. {----------------------------------------------------------------}
  158. { This routine causes problems, because it all depends on the }
  159. { operating system. It is assumed here that HIDDEN is available }
  160. { to all operating systems. }
  161. {----------------------------------------------------------------}
  162. s:='Setting read-only attribute on '+TestFName+'...';
  163. SetFAttr(f,ReadOnly);
  164. CheckDosError(0);
  165. {$IFDEF EXTATTR}
  166. GetFAttr(f,Attr);
  167. CheckDosError(0);
  168. if Attr and ReadOnly<> 0 then
  169. WriteLn(s+'Success.')
  170. else
  171. Begin
  172. WriteLn(s+'FAILURE. Read-only attribute not set.');
  173. has_errors:=true;
  174. end;
  175. { file should no longer be read only }
  176. s:='Removing read-only attribute...';
  177. SetFAttr(f,Archive);
  178. CheckDosError(0);
  179. GetFAttr(f,Attr);
  180. CheckDosError(0);
  181. if Attr and ReadOnly<> 0 then
  182. Begin
  183. WriteLn(s+'FAILURE. Read-only attribute still set.');
  184. has_errors:=true;
  185. end
  186. else
  187. WriteLn(s+'Success.');
  188. {$ENDIF}
  189. s:='Setting hidden attribute on '+TestFName+'...';
  190. SetFAttr(f,Hidden);
  191. CheckDosError(0);
  192. {$IFDEF EXTATTR}
  193. GetFAttr(f,Attr);
  194. CheckDosError(0);
  195. if Attr and Hidden<> 0 then
  196. WriteLn(s+'Success.')
  197. else
  198. Begin
  199. WriteLn(s+'FAILURE. Hidden attribute not set.');
  200. has_errors:=true;
  201. end;
  202. { file should no longer be read only }
  203. s:='Removing hidden attribute...';
  204. SetFAttr(f,Archive);
  205. CheckDosError(0);
  206. GetFAttr(f,Attr);
  207. CheckDosError(0);
  208. if Attr and Hidden<> 0 then
  209. Begin
  210. WriteLn(s+'FAILURE. Hidden attribute still set.');
  211. has_errors:=true;
  212. end
  213. else
  214. WriteLn(s+'Success.');
  215. {$ENDIF}
  216. {$IFDEF EXTATTR}
  217. s:='Setting system attribute on '+TestFName+'...';
  218. SetFAttr(f,SysFile);
  219. CheckDosError(0);
  220. GetFAttr(f,Attr);
  221. CheckDosError(0);
  222. if Attr and SysFile<> 0 then
  223. WriteLn(s+'Success.')
  224. else
  225. Begin
  226. WriteLn(s+'FAILURE. SysFile attribute not set.');
  227. has_errors:=true;
  228. end;
  229. { file should no longer be read only }
  230. s:='Removing Sysfile attribute...';
  231. SetFAttr(f,0);
  232. CheckDosError(0);
  233. GetFAttr(f,Attr);
  234. CheckDosError(0);
  235. if Attr and Sysfile<> 0 then
  236. Begin
  237. WriteLn(s+'FAILURE. SysFile attribute still set.');
  238. has_errors:=true;
  239. end
  240. else
  241. WriteLn(s+'Success.');
  242. {$ENDIF}
  243. {
  244. s:='Setting Directory attribute on '+TestFName+'...';
  245. SetFAttr(f,Directory);
  246. CheckDosError(5);
  247. GetFAttr(f,Attr);
  248. CheckDosError(0);
  249. if Attr and Directory<> 0 then
  250. Begin
  251. WriteLn(s+'FAILURE. Directory Attribute set.');
  252. has_errors:=true;
  253. end
  254. else
  255. WriteLn(s+'Success.');
  256. }
  257. {**********************************************************************}
  258. {********************** TURBO PASCAL BUG ******************************}
  259. { The File is not a volume name, and DosError = 0, which is incorrect }
  260. { it shoulf not be so in FPC. }
  261. {**********************************************************************}
  262. {********************** TURBO PASCAL BUG ******************************}
  263. s:='Setting Volume attribute on '+TestFName+'...';
  264. SetFAttr(f,VolumeID);
  265. {$ifndef tp}
  266. CheckDosError(5);
  267. {$else}
  268. CheckDosError(0);
  269. {$endif}
  270. GetFAttr(f,Attr);
  271. CheckDosError(0);
  272. if Attr and VolumeID<> 0 then
  273. Begin
  274. WriteLn(s+'FAILURE. Volume Attribute set.');
  275. has_errors:=true;
  276. end
  277. else
  278. WriteLn(s+'Success.');
  279. end;
  280. var
  281. f: file;
  282. oldexit : pointer;
  283. procedure MyExit;far;
  284. begin
  285. ExitProc := OldExit;
  286. RmDir(TestDir);
  287. Assign(f, TestFname);
  288. Erase(f);
  289. Assign(f, TestFname1);
  290. Erase(f);
  291. end;
  292. Begin
  293. {$IFDEF MACOS}
  294. pathTranslation:= true;
  295. {$ENDIF}
  296. WriteLn('File should never be executed in root path!');
  297. OldExit := ExitProc;
  298. ExitProc := @MyExit;
  299. Assign(f,TestFName);
  300. Rewrite(f,1);
  301. BlockWrite(f,Week,sizeof(Week));
  302. Close(f);
  303. Assign(f,TestFName1);
  304. Rewrite(f,1);
  305. Close(F);
  306. MkDir(TestDir);
  307. testfattr1;
  308. testfattr;
  309. if has_errors then
  310. halt(1);
  311. end.