filutil.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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:=FileOpen (FileName,Open_RdWr or Open_Creat);
  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 FileAge (Const FileName : String): Longint;
  45. Var Info : Stat;
  46. begin
  47. If not fstat (FileName,Info) then
  48. exit(-1)
  49. else
  50. Exit (Info.mtime);
  51. end;
  52. Function FileExists (Const FileName : String) : Boolean;
  53. Var Info : Stat;
  54. begin
  55. FileExists:=fstat(filename,Info);
  56. end;
  57. Function LinuxToWinAttr (FN : Char; Const Info : Stat) : Longint;
  58. begin
  59. Result:=0;
  60. If FN='.' then
  61. Result:=Result or faHidden;
  62. If (Info.Mode and STAT_IFDIR)=STAT_IFDIR then
  63. Result:=Result or faDirectory;
  64. If (Info.Mode and STAT_IWUSR)=0 Then
  65. Result:=Result or faReadOnly;
  66. end;
  67. {
  68. GlobToSearch takes a glob entry, stats the file.
  69. The glob entry is removed.
  70. If FileAttributes match, the entry is reused
  71. }
  72. Function GlobToTSearchRec (Info : TSearchRec) : Boolean;
  73. Var SInfo : Stat;
  74. p : Pglob;
  75. TAttr : Longint;
  76. begin
  77. P:=pglob(Info.FindHandle);
  78. Result:=Fstat(p^.name,SInfo);
  79. Info.FindHandle:=Longint(P^.Next);
  80. P^.Next:=Nil;
  81. GlobFree(P);
  82. If Result then
  83. begin
  84. Info.Attr:=LinuxToWinAttr(p^.name[0],SInfo);
  85. Result:=(Info.ExcludeAttr and TAttr)<>0 ;
  86. If Result Then
  87. With Info do
  88. begin
  89. Attr:=Info.Attr;
  90. Name:=strpas(p^.name);
  91. Time:=Sinfo.mtime;
  92. Size:=Sinfo.Size;
  93. end;
  94. end;
  95. end;
  96. Function DoFind(Var Rslt : TSearchRec) : Longint;
  97. begin
  98. If Rslt.FindHandle<>0 then
  99. While (Rslt.FindHandle<>0) and GlobToTSearchRec(Rslt) do;
  100. If Rslt.FindHandle=0 Then
  101. Result:=-1
  102. else
  103. Result:=0;
  104. end;
  105. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  106. begin
  107. Rslt.ExcludeAttr:=Attr; //!! Not correct !!
  108. Rslt.FindHandle:=Longint(Glob(Path));
  109. Result:=DoFind (Rslt);
  110. end;
  111. Function FindNext (Var Rslt : TSearchRec) : Longint;
  112. begin
  113. Result:=DoFind (Rslt);
  114. end;
  115. Procedure FindClose (Var F : TSearchrec);
  116. begin
  117. GlobFree (PGlob(F.FindHandle));
  118. end;
  119. Function FileGetDate (Handle : Longint) : Longint;
  120. Var Info : Stat;
  121. begin
  122. If Not(FStat(Handle,Info)) then
  123. Result:=-1
  124. else
  125. Result:=Info.Mtime;
  126. end;
  127. Function FileSetDate (Handle,Age : Longint) : Longint;
  128. begin
  129. // Impossible under Linux from FileHandle !!
  130. FileSetDate:=-1;
  131. end;
  132. Function FileGetAttr (Const FileName : String) : Longint;
  133. Var Info : Stat;
  134. begin
  135. If Not FStat (FileName,Info) then
  136. Result:=-1
  137. Else
  138. Result:=LinuxToWinAttr(FileName[1],Info);
  139. end;
  140. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  141. begin
  142. //!! Still Needs doing
  143. end;
  144. Function DeleteFile (Const FileName : String) : Boolean;
  145. begin
  146. Result:=UnLink (FileName);
  147. end;
  148. Function RenameFile (Const OldName, NewName : String) : Boolean;
  149. Var P1,P2 : String;
  150. begin
  151. RenameFile:=Linux.Rename(OldNAme,NewName);
  152. end;
  153. Function FileSearch (Const Name, DirList : String) : String;
  154. begin
  155. FileSearch:=Linux.FSearch(Name,Dirlist);
  156. end;
  157. {
  158. $Log$
  159. Revision 1.1 1998-10-11 12:21:01 michael
  160. Added file calls. Implemented for linux only
  161. }