sysutils.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2004 by Karoly Balogh
  5. Sysutils unit for MorphOS
  6. Based on Amiga version by Carl Eric Codere, and other
  7. parts of the RTL
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. unit sysutils;
  15. interface
  16. {$MODE objfpc}
  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. { Include platform independent implementation part }
  26. {$i sysutils.inc}
  27. { * Include MorphOS specific includes * }
  28. {$include execd.inc}
  29. {$include execf.inc}
  30. {$include timerd.inc}
  31. {$include doslibd.inc}
  32. {$include doslibf.inc}
  33. {$include utilf.inc}
  34. { * Followings are implemented in the system unit! * }
  35. function PathConv(path: shortstring): shortstring; external name 'PATHCONV';
  36. procedure AddToList(var l: Pointer; h: LongInt); external name 'ADDTOLIST';
  37. procedure RemoveFromList(var l: Pointer; h: LongInt); external name 'REMOVEFROMLIST';
  38. var
  39. MOS_fileList: Pointer; external name 'MOS_FILELIST';
  40. {****************************************************************************
  41. File Functions
  42. ****************************************************************************}
  43. {$I-}{ Required for correct usage of these routines }
  44. (****** non portable routines ******)
  45. function FileOpen(const FileName: string; Mode: Integer): LongInt;
  46. var
  47. dosResult: LongInt;
  48. tmpStr : array[0..255] of char;
  49. begin
  50. {$WARNING FIX ME! To do: FileOpen Access Modes}
  51. tmpStr:=PathConv(FileName)+#0;
  52. dosResult:=Open(@tmpStr,MODE_OLDFILE);
  53. if dosResult=0 then
  54. dosResult:=-1
  55. else
  56. AddToList(MOS_fileList,dosResult);
  57. FileOpen:=dosResult;
  58. end;
  59. function FileGetDate(Handle: LongInt) : LongInt;
  60. begin
  61. end;
  62. function FileSetDate(Handle, Age: LongInt) : LongInt;
  63. begin
  64. // Impossible under unix from FileHandle !!
  65. FileSetDate:=-1;
  66. end;
  67. function FileCreate(const FileName: string) : LongInt;
  68. var
  69. dosResult: LongInt;
  70. tmpStr : array[0..255] of char;
  71. begin
  72. tmpStr:=PathConv(FileName)+#0;
  73. dosResult:=Open(@tmpStr,MODE_NEWFILE);
  74. if dosResult=0 then
  75. dosResult:=-1
  76. else
  77. AddToList(MOS_fileList,dosResult);
  78. FileCreate:=dosResult;
  79. end;
  80. function FileCreate(const FileName: string; Mode: integer): LongInt;
  81. begin
  82. {$WARNING FIX ME! To do: FileCreate Access Modes}
  83. FileCreate:=FileCreate(FileName);
  84. end;
  85. function FileRead(Handle: LongInt; var Buffer; Count: LongInt): LongInt;
  86. begin
  87. FileRead:=-1;
  88. if (Count<=0) or (Handle<=0) then exit;
  89. FileRead:=dosRead(Handle,@Buffer,Count);
  90. end;
  91. function FileWrite(Handle: LongInt; const Buffer; Count: LongInt): LongInt;
  92. begin
  93. FileWrite:=-1;
  94. if (Count<=0) or (Handle<=0) then exit;
  95. FileWrite:=dosWrite(Handle,@Buffer,Count);
  96. end;
  97. function FileSeek(Handle, FOffset, Origin: LongInt) : LongInt;
  98. var
  99. seekMode: LongInt;
  100. begin
  101. FileSeek:=-1;
  102. if (Handle<=0) then exit;
  103. case Origin of
  104. fsFromBeginning: seekMode:=OFFSET_BEGINNING;
  105. fsFromCurrent : seekMode:=OFFSET_CURRENT;
  106. fsFromEnd : seekMode:=OFFSET_END;
  107. end;
  108. FileSeek:=dosSeek(Handle, FOffset, seekMode);
  109. end;
  110. function FileSeek(Handle: LongInt; FOffset, Origin: Int64): Int64;
  111. begin
  112. {$WARNING Need to add 64bit call }
  113. FileSeek:=FileSeek(Handle,LongInt(FOffset),LongInt(Origin));
  114. end;
  115. procedure FileClose(Handle: LongInt);
  116. begin
  117. if (Handle<=0) then exit;
  118. dosClose(Handle);
  119. RemoveFromList(MOS_fileList,Handle);
  120. end;
  121. function FileTruncate(Handle, Size: LongInt): Boolean;
  122. var
  123. dosResult: LongInt;
  124. begin
  125. FileTruncate:=False;
  126. if (Handle<=0) then exit;
  127. dosResult:=SetFileSize(Handle, Size, OFFSET_BEGINNING);
  128. if (dosResult<0) then exit;
  129. FileTruncate:=True;
  130. end;
  131. function DeleteFile(const FileName: string) : Boolean;
  132. var
  133. tmpStr: array[0..255] of char;
  134. begin
  135. tmpStr:=PathConv(FileName)+#0;
  136. DeleteFile:=dosDeleteFile(@tmpStr);
  137. end;
  138. function RenameFile(const OldName, NewName: string): Boolean;
  139. var
  140. tmpOldName, tmpNewName: array[0..255] of char;
  141. begin
  142. tmpOldName:=PathConv(OldName)+#0;
  143. tmpNewName:=PathConv(NewName)+#0;
  144. RenameFile:=dosRename(tmpOldName, tmpNewName);
  145. end;
  146. (****** end of non portable routines ******)
  147. Function FileAge (Const FileName : String): Longint;
  148. var F: file;
  149. Time: longint;
  150. begin
  151. Assign(F,FileName);
  152. dos.GetFTime(F,Time);
  153. { Warning this is not compatible with standard routines
  154. since Double are not supported on m68k by default!
  155. }
  156. FileAge:=Time;
  157. end;
  158. Function FileExists (Const FileName : String) : Boolean;
  159. Var
  160. F: File;
  161. OldMode : Byte;
  162. Begin
  163. OldMode := FileMode;
  164. FileMode := fmOpenRead;
  165. Assign(F,FileName);
  166. Reset(F,1);
  167. FileMode := OldMode;
  168. If IOResult <> 0 then
  169. FileExists := FALSE
  170. else
  171. Begin
  172. FileExists := TRUE;
  173. Close(F);
  174. end;
  175. end;
  176. type
  177. PDOSSearchRec = ^SearchRec;
  178. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  179. Const
  180. faSpecial = faHidden or faSysFile or faVolumeID or faDirectory;
  181. var
  182. p : pDOSSearchRec;
  183. dosattr: word;
  184. DT: Datetime;
  185. begin
  186. dosattr:=0;
  187. if Attr and faHidden <> 0 then
  188. dosattr := dosattr or Hidden;
  189. if Attr and faSysFile <> 0 then
  190. dosattr := dosattr or SysFile;
  191. if Attr and favolumeID <> 0 then
  192. dosattr := dosattr or VolumeID;
  193. if Attr and faDirectory <> 0 then
  194. dosattr := dosattr or Directory;
  195. New(p);
  196. Rslt.FindHandle := THandle(p);
  197. dos.FindFirst(path,dosattr,p^);
  198. if DosError <> 0 then
  199. begin
  200. FindFirst := -1;
  201. end
  202. else
  203. begin
  204. Rslt.Name := p^.Name;
  205. { Not compatible with other platforms! }
  206. Rslt.Time:=p^.Time;
  207. Rslt.Attr := p^.Attr;
  208. Rslt.ExcludeAttr := not p^.Attr;
  209. Rslt.Size := p^.Size;
  210. FindFirst := 0;
  211. end;
  212. end;
  213. Function FindNext (Var Rslt : TSearchRec) : Longint;
  214. var
  215. p : pDOSSearchRec;
  216. DT: Datetime;
  217. begin
  218. p:= PDOsSearchRec(Rslt.FindHandle);
  219. if not assigned(p) then
  220. begin
  221. FindNext := -1;
  222. exit;
  223. end;
  224. Dos.FindNext(p^);
  225. if DosError <> 0 then
  226. begin
  227. FindNext := -1;
  228. end
  229. else
  230. begin
  231. Rslt.Name := p^.Name;
  232. UnpackTime(p^.Time, DT);
  233. { Warning: Not compatible with other platforms }
  234. Rslt.time := p^.Time;
  235. Rslt.Attr := p^.Attr;
  236. Rslt.ExcludeAttr := not p^.Attr;
  237. Rslt.Size := p^.Size;
  238. FindNext := 0;
  239. end;
  240. end;
  241. Procedure FindClose (Var F : TSearchrec);
  242. Var
  243. p : PDOSSearchRec;
  244. begin
  245. p:=PDOSSearchRec(f.FindHandle);
  246. if not assigned(p) then
  247. exit;
  248. Dos.FindClose(p^);
  249. if assigned(p) then
  250. Dispose(p);
  251. f.FindHandle := THandle(nil);
  252. end;
  253. Function FileGetAttr (Const FileName : String) : Longint;
  254. var
  255. F: file;
  256. attr: word;
  257. begin
  258. Assign(F,FileName);
  259. dos.GetFAttr(F,attr);
  260. if DosError <> 0 then
  261. FileGetAttr := -1
  262. else
  263. FileGetAttr := Attr;
  264. end;
  265. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  266. var
  267. F: file;
  268. begin
  269. Assign(F, FileName);
  270. Dos.SetFAttr(F, Attr and $ffff);
  271. FileSetAttr := DosError;
  272. end;
  273. {****************************************************************************
  274. Disk Functions
  275. ****************************************************************************}
  276. {
  277. The Diskfree and Disksize functions need a file on the specified drive, since this
  278. is required for the statfs system call.
  279. These filenames are set in drivestr[0..26], and have been preset to :
  280. 0 - '.' (default drive - hence current dir is ok.)
  281. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  282. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  283. 3 - '/' (C: equivalent of dos is the root partition)
  284. 4..26 (can be set by you're own applications)
  285. ! Use AddDisk() to Add new drives !
  286. They both return -1 when a failure occurs.
  287. }
  288. Const
  289. FixDriveStr : array[0..3] of pchar=(
  290. '.',
  291. '/fd0/.',
  292. '/fd1/.',
  293. '/.'
  294. );
  295. var
  296. Drives : byte;
  297. DriveStr : array[4..26] of pchar;
  298. Procedure AddDisk(const path:string);
  299. begin
  300. if not (DriveStr[Drives]=nil) then
  301. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  302. GetMem(DriveStr[Drives],length(Path)+1);
  303. StrPCopy(DriveStr[Drives],path);
  304. inc(Drives);
  305. if Drives>26 then
  306. Drives:=4;
  307. end;
  308. Function DiskFree(Drive: Byte): int64;
  309. Begin
  310. DiskFree := dos.diskFree(Drive);
  311. End;
  312. Function DiskSize(Drive: Byte): int64;
  313. Begin
  314. DiskSize := dos.DiskSize(Drive);
  315. End;
  316. Function GetCurrentDir : String;
  317. begin
  318. GetDir (0,Result);
  319. end;
  320. Function SetCurrentDir (Const NewDir : String) : Boolean;
  321. begin
  322. ChDir(NewDir);
  323. result := (IOResult = 0);
  324. end;
  325. Function CreateDir (Const NewDir : String) : Boolean;
  326. begin
  327. MkDir(NewDir);
  328. result := (IOResult = 0);
  329. end;
  330. Function RemoveDir (Const Dir : String) : Boolean;
  331. begin
  332. RmDir(Dir);
  333. result := (IOResult = 0);
  334. end;
  335. Function DirectoryExists(const Directory: string): Boolean;
  336. var
  337. s: string;
  338. begin
  339. { Get old directory }
  340. s:=GetCurrentDir;
  341. ChDir(Directory);
  342. DirectoryExists := (IOResult = 0);
  343. ChDir(s);
  344. end;
  345. {****************************************************************************
  346. Misc Functions
  347. ****************************************************************************}
  348. procedure Beep;
  349. begin
  350. end;
  351. {****************************************************************************
  352. Locale Functions
  353. ****************************************************************************}
  354. Procedure GetLocalTime(var SystemTime: TSystemTime);
  355. var
  356. dayOfWeek: word;
  357. begin
  358. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
  359. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  360. end ;
  361. Procedure InitAnsi;
  362. Var
  363. i : longint;
  364. begin
  365. { Fill table entries 0 to 127 }
  366. for i := 0 to 96 do
  367. UpperCaseTable[i] := chr(i);
  368. for i := 97 to 122 do
  369. UpperCaseTable[i] := chr(i - 32);
  370. for i := 123 to 191 do
  371. UpperCaseTable[i] := chr(i);
  372. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  373. for i := 0 to 64 do
  374. LowerCaseTable[i] := chr(i);
  375. for i := 65 to 90 do
  376. LowerCaseTable[i] := chr(i + 32);
  377. for i := 91 to 191 do
  378. LowerCaseTable[i] := chr(i);
  379. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  380. end;
  381. Procedure InitInternational;
  382. begin
  383. InitAnsi;
  384. end;
  385. function SysErrorMessage(ErrorCode: Integer): String;
  386. begin
  387. { Result:=StrError(ErrorCode);}
  388. end;
  389. {****************************************************************************
  390. OS utility functions
  391. ****************************************************************************}
  392. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  393. begin
  394. Result:=Dos.Getenv(shortstring(EnvVar));
  395. end;
  396. Function GetEnvironmentVariableCount : Integer;
  397. begin
  398. // Result:=FPCCountEnvVar(EnvP);
  399. Result:=Dos.envCount;
  400. end;
  401. Function GetEnvironmentString(Index : Integer) : String;
  402. begin
  403. // Result:=FPCGetEnvStrFromP(Envp,Index);
  404. Result:=Dos.EnvStr(Index);
  405. end;
  406. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString):
  407. integer;
  408. var
  409. CommandLine: AnsiString;
  410. E: EOSError;
  411. begin
  412. Dos.Exec (Path, ComLine);
  413. if DosError <> 0 then begin
  414. if ComLine = '' then
  415. CommandLine := Path
  416. else
  417. CommandLine := Path + ' ' + ComLine;
  418. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  419. E.ErrorCode := DosError;
  420. raise E;
  421. end;
  422. end;
  423. function ExecuteProcess (const Path: AnsiString;
  424. const ComLine: array of AnsiString): integer;
  425. var
  426. CommandLine: AnsiString;
  427. I: integer;
  428. begin
  429. Commandline := '';
  430. for I := 0 to High (ComLine) do
  431. if Pos (' ', ComLine [I]) <> 0 then
  432. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  433. else
  434. CommandLine := CommandLine + ' ' + Comline [I];
  435. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  436. end;
  437. {****************************************************************************
  438. Initialization code
  439. ****************************************************************************}
  440. Initialization
  441. InitExceptions;
  442. InitInternational; { Initialize internationalization settings }
  443. Finalization
  444. DoneExceptions;
  445. end.
  446. {
  447. $Log$
  448. Revision 1.5 2005-01-30 02:36:14 karoly
  449. * fixed compilation
  450. Revision 1.4 2005/01/12 08:03:42 karoly
  451. * Few more Sysutils functions implemented
  452. Revision 1.3 2005/01/11 17:44:06 karoly
  453. * basic file I/O implemented
  454. Revision 1.2 2004/12/11 11:32:44 michael
  455. + Added GetEnvironmentVariableCount and GetEnvironmentString calls
  456. Revision 1.1 2004/06/06 00:58:02 karoly
  457. * initial revision
  458. }