sysfile.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. const
  132. { read/write permission for everyone }
  133. MODE_OPEN = S_IWUSR OR S_IRUSR OR
  134. S_IWGRP OR S_IRGRP OR
  135. S_IWOTH OR S_IROTH;
  136. var
  137. oflags : cint;
  138. Begin
  139. { close first if opened }
  140. if ((flags and $10000)=0) then
  141. begin
  142. case FileRec(f).mode of
  143. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  144. fmclosed : ;
  145. else
  146. begin
  147. inoutres:=102; {not assigned}
  148. exit;
  149. end;
  150. end;
  151. end;
  152. { reset file Handle }
  153. FileRec(f).Handle:=UnusedHandle;
  154. { We do the conversion of filemodes here, concentrated on 1 place }
  155. case (flags and 3) of
  156. 0 : begin
  157. oflags :=O_RDONLY;
  158. FileRec(f).mode:=fminput;
  159. end;
  160. 1 : begin
  161. oflags :=O_WRONLY;
  162. FileRec(f).mode:=fmoutput;
  163. end;
  164. 2 : begin
  165. oflags :=O_RDWR;
  166. FileRec(f).mode:=fminout;
  167. end;
  168. end;
  169. if (flags and $1000)=$1000 then
  170. oflags:=oflags or (O_CREAT or O_TRUNC)
  171. else
  172. if (flags and $100)=$100 then
  173. oflags:=oflags or (O_APPEND);
  174. { empty name is special }
  175. if p[0]=#0 then
  176. begin
  177. case FileRec(f).mode of
  178. fminput :
  179. FileRec(f).Handle:=StdInputHandle;
  180. fminout, { this is set by rewrite }
  181. fmoutput :
  182. FileRec(f).Handle:=StdOutputHandle;
  183. fmappend :
  184. begin
  185. FileRec(f).Handle:=StdOutputHandle;
  186. FileRec(f).mode:=fmoutput; {fool fmappend}
  187. end;
  188. end;
  189. exit;
  190. end;
  191. { real open call }
  192. FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
  193. if (FileRec(f).Handle<0) and
  194. (getErrNo=ESysEROFS) and ((OFlags and O_RDWR)<>0) then
  195. begin
  196. Oflags:=Oflags and not(O_RDWR);
  197. FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
  198. end;
  199. If Filerec(f).Handle<0 Then
  200. Errno2Inoutres
  201. else
  202. InOutRes:=0;
  203. End;
  204. {
  205. $Log$
  206. Revision 1.1 2005-02-07 22:04:55 peter
  207. * moved to unix
  208. Revision 1.1 2005/02/06 13:06:20 peter
  209. * moved file and dir functions to sysfile/sysdir
  210. * win32 thread in systemunit
  211. }