sysutils.pp 15 KB

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