2
$Id: sysfile.inc,v 1.4 2005/04/28 18:21:04 olle Exp $
3
This file is part of the Free Pascal run time library.
4
Copyright (c) 2001-2005 by Free Pascal development team
6
Low level file functions
8
See the file COPYING.FPC, included in this distribution,
9
for details about the copyright.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
**********************************************************************}
17
{*****************************************************************************
18
Low Level File Routines
19
****************************************************************************}
21
function do_isdevice(handle:longint):boolean;
26
{ close a file from the handle value }
27
procedure do_close(h : longint);
30
{Ignore error handling, according to the other targets, which seems reasonable,
31
because close might be used to clean up after an error.}
33
{$ifdef MACOS_USE_STDCLIB}
38
// OSErr2InOutRes(err);
42
procedure do_erase(p : pchar);
50
res:= PathArgToFSSpec(p, spec);
53
if not IsDirectory(spec) then
55
err:= FSpDelete(spec);
65
procedure do_rename(p1,p2 : pchar);
69
{$ifdef MACOS_USE_STDCLIB}
70
InOutRes:= PathArgToFullPath(p1, s1);
73
InOutRes:= PathArgToFullPath(p2, s2);
76
c_rename(PChar(s1),PChar(s2));
83
function do_write(h:longint;addr:pointer;len : longint) : longint;
85
{$ifdef MACOS_USE_STDCLIB}
86
do_write:= c_write(h, addr, len);
90
if FSWrite(h, len, Mac_Ptr(addr)) = noErr then
96
function do_read(h:longint;addr:pointer;len : longint) : longint;
102
{$ifdef MACOS_USE_STDCLIB}
103
len:= c_read(h, addr, len);
110
if FSread(h, len, Mac_Ptr(addr)) = noErr then
116
function do_filepos(handle : longint) : longint;
122
{$ifdef MACOS_USE_STDCLIB}
123
{This returns the filepos without moving it.}
124
do_filepos := lseek(handle, 0, SEEK_CUR);
128
if GetFPos(handle, pos) = noErr then
134
procedure do_seek(handle,pos : longint);
136
{$ifdef MACOS_USE_STDCLIB}
137
lseek(handle, pos, SEEK_SET);
141
if SetFPos(handle, fsFromStart, pos) = noErr then
146
function do_seekend(handle:longint):longint;
148
{$ifdef MACOS_USE_STDCLIB}
149
do_seekend:= lseek(handle, 0, SEEK_END);
153
if SetFPos(handle, fsFromLEOF, 0) = noErr then
155
{TODO Resulting file position is to be returned.}
159
function do_filesize(handle : longint) : longint;
165
{$ifdef MACOS_USE_STDCLIB}
166
aktfilepos:= lseek(handle, 0, SEEK_CUR);
169
do_filesize := lseek(handle, 0, SEEK_END);
170
Errno2InOutRes; {Report the error from this operation.}
171
lseek(handle, aktfilepos, SEEK_SET); {Always try to move back,
172
even in presence of error.}
178
if GetEOF(handle, pos) = noErr then
184
{ truncate at a given position }
185
procedure do_truncate (handle,pos:longint);
187
{$ifdef MACOS_USE_STDCLIB}
188
ioctl(handle, FIOSETEOF, pointer(pos));
192
do_seek(handle,pos); //TODO: Is this needed (Does the user anticipate the filemarker is at the end?)
193
if SetEOF(handle, pos) = noErr then
198
procedure do_open(var f;p:pchar;flags:longint);
200
filerec and textrec have both handle and mode as the first items so
201
they could use the same routine for opening/creating.
202
when (flags and $100) the file will be append
203
when (flags and $1000) the file will be truncate/rewritten
204
when (flags and $10000) there is no check for close (needed for textfiles)
208
scriptTag: ScriptCode;
218
fullPath: AnsiString;
224
{ close first if opened }
225
if ((flags and $10000)=0) then
227
case filerec(f).mode of
228
fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
239
{ reset file handle }
240
filerec(f).handle:=UnusedHandle;
242
{$ifdef MACOS_USE_STDCLIB}
244
{ We do the conversion of filemodes here, concentrated on 1 place }
245
case (flags and 3) of
248
filerec(f).mode:=fminput;
252
filerec(f).mode:=fmoutput;
256
filerec(f).mode:=fminout;
260
if (flags and $1000)=$1000 then
261
oflags:=oflags or (O_CREAT or O_TRUNC)
262
else if (flags and $100)=$100 then
263
oflags:=oflags or (O_APPEND);
265
{ empty name is special }
268
case FileRec(f).mode of
270
FileRec(f).Handle:=StdInputHandle;
271
fminout, { this is set by rewrite }
273
FileRec(f).Handle:=StdOutputHandle;
276
FileRec(f).Handle:=StdOutputHandle;
277
FileRec(f).mode:=fmoutput; {fool fmappend}
284
InOutRes:= PathArgToFSSpec(p, spec);
285
if (InOutRes = 0) or (InOutRes = 2) then
287
err:= FSpGetFullPath(spec, fullPath, false);
288
InOutRes:= MacOSErr2RTEerr(err);
290
if InOutRes <> 0 then
297
fh:= c_open(p, oflags);
298
if (fh = -1) and (errno = Sys_EROFS) and ((oflags and O_RDWR)<>0) then
300
oflags:=oflags and not(O_RDWR);
301
fh:= c_open(p, oflags);
306
if FileRec(f).mode in [fmoutput, fminout, fmappend] then
308
{Change of filetype and creator is always done when a file is opened
309
for some kind of writing. This ensures overwritten Darwin files will
310
get apropriate filetype. It must be done after file is opened,
311
in the case the file did not previously exist.}
313
FSpGetFInfo(spec, finderInfo);
314
finderInfo.fdType:= defaultFileType;
315
finderInfo.fdCreator:= defaultCreator;
316
FSpSetFInfo(spec, finderInfo);
318
filerec(f).handle:= fh;
321
filerec(f).handle:= UnusedHandle;
327
{ reset file handle }
328
filerec(f).handle:=UnusedHandle;
330
res:= FSpLocationFromFullPath(StrLen(p), p, spec);
331
if (res = noErr) or (res = fnfErr) then
333
if FSpCreate(spec, defaultCreator, defaultFileType, smSystemScript) = noErr then
336
if FSpOpenDF(spec, fsCurPerm, refNum) = noErr then
338
filerec(f).handle:= refNum;
343
if (filerec(f).handle=UnusedHandle) then
345
//errno:=GetLastError;
353
$Log: sysfile.inc,v $
354
Revision 1.4 2005/04/28 18:21:04 olle
355
* Set errno to zero after close
357
Revision 1.3 2005/03/20 19:37:31 olle
358
+ Added optional path translation mechanism
360
Revision 1.2 2005/02/14 17:13:30 peter
363
Revision 1.1 2005/02/07 21:30:12 peter
364
* system unit updated
366
Revision 1.1 2005/02/06 16:57:18 peter
367
* threads for go32v2,os,emx,netware
369
Revision 1.1 2005/02/06 13:06:20 peter
370
* moved file and dir functions to sysfile/sysdir
371
* win32 thread in systemunit