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. { force ansistrings }
  17. {$H+}
  18. {$DEFINE HAS_SLEEP}
  19. { Include platform independent interface part }
  20. {$i sysutilh.inc}
  21. { Platform dependent calls }
  22. Procedure AddDisk(const path:string);
  23. implementation
  24. uses dos,sysconst;
  25. {$DEFINE FPC_FEXPAND_VOLUMES} (* Full paths begin with drive specification *)
  26. {$DEFINE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  27. {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
  28. { Include platform independent implementation part }
  29. {$i sysutils.inc}
  30. { * Include MorphOS specific includes * }
  31. {$include execd.inc}
  32. {$include execf.inc}
  33. {$include timerd.inc}
  34. {$include doslibd.inc}
  35. {$include doslibf.inc}
  36. {$include utilf.inc}
  37. { * Followings are implemented in the system unit! * }
  38. function PathConv(path: shortstring): shortstring; external name 'PATHCONV';
  39. procedure AddToList(var l: Pointer; h: LongInt); external name 'ADDTOLIST';
  40. function RemoveFromList(var l: Pointer; h: LongInt): boolean; external name 'REMOVEFROMLIST';
  41. function CheckInList(var l: Pointer; h: LongInt): pointer; external name 'CHECKINLIST';
  42. var
  43. MOS_fileList: Pointer; external name 'MOS_FILELIST';
  44. function dosLock(const name: String;
  45. accessmode: Longint) : LongInt;
  46. var
  47. buffer: array[0..255] of Char;
  48. begin
  49. move(name[1],buffer,length(name));
  50. buffer[length(name)]:=#0;
  51. dosLock:=Lock(buffer,accessmode);
  52. end;
  53. function AmigaFileDateToDateTime(aDate: TDateStamp; out success: boolean): TDateTime;
  54. var
  55. tmpSecs: DWord;
  56. tmpDate: TDateTime;
  57. tmpTime: TDateTime;
  58. clockData: TClockData;
  59. begin
  60. with aDate do
  61. tmpSecs:=(ds_Days * (24 * 60 * 60)) + (ds_Minute * 60) + (ds_Tick div TICKS_PER_SECOND);
  62. Amiga2Date(tmpSecs,@clockData);
  63. {$WARNING TODO: implement msec values, if possible}
  64. with clockData do begin
  65. success:=TryEncodeDate(year,month,mday,tmpDate) and
  66. TryEncodeTime(hour,min,sec,0,tmpTime);
  67. end;
  68. result:=ComposeDateTime(tmpDate,tmpTime);
  69. end;
  70. {****************************************************************************
  71. File Functions
  72. ****************************************************************************}
  73. {$I-}{ Required for correct usage of these routines }
  74. (****** non portable routines ******)
  75. function FileOpen(const FileName: string; Mode: Integer): LongInt;
  76. var
  77. dosResult: LongInt;
  78. tmpStr : array[0..255] of char;
  79. begin
  80. {$WARNING FIX ME! To do: FileOpen Access Modes}
  81. tmpStr:=PathConv(FileName)+#0;
  82. dosResult:=Open(@tmpStr,MODE_OLDFILE);
  83. if dosResult=0 then
  84. dosResult:=-1
  85. else
  86. AddToList(MOS_fileList,dosResult);
  87. FileOpen:=dosResult;
  88. end;
  89. function FileGetDate(Handle: LongInt) : LongInt;
  90. begin
  91. {$WARNING filegetdate call is dummy}
  92. end;
  93. function FileSetDate(Handle, Age: LongInt) : LongInt;
  94. begin
  95. // Impossible under unix from FileHandle !!
  96. FileSetDate:=-1;
  97. end;
  98. function FileCreate(const FileName: string) : LongInt;
  99. var
  100. dosResult: LongInt;
  101. tmpStr : array[0..255] of char;
  102. begin
  103. tmpStr:=PathConv(FileName)+#0;
  104. dosResult:=Open(@tmpStr,MODE_NEWFILE);
  105. if dosResult=0 then
  106. dosResult:=-1
  107. else
  108. AddToList(MOS_fileList,dosResult);
  109. FileCreate:=dosResult;
  110. end;
  111. function FileCreate(const FileName: string; Mode: integer): LongInt;
  112. begin
  113. {$WARNING FIX ME! To do: FileCreate Access Modes}
  114. FileCreate:=FileCreate(FileName);
  115. end;
  116. function FileRead(Handle: LongInt; var Buffer; Count: LongInt): LongInt;
  117. begin
  118. FileRead:=-1;
  119. if (Count<=0) or (Handle<=0) then exit;
  120. FileRead:=dosRead(Handle,@Buffer,Count);
  121. end;
  122. function FileWrite(Handle: LongInt; const Buffer; Count: LongInt): LongInt;
  123. begin
  124. FileWrite:=-1;
  125. if (Count<=0) or (Handle<=0) then exit;
  126. FileWrite:=dosWrite(Handle,@Buffer,Count);
  127. end;
  128. function FileSeek(Handle, FOffset, Origin: LongInt) : LongInt;
  129. var
  130. seekMode: LongInt;
  131. begin
  132. FileSeek:=-1;
  133. if (Handle<=0) then exit;
  134. case Origin of
  135. fsFromBeginning: seekMode:=OFFSET_BEGINNING;
  136. fsFromCurrent : seekMode:=OFFSET_CURRENT;
  137. fsFromEnd : seekMode:=OFFSET_END;
  138. end;
  139. FileSeek:=dosSeek(Handle, FOffset, seekMode);
  140. end;
  141. function FileSeek(Handle: LongInt; FOffset: Int64; Origin: Longint): Int64;
  142. begin
  143. {$WARNING Need to add 64bit call }
  144. FileSeek:=FileSeek(Handle,LongInt(FOffset),LongInt(Origin));
  145. end;
  146. procedure FileClose(Handle: LongInt);
  147. begin
  148. if (Handle<=0) then exit;
  149. dosClose(Handle);
  150. RemoveFromList(MOS_fileList,Handle);
  151. end;
  152. function FileTruncate(Handle: THandle; Size: Int64): Boolean;
  153. var
  154. dosResult: LongInt;
  155. begin
  156. FileTruncate:=False;
  157. if Size > high (longint) then exit;
  158. {$WARNING Possible support for 64-bit FS to be checked!}
  159. if (Handle<=0) then exit;
  160. dosResult:=SetFileSize(Handle, Size, OFFSET_BEGINNING);
  161. if (dosResult<0) then exit;
  162. FileTruncate:=True;
  163. end;
  164. function DeleteFile(const FileName: string) : Boolean;
  165. var
  166. tmpStr: array[0..255] of char;
  167. begin
  168. tmpStr:=PathConv(FileName)+#0;
  169. DeleteFile:=dosDeleteFile(@tmpStr);
  170. end;
  171. function RenameFile(const OldName, NewName: string): Boolean;
  172. var
  173. tmpOldName, tmpNewName: array[0..255] of char;
  174. begin
  175. tmpOldName:=PathConv(OldName)+#0;
  176. tmpNewName:=PathConv(NewName)+#0;
  177. RenameFile:=dosRename(tmpOldName, tmpNewName);
  178. end;
  179. (****** end of non portable routines ******)
  180. function FileAge (const FileName : String): Longint;
  181. var
  182. tmpName: String;
  183. tmpLock: Longint;
  184. tmpFIB : PFileInfoBlock;
  185. tmpDateTime: TDateTime;
  186. validFile: boolean;
  187. begin
  188. validFile:=false;
  189. tmpName := PathConv(FileName);
  190. tmpLock := dosLock(tmpName, SHARED_LOCK);
  191. if (tmpLock <> 0) then begin
  192. new(tmpFIB);
  193. if Examine(tmpLock,tmpFIB) then begin
  194. tmpDateTime:=AmigaFileDateToDateTime(tmpFIB^.fib_Date,validFile);
  195. end;
  196. Unlock(tmpLock);
  197. dispose(tmpFIB);
  198. end;
  199. if validFile then
  200. result:=DateTimeToFileDate(tmpDateTime)
  201. else
  202. result:=-1;
  203. end;
  204. function FileExists (const FileName : String) : Boolean;
  205. var
  206. tmpName: String;
  207. tmpLock: LongInt;
  208. tmpFIB : PFileInfoBlock;
  209. begin
  210. result:=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) and (tmpFIB^.fib_DirEntryType <= 0) then
  216. result:=true;
  217. Unlock(tmpLock);
  218. dispose(tmpFIB);
  219. end;
  220. end;
  221. function FindFirst(const Path: String; Attr : Longint; out Rslt: TSearchRec): Longint;
  222. var
  223. tmpStr: array[0..255] of Char;
  224. Anchor: PAnchorPath;
  225. tmpDateTime: TDateTime;
  226. validDate: boolean;
  227. begin
  228. result:=-1; { We emulate Linux/Unix behaviour, and return -1 on errors. }
  229. tmpStr:=PathConv(path)+#0;
  230. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  231. Rslt.ExcludeAttr := (not Attr) and ($1e);
  232. Rslt.FindHandle := 0;
  233. new(Anchor);
  234. FillChar(Anchor^,sizeof(TAnchorPath),#0);
  235. if MatchFirst(@tmpStr,Anchor)<>0 then exit;
  236. Rslt.FindHandle := longint(Anchor);
  237. with Anchor^.ap_Info do begin
  238. Rslt.Name := StrPas(fib_FileName);
  239. Rslt.Size := fib_Size;
  240. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  241. if not validDate then exit;
  242. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  243. Rslt.Attr := 128;
  244. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  245. if ((fib_Protection and FIBF_READ) <> 0) and
  246. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  247. result:=0; { Return zero if everything went OK }
  248. end;
  249. end;
  250. function FindNext (var Rslt : TSearchRec): Longint;
  251. var
  252. Anchor: PAnchorPath;
  253. validDate: boolean;
  254. begin
  255. result:=-1;
  256. Anchor:=PAnchorPath(Rslt.FindHandle);
  257. if not assigned(Anchor) then exit;
  258. if MatchNext(Anchor) <> 0 then exit;
  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. procedure FindClose(var f: TSearchRec);
  273. var
  274. Anchor: PAnchorPath;
  275. begin
  276. Anchor:=PAnchorPath(f.FindHandle);
  277. if not assigned(Anchor) then exit;
  278. MatchEnd(Anchor);
  279. Dispose(Anchor);
  280. end;
  281. (****** end of non portable routines ******)
  282. Function FileGetAttr (Const FileName : String) : Longint;
  283. var
  284. F: file;
  285. attr: word;
  286. begin
  287. Assign(F,FileName);
  288. dos.GetFAttr(F,attr);
  289. if DosError <> 0 then
  290. FileGetAttr := -1
  291. else
  292. FileGetAttr := Attr;
  293. end;
  294. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  295. var
  296. F: file;
  297. begin
  298. Assign(F, FileName);
  299. Dos.SetFAttr(F, Attr and $ffff);
  300. FileSetAttr := DosError;
  301. end;
  302. {****************************************************************************
  303. Disk Functions
  304. ****************************************************************************}
  305. {
  306. The Diskfree and Disksize functions need a file on the specified drive, since this
  307. is required for the statfs system call.
  308. These filenames are set in drivestr[0..26], and have been preset to :
  309. 0 - '.' (default drive - hence current dir is ok.)
  310. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  311. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  312. 3 - '/' (C: equivalent of dos is the root partition)
  313. 4..26 (can be set by you're own applications)
  314. ! Use AddDisk() to Add new drives !
  315. They both return -1 when a failure occurs.
  316. }
  317. Const
  318. FixDriveStr : array[0..3] of pchar=(
  319. '.',
  320. '/fd0/.',
  321. '/fd1/.',
  322. '/.'
  323. );
  324. var
  325. Drives : byte;
  326. DriveStr : array[4..26] of pchar;
  327. Procedure AddDisk(const path:string);
  328. begin
  329. if not (DriveStr[Drives]=nil) then
  330. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  331. GetMem(DriveStr[Drives],length(Path)+1);
  332. StrPCopy(DriveStr[Drives],path);
  333. inc(Drives);
  334. if Drives>26 then
  335. Drives:=4;
  336. end;
  337. Function DiskFree(Drive: Byte): int64;
  338. Begin
  339. DiskFree := dos.diskFree(Drive);
  340. End;
  341. Function DiskSize(Drive: Byte): int64;
  342. Begin
  343. DiskSize := dos.DiskSize(Drive);
  344. End;
  345. function GetCurrentDir : String;
  346. begin
  347. GetDir (0,Result);
  348. end;
  349. Function SetCurrentDir (Const NewDir : String) : Boolean;
  350. begin
  351. ChDir(NewDir);
  352. result := (IOResult = 0);
  353. end;
  354. Function CreateDir (Const NewDir : String) : Boolean;
  355. begin
  356. MkDir(NewDir);
  357. result := (IOResult = 0);
  358. end;
  359. Function RemoveDir (Const Dir : String) : Boolean;
  360. begin
  361. RmDir(Dir);
  362. result := (IOResult = 0);
  363. end;
  364. function DirectoryExists(const Directory: string): Boolean;
  365. var
  366. tmpStr : String;
  367. tmpLock: LongInt;
  368. FIB : PFileInfoBlock;
  369. begin
  370. result:=false;
  371. if (Directory='') or (InOutRes<>0) then exit;
  372. tmpStr:=PathConv(Directory);
  373. tmpLock:=dosLock(tmpStr,SHARED_LOCK);
  374. if tmpLock=0 then exit;
  375. FIB:=nil; new(FIB);
  376. if (Examine(tmpLock,FIB)=True) and (FIB^.fib_DirEntryType>0) then
  377. result:=True;
  378. if tmpLock<>0 then Unlock(tmpLock);
  379. if assigned(FIB) then dispose(FIB);
  380. end;
  381. {****************************************************************************
  382. Misc Functions
  383. ****************************************************************************}
  384. procedure Beep;
  385. begin
  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):
  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): 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. Finalization
  508. DoneExceptions;
  509. end.