sysutils.pp 15 KB

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