sysfile.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. POSIX Interface to the system unit
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This is the core of the system unit *nix systems (now FreeBSD
  8. and Unix).
  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. Procedure Do_Close(Handle:thandle);
  14. Begin
  15. Fpclose(cint(Handle));
  16. End;
  17. Procedure Do_Erase(p:pchar);
  18. var
  19. fileinfo : stat;
  20. Begin
  21. { verify if the filename is actually a directory }
  22. { if so return error and do nothing, as defined }
  23. { by POSIX }
  24. if Fpstat(p,fileinfo)<0 then
  25. begin
  26. Errno2Inoutres;
  27. exit;
  28. end;
  29. if FpS_ISDIR(fileinfo.st_mode) then
  30. begin
  31. InOutRes := 2;
  32. exit;
  33. end;
  34. if Fpunlink(p)<0 then
  35. Errno2Inoutres
  36. Else
  37. InOutRes:=0;
  38. End;
  39. { truncate at a given position }
  40. procedure do_truncate (handle:thandle;fpos:longint);
  41. begin
  42. { should be simulated in cases where it is not }
  43. { available. }
  44. If Fpftruncate(handle,fpos)<0 Then
  45. Errno2Inoutres
  46. Else
  47. InOutRes:=0;
  48. end;
  49. Procedure Do_Rename(p1,p2:pchar);
  50. Begin
  51. If Fprename(p1,p2)<0 Then
  52. Errno2Inoutres
  53. Else
  54. InOutRes:=0;
  55. End;
  56. Function Do_Write(Handle:thandle;Addr:Pointer;Len:SizeInt):SizeInt;
  57. Begin
  58. repeat
  59. Do_Write:=Fpwrite(Handle,addr,len);
  60. until (Do_Write>=0) or (getErrNo<>ESysEINTR);
  61. If Do_Write<0 Then
  62. Begin
  63. Errno2InOutRes;
  64. Do_Write:=0;
  65. End
  66. else
  67. InOutRes:=0;
  68. End;
  69. Function Do_Read(Handle:thandle;Addr:Pointer;Len:SizeInt):SizeInt;
  70. Begin
  71. repeat
  72. Do_Read:=Fpread(Handle,addr,len);
  73. until (Do_Read>=0) or (getErrNo<>ESysEINTR);
  74. If Do_Read<0 Then
  75. Begin
  76. Errno2InOutRes;
  77. Do_Read:=0;
  78. End
  79. else
  80. InOutRes:=0;
  81. End;
  82. function Do_FilePos(Handle: thandle):longint;
  83. Begin
  84. do_FilePos:=Fplseek(Handle, 0, SEEK_CUR);
  85. If Do_FilePos<0 Then
  86. Errno2InOutRes
  87. else
  88. InOutRes:=0;
  89. End;
  90. Procedure Do_Seek(Handle:thandle;Pos:Longint);
  91. Begin
  92. If Fplseek(Handle, pos, SEEK_SET)<0 Then
  93. Errno2Inoutres
  94. Else
  95. InOutRes:=0;
  96. End;
  97. Function Do_SeekEnd(Handle:thandle): Longint;
  98. begin
  99. Do_SeekEnd:=Fplseek(Handle,0,SEEK_END);
  100. If Do_SeekEnd<0 Then
  101. Errno2Inoutres
  102. Else
  103. InOutRes:=0;
  104. end;
  105. Function Do_FileSize(Handle:thandle): Longint;
  106. var
  107. Info : Stat;
  108. Ret : Longint;
  109. Begin
  110. Ret:=Fpfstat(handle,info);
  111. If Ret=0 Then
  112. Do_FileSize:=Info.st_size
  113. else
  114. Do_FileSize:=0;
  115. If Ret<0 Then
  116. Errno2InOutRes
  117. Else
  118. InOutRes:=0;
  119. End;
  120. Procedure Do_Open(var f;p:pchar;flags:longint);
  121. {
  122. FileRec and textrec have both Handle and mode as the first items so
  123. they could use the same routine for opening/creating.
  124. when (flags and $100) the file will be append
  125. when (flags and $1000) the file will be truncate/rewritten
  126. when (flags and $10000) there is no check for close (needed for textfiles)
  127. }
  128. var
  129. oflags : cint;
  130. Begin
  131. { close first if opened }
  132. if ((flags and $10000)=0) then
  133. begin
  134. case FileRec(f).mode of
  135. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  136. fmclosed : ;
  137. else
  138. begin
  139. inoutres:=102; {not assigned}
  140. exit;
  141. end;
  142. end;
  143. end;
  144. { reset file Handle }
  145. FileRec(f).Handle:=UnusedHandle;
  146. { We do the conversion of filemodes here, concentrated on 1 place }
  147. case (flags and 3) of
  148. 0 : begin
  149. oflags :=O_RDONLY;
  150. FileRec(f).mode:=fminput;
  151. end;
  152. 1 : begin
  153. oflags :=O_WRONLY;
  154. FileRec(f).mode:=fmoutput;
  155. end;
  156. 2 : begin
  157. oflags :=O_RDWR;
  158. FileRec(f).mode:=fminout;
  159. end;
  160. end;
  161. if (flags and $1000)=$1000 then
  162. oflags:=oflags or (O_CREAT or O_TRUNC)
  163. else
  164. if (flags and $100)=$100 then
  165. oflags:=oflags or (O_APPEND);
  166. { empty name is special }
  167. if p[0]=#0 then
  168. begin
  169. case FileRec(f).mode of
  170. fminput :
  171. FileRec(f).Handle:=StdInputHandle;
  172. fminout, { this is set by rewrite }
  173. fmoutput :
  174. FileRec(f).Handle:=StdOutputHandle;
  175. fmappend :
  176. begin
  177. FileRec(f).Handle:=StdOutputHandle;
  178. FileRec(f).mode:=fmoutput; {fool fmappend}
  179. end;
  180. end;
  181. exit;
  182. end;
  183. { real open call }
  184. FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
  185. if (FileRec(f).Handle<0) and
  186. (getErrNo=ESysEROFS) and
  187. ((OFlags and O_RDWR)<>0) then
  188. begin
  189. Oflags:=Oflags and not(O_RDWR);
  190. FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
  191. end;
  192. If Filerec(f).Handle<0 Then
  193. Errno2Inoutres
  194. else
  195. InOutRes:=0;
  196. End;
  197. {
  198. $Log$
  199. Revision 1.1 2005-02-06 13:06:20 peter
  200. * moved file and dir functions to sysfile/sysdir
  201. * win32 thread in systemunit
  202. }