sysutils.pp 11 KB

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