filutil.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. File utility calls
  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. Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
  13. Var LinuxFlags : longint;
  14. BEGIN
  15. LinuxFlags:=0;
  16. Case (Mode and 3) of
  17. 0 : LinuxFlags:=LinuxFlags or Open_RdOnly;
  18. 1 : LinuxFlags:=LinuxFlags or Open_WrOnly;
  19. 2 : LinuxFlags:=LinuxFlags or Open_RdWr;
  20. end;
  21. FileOpen:=fdOpen (FileName,LinuxFlags);
  22. //!! We need to set locking based on Mode !!
  23. end;
  24. Function FileCreate (Const FileName : String) : Longint;
  25. begin
  26. FileCreate:=fdOpen(FileName,Open_RdWr or Open_Creat or Open_Trunc);
  27. end;
  28. Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  29. begin
  30. FileRead:=fdRead (Handle,Buffer,Count);
  31. end;
  32. Function FileWrite (Handle : Longint; Var Buffer; Count : Longint) : Longint;
  33. begin
  34. FileWrite:=fdWrite (Handle,Buffer,Count);
  35. end;
  36. Function FileSeek (Handle,Offset,Origin : Longint) : Longint;
  37. begin
  38. FileSeek:=fdSeek (Handle,Offset,Origin);
  39. end;
  40. Procedure FileClose (Handle : Longint);
  41. begin
  42. if Handle<=4 then
  43. exit;
  44. fdclose(Handle);
  45. end;
  46. Function FileTruncate (Handle,Size: Longint) : boolean;
  47. begin
  48. FileTruncate:=fdtruncate(Handle,Size);
  49. end;
  50. Function FileAge (Const FileName : String): Longint;
  51. Var Info : Stat;
  52. Y,M,D,hh,mm,ss : word;
  53. begin
  54. If not fstat (FileName,Info) then
  55. exit(-1)
  56. else
  57. begin
  58. EpochToLocal(info.mtime,y,m,d,hh,mm,ss);
  59. Result:=DateTimeToFileDate(EncodeDate(y,m,d)+EncodeTime(hh,mm,ss,0));
  60. end;
  61. end;
  62. Function FileExists (Const FileName : String) : Boolean;
  63. Var Info : Stat;
  64. begin
  65. FileExists:=fstat(filename,Info);
  66. end;
  67. Function LinuxToWinAttr (FN : Char; Const Info : Stat) : Longint;
  68. begin
  69. Result:=faArchive;
  70. If FN='.' then
  71. Result:=Result or faHidden;
  72. If (Info.Mode and STAT_IFDIR)=STAT_IFDIR then
  73. Result:=Result or faDirectory;
  74. If (Info.Mode and STAT_IWUSR)=0 Then
  75. Result:=Result or faReadOnly;
  76. If (Info.Mode and
  77. (STAT_IFSOCK or STAT_IFBLK or STAT_IFCHR or STAT_IFIFO))<>0 then
  78. Result:=Result or faSysFile;
  79. end;
  80. {
  81. GlobToSearch takes a glob entry, stats the file.
  82. The glob entry is removed.
  83. If FileAttributes match, the entry is reused
  84. }
  85. Function GlobToTSearchRec (Var Info : TSearchRec) : Boolean;
  86. Var SInfo : Stat;
  87. p : Pglob;
  88. TAttr : Longint;
  89. begin
  90. TAttr:=$ffffffff;
  91. P:=pglob(Info.FindHandle);
  92. Result:=P<>Nil;
  93. If Result then
  94. begin
  95. Info.FindHandle:=Longint(P^.Next);
  96. Result:=Fstat(p^.name,SInfo);
  97. If Result then
  98. begin
  99. Info.Attr:=LinuxToWinAttr(p^.name[0],SInfo);
  100. Result:=(Info.ExcludeAttr and TAttr)<>0;
  101. If Result Then
  102. With Info do
  103. begin
  104. Attr:=Info.Attr;
  105. If P^.Name<>Nil then
  106. Name:=strpas(p^.name);
  107. Time:=Sinfo.mtime;
  108. Size:=Sinfo.Size;
  109. end;
  110. end;
  111. P^.Next:=Nil;
  112. GlobFree(P);
  113. end;
  114. end;
  115. Function DoFind(Var Rslt : TSearchRec) : Longint;
  116. begin
  117. Result:=-1;
  118. If Rslt.FindHandle<>0 then
  119. While (Rslt.FindHandle<>0) and not (Result=0) do
  120. If GlobToTSearchRec(Rslt) Then Result:=0;
  121. end;
  122. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  123. begin
  124. Rslt.ExcludeAttr:=Attr; //!! Not correct !!
  125. Rslt.FindHandle:=Longint(Glob(Path));
  126. Result:=DoFind (Rslt);
  127. end;
  128. Function FindNext (Var Rslt : TSearchRec) : Longint;
  129. begin
  130. Result:=DoFind (Rslt);
  131. end;
  132. Procedure FindClose (Var F : TSearchrec);
  133. begin
  134. GlobFree (PGlob(F.FindHandle));
  135. end;
  136. Function FileGetDate (Handle : Longint) : Longint;
  137. Var Info : Stat;
  138. begin
  139. If Not(FStat(Handle,Info)) then
  140. Result:=-1
  141. else
  142. Result:=Info.Mtime;
  143. end;
  144. Function FileSetDate (Handle,Age : Longint) : Longint;
  145. begin
  146. // Impossible under Linux from FileHandle !!
  147. FileSetDate:=-1;
  148. end;
  149. Function FileGetAttr (Const FileName : String) : Longint;
  150. Var Info : Stat;
  151. begin
  152. If Not FStat (FileName,Info) then
  153. Result:=-1
  154. Else
  155. Result:=LinuxToWinAttr(FileName[1],Info);
  156. end;
  157. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  158. begin
  159. Result:=-1;
  160. end;
  161. Function DeleteFile (Const FileName : String) : Boolean;
  162. begin
  163. Result:=UnLink (FileName);
  164. end;
  165. Function RenameFile (Const OldName, NewName : String) : Boolean;
  166. begin
  167. RenameFile:=Linux.FRename(OldNAme,NewName);
  168. end;
  169. Function FileSearch (Const Name, DirList : String) : String;
  170. begin
  171. FileSearch:=Linux.FSearch(Name,Dirlist);
  172. end;
  173. Procedure GetLocalTime(var SystemTime: TSystemTime);
  174. begin
  175. linux.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second);
  176. linux.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  177. SystemTime.MilliSecond := 0;
  178. end ;
  179. Procedure InitAnsi;
  180. Var i : longint;
  181. begin
  182. { Fill table entries 0 to 127 }
  183. for i := 0 to 96 do
  184. UpperCaseTable[i] := chr(i);
  185. for i := 97 to 122 do
  186. UpperCaseTable[i] := chr(i - 32);
  187. for i := 123 to 191 do
  188. UpperCaseTable[i] := chr(i);
  189. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  190. for i := 0 to 64 do
  191. LowerCaseTable[i] := chr(i);
  192. for i := 65 to 90 do
  193. LowerCaseTable[i] := chr(i + 32);
  194. for i := 91 to 191 do
  195. LowerCaseTable[i] := chr(i);
  196. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  197. end;
  198. Procedure InitInternational;
  199. begin
  200. InitAnsi;
  201. end;
  202. {
  203. $Log$
  204. Revision 1.14 2000-02-09 16:59:31 peter
  205. * truncated log
  206. Revision 1.13 2000/01/16 22:25:38 peter
  207. * check handle for file closing
  208. Revision 1.12 2000/01/07 16:41:40 daniel
  209. * copyright 2000
  210. }