sysfile.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. // AllowSlash(p);
  191. { close first if opened }
  192. if ((flags and $10000)=0) then
  193. begin
  194. case filerec(f).mode of
  195. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  196. fmclosed : ;
  197. else
  198. begin
  199. {not assigned}
  200. inoutres:=102;
  201. exit;
  202. end;
  203. end;
  204. end;
  205. { reset file handle }
  206. filerec(f).handle:=UnusedHandle;
  207. {$ifdef MACOS_USE_STDCLIB}
  208. { We do the conversion of filemodes here, concentrated on 1 place }
  209. case (flags and 3) of
  210. 0 : begin
  211. oflags :=O_RDONLY;
  212. filerec(f).mode:=fminput;
  213. end;
  214. 1 : begin
  215. oflags :=O_WRONLY;
  216. filerec(f).mode:=fmoutput;
  217. end;
  218. 2 : begin
  219. oflags :=O_RDWR;
  220. filerec(f).mode:=fminout;
  221. end;
  222. end;
  223. if (flags and $1000)=$1000 then
  224. oflags:=oflags or (O_CREAT or O_TRUNC)
  225. else if (flags and $100)=$100 then
  226. oflags:=oflags or (O_APPEND);
  227. { empty name is special }
  228. if p[0]=#0 then
  229. begin
  230. case FileRec(f).mode of
  231. fminput :
  232. FileRec(f).Handle:=StdInputHandle;
  233. fminout, { this is set by rewrite }
  234. fmoutput :
  235. FileRec(f).Handle:=StdOutputHandle;
  236. fmappend :
  237. begin
  238. FileRec(f).Handle:=StdOutputHandle;
  239. FileRec(f).mode:=fmoutput; {fool fmappend}
  240. end;
  241. end;
  242. exit;
  243. end
  244. else
  245. begin
  246. InOutRes:= PathArgToFSSpec(p, spec);
  247. if (InOutRes = 0) or (InOutRes = 2) then
  248. begin
  249. err:= FSpGetFullPath(spec, fullPath, false);
  250. InOutRes:= MacOSErr2RTEerr(err);
  251. end;
  252. if InOutRes <> 0 then
  253. exit;
  254. p:= PChar(fullPath);
  255. end;
  256. fh:= c_open(p, oflags);
  257. if (fh = -1) and (errno = Sys_EROFS) and ((oflags and O_RDWR)<>0) then
  258. begin
  259. oflags:=oflags and not(O_RDWR);
  260. fh:= c_open(p, oflags);
  261. end;
  262. Errno2InOutRes;
  263. if fh <> -1 then
  264. begin
  265. if FileRec(f).mode in [fmoutput, fminout, fmappend] then
  266. begin
  267. {Change of filetype and creator is always done when a file is opened
  268. for some kind of writing. This ensures overwritten Darwin files will
  269. get apropriate filetype. It must be done after file is opened,
  270. in the case the file did not previously exist.}
  271. FSpGetFInfo(spec, finderInfo);
  272. finderInfo.fdType:= defaultFileType;
  273. finderInfo.fdCreator:= defaultCreator;
  274. FSpSetFInfo(spec, finderInfo);
  275. end;
  276. filerec(f).handle:= fh;
  277. end
  278. else
  279. filerec(f).handle:= UnusedHandle;
  280. {$else}
  281. InOutRes:=1;
  282. { reset file handle }
  283. filerec(f).handle:=UnusedHandle;
  284. res:= FSpLocationFromFullPath(StrLen(p), p, spec);
  285. if (res = noErr) or (res = fnfErr) then
  286. begin
  287. if FSpCreate(spec, defaultCreator, defaultFileType, smSystemScript) = noErr then
  288. ;
  289. if FSpOpenDF(spec, fsCurPerm, refNum) = noErr then
  290. begin
  291. filerec(f).handle:= refNum;
  292. InOutRes:=0;
  293. end;
  294. end;
  295. if (filerec(f).handle=UnusedHandle) then
  296. begin
  297. //errno:=GetLastError;
  298. //Errno2InoutRes;
  299. end;
  300. {$endif}
  301. end;
  302. {
  303. $Log$
  304. Revision 1.1 2005-02-07 21:30:12 peter
  305. * system unit updated
  306. Revision 1.1 2005/02/06 16:57:18 peter
  307. * threads for go32v2,os,emx,netware
  308. Revision 1.1 2005/02/06 13:06:20 peter
  309. * moved file and dir functions to sysfile/sysdir
  310. * win32 thread in systemunit
  311. }