filutil.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998 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. fdclose(Handle);
  43. end;
  44. Function FileTruncate (Handle,Size: Longint) : boolean;
  45. begin
  46. FileTruncate:=fdtruncate(Handle,Size);
  47. end;
  48. Function FileAge (Const FileName : String): Longint;
  49. Var Info : Stat;
  50. begin
  51. If not fstat (FileName,Info) then
  52. exit(-1)
  53. else
  54. Exit (Info.mtime);
  55. end;
  56. Function FileExists (Const FileName : String) : Boolean;
  57. Var Info : Stat;
  58. begin
  59. FileExists:=fstat(filename,Info);
  60. end;
  61. Function LinuxToWinAttr (FN : Char; Const Info : Stat) : Longint;
  62. begin
  63. Result:=0;
  64. If FN='.' then
  65. Result:=Result or faHidden;
  66. If (Info.Mode and STAT_IFDIR)=STAT_IFDIR then
  67. Result:=Result or faDirectory;
  68. If (Info.Mode and STAT_IWUSR)=0 Then
  69. Result:=Result or faReadOnly;
  70. end;
  71. {
  72. GlobToSearch takes a glob entry, stats the file.
  73. The glob entry is removed.
  74. If FileAttributes match, the entry is reused
  75. }
  76. Function GlobToTSearchRec (Info : TSearchRec) : Boolean;
  77. Var SInfo : Stat;
  78. p : Pglob;
  79. TAttr : Longint;
  80. begin
  81. P:=pglob(Info.FindHandle);
  82. Result:=Fstat(p^.name,SInfo);
  83. Info.FindHandle:=Longint(P^.Next);
  84. P^.Next:=Nil;
  85. GlobFree(P);
  86. If Result then
  87. begin
  88. Info.Attr:=LinuxToWinAttr(p^.name[0],SInfo);
  89. Result:=(Info.ExcludeAttr and TAttr)<>0 ;
  90. If Result Then
  91. With Info do
  92. begin
  93. Attr:=Info.Attr;
  94. Name:=strpas(p^.name);
  95. Time:=Sinfo.mtime;
  96. Size:=Sinfo.Size;
  97. end;
  98. end;
  99. end;
  100. Function DoFind(Var Rslt : TSearchRec) : Longint;
  101. begin
  102. If Rslt.FindHandle<>0 then
  103. While (Rslt.FindHandle<>0) and GlobToTSearchRec(Rslt) do;
  104. If Rslt.FindHandle=0 Then
  105. Result:=-1
  106. else
  107. Result:=0;
  108. end;
  109. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  110. begin
  111. Rslt.ExcludeAttr:=Attr; //!! Not correct !!
  112. Rslt.FindHandle:=Longint(Glob(Path));
  113. Result:=DoFind (Rslt);
  114. end;
  115. Function FindNext (Var Rslt : TSearchRec) : Longint;
  116. begin
  117. Result:=DoFind (Rslt);
  118. end;
  119. Procedure FindClose (Var F : TSearchrec);
  120. begin
  121. GlobFree (PGlob(F.FindHandle));
  122. end;
  123. Function FileGetDate (Handle : Longint) : Longint;
  124. Var Info : Stat;
  125. begin
  126. If Not(FStat(Handle,Info)) then
  127. Result:=-1
  128. else
  129. Result:=Info.Mtime;
  130. end;
  131. Function FileSetDate (Handle,Age : Longint) : Longint;
  132. begin
  133. // Impossible under Linux from FileHandle !!
  134. FileSetDate:=-1;
  135. end;
  136. Function FileGetAttr (Const FileName : String) : Longint;
  137. Var Info : Stat;
  138. begin
  139. If Not FStat (FileName,Info) then
  140. Result:=-1
  141. Else
  142. Result:=LinuxToWinAttr(FileName[1],Info);
  143. end;
  144. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  145. begin
  146. //!! Still Needs doing
  147. end;
  148. Function DeleteFile (Const FileName : String) : Boolean;
  149. begin
  150. Result:=UnLink (FileName);
  151. end;
  152. Function RenameFile (Const OldName, NewName : String) : Boolean;
  153. Var P1,P2 : String;
  154. begin
  155. RenameFile:=Linux.FRename(OldNAme,NewName);
  156. end;
  157. Function FileSearch (Const Name, DirList : String) : String;
  158. begin
  159. FileSearch:=Linux.FSearch(Name,Dirlist);
  160. end;
  161. Procedure GetLocalTime(var SystemTime: TSystemTime);
  162. begin
  163. linux.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second);
  164. linux.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  165. SystemTime.MilliSecond := 0;
  166. end ;
  167. Procedure InitAnsi;
  168. Var i : longint;
  169. begin
  170. { Fill table entries 0 to 127 }
  171. for i := 0 to 96 do
  172. UpperCaseTable[i] := chr(i);
  173. for i := 97 to 122 do
  174. UpperCaseTable[i] := chr(i - 32);
  175. for i := 123 to 191 do
  176. UpperCaseTable[i] := chr(i);
  177. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  178. for i := 0 to 64 do
  179. LowerCaseTable[i] := chr(i);
  180. for i := 65 to 90 do
  181. LowerCaseTable[i] := chr(i + 32);
  182. for i := 91 to 191 do
  183. LowerCaseTable[i] := chr(i);
  184. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  185. end;
  186. Procedure InitInternational;
  187. begin
  188. InitAnsi;
  189. end;
  190. {
  191. $Log$
  192. Revision 1.8 1999-02-28 13:18:10 michael
  193. + Added internationalization support
  194. Revision 1.7 1999/02/24 15:57:29 michael
  195. + Moved getlocaltime to system-dependent files
  196. Revision 1.6 1999/02/04 21:43:08 michael
  197. FileCreate must truncate the file
  198. Revision 1.5 1999/02/02 21:20:34 michael
  199. + Added filetruncate, corrected FileCreate
  200. Revision 1.4 1998/12/15 22:43:07 peter
  201. * removed temp symbols
  202. Revision 1.3 1998/11/10 14:57:55 peter
  203. * renamed rename -> FRename
  204. Revision 1.2 1998/10/13 10:20:07 peter
  205. * fix for 0.99.8 which has no auto pchar-string ;)
  206. Revision 1.1 1998/10/11 12:21:01 michael
  207. Added file calls. Implemented for linux only
  208. }