sysutils.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2004 by Olle Raab
  5. Sysutils unit for Mac OS
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit sysutils;
  13. interface
  14. {$MODE objfpc}
  15. { force ansistrings }
  16. {$H+}
  17. //{$DEFINE HAS_SLEEP} TODO
  18. //{$DEFINE HAS_OSERROR} TODO
  19. //{$DEFINE HAS_OSCONFIG} TODO
  20. { Include platform independent interface part }
  21. {$i sysutilh.inc}
  22. implementation
  23. uses
  24. Dos, Sysconst; // For some included files.
  25. { Include platform independent implementation part }
  26. {$i sysutils.inc}
  27. {****************************************************************************
  28. File Functions
  29. ****************************************************************************}
  30. Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
  31. Var LinuxFlags : longint;
  32. BEGIN
  33. (* TODO fix
  34. LinuxFlags:=0;
  35. Case (Mode and 3) of
  36. 0 : LinuxFlags:=LinuxFlags or Open_RdOnly;
  37. 1 : LinuxFlags:=LinuxFlags or Open_WrOnly;
  38. 2 : LinuxFlags:=LinuxFlags or Open_RdWr;
  39. end;
  40. FileOpen:=fdOpen (FileName,LinuxFlags);
  41. //!! We need to set locking based on Mode !!
  42. *)
  43. end;
  44. Function FileCreate (Const FileName : String) : Longint;
  45. begin
  46. (* TODO fix
  47. FileCreate:=fdOpen(FileName,Open_RdWr or Open_Creat or Open_Trunc);
  48. *)
  49. end;
  50. Function FileCreate (Const FileName : String;Mode : Longint) : Longint;
  51. Var LinuxFlags : longint;
  52. BEGIN
  53. (* TODO fix
  54. LinuxFlags:=0;
  55. Case (Mode and 3) of
  56. 0 : LinuxFlags:=LinuxFlags or Open_RdOnly;
  57. 1 : LinuxFlags:=LinuxFlags or Open_WrOnly;
  58. 2 : LinuxFlags:=LinuxFlags or Open_RdWr;
  59. end;
  60. FileCreate:=fdOpen(FileName,LinuxFlags or Open_Creat or Open_Trunc);
  61. *)
  62. end;
  63. Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  64. begin
  65. (* TODO fix
  66. FileRead:=fdRead (Handle,Buffer,Count);
  67. *)
  68. end;
  69. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  70. begin
  71. (* TODO fix
  72. FileWrite:=fdWrite (Handle,Buffer,Count);
  73. *)
  74. end;
  75. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  76. begin
  77. (* TODO fix
  78. FileSeek:=fdSeek (Handle,FOffset,Origin);
  79. *)
  80. end;
  81. Function FileSeek (Handle : Longint; FOffset,Origin : Int64) : Int64;
  82. begin
  83. (* TODO fix
  84. {$warning need to add 64bit call }
  85. FileSeek:=fdSeek (Handle,FOffset,Origin);
  86. *)
  87. end;
  88. Procedure FileClose (Handle : Longint);
  89. begin
  90. (* TODO fix
  91. fdclose(Handle);
  92. *)
  93. end;
  94. Function FileTruncate (Handle,Size: Longint) : boolean;
  95. begin
  96. (* TODO fix
  97. FileTruncate:=fdtruncate(Handle,Size);
  98. *)
  99. end;
  100. Function FileAge (Const FileName : String): Longint;
  101. (*
  102. Var Info : Stat;
  103. Y,M,D,hh,mm,ss : word;
  104. *)
  105. begin
  106. (* TODO fix
  107. If not fstat (FileName,Info) then
  108. exit(-1)
  109. else
  110. begin
  111. EpochToLocal(info.mtime,y,m,d,hh,mm,ss);
  112. Result:=DateTimeToFileDate(EncodeDate(y,m,d)+EncodeTime(hh,mm,ss,0));
  113. end;
  114. *)
  115. end;
  116. Function FileExists (Const FileName : String) : Boolean;
  117. (*
  118. Var Info : Stat;
  119. *)
  120. begin
  121. (* TODO fix
  122. FileExists:=fstat(filename,Info);
  123. *)
  124. end;
  125. Function DirectoryExists (Const Directory : String) : Boolean;
  126. (*
  127. Var Info : Stat;
  128. *)
  129. begin
  130. (* TODO fix
  131. DirectoryExists:=fstat(Directory,Info) and
  132. ((info.mode and STAT_IFMT)=STAT_IFDIR);
  133. *)
  134. end;
  135. (*
  136. Function LinuxToWinAttr (FN : Pchar; Const Info : Stat) : Longint;
  137. begin
  138. Result:=faArchive;
  139. If (Info.Mode and STAT_IFDIR)=STAT_IFDIR then
  140. Result:=Result or faDirectory;
  141. If (FN[0]='.') and (not (FN[1] in [#0,'.'])) then
  142. Result:=Result or faHidden;
  143. If (Info.Mode and STAT_IWUSR)=0 Then
  144. Result:=Result or faReadOnly;
  145. If (Info.Mode and
  146. (STAT_IFSOCK or STAT_IFBLK or STAT_IFCHR or STAT_IFIFO))<>0 then
  147. Result:=Result or faSysFile;
  148. end;
  149. {
  150. GlobToSearch takes a glob entry, stats the file.
  151. The glob entry is removed.
  152. If FileAttributes match, the entry is reused
  153. }
  154. Type
  155. TGlobSearchRec = Record
  156. Path : String;
  157. GlobHandle : PGlob;
  158. end;
  159. PGlobSearchRec = ^TGlobSearchRec;
  160. Function GlobToTSearchRec (Var Info : TSearchRec) : Boolean;
  161. Var SInfo : Stat;
  162. p : Pglob;
  163. GlobSearchRec : PGlobSearchrec;
  164. begin
  165. GlobSearchRec:=PGlobSearchrec(Info.FindHandle);
  166. P:=GlobSearchRec^.GlobHandle;
  167. Result:=P<>Nil;
  168. If Result then
  169. begin
  170. GlobSearchRec^.GlobHandle:=P^.Next;
  171. Result:=Fstat(GlobSearchRec^.Path+StrPas(p^.name),SInfo);
  172. If Result then
  173. begin
  174. Info.Attr:=LinuxToWinAttr(p^.name,SInfo);
  175. Result:=(Info.ExcludeAttr and Info.Attr)=0;
  176. If Result Then
  177. With Info do
  178. begin
  179. Attr:=Info.Attr;
  180. If P^.Name<>Nil then
  181. Name:=strpas(p^.name);
  182. Time:=Sinfo.mtime;
  183. Size:=Sinfo.Size;
  184. end;
  185. end;
  186. P^.Next:=Nil;
  187. GlobFree(P);
  188. end;
  189. end;
  190. *)
  191. Function DoFind(Var Rslt : TSearchRec) : Longint;
  192. (*
  193. Var
  194. GlobSearchRec : PGlobSearchRec;
  195. *)
  196. begin
  197. (* TODO fix
  198. Result:=-1;
  199. GlobSearchRec:=PGlobSearchRec(Rslt.FindHandle);
  200. If (GlobSearchRec^.GlobHandle<>Nil) then
  201. While (GlobSearchRec^.GlobHandle<>Nil) and not (Result=0) do
  202. If GlobToTSearchRec(Rslt) Then Result:=0;
  203. *)
  204. end;
  205. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  206. (*
  207. Var
  208. GlobSearchRec : PGlobSearchRec;
  209. *)
  210. begin
  211. (* TODO fix
  212. New(GlobSearchRec);
  213. GlobSearchRec^.Path:=ExpandFileName(ExtractFilePath(Path));
  214. GlobSearchRec^.GlobHandle:=Glob(Path);
  215. Rslt.ExcludeAttr:=Not Attr; //!! Not correct !!
  216. Rslt.FindHandle:=Longint(GlobSearchRec);
  217. Result:=DoFind (Rslt);
  218. *)
  219. end;
  220. Function FindNext (Var Rslt : TSearchRec) : Longint;
  221. begin
  222. Result:=DoFind (Rslt);
  223. end;
  224. Procedure FindClose (Var F : TSearchrec);
  225. (*
  226. Var
  227. GlobSearchRec : PGlobSearchRec;
  228. *)
  229. begin
  230. (* TODO fix
  231. GlobSearchRec:=PGlobSearchRec(F.FindHandle);
  232. GlobFree (GlobSearchRec^.GlobHandle);
  233. Dispose(GlobSearchRec);
  234. *)
  235. end;
  236. Function FileGetDate (Handle : Longint) : Longint;
  237. (*
  238. Var Info : Stat;
  239. *)
  240. begin
  241. (* TODO fix
  242. If Not(FStat(Handle,Info)) then
  243. Result:=-1
  244. else
  245. Result:=Info.Mtime;
  246. *)
  247. end;
  248. Function FileSetDate (Handle,Age : Longint) : Longint;
  249. begin
  250. // TODO fix
  251. // Impossible under Linux from FileHandle !!
  252. FileSetDate:=-1;
  253. end;
  254. Function FileGetAttr (Const FileName : String) : Longint;
  255. (*
  256. Var Info : Stat;
  257. *)
  258. begin
  259. (* TODO fix
  260. If Not FStat (FileName,Info) then
  261. Result:=-1
  262. Else
  263. Result:=LinuxToWinAttr(Pchar(FileName),Info);
  264. *)
  265. end;
  266. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  267. begin
  268. Result:=-1;
  269. end;
  270. Function DeleteFile (Const FileName : String) : Boolean;
  271. begin
  272. (* TODO fix
  273. Result:=UnLink (FileName);
  274. *)
  275. end;
  276. Function RenameFile (Const OldName, NewName : String) : Boolean;
  277. begin
  278. (* TODO fix
  279. RenameFile:=Unix.FRename(OldNAme,NewName);
  280. *)
  281. end;
  282. {****************************************************************************
  283. Disk Functions
  284. ****************************************************************************}
  285. {
  286. The Diskfree and Disksize functions need a file on the specified drive, since this
  287. is required for the statfs system call.
  288. These filenames are set in drivestr[0..26], and have been preset to :
  289. 0 - '.' (default drive - hence current dir is ok.)
  290. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  291. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  292. 3 - '/' (C: equivalent of dos is the root partition)
  293. 4..26 (can be set by you're own applications)
  294. ! Use AddDisk() to Add new drives !
  295. They both return -1 when a failure occurs.
  296. }
  297. Const
  298. FixDriveStr : array[0..3] of pchar=(
  299. '.',
  300. '/fd0/.',
  301. '/fd1/.',
  302. '/.'
  303. );
  304. var
  305. Drives : byte;
  306. DriveStr : array[4..26] of pchar;
  307. Procedure AddDisk(const path:string);
  308. begin
  309. if not (DriveStr[Drives]=nil) then
  310. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  311. GetMem(DriveStr[Drives],length(Path)+1);
  312. StrPCopy(DriveStr[Drives],path);
  313. inc(Drives);
  314. if Drives>26 then
  315. Drives:=4;
  316. end;
  317. Function DiskFree(Drive: Byte): int64;
  318. (*
  319. var
  320. fs : tstatfs;
  321. *)
  322. Begin
  323. (* TODO fix
  324. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and statfs(StrPas(fixdrivestr[drive]),fs)) or
  325. ((not (drivestr[Drive]=nil)) and statfs(StrPas(drivestr[drive]),fs)) then
  326. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  327. else
  328. Diskfree:=-1;
  329. *)
  330. End;
  331. Function DiskSize(Drive: Byte): int64;
  332. (*
  333. var
  334. fs : tstatfs;
  335. *)
  336. Begin
  337. (* TODO fix
  338. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and statfs(StrPas(fixdrivestr[drive]),fs)) or
  339. ((not (drivestr[Drive]=nil)) and statfs(StrPas(drivestr[drive]),fs)) then
  340. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  341. else
  342. DiskSize:=-1;
  343. *)
  344. End;
  345. Function GetCurrentDir : String;
  346. begin
  347. GetDir (0,Result);
  348. end;
  349. Function SetCurrentDir (Const NewDir : String) : Boolean;
  350. begin
  351. {$I-}
  352. ChDir(NewDir);
  353. {$I+}
  354. result := (IOResult = 0);
  355. end;
  356. Function CreateDir (Const NewDir : String) : Boolean;
  357. begin
  358. {$I-}
  359. MkDir(NewDir);
  360. {$I+}
  361. result := (IOResult = 0);
  362. end;
  363. Function RemoveDir (Const Dir : String) : Boolean;
  364. begin
  365. {$I-}
  366. RmDir(Dir);
  367. {$I+}
  368. result := (IOResult = 0);
  369. end;
  370. {****************************************************************************
  371. Misc Functions
  372. ****************************************************************************}
  373. procedure Beep;
  374. begin
  375. //TODO fix
  376. end;
  377. {****************************************************************************
  378. Locale Functions
  379. ****************************************************************************}
  380. Procedure GetLocalTime(var SystemTime: TSystemTime);
  381. begin
  382. (* TODO fix
  383. Unix.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second);
  384. Unix.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  385. SystemTime.MilliSecond := 0;
  386. *)
  387. end ;
  388. Procedure InitAnsi;
  389. Var
  390. i : longint;
  391. begin
  392. { Fill table entries 0 to 127 }
  393. for i := 0 to 96 do
  394. UpperCaseTable[i] := chr(i);
  395. for i := 97 to 122 do
  396. UpperCaseTable[i] := chr(i - 32);
  397. for i := 123 to 191 do
  398. UpperCaseTable[i] := chr(i);
  399. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  400. for i := 0 to 64 do
  401. LowerCaseTable[i] := chr(i);
  402. for i := 65 to 90 do
  403. LowerCaseTable[i] := chr(i + 32);
  404. for i := 91 to 191 do
  405. LowerCaseTable[i] := chr(i);
  406. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  407. end;
  408. Procedure InitInternational;
  409. begin
  410. InitAnsi;
  411. end;
  412. function SysErrorMessage(ErrorCode: Integer): String;
  413. begin
  414. (* TODO fix
  415. Result:=StrError(ErrorCode);
  416. *)
  417. end;
  418. {****************************************************************************
  419. OS utility functions
  420. ****************************************************************************}
  421. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  422. begin
  423. (* TODO fix
  424. Result:=StrPas(Unix.Getenv(PChar(EnvVar)));
  425. *)
  426. end;
  427. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString):integer;
  428. begin
  429. end;
  430. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array Of AnsiString):integer;
  431. begin
  432. end;
  433. procedure Sleep(milliseconds: Cardinal);
  434. begin
  435. end;
  436. (*
  437. Function GetLastOSError : Integer;
  438. begin
  439. end;
  440. *)
  441. {****************************************************************************
  442. Initialization code
  443. ****************************************************************************}
  444. Initialization
  445. InitExceptions; { Initialize exceptions. OS independent }
  446. InitInternational; { Initialize internationalization settings }
  447. Finalization
  448. DoneExceptions;
  449. end.
  450. {
  451. $Log$
  452. Revision 1.1 2004-09-28 15:39:29 olle
  453. + added skeleton version
  454. }