sysfile.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2001 by Free Pascal development team
  5. Low leve file functions
  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. {*****************************************************************************
  13. Low Level File Routines
  14. ****************************************************************************}
  15. function do_isdevice(handle:longint):boolean;
  16. begin
  17. do_isdevice:=false;
  18. end;
  19. { close a file from the handle value }
  20. procedure do_close(h : longint);
  21. var
  22. err: OSErr;
  23. {No error handling, according to the other targets, which seems reasonable,
  24. because close might be used to clean up after an error.}
  25. begin
  26. {$ifdef MACOS_USE_STDCLIB}
  27. c_close(h);
  28. // Errno2InOutRes;
  29. {$else}
  30. err:= FSClose(h);
  31. // OSErr2InOutRes(err);
  32. {$endif}
  33. end;
  34. procedure do_erase(p : pchar);
  35. var
  36. spec: FSSpec;
  37. err: OSErr;
  38. res: Integer;
  39. begin
  40. res:= PathArgToFSSpec(p, spec);
  41. if (res = 0) then
  42. begin
  43. if not IsDirectory(spec) then
  44. begin
  45. err:= FSpDelete(spec);
  46. OSErr2InOutRes(err);
  47. end
  48. else
  49. InOutRes:= 2;
  50. end
  51. else
  52. InOutRes:=res;
  53. end;
  54. procedure do_rename(p1,p2 : pchar);
  55. var
  56. s1,s2: AnsiString;
  57. begin
  58. {$ifdef MACOS_USE_STDCLIB}
  59. InOutRes:= PathArgToFullPath(p1, s1);
  60. if InOutRes <> 0 then
  61. exit;
  62. InOutRes:= PathArgToFullPath(p2, s2);
  63. if InOutRes <> 0 then
  64. exit;
  65. c_rename(PChar(s1),PChar(s2));
  66. Errno2InoutRes;
  67. {$else}
  68. InOutRes:=1;
  69. {$endif}
  70. end;
  71. function do_write(h:longint;addr:pointer;len : longint) : longint;
  72. begin
  73. {$ifdef MACOS_USE_STDCLIB}
  74. do_write:= c_write(h, addr, len);
  75. Errno2InoutRes;
  76. {$else}
  77. InOutRes:=1;
  78. if FSWrite(h, len, Mac_Ptr(addr)) = noErr then
  79. InOutRes:=0;
  80. do_write:= len;
  81. {$endif}
  82. end;
  83. function do_read(h:longint;addr:pointer;len : longint) : longint;
  84. var
  85. i: Longint;
  86. begin
  87. {$ifdef MACOS_USE_STDCLIB}
  88. len:= c_read(h, addr, len);
  89. Errno2InoutRes;
  90. do_read:= len;
  91. {$else}
  92. InOutRes:=1;
  93. if FSread(h, len, Mac_Ptr(addr)) = noErr then
  94. InOutRes:=0;
  95. do_read:= len;
  96. {$endif}
  97. end;
  98. function do_filepos(handle : longint) : longint;
  99. var
  100. pos: Longint;
  101. begin
  102. {$ifdef MACOS_USE_STDCLIB}
  103. {This returns the filepos without moving it.}
  104. do_filepos := lseek(handle, 0, SEEK_CUR);
  105. Errno2InoutRes;
  106. {$else}
  107. InOutRes:=1;
  108. if GetFPos(handle, pos) = noErr then
  109. InOutRes:=0;
  110. do_filepos:= pos;
  111. {$endif}
  112. end;
  113. procedure do_seek(handle,pos : longint);
  114. begin
  115. {$ifdef MACOS_USE_STDCLIB}
  116. lseek(handle, pos, SEEK_SET);
  117. Errno2InoutRes;
  118. {$else}
  119. InOutRes:=1;
  120. if SetFPos(handle, fsFromStart, pos) = noErr then
  121. InOutRes:=0;
  122. {$endif}
  123. end;
  124. function do_seekend(handle:longint):longint;
  125. begin
  126. {$ifdef MACOS_USE_STDCLIB}
  127. do_seekend:= lseek(handle, 0, SEEK_END);
  128. Errno2InoutRes;
  129. {$else}
  130. InOutRes:=1;
  131. if SetFPos(handle, fsFromLEOF, 0) = noErr then
  132. InOutRes:=0;
  133. {TODO Resulting file position is to be returned.}
  134. {$endif}
  135. end;
  136. function do_filesize(handle : longint) : longint;
  137. var
  138. aktfilepos: Longint;
  139. begin
  140. {$ifdef MACOS_USE_STDCLIB}
  141. aktfilepos:= lseek(handle, 0, SEEK_CUR);
  142. if errno = 0 then
  143. begin
  144. do_filesize := lseek(handle, 0, SEEK_END);
  145. Errno2InOutRes; {Report the error from this operation.}
  146. lseek(handle, aktfilepos, SEEK_SET); {Always try to move back,
  147. even in presence of error.}
  148. end
  149. else
  150. Errno2InOutRes;
  151. {$else}
  152. InOutRes:=1;
  153. if GetEOF(handle, pos) = noErr then
  154. InOutRes:=0;
  155. do_filesize:= pos;
  156. {$endif}
  157. end;
  158. { truncate at a given position }
  159. procedure do_truncate (handle,pos:longint);
  160. begin
  161. {$ifdef MACOS_USE_STDCLIB}
  162. ioctl(handle, FIOSETEOF, pointer(pos));
  163. Errno2InoutRes;
  164. {$else}
  165. InOutRes:=1;
  166. do_seek(handle,pos); //TODO: Is this needed (Does the user anticipate the filemarker is at the end?)
  167. if SetEOF(handle, pos) = noErr then
  168. InOutRes:=0;
  169. {$endif}
  170. end;
  171. procedure do_open(var f;p:pchar;flags:longint);
  172. {
  173. filerec and textrec have both handle and mode as the first items so
  174. they could use the same routine for opening/creating.
  175. when (flags and $100) the file will be append
  176. when (flags and $1000) the file will be truncate/rewritten
  177. when (flags and $10000) there is no check for close (needed for textfiles)
  178. }
  179. var
  180. scriptTag: ScriptCode;
  181. refNum: Integer;
  182. err: OSErr;
  183. res: Integer;
  184. spec: FSSpec;
  185. fh: Longint;
  186. oflags : longint;
  187. fullPath: AnsiString;
  188. finderInfo: FInfo;
  189. begin
  190. { close first if opened }
  191. if ((flags and $10000)=0) then
  192. begin
  193. case filerec(f).mode of
  194. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  195. fmclosed : ;
  196. else
  197. begin
  198. {not assigned}
  199. inoutres:=102;
  200. exit;
  201. end;
  202. end;
  203. end;
  204. { reset file handle }
  205. filerec(f).handle:=UnusedHandle;
  206. {$ifdef MACOS_USE_STDCLIB}
  207. { We do the conversion of filemodes here, concentrated on 1 place }
  208. case (flags and 3) of
  209. 0 : begin
  210. oflags :=O_RDONLY;
  211. filerec(f).mode:=fminput;
  212. end;
  213. 1 : begin
  214. oflags :=O_WRONLY;
  215. filerec(f).mode:=fmoutput;
  216. end;
  217. 2 : begin
  218. oflags :=O_RDWR;
  219. filerec(f).mode:=fminout;
  220. end;
  221. end;
  222. if (flags and $1000)=$1000 then
  223. oflags:=oflags or (O_CREAT or O_TRUNC)
  224. else if (flags and $100)=$100 then
  225. oflags:=oflags or (O_APPEND);
  226. { empty name is special }
  227. if p[0]=#0 then
  228. begin
  229. case FileRec(f).mode of
  230. fminput :
  231. FileRec(f).Handle:=StdInputHandle;
  232. fminout, { this is set by rewrite }
  233. fmoutput :
  234. FileRec(f).Handle:=StdOutputHandle;
  235. fmappend :
  236. begin
  237. FileRec(f).Handle:=StdOutputHandle;
  238. FileRec(f).mode:=fmoutput; {fool fmappend}
  239. end;
  240. end;
  241. exit;
  242. end
  243. else
  244. begin
  245. InOutRes:= PathArgToFSSpec(p, spec);
  246. if (InOutRes = 0) or (InOutRes = 2) then
  247. begin
  248. err:= FSpGetFullPath(spec, fullPath, false);
  249. InOutRes:= MacOSErr2RTEerr(err);
  250. end;
  251. if InOutRes <> 0 then
  252. exit;
  253. p:= PChar(fullPath);
  254. end;
  255. fh:= c_open(p, oflags);
  256. if (fh = -1) and (errno = Sys_EROFS) and ((oflags and O_RDWR)<>0) then
  257. begin
  258. oflags:=oflags and not(O_RDWR);
  259. fh:= c_open(p, oflags);
  260. end;
  261. Errno2InOutRes;
  262. if fh <> -1 then
  263. begin
  264. if FileRec(f).mode in [fmoutput, fminout, fmappend] then
  265. begin
  266. {Change of filetype and creator is always done when a file is opened
  267. for some kind of writing. This ensures overwritten Darwin files will
  268. get apropriate filetype. It must be done after file is opened,
  269. in the case the file did not previously exist.}
  270. FSpGetFInfo(spec, finderInfo);
  271. finderInfo.fdType:= defaultFileType;
  272. finderInfo.fdCreator:= defaultCreator;
  273. FSpSetFInfo(spec, finderInfo);
  274. end;
  275. filerec(f).handle:= fh;
  276. end
  277. else
  278. filerec(f).handle:= UnusedHandle;
  279. {$else}
  280. InOutRes:=1;
  281. { reset file handle }
  282. filerec(f).handle:=UnusedHandle;
  283. res:= FSpLocationFromFullPath(StrLen(p), p, spec);
  284. if (res = noErr) or (res = fnfErr) then
  285. begin
  286. if FSpCreate(spec, defaultCreator, defaultFileType, smSystemScript) = noErr then
  287. ;
  288. if FSpOpenDF(spec, fsCurPerm, refNum) = noErr then
  289. begin
  290. filerec(f).handle:= refNum;
  291. InOutRes:=0;
  292. end;
  293. end;
  294. if (filerec(f).handle=UnusedHandle) then
  295. begin
  296. //errno:=GetLastError;
  297. //Errno2InoutRes;
  298. end;
  299. {$endif}
  300. end;
  301. {
  302. $Log$
  303. Revision 1.3 2005-03-20 19:37:31 olle
  304. + Added optional path translation mechanism
  305. Revision 1.2 2005/02/14 17:13:30 peter
  306. * truncate log
  307. Revision 1.1 2005/02/07 21:30:12 peter
  308. * system unit updated
  309. Revision 1.1 2005/02/06 16:57:18 peter
  310. * threads for go32v2,os,emx,netware
  311. Revision 1.1 2005/02/06 13:06:20 peter
  312. * moved file and dir functions to sysfile/sysdir
  313. * win32 thread in systemunit
  314. }