sysutils.pp 10 KB

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