sysutils.pp 17 KB

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