sysutils.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. Sysutils unit for netware
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit sysutils;
  13. interface
  14. {$MODE objfpc}
  15. {$MODESWITCH OUT}
  16. { force ansistrings }
  17. {$H+}
  18. uses DOS;
  19. {$I nwsys.inc}
  20. {$I errno.inc}
  21. {$DEFINE HAS_SLEEP}
  22. {$DEFINE HAS_OSERROR}
  23. { used OS file system APIs use ansistring }
  24. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  25. { OS has an ansistring/single byte environment variable API }
  26. {$define SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
  27. TYPE
  28. TNetwareFindData =
  29. RECORD
  30. DirP : PNWDirEnt; { used for opendir }
  31. EntryP: PNWDirEnt; { and readdir }
  32. Magic : WORD; { to avoid abends with uninitialized TSearchRec }
  33. END;
  34. { Include platform independent interface part }
  35. {$i sysutilh.inc}
  36. { additional NetWare file flags}
  37. CONST
  38. faSHARE = $00000080; { Sharable file }
  39. faNO_SUBALLOC = $00000800; { Don't sub alloc. this file }
  40. faTRANS = $00001000; { Transactional file (TTS usable) }
  41. faREADAUD = $00004000; { Read audit }
  42. faWRITAUD = $00008000; { Write audit }
  43. faIMMPURG = $00010000; { Immediate purge }
  44. faNORENAM = $00020000; { Rename inhibit }
  45. faNODELET = $00040000; { Delete inhibit }
  46. faNOCOPY = $00080000; { Copy inhibit }
  47. faFILE_MIGRATED = $00400000; { File has been migrated }
  48. faDONT_MIGRATE = $00800000; { Don't migrate this file }
  49. faIMMEDIATE_COMPRESS = $02000000; { Compress this file immediately }
  50. faFILE_COMPRESSED = $04000000; { File is compressed }
  51. faDONT_COMPRESS = $08000000; { Don't compress this file }
  52. faCANT_COMPRESS = $20000000; { Can't compress this file }
  53. faATTR_ARCHIVE = $40000000; { Entry has had an EA modified, }
  54. { an ownerID changed, or trustee }
  55. { info changed, etc. }
  56. implementation
  57. uses
  58. sysconst;
  59. {$define FPC_FEXPAND_DRIVES}
  60. {$define FPC_FEXPAND_VOLUMES}
  61. {$define FPC_FEXPAND_NO_DEFAULT_PATHS}
  62. { Include platform independent implementation part }
  63. {$i sysutils.inc}
  64. {****************************************************************************
  65. File Functions
  66. ****************************************************************************}
  67. Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
  68. VAR NWOpenFlags : longint;
  69. SystemFileName: RawByteString;
  70. begin
  71. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  72. NWOpenFlags:=0;
  73. Case (Mode and 3) of
  74. 0 : NWOpenFlags:=NWOpenFlags or O_RDONLY;
  75. 1 : NWOpenFlags:=NWOpenFlags or O_WRONLY;
  76. 2 : NWOpenFlags:=NWOpenFlags or O_RDWR;
  77. end;
  78. FileOpen := _open (pchar(SystemFileName),NWOpenFlags,0);
  79. //!! We need to set locking based on Mode !!
  80. end;
  81. Function FileCreate (Const FileName : RawByteString) : THandle;
  82. VAR SystemFileName: RawByteString;
  83. begin
  84. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  85. FileCreate:=_open(Pchar(SystemFileName),O_RdWr or O_Creat or O_Trunc,0);
  86. end;
  87. Function FileCreate (Const FileName : RawByteString; Rights:longint) : THandle;
  88. begin
  89. FileCreate:=FileCreate (FileName);
  90. end;
  91. Function FileCreate (Const FileName : RawByteString; ShareMode: Longint; Rights:longint) : THandle;
  92. begin
  93. FileCreate:=FileCreate (FileName);
  94. end;
  95. Function FileRead (Handle : THandle; Out Buffer; Count : longint) : longint;
  96. begin
  97. FileRead:=_read (Handle,@Buffer,Count);
  98. end;
  99. Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : longint;
  100. begin
  101. FileWrite:=_write (Handle,@Buffer,Count);
  102. end;
  103. Function FileSeek (Handle : THandle; FOffset,Origin : Longint) : Longint;
  104. begin
  105. FileSeek:=_lseek (Handle,FOffset,Origin);
  106. end;
  107. Function FileSeek (Handle : THandle; FOffset: Int64; Origin: Longint) : Int64;
  108. begin
  109. {$warning need to add 64bit FileSeek }
  110. FileSeek:=FileSeek(Handle,Longint(FOffset),Longint(Origin));
  111. end;
  112. Procedure FileClose (Handle : THandle);
  113. begin
  114. _close(Handle);
  115. end;
  116. Function FileTruncate (Handle : THandle; Size: Int64) : boolean;
  117. begin
  118. if Size > high (longint) then
  119. FileTruncate := false
  120. {$WARNING Possible support for 64-bit FS to be checked!}
  121. else
  122. FileTruncate:=(_chsize(Handle,Size) = 0);
  123. end;
  124. Function FileLock (Handle,FOffset,FLen : Longint) : Longint;
  125. begin
  126. FileLock := _lock (Handle,FOffset,FLen);
  127. end;
  128. Function FileLock (Handle : Longint; FOffset,FLen : Int64) : Longint;
  129. begin
  130. {$warning need to add 64bit FileLock call }
  131. FileLock := FileLock (Handle, longint(FOffset),longint(FLen));
  132. end;
  133. Function FileUnlock (Handle,FOffset,FLen : Longint) : Longint;
  134. begin
  135. FileUnlock := _unlock (Handle,FOffset,FLen);
  136. end;
  137. Function FileUnlock (Handle : Longint; FOffset,FLen : Int64) : Longint;
  138. begin
  139. {$warning need to add 64bit FileUnlock call }
  140. FileUnlock := FileUnlock (Handle, longint(FOffset),longint(FLen));
  141. end;
  142. Function FileAge (Const FileName : String): Longint;
  143. VAR Info : NWStatBufT;
  144. PTM : PNWTM;
  145. begin
  146. If _stat (pchar(FileName),Info) <> 0 then
  147. exit(-1)
  148. else
  149. begin
  150. PTM := _localtime (Info.st_mtime);
  151. IF PTM = NIL THEN
  152. exit(-1)
  153. else
  154. WITH PTM^ DO
  155. Result:=DateTimeToFileDate(EncodeDate(tm_year+1900,tm_mon+1,tm_mday)+EncodeTime(tm_hour,tm_min,tm_sec,0));
  156. end;
  157. end;
  158. Function FileExists (Const FileName : RawByteString) : Boolean;
  159. VAR Info : NWStatBufT;
  160. SystemFileName: RawByteString;
  161. begin
  162. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  163. FileExists:=(_stat(pchar(SystemFileName),Info) = 0);
  164. end;
  165. PROCEDURE find_setfields (VAR f : TsearchRec);
  166. VAR T : Dos.DateTime;
  167. BEGIN
  168. WITH F DO
  169. BEGIN
  170. IF FindData.Magic = $AD01 THEN
  171. BEGIN
  172. {attr := FindData.EntryP^.d_attr AND $FF;} // lowest 8 bit -> same as dos
  173. attr := FindData.EntryP^.d_attr; { return complete netware attributes }
  174. UnpackTime(FindData.EntryP^.d_time + (LONGINT (FindData.EntryP^.d_date) SHL 16), T);
  175. time := DateTimeToFileDate(EncodeDate(T.Year,T.Month,T.day)+EncodeTime(T.Hour,T.Min,T.Sec,0));
  176. size := FindData.EntryP^.d_size;
  177. name := strpas (FindData.EntryP^.d_nameDOS);
  178. END ELSE
  179. BEGIN
  180. FillChar (f,SIZEOF(f),0);
  181. END;
  182. END;
  183. END;
  184. Function FindFirst (Const Path : String; Attr : Longint; out Rslt : TSearchRec) : Longint;
  185. begin
  186. IF path = '' then
  187. exit (18);
  188. Rslt.FindData.DirP := _opendir (pchar(Path));
  189. IF Rslt.FindData.DirP = NIL THEN
  190. exit (18);
  191. IF attr <> faAnyFile THEN
  192. _SetReaddirAttribute (Rslt.FindData.DirP, attr);
  193. Rslt.FindData.Magic := $AD01;
  194. Rslt.FindData.EntryP := _readdir (Rslt.FindData.DirP);
  195. if Rslt.FindData.EntryP = nil then
  196. begin
  197. _closedir (Rslt.FindData.DirP);
  198. Rslt.FindData.DirP := NIL;
  199. result := 18;
  200. end else
  201. begin
  202. find_setfields (Rslt);
  203. result := 0;
  204. end;
  205. end;
  206. Function FindNext (Var Rslt : TSearchRec) : Longint;
  207. begin
  208. IF Rslt.FindData.Magic <> $AD01 THEN
  209. exit (18);
  210. Rslt.FindData.EntryP := _readdir (Rslt.FindData.DirP);
  211. IF Rslt.FindData.EntryP = NIL THEN
  212. exit (18);
  213. find_setfields (Rslt);
  214. result := 0;
  215. end;
  216. Procedure FindClose (Var F : TSearchrec);
  217. begin
  218. IF F.FindData.Magic = $AD01 THEN
  219. BEGIN
  220. IF F.FindData.DirP <> NIL THEN
  221. _closedir (F.FindData.DirP);
  222. F.FindData.Magic := 0;
  223. F.FindData.DirP := NIL;
  224. F.FindData.EntryP := NIL;
  225. END;
  226. end;
  227. Function FileGetDate (Handle : THandle) : Longint;
  228. Var Info : NWStatBufT;
  229. PTM : PNWTM;
  230. begin
  231. If _fstat(Handle,Info) <> 0 then
  232. Result:=-1
  233. else
  234. begin
  235. PTM := _localtime (Info.st_mtime);
  236. IF PTM = NIL THEN
  237. exit(-1)
  238. else
  239. WITH PTM^ DO
  240. Result:=DateTimeToFileDate(EncodeDate(tm_year+1900,tm_mon+1,tm_mday)+EncodeTime(tm_hour,tm_min,tm_sec,0));
  241. end;
  242. end;
  243. Function FileSetDate (Handle : THandle; Age : Longint) : Longint;
  244. begin
  245. { i think its impossible under netware from FileHandle. I dident found a way to get the
  246. complete pathname of a filehandle, that would be needed for ChangeDirectoryEntry }
  247. FileSetDate:=-1;
  248. ConsolePrintf ('warning: fpc sysutils.FileSetDate not implemented'#13#10,0);
  249. {$warning FileSetDate not implemented (i think is impossible) }
  250. end;
  251. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  252. Var Info : NWStatBufT;
  253. SystemFileName: RawByteString;
  254. begin
  255. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  256. If _stat (pchar(SystemFileName),Info) <> 0 then
  257. Result:=-1
  258. Else
  259. Result := Info.st_attr AND $FFFF;
  260. end;
  261. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  262. VAR MS : NWModifyStructure;
  263. SystemFileName: RawByteString;
  264. begin
  265. { The Attr parameter is not used! }
  266. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  267. FillChar (MS, SIZEOF (MS), 0);
  268. if _ChangeDirectoryEntry (PChar (SystemFilename), MS, MFileAtrributesBit, 0) <> 0 then
  269. result := -1
  270. else
  271. result := 0;
  272. end;
  273. Function DeleteFile (Const FileName : RawByteString) : Boolean;
  274. var
  275. SystemFileName: RawByteString;
  276. begin
  277. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  278. Result:= (_UnLink (pchar(SystemFileName)) = 0);
  279. end;
  280. Function RenameFile (Const OldName, NewName : RawByteString) : Boolean;
  281. var
  282. OldSystemFileName, NewSystemFileName: RawByteString;
  283. begin
  284. OldSystemFileName:=ToSingleByteFileSystemEncodedFileName(OldName);
  285. NewSystemFileName:=ToSingleByteFileSystemEncodedFileName(NewName);
  286. RenameFile:=(_rename(pchar(OldSystemFileName),pchar(NewSystemFileName)) = 0);
  287. end;
  288. {****************************************************************************
  289. Disk Functions
  290. ****************************************************************************}
  291. {
  292. The Diskfree and Disksize functions need a file on the specified drive, since this
  293. is required for the statfs system call.
  294. These filenames are set in drivestr[0..26], and have been preset to :
  295. 0 - '.' (default drive - hence current dir is ok.)
  296. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  297. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  298. 3 - '/' (C: equivalent of dos is the root partition)
  299. 4..26 (can be set by you're own applications)
  300. ! Use AddDisk() to Add new drives !
  301. They both return -1 when a failure occurs.
  302. }
  303. Const
  304. FixDriveStr : array[0..3] of pchar=(
  305. '.',
  306. 'a:.',
  307. 'b:.',
  308. 'sys:/'
  309. );
  310. var
  311. Drives : byte;
  312. DriveStr : array[4..26] of pchar;
  313. Procedure AddDisk(const path:string);
  314. begin
  315. if not (DriveStr[Drives]=nil) then
  316. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  317. GetMem(DriveStr[Drives],length(Path)+1);
  318. StrPCopy(DriveStr[Drives],path);
  319. inc(Drives);
  320. if Drives>26 then
  321. Drives:=4;
  322. end;
  323. Function DiskFree(Drive: Byte): int64;
  324. //var fs : statfs;
  325. Begin
  326. { if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  327. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  328. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  329. else
  330. Diskfree:=-1;}
  331. DiskFree := -1;
  332. ConsolePrintf ('warning: fpc sysutils.diskfree not implemented'#13#10,0);
  333. {$warning DiskFree not implemented (does it make sense ?) }
  334. End;
  335. Function DiskSize(Drive: Byte): int64;
  336. //var fs : statfs;
  337. Begin
  338. { if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  339. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  340. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  341. else
  342. DiskSize:=-1;}
  343. DiskSize := -1;
  344. ConsolePrintf ('warning: fpc sysutils.disksize not implemented'#13#10,0);
  345. {$warning DiskSize not implemented (does it make sense ?) }
  346. End;
  347. function DirectoryExists (const Directory: string): boolean;
  348. var
  349. Info : NWStatBufT;
  350. SystemFileName: RawByteString;
  351. begin
  352. SystemFileName:=ToSingleByteFileSystemEncodedFileName(Directory);
  353. If _stat (pchar(SystemFileName),Info) <> 0 then
  354. exit(false)
  355. else
  356. Exit ((Info.st_attr and faDirectory) <> 0);
  357. end;
  358. {****************************************************************************
  359. Misc Functions
  360. ****************************************************************************}
  361. procedure SysBeep;
  362. begin
  363. _RingTheBell;
  364. end;
  365. {****************************************************************************
  366. Locale Functions
  367. ****************************************************************************}
  368. Procedure GetLocalTime(var SystemTime: TSystemTime);
  369. var xx : word;
  370. begin
  371. Dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, xx);
  372. Dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, xx);
  373. SystemTime.MilliSecond := 0;
  374. end;
  375. Procedure InitAnsi;
  376. Var i : longint;
  377. begin
  378. { Fill table entries 0 to 127 }
  379. for i := 0 to 96 do
  380. UpperCaseTable[i] := chr(i);
  381. for i := 97 to 122 do
  382. UpperCaseTable[i] := chr(i - 32);
  383. for i := 123 to 191 do
  384. UpperCaseTable[i] := chr(i);
  385. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  386. for i := 0 to 64 do
  387. LowerCaseTable[i] := chr(i);
  388. for i := 65 to 90 do
  389. LowerCaseTable[i] := chr(i + 32);
  390. for i := 91 to 191 do
  391. LowerCaseTable[i] := chr(i);
  392. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  393. end;
  394. Procedure InitInternational;
  395. begin
  396. InitInternationalGeneric;
  397. InitAnsi;
  398. end;
  399. function SysErrorMessage(ErrorCode: Integer): String;
  400. begin
  401. Result:=''; // StrError(ErrorCode);
  402. end;
  403. {****************************************************************************
  404. OS utility functions
  405. ****************************************************************************}
  406. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  407. begin
  408. Result:=_getenv(PChar(EnvVar));
  409. end;
  410. Function GetEnvironmentVariableCount : Integer;
  411. begin
  412. // Result:=FPCCountEnvVar(EnvP);
  413. Result:=0;
  414. end;
  415. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  416. begin
  417. // Result:=FPCGetEnvStrFromP(Envp,Index);
  418. Result:='';
  419. end;
  420. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
  421. var
  422. e : EOSError;
  423. CommandLine: AnsiString;
  424. begin
  425. dos.exec(path,comline);
  426. if (Dos.DosError <> 0) then
  427. begin
  428. if ComLine <> '' then
  429. CommandLine := Path + ' ' + ComLine
  430. else
  431. CommandLine := Path;
  432. e:=EOSError.CreateFmt(SExecuteProcessFailed,[CommandLine,Dos.DosError]);
  433. e.ErrorCode:=Dos.DosError;
  434. raise e;
  435. end;
  436. Result := DosExitCode;
  437. end;
  438. function ExecuteProcess (const Path: AnsiString;
  439. const ComLine: array of AnsiString;Flags:TExecuteFlags=[]): integer;
  440. var
  441. CommandLine: AnsiString;
  442. I: integer;
  443. begin
  444. Commandline := '';
  445. for I := 0 to High (ComLine) do
  446. if Pos (' ', ComLine [I]) <> 0 then
  447. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  448. else
  449. CommandLine := CommandLine + ' ' + Comline [I];
  450. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  451. end;
  452. procedure Sleep(milliseconds: Cardinal);
  453. begin
  454. _delay (milliseconds);
  455. end;
  456. Function GetLastOSError : Integer;
  457. begin
  458. Result:=Integer(__get_errno_ptr^);
  459. end;
  460. {****************************************************************************
  461. Initialization code
  462. ****************************************************************************}
  463. Initialization
  464. InitExceptions; { Initialize exceptions. OS independent }
  465. InitInternational; { Initialize internationalization settings }
  466. OnBeep:=@SysBeep;
  467. Finalization
  468. DoneExceptions;
  469. end.