sysutils.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. Sysutils unit for netware
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  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.
  12. **********************************************************************}
  13. unit sysutils;
  14. interface
  15. {$MODE objfpc}
  16. { force ansistrings }
  17. {$H+}
  18. uses DOS;
  19. {$I nwsys.inc}
  20. {$I errno.inc}
  21. TYPE
  22. TNetwareFindData =
  23. RECORD
  24. DirP : PNWDirEnt; { used for opendir }
  25. EntryP: PNWDirEnt; { and readdir }
  26. Magic : WORD; { to avoid abends with uninitialized TSearchRec }
  27. END;
  28. { Include platform independent interface part }
  29. {$i sysutilh.inc}
  30. { additional NetWare file flags}
  31. CONST
  32. faSHARE = $00000080; { Sharable file }
  33. faNO_SUBALLOC = $00000800; { Don't sub alloc. this file }
  34. faTRANS = $00001000; { Transactional file (TTS usable) }
  35. faREADAUD = $00004000; { Read audit }
  36. faWRITAUD = $00008000; { Write audit }
  37. faIMMPURG = $00010000; { Immediate purge }
  38. faNORENAM = $00020000; { Rename inhibit }
  39. faNODELET = $00040000; { Delete inhibit }
  40. faNOCOPY = $00080000; { Copy inhibit }
  41. faFILE_MIGRATED = $00400000; { File has been migrated }
  42. faDONT_MIGRATE = $00800000; { Don't migrate this file }
  43. faIMMEDIATE_COMPRESS = $02000000; { Compress this file immediately }
  44. faFILE_COMPRESSED = $04000000; { File is compressed }
  45. faDONT_COMPRESS = $08000000; { Don't compress this file }
  46. faCANT_COMPRESS = $20000000; { Can't compress this file }
  47. faATTR_ARCHIVE = $40000000; { Entry has had an EA modified, }
  48. { an ownerID changed, or trustee }
  49. { info changed, etc. }
  50. implementation
  51. uses
  52. sysconst;
  53. { Include platform independent implementation part }
  54. {$i sysutils.inc}
  55. {****************************************************************************
  56. File Functions
  57. ****************************************************************************}
  58. Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
  59. VAR NWOpenFlags : longint;
  60. BEGIN
  61. NWOpenFlags:=0;
  62. Case (Mode and 3) of
  63. 0 : NWOpenFlags:=NWOpenFlags or O_RDONLY;
  64. 1 : NWOpenFlags:=NWOpenFlags or O_WRONLY;
  65. 2 : NWOpenFlags:=NWOpenFlags or O_RDWR;
  66. end;
  67. FileOpen := _open (pchar(FileName),NWOpenFlags,0);
  68. //!! We need to set locking based on Mode !!
  69. end;
  70. Function FileCreate (Const FileName : String) : Longint;
  71. begin
  72. FileCreate:=_open(Pchar(FileName),O_RdWr or O_Creat or O_Trunc,0);
  73. end;
  74. Function FileCreate (Const FileName : String; mode:longint) : Longint;
  75. begin
  76. FileCreate:=FileCreate (FileName);
  77. end;
  78. Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  79. begin
  80. FileRead:=_read (Handle,@Buffer,Count);
  81. end;
  82. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  83. begin
  84. FileWrite:=_write (Handle,@Buffer,Count);
  85. end;
  86. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  87. begin
  88. FileSeek:=_lseek (Handle,FOffset,Origin);
  89. end;
  90. Function FileSeek (Handle : Longint; FOffset,Origin : Int64) : Int64;
  91. begin
  92. {$warning need to add 64bit FileSeek }
  93. FileSeek:=FileSeek(Handle,Longint(FOffset),Longint(Origin));
  94. end;
  95. Procedure FileClose (Handle : Longint);
  96. begin
  97. _close(Handle);
  98. end;
  99. Function FileTruncate (Handle,Size: Longint) : boolean;
  100. begin
  101. FileTruncate:=(_chsize(Handle,Size) = 0);
  102. end;
  103. Function FileAge (Const FileName : String): Longint;
  104. VAR Info : NWStatBufT;
  105. PTM : PNWTM;
  106. begin
  107. If _stat (pchar(FileName),Info) <> 0 then
  108. exit(-1)
  109. else
  110. begin
  111. PTM := _localtime (Info.st_mtime);
  112. IF PTM = NIL THEN
  113. exit(-1)
  114. else
  115. WITH PTM^ DO
  116. Result:=DateTimeToFileDate(EncodeDate(tm_year+1900,tm_mon+1,tm_mday)+EncodeTime(tm_hour,tm_min,tm_sec,0));
  117. end;
  118. end;
  119. Function FileExists (Const FileName : String) : Boolean;
  120. VAR Info : NWStatBufT;
  121. begin
  122. FileExists:=(_stat(pchar(filename),Info) = 0);
  123. end;
  124. PROCEDURE find_setfields (VAR f : TsearchRec);
  125. VAR T : Dos.DateTime;
  126. BEGIN
  127. WITH F DO
  128. BEGIN
  129. IF FindData.Magic = $AD01 THEN
  130. BEGIN
  131. {attr := FindData.EntryP^.d_attr AND $FF;} // lowest 8 bit -> same as dos
  132. attr := FindData.EntryP^.d_attr; { return complete netware attributes }
  133. UnpackTime(FindData.EntryP^.d_time + (LONGINT (FindData.EntryP^.d_date) SHL 16), T);
  134. time := DateTimeToFileDate(EncodeDate(T.Year,T.Month,T.day)+EncodeTime(T.Hour,T.Min,T.Sec,0));
  135. size := FindData.EntryP^.d_size;
  136. name := strpas (FindData.EntryP^.d_nameDOS);
  137. END ELSE
  138. BEGIN
  139. FillChar (f,SIZEOF(f),0);
  140. END;
  141. END;
  142. END;
  143. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  144. begin
  145. IF path = '' then
  146. exit (18);
  147. Rslt.FindData.DirP := _opendir (pchar(Path));
  148. IF Rslt.FindData.DirP = NIL THEN
  149. exit (18);
  150. IF attr <> faAnyFile THEN
  151. _SetReaddirAttribute (Rslt.FindData.DirP, attr);
  152. Rslt.FindData.Magic := $AD01;
  153. Rslt.FindData.EntryP := _readdir (Rslt.FindData.DirP);
  154. if Rslt.FindData.EntryP = nil then
  155. begin
  156. _closedir (Rslt.FindData.DirP);
  157. Rslt.FindData.DirP := NIL;
  158. result := 18;
  159. end else
  160. begin
  161. find_setfields (Rslt);
  162. result := 0;
  163. end;
  164. end;
  165. Function FindNext (Var Rslt : TSearchRec) : Longint;
  166. begin
  167. IF Rslt.FindData.Magic <> $AD01 THEN
  168. exit (18);
  169. Rslt.FindData.EntryP := _readdir (Rslt.FindData.DirP);
  170. IF Rslt.FindData.EntryP = NIL THEN
  171. exit (18);
  172. find_setfields (Rslt);
  173. result := 0;
  174. end;
  175. Procedure FindClose (Var F : TSearchrec);
  176. begin
  177. IF F.FindData.Magic = $AD01 THEN
  178. BEGIN
  179. IF F.FindData.DirP <> NIL THEN
  180. _closedir (F.FindData.DirP);
  181. F.FindData.Magic := 0;
  182. F.FindData.DirP := NIL;
  183. F.FindData.EntryP := NIL;
  184. END;
  185. end;
  186. Function FileGetDate (Handle : Longint) : Longint;
  187. Var Info : NWStatBufT;
  188. PTM : PNWTM;
  189. begin
  190. If _fstat(Handle,Info) <> 0 then
  191. Result:=-1
  192. else
  193. begin
  194. PTM := _localtime (Info.st_mtime);
  195. IF PTM = NIL THEN
  196. exit(-1)
  197. else
  198. WITH PTM^ DO
  199. Result:=DateTimeToFileDate(EncodeDate(tm_year+1900,tm_mon+1,tm_mday)+EncodeTime(tm_hour,tm_min,tm_sec,0));
  200. end;
  201. end;
  202. Function FileSetDate (Handle,Age : Longint) : Longint;
  203. begin
  204. { i think its impossible under netware from FileHandle. I dident found a way to get the
  205. complete pathname of a filehandle, that would be needed for ChangeDirectoryEntry }
  206. FileSetDate:=-1;
  207. ConsolePrintf ('warning: fpc sysutils.FileSetDate not implemented'#13#10,0);
  208. {$warning FileSetDate not implemented (i think is impossible) }
  209. end;
  210. Function FileGetAttr (Const FileName : String) : Longint;
  211. Var Info : NWStatBufT;
  212. begin
  213. If _stat (pchar(FileName),Info) <> 0 then
  214. Result:=-1
  215. Else
  216. Result := Info.st_attr AND $FFFF;
  217. end;
  218. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  219. VAR MS : NWModifyStructure;
  220. begin
  221. FillChar (MS, SIZEOF (MS), 0);
  222. if _ChangeDirectoryEntry (PChar (Filename), MS, MFileAtrributesBit, 0) <> 0 then
  223. result := -1
  224. else
  225. result := 0;
  226. end;
  227. Function DeleteFile (Const FileName : String) : Boolean;
  228. begin
  229. Result:= (_UnLink (pchar(FileName)) = 0);
  230. end;
  231. Function RenameFile (Const OldName, NewName : String) : Boolean;
  232. begin
  233. RenameFile:=(_rename(pchar(OldName),pchar(NewName)) = 0);
  234. end;
  235. {****************************************************************************
  236. Disk Functions
  237. ****************************************************************************}
  238. {
  239. The Diskfree and Disksize functions need a file on the specified drive, since this
  240. is required for the statfs system call.
  241. These filenames are set in drivestr[0..26], and have been preset to :
  242. 0 - '.' (default drive - hence current dir is ok.)
  243. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  244. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  245. 3 - '/' (C: equivalent of dos is the root partition)
  246. 4..26 (can be set by you're own applications)
  247. ! Use AddDisk() to Add new drives !
  248. They both return -1 when a failure occurs.
  249. }
  250. Const
  251. FixDriveStr : array[0..3] of pchar=(
  252. '.',
  253. 'a:.',
  254. 'b:.',
  255. 'sys:/'
  256. );
  257. var
  258. Drives : byte;
  259. DriveStr : array[4..26] of pchar;
  260. Procedure AddDisk(const path:string);
  261. begin
  262. if not (DriveStr[Drives]=nil) then
  263. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  264. GetMem(DriveStr[Drives],length(Path)+1);
  265. StrPCopy(DriveStr[Drives],path);
  266. inc(Drives);
  267. if Drives>26 then
  268. Drives:=4;
  269. end;
  270. Function DiskFree(Drive: Byte): int64;
  271. //var fs : statfs;
  272. Begin
  273. { if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  274. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  275. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  276. else
  277. Diskfree:=-1;}
  278. DiskFree := -1;
  279. ConsolePrintf ('warning: fpc sysutils.diskfree not implemented'#13#10,0);
  280. {$warning DiskFree not implemented (does it make sense ?) }
  281. End;
  282. Function DiskSize(Drive: Byte): int64;
  283. //var fs : statfs;
  284. Begin
  285. { if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  286. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  287. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  288. else
  289. DiskSize:=-1;}
  290. DiskSize := -1;
  291. ConsolePrintf ('warning: fpc sysutils.disksize not implemented'#13#10,0);
  292. {$warning DiskSize not implemented (does it make sense ?) }
  293. End;
  294. Function GetCurrentDir : String;
  295. begin
  296. GetDir (0,Result);
  297. end;
  298. Function SetCurrentDir (Const NewDir : String) : Boolean;
  299. begin
  300. {$I-}
  301. ChDir(NewDir);
  302. {$I+}
  303. result := (IOResult = 0);
  304. end;
  305. Function CreateDir (Const NewDir : String) : Boolean;
  306. begin
  307. {$I-}
  308. MkDir(NewDir);
  309. {$I+}
  310. result := (IOResult = 0);
  311. end;
  312. Function RemoveDir (Const Dir : String) : Boolean;
  313. begin
  314. {$I-}
  315. RmDir(Dir);
  316. {$I+}
  317. result := (IOResult = 0);
  318. end;
  319. function DirectoryExists (const Directory: string): boolean;
  320. VAR Info : NWStatBufT;
  321. begin
  322. If _stat (pchar(Directory),Info) <> 0 then
  323. exit(false)
  324. else
  325. Exit ((Info.st_attr and faDirectory) <> 0);
  326. end;
  327. {****************************************************************************
  328. Misc Functions
  329. ****************************************************************************}
  330. procedure Beep;
  331. begin
  332. _RingTheBell;
  333. end;
  334. {****************************************************************************
  335. Locale Functions
  336. ****************************************************************************}
  337. Procedure GetLocalTime(var SystemTime: TSystemTime);
  338. var xx : word;
  339. begin
  340. Dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, xx);
  341. Dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, xx);
  342. SystemTime.MilliSecond := 0;
  343. end;
  344. Procedure InitAnsi;
  345. Var i : longint;
  346. begin
  347. { Fill table entries 0 to 127 }
  348. for i := 0 to 96 do
  349. UpperCaseTable[i] := chr(i);
  350. for i := 97 to 122 do
  351. UpperCaseTable[i] := chr(i - 32);
  352. for i := 123 to 191 do
  353. UpperCaseTable[i] := chr(i);
  354. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  355. for i := 0 to 64 do
  356. LowerCaseTable[i] := chr(i);
  357. for i := 65 to 90 do
  358. LowerCaseTable[i] := chr(i + 32);
  359. for i := 91 to 191 do
  360. LowerCaseTable[i] := chr(i);
  361. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  362. end;
  363. Procedure InitInternational;
  364. begin
  365. InitAnsi;
  366. end;
  367. function SysErrorMessage(ErrorCode: Integer): String;
  368. begin
  369. Result:=''; // StrError(ErrorCode);
  370. end;
  371. {****************************************************************************
  372. OS utility functions
  373. ****************************************************************************}
  374. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  375. begin
  376. Result:=StrPas(_getenv(PChar(EnvVar)));
  377. end;
  378. {****************************************************************************
  379. Initialization code
  380. ****************************************************************************}
  381. Initialization
  382. InitExceptions; { Initialize exceptions. OS independent }
  383. InitInternational; { Initialize internationalization settings }
  384. Finalization
  385. DoneExceptions;
  386. end.
  387. {
  388. $Log$
  389. Revision 1.13 2003-11-26 20:00:19 florian
  390. * error handling for Variants improved
  391. Revision 1.12 2003/10/25 23:42:35 hajny
  392. * THandle in sysutils common using System.THandle
  393. Revision 1.11 2003/04/12 13:21:27 armin
  394. * added THandle
  395. Revision 1.10 2003/03/30 12:35:43 armin
  396. * removed uses netware from winsock, DirectoryExists implemented
  397. Revision 1.9 2003/03/29 15:16:26 hajny
  398. * dummy DirectoryExists added
  399. Revision 1.8 2003/02/15 19:12:54 armin
  400. * changes for new threadvar support
  401. Revision 1.7 2002/09/07 16:01:21 peter
  402. * old logs removed and tabs fixed
  403. Revision 1.6 2002/04/01 10:47:31 armin
  404. makefile.fpc for netware
  405. stderr to netware console
  406. free all memory (threadvars and heap) to avoid error message while unloading nlm
  407. Revision 1.5 2002/03/08 19:10:14 armin
  408. * added 64 bit fileseek (currently only 32 bit supported)
  409. }