tfattr.pp 7.1 KB

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