sysutils.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. function RemoveFromList(var l: Pointer; h: LongInt): boolean; external name 'REMOVEFROMLIST';
  37. function CheckInList(var l: Pointer; h: LongInt): pointer; external name 'CHECKINLIST';
  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; Out 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. tmpStr : array[0..255] of Char;
  338. tmpLock: LongInt;
  339. FIB : PFileInfoBlock;
  340. begin
  341. DirectoryExists:=False;
  342. If (Directory='') or (InOutRes<>0) then exit;
  343. tmpStr:=PathConv(Directory)+#0;
  344. tmpLock:=0;
  345. tmpLock:=Lock(@tmpStr,SHARED_LOCK);
  346. if tmpLock=0 then exit;
  347. FIB:=nil; new(FIB);
  348. if (Examine(tmpLock,FIB)=True) and (FIB^.fib_DirEntryType>0) then begin
  349. DirectoryExists:=True;
  350. end;
  351. if tmpLock<>0 then Unlock(tmpLock);
  352. if assigned(FIB) then dispose(FIB);
  353. end;
  354. {****************************************************************************
  355. Misc Functions
  356. ****************************************************************************}
  357. procedure Beep;
  358. begin
  359. end;
  360. {****************************************************************************
  361. Locale Functions
  362. ****************************************************************************}
  363. Procedure GetLocalTime(var SystemTime: TSystemTime);
  364. var
  365. dayOfWeek: word;
  366. begin
  367. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
  368. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  369. end ;
  370. Procedure InitAnsi;
  371. Var
  372. i : longint;
  373. begin
  374. { Fill table entries 0 to 127 }
  375. for i := 0 to 96 do
  376. UpperCaseTable[i] := chr(i);
  377. for i := 97 to 122 do
  378. UpperCaseTable[i] := chr(i - 32);
  379. for i := 123 to 191 do
  380. UpperCaseTable[i] := chr(i);
  381. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  382. for i := 0 to 64 do
  383. LowerCaseTable[i] := chr(i);
  384. for i := 65 to 90 do
  385. LowerCaseTable[i] := chr(i + 32);
  386. for i := 91 to 191 do
  387. LowerCaseTable[i] := chr(i);
  388. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  389. end;
  390. Procedure InitInternational;
  391. begin
  392. InitInternationalGeneric;
  393. InitAnsi;
  394. end;
  395. function SysErrorMessage(ErrorCode: Integer): String;
  396. begin
  397. { Result:=StrError(ErrorCode);}
  398. end;
  399. {****************************************************************************
  400. OS utility functions
  401. ****************************************************************************}
  402. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  403. begin
  404. Result:=Dos.Getenv(shortstring(EnvVar));
  405. end;
  406. Function GetEnvironmentVariableCount : Integer;
  407. begin
  408. // Result:=FPCCountEnvVar(EnvP);
  409. Result:=Dos.envCount;
  410. end;
  411. Function GetEnvironmentString(Index : Integer) : String;
  412. begin
  413. // Result:=FPCGetEnvStrFromP(Envp,Index);
  414. Result:=Dos.EnvStr(Index);
  415. end;
  416. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString):
  417. integer;
  418. var
  419. CommandLine: AnsiString;
  420. E: EOSError;
  421. begin
  422. Dos.Exec (Path, ComLine);
  423. if DosError <> 0 then begin
  424. if ComLine = '' then
  425. CommandLine := Path
  426. else
  427. CommandLine := Path + ' ' + ComLine;
  428. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  429. E.ErrorCode := DosError;
  430. raise E;
  431. end;
  432. end;
  433. function ExecuteProcess (const Path: AnsiString;
  434. const ComLine: array of AnsiString): integer;
  435. var
  436. CommandLine: AnsiString;
  437. I: integer;
  438. begin
  439. Commandline := '';
  440. for I := 0 to High (ComLine) do
  441. if Pos (' ', ComLine [I]) <> 0 then
  442. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  443. else
  444. CommandLine := CommandLine + ' ' + Comline [I];
  445. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  446. end;
  447. {****************************************************************************
  448. Initialization code
  449. ****************************************************************************}
  450. Initialization
  451. InitExceptions;
  452. InitInternational; { Initialize internationalization settings }
  453. Finalization
  454. DoneExceptions;
  455. end.