sysutils.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. {$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 'MOS_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: THandle; 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. Locale Functions
  390. ****************************************************************************}
  391. Procedure GetLocalTime(var SystemTime: TSystemTime);
  392. var
  393. dayOfWeek: word;
  394. begin
  395. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
  396. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  397. end;
  398. Procedure InitAnsi;
  399. Var
  400. i : longint;
  401. begin
  402. { Fill table entries 0 to 127 }
  403. for i := 0 to 96 do
  404. UpperCaseTable[i] := chr(i);
  405. for i := 97 to 122 do
  406. UpperCaseTable[i] := chr(i - 32);
  407. for i := 123 to 191 do
  408. UpperCaseTable[i] := chr(i);
  409. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  410. for i := 0 to 64 do
  411. LowerCaseTable[i] := chr(i);
  412. for i := 65 to 90 do
  413. LowerCaseTable[i] := chr(i + 32);
  414. for i := 91 to 191 do
  415. LowerCaseTable[i] := chr(i);
  416. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  417. end;
  418. Procedure InitInternational;
  419. begin
  420. InitInternationalGeneric;
  421. InitAnsi;
  422. end;
  423. function SysErrorMessage(ErrorCode: Integer): String;
  424. begin
  425. { Result:=StrError(ErrorCode);}
  426. end;
  427. function GetLastOSError: Integer;
  428. begin
  429. result:=-1;
  430. end;
  431. {****************************************************************************
  432. OS utility functions
  433. ****************************************************************************}
  434. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  435. begin
  436. Result:=Dos.Getenv(shortstring(EnvVar));
  437. end;
  438. Function GetEnvironmentVariableCount : Integer;
  439. begin
  440. // Result:=FPCCountEnvVar(EnvP);
  441. Result:=Dos.envCount;
  442. end;
  443. Function GetEnvironmentString(Index : Integer) : String;
  444. begin
  445. // Result:=FPCGetEnvStrFromP(Envp,Index);
  446. Result:=Dos.EnvStr(Index);
  447. end;
  448. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString;Flags:TExecuteFlags=[]):
  449. integer;
  450. var
  451. tmpPath: AnsiString;
  452. convPath: AnsiString;
  453. CommandLine: AnsiString;
  454. tmpLock: longint;
  455. E: EOSError;
  456. begin
  457. DosError:= 0;
  458. convPath:=PathConv(Path);
  459. tmpPath:=convPath+' '+ComLine;
  460. { Here we must first check if the command we wish to execute }
  461. { actually exists, because this is NOT handled by the }
  462. { _SystemTagList call (program will abort!!) }
  463. { Try to open with shared lock }
  464. tmpLock:=Lock(PChar(convPath),SHARED_LOCK);
  465. if tmpLock<>0 then
  466. begin
  467. { File exists - therefore unlock it }
  468. Unlock(tmpLock);
  469. result:=SystemTagList(PChar(tmpPath),nil);
  470. { on return of -1 the shell could not be executed }
  471. { probably because there was not enough memory }
  472. if result = -1 then
  473. DosError:=8;
  474. end
  475. else
  476. DosError:=3;
  477. if DosError <> 0 then begin
  478. if ComLine = '' then
  479. CommandLine := Path
  480. else
  481. CommandLine := Path + ' ' + ComLine;
  482. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  483. E.ErrorCode := DosError;
  484. raise E;
  485. end;
  486. end;
  487. function ExecuteProcess (const Path: AnsiString;
  488. const ComLine: array of AnsiString;Flags:TExecuteFlags=[]): integer;
  489. var
  490. CommandLine: AnsiString;
  491. I: integer;
  492. begin
  493. Commandline := '';
  494. for I := 0 to High (ComLine) do
  495. if Pos (' ', ComLine [I]) <> 0 then
  496. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  497. else
  498. CommandLine := CommandLine + ' ' + Comline [I];
  499. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  500. end;
  501. procedure Sleep (Milliseconds: cardinal);
  502. begin
  503. // Amiga/MorphOS dos.library Delay() has precision of 1/50 seconds
  504. Delay(Milliseconds div 20);
  505. end;
  506. {****************************************************************************
  507. Initialization code
  508. ****************************************************************************}
  509. Initialization
  510. InitExceptions;
  511. InitInternational; { Initialize internationalization settings }
  512. OnBeep:=Nil; { No SysBeep() on MorphOS, for now. Figure out if we want
  513. to use intuition.library/DisplayBeep() for this (KB) }
  514. Finalization
  515. DoneExceptions;
  516. end.