sysutils.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004 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. { Include platform independent implementation part }
  25. {$i sysutils.inc}
  26. { * Include MorphOS specific includes * }
  27. {$include execd.inc}
  28. {$include execf.inc}
  29. {$include timerd.inc}
  30. {$include doslibd.inc}
  31. {$include doslibf.inc}
  32. {$include utilf.inc}
  33. { * Followings are implemented in the system unit! * }
  34. function PathConv(path: shortstring): shortstring; external name 'PATHCONV';
  35. procedure AddToList(var l: Pointer; h: LongInt); external name 'ADDTOLIST';
  36. procedure RemoveFromList(var l: Pointer; h: LongInt); external name 'REMOVEFROMLIST';
  37. var
  38. MOS_fileList: Pointer; external name 'MOS_FILELIST';
  39. {****************************************************************************
  40. File Functions
  41. ****************************************************************************}
  42. {$I-}{ Required for correct usage of these routines }
  43. (****** non portable routines ******)
  44. function FileOpen(const FileName: string; Mode: Integer): LongInt;
  45. var
  46. dosResult: LongInt;
  47. tmpStr : array[0..255] of char;
  48. begin
  49. {$WARNING FIX ME! To do: FileOpen Access Modes}
  50. tmpStr:=PathConv(FileName)+#0;
  51. dosResult:=Open(@tmpStr,MODE_OLDFILE);
  52. if dosResult=0 then
  53. dosResult:=-1
  54. else
  55. AddToList(MOS_fileList,dosResult);
  56. FileOpen:=dosResult;
  57. end;
  58. function FileGetDate(Handle: LongInt) : LongInt;
  59. begin
  60. end;
  61. function FileSetDate(Handle, Age: LongInt) : LongInt;
  62. begin
  63. // Impossible under unix from FileHandle !!
  64. FileSetDate:=-1;
  65. end;
  66. function FileCreate(const FileName: string) : LongInt;
  67. var
  68. dosResult: LongInt;
  69. tmpStr : array[0..255] of char;
  70. begin
  71. tmpStr:=PathConv(FileName)+#0;
  72. dosResult:=Open(@tmpStr,MODE_NEWFILE);
  73. if dosResult=0 then
  74. dosResult:=-1
  75. else
  76. AddToList(MOS_fileList,dosResult);
  77. FileCreate:=dosResult;
  78. end;
  79. function FileCreate(const FileName: string; Mode: integer): LongInt;
  80. begin
  81. {$WARNING FIX ME! To do: FileCreate Access Modes}
  82. FileCreate:=FileCreate(FileName);
  83. end;
  84. function FileRead(Handle: LongInt; var Buffer; Count: LongInt): LongInt;
  85. begin
  86. FileRead:=-1;
  87. if (Count<=0) or (Handle<=0) then exit;
  88. FileRead:=dosRead(Handle,@Buffer,Count);
  89. end;
  90. function FileWrite(Handle: LongInt; const Buffer; Count: LongInt): LongInt;
  91. begin
  92. FileWrite:=-1;
  93. if (Count<=0) or (Handle<=0) then exit;
  94. FileWrite:=dosWrite(Handle,@Buffer,Count);
  95. end;
  96. function FileSeek(Handle, FOffset, Origin: LongInt) : LongInt;
  97. var
  98. seekMode: LongInt;
  99. begin
  100. FileSeek:=-1;
  101. if (Handle<=0) then exit;
  102. case Origin of
  103. fsFromBeginning: seekMode:=OFFSET_BEGINNING;
  104. fsFromCurrent : seekMode:=OFFSET_CURRENT;
  105. fsFromEnd : seekMode:=OFFSET_END;
  106. end;
  107. FileSeek:=dosSeek(Handle, FOffset, seekMode);
  108. end;
  109. function FileSeek(Handle: LongInt; FOffset, Origin: Int64): Int64;
  110. begin
  111. {$WARNING Need to add 64bit call }
  112. FileSeek:=FileSeek(Handle,LongInt(FOffset),LongInt(Origin));
  113. end;
  114. procedure FileClose(Handle: LongInt);
  115. begin
  116. if (Handle<=0) then exit;
  117. dosClose(Handle);
  118. RemoveFromList(MOS_fileList,Handle);
  119. end;
  120. function FileTruncate(Handle, Size: LongInt): Boolean;
  121. var
  122. dosResult: LongInt;
  123. begin
  124. FileTruncate:=False;
  125. if (Handle<=0) then exit;
  126. dosResult:=SetFileSize(Handle, Size, OFFSET_BEGINNING);
  127. if (dosResult<0) then exit;
  128. FileTruncate:=True;
  129. end;
  130. function DeleteFile(const FileName: string) : Boolean;
  131. var
  132. tmpStr: array[0..255] of char;
  133. begin
  134. tmpStr:=PathConv(FileName)+#0;
  135. DeleteFile:=dosDeleteFile(@tmpStr);
  136. end;
  137. function RenameFile(const OldName, NewName: string): Boolean;
  138. var
  139. tmpOldName, tmpNewName: array[0..255] of char;
  140. begin
  141. tmpOldName:=PathConv(OldName)+#0;
  142. tmpNewName:=PathConv(NewName)+#0;
  143. RenameFile:=dosRename(tmpOldName, tmpNewName);
  144. end;
  145. (****** end of non portable routines ******)
  146. Function FileAge (Const FileName : String): Longint;
  147. var F: file;
  148. Time: longint;
  149. begin
  150. Assign(F,FileName);
  151. dos.GetFTime(F,Time);
  152. { Warning this is not compatible with standard routines
  153. since Double are not supported on m68k by default!
  154. }
  155. FileAge:=Time;
  156. end;
  157. Function FileExists (Const FileName : String) : Boolean;
  158. Var
  159. F: File;
  160. OldMode : Byte;
  161. Begin
  162. OldMode := FileMode;
  163. FileMode := fmOpenRead;
  164. Assign(F,FileName);
  165. Reset(F,1);
  166. FileMode := OldMode;
  167. If IOResult <> 0 then
  168. FileExists := FALSE
  169. else
  170. Begin
  171. FileExists := TRUE;
  172. Close(F);
  173. end;
  174. end;
  175. type
  176. PDOSSearchRec = ^SearchRec;
  177. Function FindFirst (Const Path : String; Attr : Longint; Out Rslt : TSearchRec) : Longint;
  178. Const
  179. faSpecial = faHidden or faSysFile or faVolumeID or faDirectory;
  180. var
  181. p : pDOSSearchRec;
  182. dosattr: word;
  183. DT: Datetime;
  184. begin
  185. dosattr:=0;
  186. if Attr and faHidden <> 0 then
  187. dosattr := dosattr or Hidden;
  188. if Attr and faSysFile <> 0 then
  189. dosattr := dosattr or SysFile;
  190. if Attr and favolumeID <> 0 then
  191. dosattr := dosattr or VolumeID;
  192. if Attr and faDirectory <> 0 then
  193. dosattr := dosattr or Directory;
  194. New(p);
  195. Rslt.FindHandle := THandle(p);
  196. dos.FindFirst(path,dosattr,p^);
  197. if DosError <> 0 then
  198. begin
  199. FindFirst := -1;
  200. end
  201. else
  202. begin
  203. Rslt.Name := p^.Name;
  204. { Not compatible with other platforms! }
  205. Rslt.Time:=p^.Time;
  206. Rslt.Attr := p^.Attr;
  207. Rslt.ExcludeAttr := not p^.Attr;
  208. Rslt.Size := p^.Size;
  209. FindFirst := 0;
  210. end;
  211. end;
  212. Function FindNext (Var Rslt : TSearchRec) : Longint;
  213. var
  214. p : pDOSSearchRec;
  215. DT: Datetime;
  216. begin
  217. p:= PDOsSearchRec(Rslt.FindHandle);
  218. if not assigned(p) then
  219. begin
  220. FindNext := -1;
  221. exit;
  222. end;
  223. Dos.FindNext(p^);
  224. if DosError <> 0 then
  225. begin
  226. FindNext := -1;
  227. end
  228. else
  229. begin
  230. Rslt.Name := p^.Name;
  231. UnpackTime(p^.Time, DT);
  232. { Warning: Not compatible with other platforms }
  233. Rslt.time := p^.Time;
  234. Rslt.Attr := p^.Attr;
  235. Rslt.ExcludeAttr := not p^.Attr;
  236. Rslt.Size := p^.Size;
  237. FindNext := 0;
  238. end;
  239. end;
  240. Procedure FindClose (Var F : TSearchrec);
  241. Var
  242. p : PDOSSearchRec;
  243. begin
  244. p:=PDOSSearchRec(f.FindHandle);
  245. if not assigned(p) then
  246. exit;
  247. Dos.FindClose(p^);
  248. if assigned(p) then
  249. Dispose(p);
  250. f.FindHandle := THandle(nil);
  251. end;
  252. Function FileGetAttr (Const FileName : String) : Longint;
  253. var
  254. F: file;
  255. attr: word;
  256. begin
  257. Assign(F,FileName);
  258. dos.GetFAttr(F,attr);
  259. if DosError <> 0 then
  260. FileGetAttr := -1
  261. else
  262. FileGetAttr := Attr;
  263. end;
  264. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  265. var
  266. F: file;
  267. begin
  268. Assign(F, FileName);
  269. Dos.SetFAttr(F, Attr and $ffff);
  270. FileSetAttr := DosError;
  271. end;
  272. {****************************************************************************
  273. Disk Functions
  274. ****************************************************************************}
  275. {
  276. The Diskfree and Disksize functions need a file on the specified drive, since this
  277. is required for the statfs system call.
  278. These filenames are set in drivestr[0..26], and have been preset to :
  279. 0 - '.' (default drive - hence current dir is ok.)
  280. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  281. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  282. 3 - '/' (C: equivalent of dos is the root partition)
  283. 4..26 (can be set by you're own applications)
  284. ! Use AddDisk() to Add new drives !
  285. They both return -1 when a failure occurs.
  286. }
  287. Const
  288. FixDriveStr : array[0..3] of pchar=(
  289. '.',
  290. '/fd0/.',
  291. '/fd1/.',
  292. '/.'
  293. );
  294. var
  295. Drives : byte;
  296. DriveStr : array[4..26] of pchar;
  297. Procedure AddDisk(const path:string);
  298. begin
  299. if not (DriveStr[Drives]=nil) then
  300. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  301. GetMem(DriveStr[Drives],length(Path)+1);
  302. StrPCopy(DriveStr[Drives],path);
  303. inc(Drives);
  304. if Drives>26 then
  305. Drives:=4;
  306. end;
  307. Function DiskFree(Drive: Byte): int64;
  308. Begin
  309. DiskFree := dos.diskFree(Drive);
  310. End;
  311. Function DiskSize(Drive: Byte): int64;
  312. Begin
  313. DiskSize := dos.DiskSize(Drive);
  314. End;
  315. Function GetCurrentDir : String;
  316. begin
  317. GetDir (0,Result);
  318. end;
  319. Function SetCurrentDir (Const NewDir : String) : Boolean;
  320. begin
  321. ChDir(NewDir);
  322. result := (IOResult = 0);
  323. end;
  324. Function CreateDir (Const NewDir : String) : Boolean;
  325. begin
  326. MkDir(NewDir);
  327. result := (IOResult = 0);
  328. end;
  329. Function RemoveDir (Const Dir : String) : Boolean;
  330. begin
  331. RmDir(Dir);
  332. result := (IOResult = 0);
  333. end;
  334. Function DirectoryExists(const Directory: string): Boolean;
  335. var
  336. s: string;
  337. begin
  338. { Get old directory }
  339. s:=GetCurrentDir;
  340. ChDir(Directory);
  341. DirectoryExists := (IOResult = 0);
  342. ChDir(s);
  343. end;
  344. {****************************************************************************
  345. Misc Functions
  346. ****************************************************************************}
  347. procedure Beep;
  348. begin
  349. end;
  350. {****************************************************************************
  351. Locale Functions
  352. ****************************************************************************}
  353. Procedure GetLocalTime(var SystemTime: TSystemTime);
  354. var
  355. dayOfWeek: word;
  356. begin
  357. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
  358. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  359. end ;
  360. Procedure InitAnsi;
  361. Var
  362. i : longint;
  363. begin
  364. { Fill table entries 0 to 127 }
  365. for i := 0 to 96 do
  366. UpperCaseTable[i] := chr(i);
  367. for i := 97 to 122 do
  368. UpperCaseTable[i] := chr(i - 32);
  369. for i := 123 to 191 do
  370. UpperCaseTable[i] := chr(i);
  371. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  372. for i := 0 to 64 do
  373. LowerCaseTable[i] := chr(i);
  374. for i := 65 to 90 do
  375. LowerCaseTable[i] := chr(i + 32);
  376. for i := 91 to 191 do
  377. LowerCaseTable[i] := chr(i);
  378. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  379. end;
  380. Procedure InitInternational;
  381. begin
  382. InitInternationalGeneric;
  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.