sysutils.pp 15 KB

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