sysutils.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. var
  93. tmpFIB : PFileInfoBlock;
  94. tmpDateTime: TDateTime;
  95. validFile: boolean;
  96. begin
  97. validFile:=false;
  98. if (Handle <> 0) then begin
  99. new(tmpFIB);
  100. if ExamineFH(Handle,tmpFIB) then begin
  101. tmpDateTime:=AmigaFileDateToDateTime(tmpFIB^.fib_Date,validFile);
  102. end;
  103. dispose(tmpFIB);
  104. end;
  105. if validFile then
  106. result:=DateTimeToFileDate(tmpDateTime)
  107. else
  108. result:=-1;
  109. end;
  110. function FileSetDate(Handle, Age: LongInt) : LongInt;
  111. begin
  112. // Impossible under unix from FileHandle !!
  113. FileSetDate:=-1;
  114. end;
  115. function FileCreate(const FileName: string) : LongInt;
  116. var
  117. dosResult: LongInt;
  118. tmpStr : array[0..255] of char;
  119. begin
  120. tmpStr:=PathConv(FileName)+#0;
  121. dosResult:=Open(@tmpStr,MODE_NEWFILE);
  122. if dosResult=0 then
  123. dosResult:=-1
  124. else
  125. AddToList(MOS_fileList,dosResult);
  126. FileCreate:=dosResult;
  127. end;
  128. function FileCreate(const FileName: string; Rights: integer): LongInt;
  129. begin
  130. {$WARNING FIX ME! To do: FileCreate Access Modes}
  131. FileCreate:=FileCreate(FileName);
  132. end;
  133. function FileCreate(const FileName: string; ShareMode: integer; Rights : integer): LongInt;
  134. begin
  135. {$WARNING FIX ME! To do: FileCreate Access Modes}
  136. FileCreate:=FileCreate(FileName);
  137. end;
  138. function FileRead(Handle: LongInt; out Buffer; Count: LongInt): LongInt;
  139. begin
  140. FileRead:=-1;
  141. if (Count<=0) or (Handle<=0) then exit;
  142. FileRead:=dosRead(Handle,@Buffer,Count);
  143. end;
  144. function FileWrite(Handle: LongInt; const Buffer; Count: LongInt): LongInt;
  145. begin
  146. FileWrite:=-1;
  147. if (Count<=0) or (Handle<=0) then exit;
  148. FileWrite:=dosWrite(Handle,@Buffer,Count);
  149. end;
  150. function FileSeek(Handle, FOffset, Origin: LongInt) : LongInt;
  151. var
  152. seekMode: LongInt;
  153. begin
  154. FileSeek:=-1;
  155. if (Handle<=0) then exit;
  156. case Origin of
  157. fsFromBeginning: seekMode:=OFFSET_BEGINNING;
  158. fsFromCurrent : seekMode:=OFFSET_CURRENT;
  159. fsFromEnd : seekMode:=OFFSET_END;
  160. end;
  161. FileSeek:=dosSeek(Handle, FOffset, seekMode);
  162. end;
  163. function FileSeek(Handle: LongInt; FOffset: Int64; Origin: Longint): Int64;
  164. begin
  165. {$WARNING Need to add 64bit call }
  166. FileSeek:=FileSeek(Handle,LongInt(FOffset),LongInt(Origin));
  167. end;
  168. procedure FileClose(Handle: LongInt);
  169. begin
  170. if (Handle<=0) then exit;
  171. dosClose(Handle);
  172. RemoveFromList(MOS_fileList,Handle);
  173. end;
  174. function FileTruncate(Handle: THandle; Size: Int64): Boolean;
  175. var
  176. dosResult: LongInt;
  177. begin
  178. FileTruncate:=False;
  179. if Size > high (longint) then exit;
  180. {$WARNING Possible support for 64-bit FS to be checked!}
  181. if (Handle<=0) then exit;
  182. dosResult:=SetFileSize(Handle, Size, OFFSET_BEGINNING);
  183. if (dosResult<0) then exit;
  184. FileTruncate:=True;
  185. end;
  186. function DeleteFile(const FileName: string) : Boolean;
  187. var
  188. tmpStr: array[0..255] of char;
  189. begin
  190. tmpStr:=PathConv(FileName)+#0;
  191. DeleteFile:=dosDeleteFile(@tmpStr);
  192. end;
  193. function RenameFile(const OldName, NewName: string): Boolean;
  194. var
  195. tmpOldName, tmpNewName: array[0..255] of char;
  196. begin
  197. tmpOldName:=PathConv(OldName)+#0;
  198. tmpNewName:=PathConv(NewName)+#0;
  199. RenameFile:=dosRename(tmpOldName, tmpNewName);
  200. end;
  201. (****** end of non portable routines ******)
  202. function FileAge (const FileName : String): Longint;
  203. var
  204. tmpName: String;
  205. tmpLock: Longint;
  206. tmpFIB : PFileInfoBlock;
  207. tmpDateTime: TDateTime;
  208. validFile: boolean;
  209. begin
  210. validFile:=false;
  211. tmpName := PathConv(FileName);
  212. tmpLock := dosLock(tmpName, SHARED_LOCK);
  213. if (tmpLock <> 0) then begin
  214. new(tmpFIB);
  215. if Examine(tmpLock,tmpFIB) then begin
  216. tmpDateTime:=AmigaFileDateToDateTime(tmpFIB^.fib_Date,validFile);
  217. end;
  218. Unlock(tmpLock);
  219. dispose(tmpFIB);
  220. end;
  221. if validFile then
  222. result:=DateTimeToFileDate(tmpDateTime)
  223. else
  224. result:=-1;
  225. end;
  226. function FileExists (const FileName : String) : Boolean;
  227. var
  228. tmpName: String;
  229. tmpLock: LongInt;
  230. tmpFIB : PFileInfoBlock;
  231. begin
  232. result:=false;
  233. tmpName := PathConv(FileName);
  234. tmpLock := dosLock(tmpName, SHARED_LOCK);
  235. if (tmpLock <> 0) then begin
  236. new(tmpFIB);
  237. if Examine(tmpLock,tmpFIB) and (tmpFIB^.fib_DirEntryType <= 0) then
  238. result:=true;
  239. Unlock(tmpLock);
  240. dispose(tmpFIB);
  241. end;
  242. end;
  243. function FindFirst(const Path: String; Attr : Longint; out Rslt: TSearchRec): Longint;
  244. var
  245. tmpStr: array[0..255] of Char;
  246. Anchor: PAnchorPath;
  247. tmpDateTime: TDateTime;
  248. validDate: boolean;
  249. begin
  250. result:=-1; { We emulate Linux/Unix behaviour, and return -1 on errors. }
  251. tmpStr:=PathConv(path)+#0;
  252. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  253. Rslt.ExcludeAttr := (not Attr) and ($1e);
  254. Rslt.FindHandle := 0;
  255. new(Anchor);
  256. FillChar(Anchor^,sizeof(TAnchorPath),#0);
  257. if MatchFirst(@tmpStr,Anchor)<>0 then exit;
  258. Rslt.FindHandle := longint(Anchor);
  259. with Anchor^.ap_Info do begin
  260. Rslt.Name := StrPas(fib_FileName);
  261. Rslt.Size := fib_Size;
  262. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  263. if not validDate then exit;
  264. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  265. Rslt.Attr := 128;
  266. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  267. if ((fib_Protection and FIBF_READ) <> 0) and
  268. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  269. result:=0; { Return zero if everything went OK }
  270. end;
  271. end;
  272. function FindNext (var Rslt : TSearchRec): Longint;
  273. var
  274. Anchor: PAnchorPath;
  275. validDate: boolean;
  276. begin
  277. result:=-1;
  278. Anchor:=PAnchorPath(Rslt.FindHandle);
  279. if not assigned(Anchor) then exit;
  280. if MatchNext(Anchor) <> 0 then exit;
  281. with Anchor^.ap_Info do begin
  282. Rslt.Name := StrPas(fib_FileName);
  283. Rslt.Size := fib_Size;
  284. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  285. if not validDate then exit;
  286. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  287. Rslt.Attr := 128;
  288. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  289. if ((fib_Protection and FIBF_READ) <> 0) and
  290. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  291. result:=0; { Return zero if everything went OK }
  292. end;
  293. end;
  294. procedure FindClose(var f: TSearchRec);
  295. var
  296. Anchor: PAnchorPath;
  297. begin
  298. Anchor:=PAnchorPath(f.FindHandle);
  299. if not assigned(Anchor) then exit;
  300. MatchEnd(Anchor);
  301. Dispose(Anchor);
  302. end;
  303. (****** end of non portable routines ******)
  304. Function FileGetAttr (Const FileName : String) : Longint;
  305. var
  306. F: file;
  307. attr: word;
  308. begin
  309. Assign(F,FileName);
  310. dos.GetFAttr(F,attr);
  311. if DosError <> 0 then
  312. FileGetAttr := -1
  313. else
  314. FileGetAttr := Attr;
  315. end;
  316. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  317. var
  318. F: file;
  319. begin
  320. Assign(F, FileName);
  321. Dos.SetFAttr(F, Attr and $ffff);
  322. FileSetAttr := DosError;
  323. end;
  324. {****************************************************************************
  325. Disk Functions
  326. ****************************************************************************}
  327. {
  328. The Diskfree and Disksize functions need a file on the specified drive, since this
  329. is required for the statfs system call.
  330. These filenames are set in drivestr[0..26], and have been preset to :
  331. 0 - '.' (default drive - hence current dir is ok.)
  332. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  333. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  334. 3 - '/' (C: equivalent of dos is the root partition)
  335. 4..26 (can be set by you're own applications)
  336. ! Use AddDisk() to Add new drives !
  337. They both return -1 when a failure occurs.
  338. }
  339. Const
  340. FixDriveStr : array[0..3] of pchar=(
  341. '.',
  342. '/fd0/.',
  343. '/fd1/.',
  344. '/.'
  345. );
  346. var
  347. Drives : byte;
  348. DriveStr : array[4..26] of pchar;
  349. Procedure AddDisk(const path:string);
  350. begin
  351. if not (DriveStr[Drives]=nil) then
  352. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  353. GetMem(DriveStr[Drives],length(Path)+1);
  354. StrPCopy(DriveStr[Drives],path);
  355. inc(Drives);
  356. if Drives>26 then
  357. Drives:=4;
  358. end;
  359. Function DiskFree(Drive: Byte): int64;
  360. Begin
  361. DiskFree := dos.diskFree(Drive);
  362. End;
  363. Function DiskSize(Drive: Byte): int64;
  364. Begin
  365. DiskSize := dos.DiskSize(Drive);
  366. End;
  367. function GetCurrentDir : String;
  368. begin
  369. GetDir (0,Result);
  370. end;
  371. Function SetCurrentDir (Const NewDir : String) : Boolean;
  372. begin
  373. ChDir(NewDir);
  374. result := (IOResult = 0);
  375. end;
  376. Function CreateDir (Const NewDir : String) : Boolean;
  377. begin
  378. MkDir(NewDir);
  379. result := (IOResult = 0);
  380. end;
  381. Function RemoveDir (Const Dir : String) : Boolean;
  382. begin
  383. RmDir(Dir);
  384. result := (IOResult = 0);
  385. end;
  386. function DirectoryExists(const Directory: string): Boolean;
  387. var
  388. tmpStr : String;
  389. tmpLock: LongInt;
  390. FIB : PFileInfoBlock;
  391. begin
  392. result:=false;
  393. if (Directory='') or (InOutRes<>0) then exit;
  394. tmpStr:=PathConv(Directory);
  395. tmpLock:=dosLock(tmpStr,SHARED_LOCK);
  396. if tmpLock=0 then exit;
  397. FIB:=nil; new(FIB);
  398. if (Examine(tmpLock,FIB)=True) and (FIB^.fib_DirEntryType>0) then
  399. result:=True;
  400. if tmpLock<>0 then Unlock(tmpLock);
  401. if assigned(FIB) then dispose(FIB);
  402. end;
  403. {****************************************************************************
  404. Locale Functions
  405. ****************************************************************************}
  406. Procedure GetLocalTime(var SystemTime: TSystemTime);
  407. var
  408. dayOfWeek: word;
  409. begin
  410. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
  411. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  412. end;
  413. Procedure InitAnsi;
  414. Var
  415. i : longint;
  416. begin
  417. { Fill table entries 0 to 127 }
  418. for i := 0 to 96 do
  419. UpperCaseTable[i] := chr(i);
  420. for i := 97 to 122 do
  421. UpperCaseTable[i] := chr(i - 32);
  422. for i := 123 to 191 do
  423. UpperCaseTable[i] := chr(i);
  424. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  425. for i := 0 to 64 do
  426. LowerCaseTable[i] := chr(i);
  427. for i := 65 to 90 do
  428. LowerCaseTable[i] := chr(i + 32);
  429. for i := 91 to 191 do
  430. LowerCaseTable[i] := chr(i);
  431. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  432. end;
  433. Procedure InitInternational;
  434. begin
  435. InitInternationalGeneric;
  436. InitAnsi;
  437. end;
  438. function SysErrorMessage(ErrorCode: Integer): String;
  439. begin
  440. { Result:=StrError(ErrorCode);}
  441. end;
  442. function GetLastOSError: Integer;
  443. begin
  444. result:=-1;
  445. end;
  446. {****************************************************************************
  447. OS utility functions
  448. ****************************************************************************}
  449. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  450. begin
  451. Result:=Dos.Getenv(shortstring(EnvVar));
  452. end;
  453. Function GetEnvironmentVariableCount : Integer;
  454. begin
  455. // Result:=FPCCountEnvVar(EnvP);
  456. Result:=Dos.envCount;
  457. end;
  458. Function GetEnvironmentString(Index : Integer) : String;
  459. begin
  460. // Result:=FPCGetEnvStrFromP(Envp,Index);
  461. Result:=Dos.EnvStr(Index);
  462. end;
  463. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString;Flags:TExecuteFlags=[]):
  464. integer;
  465. var
  466. tmpPath: AnsiString;
  467. convPath: AnsiString;
  468. CommandLine: AnsiString;
  469. tmpLock: longint;
  470. E: EOSError;
  471. begin
  472. DosError:= 0;
  473. convPath:=PathConv(Path);
  474. tmpPath:=convPath+' '+ComLine;
  475. { Here we must first check if the command we wish to execute }
  476. { actually exists, because this is NOT handled by the }
  477. { _SystemTagList call (program will abort!!) }
  478. { Try to open with shared lock }
  479. tmpLock:=Lock(PChar(convPath),SHARED_LOCK);
  480. if tmpLock<>0 then
  481. begin
  482. { File exists - therefore unlock it }
  483. Unlock(tmpLock);
  484. result:=SystemTagList(PChar(tmpPath),nil);
  485. { on return of -1 the shell could not be executed }
  486. { probably because there was not enough memory }
  487. if result = -1 then
  488. DosError:=8;
  489. end
  490. else
  491. DosError:=3;
  492. if DosError <> 0 then begin
  493. if ComLine = '' then
  494. CommandLine := Path
  495. else
  496. CommandLine := Path + ' ' + ComLine;
  497. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  498. E.ErrorCode := DosError;
  499. raise E;
  500. end;
  501. end;
  502. function ExecuteProcess (const Path: AnsiString;
  503. const ComLine: array of AnsiString;Flags:TExecuteFlags=[]): integer;
  504. var
  505. CommandLine: AnsiString;
  506. I: integer;
  507. begin
  508. Commandline := '';
  509. for I := 0 to High (ComLine) do
  510. if Pos (' ', ComLine [I]) <> 0 then
  511. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  512. else
  513. CommandLine := CommandLine + ' ' + Comline [I];
  514. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  515. end;
  516. procedure Sleep (Milliseconds: cardinal);
  517. begin
  518. // Amiga/MorphOS dos.library Delay() has precision of 1/50 seconds
  519. Delay(Milliseconds div 20);
  520. end;
  521. {****************************************************************************
  522. Initialization code
  523. ****************************************************************************}
  524. Initialization
  525. InitExceptions;
  526. InitInternational; { Initialize internationalization settings }
  527. OnBeep:=Nil; { No SysBeep() on MorphOS, for now. Figure out if we want
  528. to use intuition.library/DisplayBeep() for this (KB) }
  529. Finalization
  530. DoneExceptions;
  531. end.