sysutils.pp 15 KB

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