sysutils.pp 16 KB

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