sysutils.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004-2006 by Karoly Balogh
  4. Sysutils unit for MorphOS
  5. Based on Amiga version by Carl Eric Codere, and other
  6. parts of the RTL
  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. {$MODESWITCH OUT}
  17. { force ansistrings }
  18. {$H+}
  19. {$DEFINE HAS_SLEEP}
  20. { Include platform independent interface part }
  21. {$i sysutilh.inc}
  22. { Platform dependent calls }
  23. Procedure AddDisk(const path:string);
  24. implementation
  25. uses dos,sysconst;
  26. {$DEFINE FPC_FEXPAND_VOLUMES} (* Full paths begin with drive specification *)
  27. {$DEFINE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  28. {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
  29. { used OS file system APIs use ansistring }
  30. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  31. { Include platform independent implementation part }
  32. {$i sysutils.inc}
  33. { * Include MorphOS specific includes * }
  34. {$include execd.inc}
  35. {$include execf.inc}
  36. {$include timerd.inc}
  37. {$include doslibd.inc}
  38. {$include doslibf.inc}
  39. {$include utilf.inc}
  40. { * Followings are implemented in the system unit! * }
  41. function PathConv(path: shortstring): shortstring; external name 'PATHCONV';
  42. function PathConv(path: RawByteString): shortstring; external name 'PATHCONVRBS';
  43. procedure AddToList(var l: Pointer; h: LongInt); external name 'ADDTOLIST';
  44. function RemoveFromList(var l: Pointer; h: LongInt): boolean; external name 'REMOVEFROMLIST';
  45. function CheckInList(var l: Pointer; h: LongInt): pointer; external name 'CHECKINLIST';
  46. var
  47. MOS_fileList: Pointer; external name 'AOS_FILELIST';
  48. function dosLock(const name: String;
  49. accessmode: Longint) : LongInt;
  50. var
  51. buffer: array[0..255] of Char;
  52. begin
  53. move(name[1],buffer,length(name));
  54. buffer[length(name)]:=#0;
  55. dosLock:=Lock(buffer,accessmode);
  56. end;
  57. function AmigaFileDateToDateTime(aDate: TDateStamp; out success: boolean): TDateTime;
  58. var
  59. tmpSecs: DWord;
  60. tmpDate: TDateTime;
  61. tmpTime: TDateTime;
  62. clockData: TClockData;
  63. begin
  64. with aDate do
  65. tmpSecs:=(ds_Days * (24 * 60 * 60)) + (ds_Minute * 60) + (ds_Tick div TICKS_PER_SECOND);
  66. Amiga2Date(tmpSecs,@clockData);
  67. {$WARNING TODO: implement msec values, if possible}
  68. with clockData do begin
  69. success:=TryEncodeDate(year,month,mday,tmpDate) and
  70. TryEncodeTime(hour,min,sec,0,tmpTime);
  71. end;
  72. result:=ComposeDateTime(tmpDate,tmpTime);
  73. end;
  74. {****************************************************************************
  75. File Functions
  76. ****************************************************************************}
  77. {$I-}{ Required for correct usage of these routines }
  78. (****** non portable routines ******)
  79. function FileOpen(const FileName: rawbytestring; Mode: Integer): LongInt;
  80. var
  81. SystemFileName: RawByteString;
  82. dosResult: LongInt;
  83. begin
  84. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  85. {$WARNING FIX ME! To do: FileOpen Access Modes}
  86. dosResult:=Open(PChar(SystemFileName),MODE_OLDFILE);
  87. if dosResult=0 then
  88. dosResult:=-1
  89. else
  90. AddToList(MOS_fileList,dosResult);
  91. FileOpen:=dosResult;
  92. end;
  93. function FileGetDate(Handle: LongInt) : LongInt;
  94. begin
  95. {$WARNING filegetdate call is dummy}
  96. end;
  97. function FileSetDate(Handle, Age: LongInt) : LongInt;
  98. begin
  99. // Impossible under unix from FileHandle !!
  100. FileSetDate:=-1;
  101. end;
  102. function FileCreate(const FileName: RawByteString) : LongInt;
  103. var
  104. SystemFileName: RawByteString;
  105. dosResult: LongInt;
  106. begin
  107. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  108. dosResult:=Open(PChar(FileName),MODE_NEWFILE);
  109. if dosResult=0 then
  110. dosResult:=-1
  111. else
  112. AddToList(MOS_fileList,dosResult);
  113. FileCreate:=dosResult;
  114. end;
  115. function FileCreate(const FileName: RawByteString; Rights: integer): LongInt;
  116. begin
  117. {$WARNING FIX ME! To do: FileCreate Access Modes}
  118. FileCreate:=FileCreate(FileName);
  119. end;
  120. function FileCreate(const FileName: RawByteString; ShareMode: integer; Rights : Integer): LongInt;
  121. begin
  122. {$WARNING FIX ME! To do: FileCreate Access Modes}
  123. FileCreate:=FileCreate(FileName);
  124. end;
  125. function FileRead(Handle: LongInt; Out Buffer; Count: LongInt): LongInt;
  126. begin
  127. FileRead:=-1;
  128. if (Count<=0) or (Handle<=0) then exit;
  129. FileRead:=dosRead(Handle,@Buffer,Count);
  130. end;
  131. function FileWrite(Handle: LongInt; const Buffer; Count: LongInt): LongInt;
  132. begin
  133. FileWrite:=-1;
  134. if (Count<=0) or (Handle<=0) then exit;
  135. FileWrite:=dosWrite(Handle,@Buffer,Count);
  136. end;
  137. function FileSeek(Handle, FOffset, Origin: LongInt) : LongInt;
  138. var
  139. seekMode: LongInt;
  140. begin
  141. FileSeek:=-1;
  142. if (Handle<=0) then exit;
  143. case Origin of
  144. fsFromBeginning: seekMode:=OFFSET_BEGINNING;
  145. fsFromCurrent : seekMode:=OFFSET_CURRENT;
  146. fsFromEnd : seekMode:=OFFSET_END;
  147. end;
  148. FileSeek:=dosSeek(Handle, FOffset, seekMode);
  149. end;
  150. function FileSeek(Handle: LongInt; FOffset: Int64; Origin: Longint): Int64;
  151. begin
  152. {$WARNING Need to add 64bit call }
  153. FileSeek:=FileSeek(Handle,LongInt(FOffset),LongInt(Origin));
  154. end;
  155. procedure FileClose(Handle: LongInt);
  156. begin
  157. if (Handle<=0) then exit;
  158. dosClose(Handle);
  159. RemoveFromList(MOS_fileList,Handle);
  160. end;
  161. function FileTruncate(Handle: longint; Size: Int64): Boolean;
  162. var
  163. dosResult: LongInt;
  164. begin
  165. FileTruncate:=False;
  166. if Size > high (longint) then exit;
  167. {$WARNING Possible support for 64-bit FS to be checked!}
  168. if (Handle<=0) then exit;
  169. dosResult:=SetFileSize(Handle, Size, OFFSET_BEGINNING);
  170. if (dosResult<0) then exit;
  171. FileTruncate:=True;
  172. end;
  173. function DeleteFile(const FileName: RawByteString) : Boolean;
  174. var
  175. SystemFileName: RawByteString;
  176. begin
  177. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  178. DeleteFile:=dosDeleteFile(PChar(SystemFileName));
  179. end;
  180. function RenameFile(const OldName, NewName: string): Boolean;
  181. var
  182. OldSystemFileName, NewSystemFileName: RawByteString;
  183. begin
  184. OldSystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(OldName));
  185. NewSystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(NewName));
  186. RenameFile:=dosRename(PChar(OldSystemFileName), PChar(NewSystemFileName));
  187. end;
  188. (****** end of non portable routines ******)
  189. function FileAge (const FileName : String): Longint;
  190. var
  191. tmpName: String;
  192. tmpLock: Longint;
  193. tmpFIB : PFileInfoBlock;
  194. tmpDateTime: TDateTime;
  195. validFile: boolean;
  196. begin
  197. validFile:=false;
  198. tmpName := PathConv(FileName);
  199. tmpLock := dosLock(tmpName, SHARED_LOCK);
  200. if (tmpLock <> 0) then begin
  201. new(tmpFIB);
  202. if Examine(tmpLock,tmpFIB) then begin
  203. tmpDateTime:=AmigaFileDateToDateTime(tmpFIB^.fib_Date,validFile);
  204. end;
  205. Unlock(tmpLock);
  206. dispose(tmpFIB);
  207. end;
  208. if validFile then
  209. result:=DateTimeToFileDate(tmpDateTime)
  210. else
  211. result:=-1;
  212. end;
  213. function FileExists (const FileName : RawByteString) : Boolean;
  214. var
  215. tmpLock: LongInt;
  216. tmpFIB : PFileInfoBlock;
  217. SystemFileName: RawByteString;
  218. begin
  219. result:=false;
  220. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  221. tmpLock := dosLock(PChar(SystemFileName), SHARED_LOCK);
  222. if (tmpLock <> 0) then begin
  223. new(tmpFIB);
  224. if Examine(tmpLock,tmpFIB) and (tmpFIB^.fib_DirEntryType <= 0) then
  225. result:=true;
  226. Unlock(tmpLock);
  227. dispose(tmpFIB);
  228. end;
  229. end;
  230. function FindFirst(const Path: String; Attr : Longint; out Rslt: TSearchRec): Longint;
  231. var
  232. tmpStr: array[0..255] of Char;
  233. Anchor: PAnchorPath;
  234. tmpDateTime: TDateTime;
  235. validDate: boolean;
  236. begin
  237. result:=-1; { We emulate Linux/Unix behaviour, and return -1 on errors. }
  238. tmpStr:=PathConv(path)+#0;
  239. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  240. Rslt.ExcludeAttr := (not Attr) and ($1e);
  241. Rslt.FindHandle := 0;
  242. new(Anchor);
  243. FillChar(Anchor^,sizeof(TAnchorPath),#0);
  244. if MatchFirst(@tmpStr,Anchor)<>0 then exit;
  245. Rslt.FindHandle := longint(Anchor);
  246. with Anchor^.ap_Info do begin
  247. Rslt.Name := StrPas(fib_FileName);
  248. Rslt.Size := fib_Size;
  249. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  250. if not validDate then exit;
  251. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  252. Rslt.Attr := 128;
  253. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  254. if ((fib_Protection and FIBF_READ) <> 0) and
  255. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  256. result:=0; { Return zero if everything went OK }
  257. end;
  258. end;
  259. function FindNext (var Rslt : TSearchRec): Longint;
  260. var
  261. Anchor: PAnchorPath;
  262. validDate: boolean;
  263. begin
  264. result:=-1;
  265. Anchor:=PAnchorPath(Rslt.FindHandle);
  266. if not assigned(Anchor) then exit;
  267. if MatchNext(Anchor) <> 0 then exit;
  268. with Anchor^.ap_Info do begin
  269. Rslt.Name := StrPas(fib_FileName);
  270. Rslt.Size := fib_Size;
  271. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  272. if not validDate then exit;
  273. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  274. Rslt.Attr := 128;
  275. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  276. if ((fib_Protection and FIBF_READ) <> 0) and
  277. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  278. result:=0; { Return zero if everything went OK }
  279. end;
  280. end;
  281. procedure FindClose(var f: TSearchRec);
  282. var
  283. Anchor: PAnchorPath;
  284. begin
  285. Anchor:=PAnchorPath(f.FindHandle);
  286. if not assigned(Anchor) then exit;
  287. MatchEnd(Anchor);
  288. Dispose(Anchor);
  289. end;
  290. (****** end of non portable routines ******)
  291. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  292. var
  293. F: file;
  294. attr: word;
  295. begin
  296. Assign(F,FileName);
  297. dos.GetFAttr(F,attr);
  298. if DosError <> 0 then
  299. FileGetAttr := -1
  300. else
  301. FileGetAttr := Attr;
  302. end;
  303. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  304. var
  305. F: file;
  306. begin
  307. Assign(F, FileName);
  308. Dos.SetFAttr(F, Attr and $ffff);
  309. FileSetAttr := DosError;
  310. end;
  311. {****************************************************************************
  312. Disk Functions
  313. ****************************************************************************}
  314. {
  315. The Diskfree and Disksize functions need a file on the specified drive, since this
  316. is required for the statfs system call.
  317. These filenames are set in drivestr[0..26], and have been preset to :
  318. 0 - '.' (default drive - hence current dir is ok.)
  319. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  320. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  321. 3 - '/' (C: equivalent of dos is the root partition)
  322. 4..26 (can be set by you're own applications)
  323. ! Use AddDisk() to Add new drives !
  324. They both return -1 when a failure occurs.
  325. }
  326. Const
  327. FixDriveStr : array[0..3] of pchar=(
  328. '.',
  329. '/fd0/.',
  330. '/fd1/.',
  331. '/.'
  332. );
  333. var
  334. Drives : byte;
  335. DriveStr : array[4..26] of pchar;
  336. Procedure AddDisk(const path:string);
  337. begin
  338. if not (DriveStr[Drives]=nil) then
  339. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  340. GetMem(DriveStr[Drives],length(Path)+1);
  341. StrPCopy(DriveStr[Drives],path);
  342. inc(Drives);
  343. if Drives>26 then
  344. Drives:=4;
  345. end;
  346. Function DiskFree(Drive: Byte): int64;
  347. Begin
  348. DiskFree := dos.diskFree(Drive);
  349. End;
  350. Function DiskSize(Drive: Byte): int64;
  351. Begin
  352. DiskSize := dos.DiskSize(Drive);
  353. End;
  354. function GetCurrentDir : String;
  355. begin
  356. GetDir (0,Result);
  357. end;
  358. Function SetCurrentDir (Const NewDir : String) : Boolean;
  359. begin
  360. ChDir(NewDir);
  361. result := (IOResult = 0);
  362. end;
  363. Function CreateDir (Const NewDir : String) : Boolean;
  364. begin
  365. MkDir(NewDir);
  366. result := (IOResult = 0);
  367. end;
  368. Function RemoveDir (Const Dir : String) : Boolean;
  369. begin
  370. RmDir(Dir);
  371. result := (IOResult = 0);
  372. end;
  373. function DirectoryExists(const Directory: RawBytetring): Boolean;
  374. var
  375. tmpStr : String;
  376. tmpLock: LongInt;
  377. FIB : PFileInfoBlock;
  378. SystemFileName: RawByteString;
  379. begin
  380. result:=false;
  381. if (Directory='') or (InOutRes<>0) then exit;
  382. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  383. tmpLock:=dosLock(PChar(SystemFileName),SHARED_LOCK);
  384. if tmpLock=0 then exit;
  385. FIB:=nil; new(FIB);
  386. if (Examine(tmpLock,FIB)=True) and (FIB^.fib_DirEntryType>0) then
  387. result:=True;
  388. if tmpLock<>0 then Unlock(tmpLock);
  389. if assigned(FIB) then dispose(FIB);
  390. end;
  391. {****************************************************************************
  392. Misc Functions
  393. ****************************************************************************}
  394. procedure SysBeep;
  395. begin
  396. // TODO
  397. end;
  398. {****************************************************************************
  399. Locale Functions
  400. ****************************************************************************}
  401. Procedure GetLocalTime(var SystemTime: TSystemTime);
  402. var
  403. dayOfWeek: word;
  404. begin
  405. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
  406. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  407. end;
  408. Procedure InitAnsi;
  409. Var
  410. i : longint;
  411. begin
  412. { Fill table entries 0 to 127 }
  413. for i := 0 to 96 do
  414. UpperCaseTable[i] := chr(i);
  415. for i := 97 to 122 do
  416. UpperCaseTable[i] := chr(i - 32);
  417. for i := 123 to 191 do
  418. UpperCaseTable[i] := chr(i);
  419. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  420. for i := 0 to 64 do
  421. LowerCaseTable[i] := chr(i);
  422. for i := 65 to 90 do
  423. LowerCaseTable[i] := chr(i + 32);
  424. for i := 91 to 191 do
  425. LowerCaseTable[i] := chr(i);
  426. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  427. end;
  428. Procedure InitInternational;
  429. begin
  430. InitInternationalGeneric;
  431. InitAnsi;
  432. end;
  433. function SysErrorMessage(ErrorCode: Integer): String;
  434. begin
  435. { Result:=StrError(ErrorCode);}
  436. end;
  437. {****************************************************************************
  438. OS utility functions
  439. ****************************************************************************}
  440. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  441. begin
  442. Result:=Dos.Getenv(shortstring(EnvVar));
  443. end;
  444. Function GetEnvironmentVariableCount : Integer;
  445. begin
  446. // Result:=FPCCountEnvVar(EnvP);
  447. Result:=Dos.envCount;
  448. end;
  449. Function GetEnvironmentString(Index : Integer) : String;
  450. begin
  451. // Result:=FPCGetEnvStrFromP(Envp,Index);
  452. Result:=Dos.EnvStr(Index);
  453. end;
  454. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString;Flags:TExecuteFlags=[]):
  455. integer;
  456. var
  457. CommandLine: AnsiString;
  458. E: EOSError;
  459. begin
  460. Dos.Exec (Path, ComLine);
  461. if DosError <> 0 then begin
  462. if ComLine = '' then
  463. CommandLine := Path
  464. else
  465. CommandLine := Path + ' ' + ComLine;
  466. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  467. E.ErrorCode := DosError;
  468. raise E;
  469. end;
  470. end;
  471. function ExecuteProcess (const Path: AnsiString;
  472. const ComLine: array of AnsiString;Flags:TExecuteFlags=[]): integer;
  473. var
  474. CommandLine: AnsiString;
  475. I: integer;
  476. begin
  477. Commandline := '';
  478. for I := 0 to High (ComLine) do
  479. if Pos (' ', ComLine [I]) <> 0 then
  480. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  481. else
  482. CommandLine := CommandLine + ' ' + Comline [I];
  483. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  484. end;
  485. procedure Sleep(Milliseconds: cardinal);
  486. begin
  487. // Amiga dos.library Delay() has precision of 1/50 seconds
  488. Delay(Milliseconds div 20);
  489. end;
  490. {****************************************************************************
  491. Initialization code
  492. ****************************************************************************}
  493. Initialization
  494. InitExceptions;
  495. InitInternational; { Initialize internationalization settings }
  496. Finalization
  497. DoneExceptions;
  498. end.