sysfile.inc 4.9 KB

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