sysutils.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 'MOS_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. Rslt.Name := tmpStr;
  228. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  229. Rslt.Attr := Attr or 128;
  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.Size := fib_Size;
  239. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  240. if not validDate then exit;
  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.Size := fib_Size;
  258. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  259. if not validDate then exit;
  260. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  261. if ((fib_Protection and FIBF_READ) <> 0) and
  262. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  263. result:=0; { Return zero if everything went OK }
  264. end;
  265. end;
  266. procedure FindClose(var f: TSearchRec);
  267. var
  268. Anchor: PAnchorPath;
  269. begin
  270. Anchor:=PAnchorPath(f.FindHandle);
  271. if not assigned(Anchor) then exit;
  272. MatchEnd(Anchor);
  273. Dispose(Anchor);
  274. end;
  275. (****** end of non portable routines ******)
  276. Function FileGetAttr (Const FileName : String) : Longint;
  277. var
  278. F: file;
  279. attr: word;
  280. begin
  281. Assign(F,FileName);
  282. dos.GetFAttr(F,attr);
  283. if DosError <> 0 then
  284. FileGetAttr := -1
  285. else
  286. FileGetAttr := Attr;
  287. end;
  288. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  289. var
  290. F: file;
  291. begin
  292. Assign(F, FileName);
  293. Dos.SetFAttr(F, Attr and $ffff);
  294. FileSetAttr := DosError;
  295. end;
  296. {****************************************************************************
  297. Disk Functions
  298. ****************************************************************************}
  299. {
  300. The Diskfree and Disksize functions need a file on the specified drive, since this
  301. is required for the statfs system call.
  302. These filenames are set in drivestr[0..26], and have been preset to :
  303. 0 - '.' (default drive - hence current dir is ok.)
  304. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  305. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  306. 3 - '/' (C: equivalent of dos is the root partition)
  307. 4..26 (can be set by you're own applications)
  308. ! Use AddDisk() to Add new drives !
  309. They both return -1 when a failure occurs.
  310. }
  311. Const
  312. FixDriveStr : array[0..3] of pchar=(
  313. '.',
  314. '/fd0/.',
  315. '/fd1/.',
  316. '/.'
  317. );
  318. var
  319. Drives : byte;
  320. DriveStr : array[4..26] of pchar;
  321. Procedure AddDisk(const path:string);
  322. begin
  323. if not (DriveStr[Drives]=nil) then
  324. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  325. GetMem(DriveStr[Drives],length(Path)+1);
  326. StrPCopy(DriveStr[Drives],path);
  327. inc(Drives);
  328. if Drives>26 then
  329. Drives:=4;
  330. end;
  331. Function DiskFree(Drive: Byte): int64;
  332. Begin
  333. DiskFree := dos.diskFree(Drive);
  334. End;
  335. Function DiskSize(Drive: Byte): int64;
  336. Begin
  337. DiskSize := dos.DiskSize(Drive);
  338. End;
  339. function GetCurrentDir : String;
  340. begin
  341. GetDir (0,Result);
  342. end;
  343. Function SetCurrentDir (Const NewDir : String) : Boolean;
  344. begin
  345. ChDir(NewDir);
  346. result := (IOResult = 0);
  347. end;
  348. Function CreateDir (Const NewDir : String) : Boolean;
  349. begin
  350. MkDir(NewDir);
  351. result := (IOResult = 0);
  352. end;
  353. Function RemoveDir (Const Dir : String) : Boolean;
  354. begin
  355. RmDir(Dir);
  356. result := (IOResult = 0);
  357. end;
  358. function DirectoryExists(const Directory: string): Boolean;
  359. var
  360. tmpStr : String;
  361. tmpLock: LongInt;
  362. FIB : PFileInfoBlock;
  363. begin
  364. result:=false;
  365. if (Directory='') or (InOutRes<>0) then exit;
  366. tmpStr:=PathConv(Directory);
  367. tmpLock:=dosLock(tmpStr,SHARED_LOCK);
  368. if tmpLock=0 then exit;
  369. FIB:=nil; new(FIB);
  370. if (Examine(tmpLock,FIB)=True) and (FIB^.fib_DirEntryType>0) then
  371. result:=True;
  372. if tmpLock<>0 then Unlock(tmpLock);
  373. if assigned(FIB) then dispose(FIB);
  374. end;
  375. {****************************************************************************
  376. Misc Functions
  377. ****************************************************************************}
  378. procedure Beep;
  379. begin
  380. end;
  381. {****************************************************************************
  382. Locale Functions
  383. ****************************************************************************}
  384. Procedure GetLocalTime(var SystemTime: TSystemTime);
  385. var
  386. dayOfWeek: word;
  387. begin
  388. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
  389. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  390. end;
  391. Procedure InitAnsi;
  392. Var
  393. i : longint;
  394. begin
  395. { Fill table entries 0 to 127 }
  396. for i := 0 to 96 do
  397. UpperCaseTable[i] := chr(i);
  398. for i := 97 to 122 do
  399. UpperCaseTable[i] := chr(i - 32);
  400. for i := 123 to 191 do
  401. UpperCaseTable[i] := chr(i);
  402. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  403. for i := 0 to 64 do
  404. LowerCaseTable[i] := chr(i);
  405. for i := 65 to 90 do
  406. LowerCaseTable[i] := chr(i + 32);
  407. for i := 91 to 191 do
  408. LowerCaseTable[i] := chr(i);
  409. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  410. end;
  411. Procedure InitInternational;
  412. begin
  413. InitInternationalGeneric;
  414. InitAnsi;
  415. end;
  416. function SysErrorMessage(ErrorCode: Integer): String;
  417. begin
  418. { Result:=StrError(ErrorCode);}
  419. end;
  420. {****************************************************************************
  421. OS utility functions
  422. ****************************************************************************}
  423. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  424. begin
  425. Result:=Dos.Getenv(shortstring(EnvVar));
  426. end;
  427. Function GetEnvironmentVariableCount : Integer;
  428. begin
  429. // Result:=FPCCountEnvVar(EnvP);
  430. Result:=Dos.envCount;
  431. end;
  432. Function GetEnvironmentString(Index : Integer) : String;
  433. begin
  434. // Result:=FPCGetEnvStrFromP(Envp,Index);
  435. Result:=Dos.EnvStr(Index);
  436. end;
  437. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString):
  438. integer;
  439. var
  440. CommandLine: AnsiString;
  441. E: EOSError;
  442. begin
  443. Dos.Exec (Path, ComLine);
  444. if DosError <> 0 then begin
  445. if ComLine = '' then
  446. CommandLine := Path
  447. else
  448. CommandLine := Path + ' ' + ComLine;
  449. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  450. E.ErrorCode := DosError;
  451. raise E;
  452. end;
  453. end;
  454. function ExecuteProcess (const Path: AnsiString;
  455. const ComLine: array of AnsiString): integer;
  456. var
  457. CommandLine: AnsiString;
  458. I: integer;
  459. begin
  460. Commandline := '';
  461. for I := 0 to High (ComLine) do
  462. if Pos (' ', ComLine [I]) <> 0 then
  463. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  464. else
  465. CommandLine := CommandLine + ' ' + Comline [I];
  466. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  467. end;
  468. {****************************************************************************
  469. Initialization code
  470. ****************************************************************************}
  471. Initialization
  472. InitExceptions;
  473. InitInternational; { Initialize internationalization settings }
  474. Finalization
  475. DoneExceptions;
  476. end.