sysutils.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. var
  87. dosResult: LongInt;
  88. begin
  89. FileRead:=-1;
  90. if (Count<=0) or (Handle<=0) then exit;
  91. FileRead:=dosRead(Handle,@Buffer,Count);
  92. end;
  93. function FileWrite(Handle: LongInt; const Buffer; Count: LongInt): LongInt;
  94. var
  95. dosResult: LongInt;
  96. begin
  97. FileWrite:=-1;
  98. if (Count<=0) or (Handle<=0) then exit;
  99. FileWrite:=dosWrite(Handle,@Buffer,Count);
  100. end;
  101. function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  102. begin
  103. FileSeek:=-1;
  104. end;
  105. function FileSeek (Handle: longint; FOffset, Origin: Int64): Int64;
  106. begin
  107. end;
  108. procedure FileClose(Handle: LongInt);
  109. begin
  110. if Handle<=0 then exit;
  111. dosClose(Handle);
  112. RemoveFromList(MOS_fileList,Handle);
  113. end;
  114. Function FileTruncate (Handle,Size: Longint) : boolean;
  115. begin
  116. end;
  117. (* end of non portable routines *)
  118. Function FileAge (Const FileName : String): Longint;
  119. var F: file;
  120. Time: longint;
  121. begin
  122. Assign(F,FileName);
  123. dos.GetFTime(F,Time);
  124. { Warning this is not compatible with standard routines
  125. since Double are not supported on m68k by default!
  126. }
  127. FileAge:=Time;
  128. end;
  129. Function FileExists (Const FileName : String) : Boolean;
  130. Var
  131. F: File;
  132. OldMode : Byte;
  133. Begin
  134. OldMode := FileMode;
  135. FileMode := fmOpenRead;
  136. Assign(F,FileName);
  137. Reset(F,1);
  138. FileMode := OldMode;
  139. If IOResult <> 0 then
  140. FileExists := FALSE
  141. else
  142. Begin
  143. FileExists := TRUE;
  144. Close(F);
  145. end;
  146. end;
  147. type
  148. PDOSSearchRec = ^SearchRec;
  149. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  150. Const
  151. faSpecial = faHidden or faSysFile or faVolumeID or faDirectory;
  152. var
  153. p : pDOSSearchRec;
  154. dosattr: word;
  155. DT: Datetime;
  156. begin
  157. dosattr:=0;
  158. if Attr and faHidden <> 0 then
  159. dosattr := dosattr or Hidden;
  160. if Attr and faSysFile <> 0 then
  161. dosattr := dosattr or SysFile;
  162. if Attr and favolumeID <> 0 then
  163. dosattr := dosattr or VolumeID;
  164. if Attr and faDirectory <> 0 then
  165. dosattr := dosattr or Directory;
  166. New(p);
  167. Rslt.FindHandle := THandle(p);
  168. dos.FindFirst(path,dosattr,p^);
  169. if DosError <> 0 then
  170. begin
  171. FindFirst := -1;
  172. end
  173. else
  174. begin
  175. Rslt.Name := p^.Name;
  176. { Not compatible with other platforms! }
  177. Rslt.Time:=p^.Time;
  178. Rslt.Attr := p^.Attr;
  179. Rslt.ExcludeAttr := not p^.Attr;
  180. Rslt.Size := p^.Size;
  181. FindFirst := 0;
  182. end;
  183. end;
  184. Function FindNext (Var Rslt : TSearchRec) : Longint;
  185. var
  186. p : pDOSSearchRec;
  187. DT: Datetime;
  188. begin
  189. p:= PDOsSearchRec(Rslt.FindHandle);
  190. if not assigned(p) then
  191. begin
  192. FindNext := -1;
  193. exit;
  194. end;
  195. Dos.FindNext(p^);
  196. if DosError <> 0 then
  197. begin
  198. FindNext := -1;
  199. end
  200. else
  201. begin
  202. Rslt.Name := p^.Name;
  203. UnpackTime(p^.Time, DT);
  204. { Warning: 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. FindNext := 0;
  210. end;
  211. end;
  212. Procedure FindClose (Var F : TSearchrec);
  213. Var
  214. p : PDOSSearchRec;
  215. begin
  216. p:=PDOSSearchRec(f.FindHandle);
  217. if not assigned(p) then
  218. exit;
  219. Dos.FindClose(p^);
  220. if assigned(p) then
  221. Dispose(p);
  222. f.FindHandle := THandle(nil);
  223. end;
  224. Function FileGetAttr (Const FileName : String) : Longint;
  225. var
  226. F: file;
  227. attr: word;
  228. begin
  229. Assign(F,FileName);
  230. dos.GetFAttr(F,attr);
  231. if DosError <> 0 then
  232. FileGetAttr := -1
  233. else
  234. FileGetAttr := Attr;
  235. end;
  236. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  237. var
  238. F: file;
  239. begin
  240. Assign(F, FileName);
  241. Dos.SetFAttr(F, Attr and $ffff);
  242. FileSetAttr := DosError;
  243. end;
  244. function DeleteFile(const FileName: string) : Boolean;
  245. var
  246. tmpStr: array[0..255] of char;
  247. begin
  248. tmpStr:=PathConv(FileName)+#0;
  249. DeleteFile:=dosDeleteFile(@tmpStr);
  250. end;
  251. Function RenameFile (Const OldName, NewName : String) : Boolean;
  252. var
  253. F: File;
  254. begin
  255. Assign(F,OldName);
  256. Rename(F,NewName);
  257. RenameFile := (IOResult = 0);
  258. end;
  259. {****************************************************************************
  260. Disk Functions
  261. ****************************************************************************}
  262. {
  263. The Diskfree and Disksize functions need a file on the specified drive, since this
  264. is required for the statfs system call.
  265. These filenames are set in drivestr[0..26], and have been preset to :
  266. 0 - '.' (default drive - hence current dir is ok.)
  267. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  268. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  269. 3 - '/' (C: equivalent of dos is the root partition)
  270. 4..26 (can be set by you're own applications)
  271. ! Use AddDisk() to Add new drives !
  272. They both return -1 when a failure occurs.
  273. }
  274. Const
  275. FixDriveStr : array[0..3] of pchar=(
  276. '.',
  277. '/fd0/.',
  278. '/fd1/.',
  279. '/.'
  280. );
  281. var
  282. Drives : byte;
  283. DriveStr : array[4..26] of pchar;
  284. Procedure AddDisk(const path:string);
  285. begin
  286. if not (DriveStr[Drives]=nil) then
  287. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  288. GetMem(DriveStr[Drives],length(Path)+1);
  289. StrPCopy(DriveStr[Drives],path);
  290. inc(Drives);
  291. if Drives>26 then
  292. Drives:=4;
  293. end;
  294. Function DiskFree(Drive: Byte): int64;
  295. Begin
  296. DiskFree := dos.diskFree(Drive);
  297. End;
  298. Function DiskSize(Drive: Byte): int64;
  299. Begin
  300. DiskSize := dos.DiskSize(Drive);
  301. End;
  302. Function GetCurrentDir : String;
  303. begin
  304. GetDir (0,Result);
  305. end;
  306. Function SetCurrentDir (Const NewDir : String) : Boolean;
  307. begin
  308. ChDir(NewDir);
  309. result := (IOResult = 0);
  310. end;
  311. Function CreateDir (Const NewDir : String) : Boolean;
  312. begin
  313. MkDir(NewDir);
  314. result := (IOResult = 0);
  315. end;
  316. Function RemoveDir (Const Dir : String) : Boolean;
  317. begin
  318. RmDir(Dir);
  319. result := (IOResult = 0);
  320. end;
  321. Function DirectoryExists(const Directory: string): Boolean;
  322. var
  323. s: string;
  324. begin
  325. { Get old directory }
  326. s:=GetCurrentDir;
  327. ChDir(Directory);
  328. DirectoryExists := (IOResult = 0);
  329. ChDir(s);
  330. end;
  331. {****************************************************************************
  332. Misc Functions
  333. ****************************************************************************}
  334. procedure Beep;
  335. begin
  336. end;
  337. {****************************************************************************
  338. Locale Functions
  339. ****************************************************************************}
  340. Procedure GetLocalTime(var SystemTime: TSystemTime);
  341. var
  342. dayOfWeek: word;
  343. begin
  344. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
  345. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  346. end ;
  347. Procedure InitAnsi;
  348. Var
  349. i : longint;
  350. begin
  351. { Fill table entries 0 to 127 }
  352. for i := 0 to 96 do
  353. UpperCaseTable[i] := chr(i);
  354. for i := 97 to 122 do
  355. UpperCaseTable[i] := chr(i - 32);
  356. for i := 123 to 191 do
  357. UpperCaseTable[i] := chr(i);
  358. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  359. for i := 0 to 64 do
  360. LowerCaseTable[i] := chr(i);
  361. for i := 65 to 90 do
  362. LowerCaseTable[i] := chr(i + 32);
  363. for i := 91 to 191 do
  364. LowerCaseTable[i] := chr(i);
  365. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  366. end;
  367. Procedure InitInternational;
  368. begin
  369. InitAnsi;
  370. end;
  371. function SysErrorMessage(ErrorCode: Integer): String;
  372. begin
  373. { Result:=StrError(ErrorCode);}
  374. end;
  375. {****************************************************************************
  376. OS utility functions
  377. ****************************************************************************}
  378. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  379. begin
  380. Result:=Dos.Getenv(shortstring(EnvVar));
  381. end;
  382. Function GetEnvironmentVariableCount : Integer;
  383. begin
  384. // Result:=FPCCountEnvVar(EnvP);
  385. Result:=Dos.envCount;
  386. end;
  387. Function GetEnvironmentString(Index : Integer) : String;
  388. begin
  389. // Result:=FPCGetEnvStrFromP(Envp,Index);
  390. Result:=Dos.EnvStr(Index);
  391. end;
  392. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString):
  393. integer;
  394. var
  395. CommandLine: AnsiString;
  396. E: EOSError;
  397. begin
  398. Dos.Exec (Path, ComLine);
  399. if DosError <> 0 then begin
  400. if ComLine = '' then
  401. CommandLine := Path
  402. else
  403. CommandLine := Path + ' ' + ComLine;
  404. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  405. E.ErrorCode := DosError;
  406. raise E;
  407. end;
  408. end;
  409. function ExecuteProcess (const Path: AnsiString;
  410. const ComLine: array of AnsiString): integer;
  411. var
  412. CommandLine: AnsiString;
  413. I: integer;
  414. begin
  415. Commandline := '';
  416. for I := 0 to High (ComLine) do
  417. if Pos (' ', ComLine [I]) <> 0 then
  418. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  419. else
  420. CommandLine := CommandLine + ' ' + Comline [I];
  421. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  422. end;
  423. {****************************************************************************
  424. Initialization code
  425. ****************************************************************************}
  426. Initialization
  427. InitExceptions;
  428. InitInternational; { Initialize internationalization settings }
  429. Finalization
  430. DoneExceptions;
  431. end.
  432. {
  433. $Log$
  434. Revision 1.3 2005-01-11 17:44:06 karoly
  435. * basic file I/O implemented
  436. Revision 1.2 2004/12/11 11:32:44 michael
  437. + Added GetEnvironmentVariableCount and GetEnvironmentString calls
  438. Revision 1.1 2004/06/06 00:58:02 karoly
  439. * initial revision
  440. }