sysutils.pp 16 KB

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