filutil.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. TAttr:=$ffffffff;
  82. P:=pglob(Info.FindHandle);
  83. Result:=Fstat(p^.name,SInfo);
  84. Info.FindHandle:=Longint(P^.Next);
  85. P^.Next:=Nil;
  86. GlobFree(P);
  87. If Result then
  88. begin
  89. Info.Attr:=LinuxToWinAttr(p^.name[0],SInfo);
  90. Result:=(Info.ExcludeAttr and TAttr)<>0 ;
  91. If Result Then
  92. With Info do
  93. begin
  94. Attr:=Info.Attr;
  95. Name:=strpas(p^.name);
  96. Time:=Sinfo.mtime;
  97. Size:=Sinfo.Size;
  98. end;
  99. end;
  100. end;
  101. Function DoFind(Var Rslt : TSearchRec) : Longint;
  102. begin
  103. If Rslt.FindHandle<>0 then
  104. While (Rslt.FindHandle<>0) and GlobToTSearchRec(Rslt) do;
  105. If Rslt.FindHandle=0 Then
  106. Result:=-1
  107. else
  108. Result:=0;
  109. end;
  110. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  111. begin
  112. Rslt.ExcludeAttr:=Attr; //!! Not correct !!
  113. Rslt.FindHandle:=Longint(Glob(Path));
  114. Result:=DoFind (Rslt);
  115. end;
  116. Function FindNext (Var Rslt : TSearchRec) : Longint;
  117. begin
  118. Result:=DoFind (Rslt);
  119. end;
  120. Procedure FindClose (Var F : TSearchrec);
  121. begin
  122. GlobFree (PGlob(F.FindHandle));
  123. end;
  124. Function FileGetDate (Handle : Longint) : Longint;
  125. Var Info : Stat;
  126. begin
  127. If Not(FStat(Handle,Info)) then
  128. Result:=-1
  129. else
  130. Result:=Info.Mtime;
  131. end;
  132. Function FileSetDate (Handle,Age : Longint) : Longint;
  133. begin
  134. // Impossible under Linux from FileHandle !!
  135. FileSetDate:=-1;
  136. end;
  137. Function FileGetAttr (Const FileName : String) : Longint;
  138. Var Info : Stat;
  139. begin
  140. If Not FStat (FileName,Info) then
  141. Result:=-1
  142. Else
  143. Result:=LinuxToWinAttr(FileName[1],Info);
  144. end;
  145. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  146. begin
  147. //!! Still Needs doing
  148. Result:=0;
  149. end;
  150. Function DeleteFile (Const FileName : String) : Boolean;
  151. begin
  152. Result:=UnLink (FileName);
  153. end;
  154. Function RenameFile (Const OldName, NewName : String) : Boolean;
  155. begin
  156. RenameFile:=Linux.FRename(OldNAme,NewName);
  157. end;
  158. Function FileSearch (Const Name, DirList : String) : String;
  159. begin
  160. FileSearch:=Linux.FSearch(Name,Dirlist);
  161. end;
  162. Procedure GetLocalTime(var SystemTime: TSystemTime);
  163. begin
  164. linux.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second);
  165. linux.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  166. SystemTime.MilliSecond := 0;
  167. end ;
  168. Procedure InitAnsi;
  169. Var i : longint;
  170. begin
  171. { Fill table entries 0 to 127 }
  172. for i := 0 to 96 do
  173. UpperCaseTable[i] := chr(i);
  174. for i := 97 to 122 do
  175. UpperCaseTable[i] := chr(i - 32);
  176. for i := 123 to 191 do
  177. UpperCaseTable[i] := chr(i);
  178. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  179. for i := 0 to 64 do
  180. LowerCaseTable[i] := chr(i);
  181. for i := 65 to 90 do
  182. LowerCaseTable[i] := chr(i + 32);
  183. for i := 91 to 191 do
  184. LowerCaseTable[i] := chr(i);
  185. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  186. end;
  187. Procedure InitInternational;
  188. begin
  189. InitAnsi;
  190. end;
  191. {
  192. $Log$
  193. Revision 1.9 1999-04-08 11:31:01 peter
  194. * removed warnings
  195. Revision 1.8 1999/02/28 13:18:10 michael
  196. + Added internationalization support
  197. Revision 1.7 1999/02/24 15:57:29 michael
  198. + Moved getlocaltime to system-dependent files
  199. Revision 1.6 1999/02/04 21:43:08 michael
  200. FileCreate must truncate the file
  201. Revision 1.5 1999/02/02 21:20:34 michael
  202. + Added filetruncate, corrected FileCreate
  203. Revision 1.4 1998/12/15 22:43:07 peter
  204. * removed temp symbols
  205. Revision 1.3 1998/11/10 14:57:55 peter
  206. * renamed rename -> FRename
  207. Revision 1.2 1998/10/13 10:20:07 peter
  208. * fix for 0.99.8 which has no auto pchar-string ;)
  209. Revision 1.1 1998/10/11 12:21:01 michael
  210. Added file calls. Implemented for linux only
  211. }