sysutils.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. Sysutils unit for linux
  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. uses
  19. Unix,errors;
  20. type
  21. THandle = type Longint;
  22. { Include platform independent interface part }
  23. {$i sysutilh.inc}
  24. implementation
  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. LinuxFlags:=0;
  34. Case (Mode and 3) of
  35. 0 : LinuxFlags:=LinuxFlags or Open_RdOnly;
  36. 1 : LinuxFlags:=LinuxFlags or Open_WrOnly;
  37. 2 : LinuxFlags:=LinuxFlags or Open_RdWr;
  38. end;
  39. FileOpen:=fdOpen (FileName,LinuxFlags);
  40. //!! We need to set locking based on Mode !!
  41. end;
  42. Function FileCreate (Const FileName : String) : Longint;
  43. begin
  44. FileCreate:=fdOpen(FileName,Open_RdWr or Open_Creat or Open_Trunc);
  45. end;
  46. Function FileCreate (Const FileName : String;Mode : Longint) : Longint;
  47. Var LinuxFlags : longint;
  48. BEGIN
  49. LinuxFlags:=0;
  50. Case (Mode and 3) of
  51. 0 : LinuxFlags:=LinuxFlags or Open_RdOnly;
  52. 1 : LinuxFlags:=LinuxFlags or Open_WrOnly;
  53. 2 : LinuxFlags:=LinuxFlags or Open_RdWr;
  54. end;
  55. FileCreate:=fdOpen(FileName,LinuxFlags or Open_Creat or Open_Trunc);
  56. end;
  57. Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  58. begin
  59. FileRead:=fdRead (Handle,Buffer,Count);
  60. end;
  61. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  62. begin
  63. FileWrite:=fdWrite (Handle,Buffer,Count);
  64. end;
  65. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  66. begin
  67. FileSeek:=fdSeek (Handle,FOffset,Origin);
  68. end;
  69. Function FileSeek (Handle : Longint; FOffset,Origin : Int64) : Int64;
  70. begin
  71. {$warning need to add 64bit call }
  72. FileSeek:=fdSeek (Handle,FOffset,Origin);
  73. end;
  74. Procedure FileClose (Handle : Longint);
  75. begin
  76. fdclose(Handle);
  77. end;
  78. Function FileTruncate (Handle,Size: Longint) : boolean;
  79. begin
  80. FileTruncate:=fdtruncate(Handle,Size);
  81. end;
  82. Function FileAge (Const FileName : String): Longint;
  83. Var Info : Stat;
  84. Y,M,D,hh,mm,ss : word;
  85. begin
  86. If not fstat (FileName,Info) then
  87. exit(-1)
  88. else
  89. begin
  90. EpochToLocal(info.mtime,y,m,d,hh,mm,ss);
  91. Result:=DateTimeToFileDate(EncodeDate(y,m,d)+EncodeTime(hh,mm,ss,0));
  92. end;
  93. end;
  94. Function FileExists (Const FileName : String) : Boolean;
  95. Var Info : Stat;
  96. begin
  97. FileExists:=fstat(filename,Info);
  98. end;
  99. Function DirectoryExists (Const Directory : String) : Boolean;
  100. Var Info : Stat;
  101. begin
  102. DirectoryExists:=fstat(Directory,Info) and
  103. ((info.mode and STAT_IFMT)=STAT_IFDIR);
  104. end;
  105. Function LinuxToWinAttr (FN : Pchar; Const Info : Stat) : Longint;
  106. begin
  107. Result:=faArchive;
  108. If (Info.Mode and STAT_IFDIR)=STAT_IFDIR then
  109. Result:=Result or faDirectory;
  110. If (FN[0]='.') and (not (FN[1] in [#0,'.'])) then
  111. Result:=Result or faHidden;
  112. If (Info.Mode and STAT_IWUSR)=0 Then
  113. Result:=Result or faReadOnly;
  114. If (Info.Mode and
  115. (STAT_IFSOCK or STAT_IFBLK or STAT_IFCHR or STAT_IFIFO))<>0 then
  116. Result:=Result or faSysFile;
  117. end;
  118. {
  119. GlobToSearch takes a glob entry, stats the file.
  120. The glob entry is removed.
  121. If FileAttributes match, the entry is reused
  122. }
  123. Type
  124. TGlobSearchRec = Record
  125. Path : String;
  126. GlobHandle : PGlob;
  127. end;
  128. PGlobSearchRec = ^TGlobSearchRec;
  129. Function GlobToTSearchRec (Var Info : TSearchRec) : Boolean;
  130. Var SInfo : Stat;
  131. p : Pglob;
  132. GlobSearchRec : PGlobSearchrec;
  133. begin
  134. GlobSearchRec:=PGlobSearchrec(Info.FindHandle);
  135. P:=GlobSearchRec^.GlobHandle;
  136. Result:=P<>Nil;
  137. If Result then
  138. begin
  139. GlobSearchRec^.GlobHandle:=P^.Next;
  140. Result:=Fstat(GlobSearchRec^.Path+StrPas(p^.name),SInfo);
  141. If Result then
  142. begin
  143. Info.Attr:=LinuxToWinAttr(p^.name,SInfo);
  144. Result:=(Info.ExcludeAttr and Info.Attr)=0;
  145. If Result Then
  146. With Info do
  147. begin
  148. Attr:=Info.Attr;
  149. If P^.Name<>Nil then
  150. Name:=strpas(p^.name);
  151. Time:=Sinfo.mtime;
  152. Size:=Sinfo.Size;
  153. end;
  154. end;
  155. P^.Next:=Nil;
  156. GlobFree(P);
  157. end;
  158. end;
  159. Function DoFind(Var Rslt : TSearchRec) : Longint;
  160. Var
  161. GlobSearchRec : PGlobSearchRec;
  162. begin
  163. Result:=-1;
  164. GlobSearchRec:=PGlobSearchRec(Rslt.FindHandle);
  165. If (GlobSearchRec^.GlobHandle<>Nil) then
  166. While (GlobSearchRec^.GlobHandle<>Nil) and not (Result=0) do
  167. If GlobToTSearchRec(Rslt) Then Result:=0;
  168. end;
  169. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  170. Var
  171. GlobSearchRec : PGlobSearchRec;
  172. begin
  173. New(GlobSearchRec);
  174. GlobSearchRec^.Path:=ExpandFileName(ExtractFilePath(Path));
  175. GlobSearchRec^.GlobHandle:=Glob(Path);
  176. Rslt.ExcludeAttr:=Not Attr; //!! Not correct !!
  177. Rslt.FindHandle:=Longint(GlobSearchRec);
  178. Result:=DoFind (Rslt);
  179. end;
  180. Function FindNext (Var Rslt : TSearchRec) : Longint;
  181. begin
  182. Result:=DoFind (Rslt);
  183. end;
  184. Procedure FindClose (Var F : TSearchrec);
  185. Var
  186. GlobSearchRec : PGlobSearchRec;
  187. begin
  188. GlobSearchRec:=PGlobSearchRec(F.FindHandle);
  189. GlobFree (GlobSearchRec^.GlobHandle);
  190. Dispose(GlobSearchRec);
  191. end;
  192. Function FileGetDate (Handle : Longint) : Longint;
  193. Var Info : Stat;
  194. begin
  195. If Not(FStat(Handle,Info)) then
  196. Result:=-1
  197. else
  198. Result:=Info.Mtime;
  199. end;
  200. Function FileSetDate (Handle,Age : Longint) : Longint;
  201. begin
  202. // Impossible under Linux from FileHandle !!
  203. FileSetDate:=-1;
  204. end;
  205. Function FileGetAttr (Const FileName : String) : Longint;
  206. Var Info : Stat;
  207. begin
  208. If Not FStat (FileName,Info) then
  209. Result:=-1
  210. Else
  211. Result:=LinuxToWinAttr(Pchar(FileName),Info);
  212. end;
  213. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  214. begin
  215. Result:=-1;
  216. end;
  217. Function DeleteFile (Const FileName : String) : Boolean;
  218. begin
  219. Result:=UnLink (FileName);
  220. end;
  221. Function RenameFile (Const OldName, NewName : String) : Boolean;
  222. begin
  223. RenameFile:=Unix.FRename(OldNAme,NewName);
  224. end;
  225. {****************************************************************************
  226. Disk Functions
  227. ****************************************************************************}
  228. {
  229. The Diskfree and Disksize functions need a file on the specified drive, since this
  230. is required for the statfs system call.
  231. These filenames are set in drivestr[0..26], and have been preset to :
  232. 0 - '.' (default drive - hence current dir is ok.)
  233. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  234. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  235. 3 - '/' (C: equivalent of dos is the root partition)
  236. 4..26 (can be set by you're own applications)
  237. ! Use AddDisk() to Add new drives !
  238. They both return -1 when a failure occurs.
  239. }
  240. Const
  241. FixDriveStr : array[0..3] of pchar=(
  242. '.',
  243. '/fd0/.',
  244. '/fd1/.',
  245. '/.'
  246. );
  247. var
  248. Drives : byte;
  249. DriveStr : array[4..26] of pchar;
  250. Procedure AddDisk(const path:string);
  251. begin
  252. if not (DriveStr[Drives]=nil) then
  253. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  254. GetMem(DriveStr[Drives],length(Path)+1);
  255. StrPCopy(DriveStr[Drives],path);
  256. inc(Drives);
  257. if Drives>26 then
  258. Drives:=4;
  259. end;
  260. Function DiskFree(Drive: Byte): int64;
  261. var
  262. fs : tstatfs;
  263. Begin
  264. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and statfs(StrPas(fixdrivestr[drive]),fs)) or
  265. ((not (drivestr[Drive]=nil)) and statfs(StrPas(drivestr[drive]),fs)) then
  266. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  267. else
  268. Diskfree:=-1;
  269. End;
  270. Function DiskSize(Drive: Byte): int64;
  271. var
  272. fs : tstatfs;
  273. Begin
  274. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and statfs(StrPas(fixdrivestr[drive]),fs)) or
  275. ((not (drivestr[Drive]=nil)) and statfs(StrPas(drivestr[drive]),fs)) then
  276. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  277. else
  278. DiskSize:=-1;
  279. End;
  280. Function GetCurrentDir : String;
  281. begin
  282. GetDir (0,Result);
  283. end;
  284. Function SetCurrentDir (Const NewDir : String) : Boolean;
  285. begin
  286. {$I-}
  287. ChDir(NewDir);
  288. {$I+}
  289. result := (IOResult = 0);
  290. end;
  291. Function CreateDir (Const NewDir : String) : Boolean;
  292. begin
  293. {$I-}
  294. MkDir(NewDir);
  295. {$I+}
  296. result := (IOResult = 0);
  297. end;
  298. Function RemoveDir (Const Dir : String) : Boolean;
  299. begin
  300. {$I-}
  301. RmDir(Dir);
  302. {$I+}
  303. result := (IOResult = 0);
  304. end;
  305. {****************************************************************************
  306. Misc Functions
  307. ****************************************************************************}
  308. procedure Beep;
  309. begin
  310. end;
  311. {****************************************************************************
  312. Locale Functions
  313. ****************************************************************************}
  314. Procedure GetLocalTime(var SystemTime: TSystemTime);
  315. begin
  316. Unix.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second);
  317. Unix.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  318. SystemTime.MilliSecond := 0;
  319. end ;
  320. Procedure InitAnsi;
  321. Var
  322. i : longint;
  323. begin
  324. { Fill table entries 0 to 127 }
  325. for i := 0 to 96 do
  326. UpperCaseTable[i] := chr(i);
  327. for i := 97 to 122 do
  328. UpperCaseTable[i] := chr(i - 32);
  329. for i := 123 to 191 do
  330. UpperCaseTable[i] := chr(i);
  331. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  332. for i := 0 to 64 do
  333. LowerCaseTable[i] := chr(i);
  334. for i := 65 to 90 do
  335. LowerCaseTable[i] := chr(i + 32);
  336. for i := 91 to 191 do
  337. LowerCaseTable[i] := chr(i);
  338. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  339. end;
  340. Procedure InitInternational;
  341. begin
  342. InitAnsi;
  343. end;
  344. function SysErrorMessage(ErrorCode: Integer): String;
  345. begin
  346. Result:=StrError(ErrorCode);
  347. end;
  348. {****************************************************************************
  349. OS utility functions
  350. ****************************************************************************}
  351. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  352. begin
  353. Result:=StrPas(Unix.Getenv(PChar(EnvVar)));
  354. end;
  355. {****************************************************************************
  356. Initialization code
  357. ****************************************************************************}
  358. Initialization
  359. InitExceptions; { Initialize exceptions. OS independent }
  360. InitInternational; { Initialize internationalization settings }
  361. Finalization
  362. DoneExceptions;
  363. end.
  364. {
  365. $Log$
  366. Revision 1.18 2003-04-01 15:57:41 peter
  367. * made THandle platform dependent and unique type
  368. Revision 1.17 2003/03/30 10:38:00 armin
  369. * corrected typo in DirectoryExists
  370. Revision 1.16 2003/03/29 18:21:42 hajny
  371. * DirectoryExists declaration changed to that one from fixes branch
  372. Revision 1.15 2003/03/28 19:06:59 peter
  373. * directoryexists added
  374. Revision 1.14 2003/01/03 20:41:04 peter
  375. * FileCreate(string,mode) overload added
  376. Revision 1.13 2002/09/07 16:01:28 peter
  377. * old logs removed and tabs fixed
  378. Revision 1.12 2002/01/25 16:23:03 peter
  379. * merged filesearch() fix
  380. }