sysutils.pp 15 KB

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