mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-26 16:13:48 +02:00
337 lines
8.2 KiB
PHP
337 lines
8.2 KiB
PHP
{
|
|
This file is part of the Free Pascal run time library.
|
|
Copyright (c) 2001 by Free Pascal development team
|
|
|
|
Low leve file functions
|
|
|
|
See the file COPYING.FPC, included in this distribution,
|
|
for details about the copyright.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
**********************************************************************}
|
|
|
|
{*****************************************************************************
|
|
MorphOS File-handling Support Functions
|
|
*****************************************************************************}
|
|
type
|
|
{ AmigaOS does not automatically close opened files on exit back to }
|
|
{ the operating system, therefore as a precuation we close all files }
|
|
{ manually on exit. }
|
|
PFileList = ^TFileList;
|
|
TFileList = record { no packed, must be correctly aligned }
|
|
handle : LongInt; { Handle to file }
|
|
next : PFileList; { Next file in list }
|
|
end;
|
|
|
|
var
|
|
MOS_fileList: PFileList; public name 'MOS_FILELIST'; { List pointer to opened files }
|
|
|
|
{ Function to be called at program shutdown, to close all opened files }
|
|
procedure CloseList(l: PFileList);
|
|
var
|
|
tmpNext : PFileList;
|
|
tmpHandle : LongInt;
|
|
begin
|
|
if l=nil then exit;
|
|
|
|
{ First, close all tracked files }
|
|
tmpNext:=l^.next;
|
|
while tmpNext<>nil do begin
|
|
tmpHandle:=tmpNext^.handle;
|
|
if (tmpHandle<>StdInputHandle) and (tmpHandle<>StdOutputHandle)
|
|
and (tmpHandle<>StdErrorHandle) then begin
|
|
dosClose(tmpHandle);
|
|
end;
|
|
tmpNext:=tmpNext^.next;
|
|
end;
|
|
|
|
{ Next, erase the linked list }
|
|
while l<>nil do begin
|
|
tmpNext:=l;
|
|
l:=l^.next;
|
|
dispose(tmpNext);
|
|
end;
|
|
end;
|
|
|
|
{ Function to be called to add a file to the opened file list }
|
|
procedure AddToList(var l: PFileList; h: LongInt); alias: 'ADDTOLIST'; [public];
|
|
var
|
|
p : PFileList;
|
|
inList: Boolean;
|
|
begin
|
|
inList:=False;
|
|
if l<>nil then begin
|
|
{ if there is a valid filelist, search for the value }
|
|
{ in the list to avoid double additions }
|
|
p:=l;
|
|
while (p^.next<>nil) and (not inList) do
|
|
if p^.next^.handle=h then inList:=True
|
|
else p:=p^.next;
|
|
p:=nil;
|
|
end else begin
|
|
{ if the list is not yet allocated, allocate it. }
|
|
New(l);
|
|
l^.next:=nil;
|
|
end;
|
|
|
|
if not inList then begin
|
|
New(p);
|
|
p^.handle:=h;
|
|
p^.next:=l^.next;
|
|
l^.next:=p;
|
|
end;
|
|
end;
|
|
|
|
{ Function to be called to remove a file from the list }
|
|
procedure RemoveFromList(var l: PFileList; h: LongInt); alias: 'REMOVEFROMLIST'; [public];
|
|
var
|
|
p : PFileList;
|
|
inList: Boolean;
|
|
begin
|
|
if l=nil then exit;
|
|
|
|
inList:=False;
|
|
p:=l;
|
|
while (p^.next<>nil) and (not inList) do
|
|
if p^.next^.handle=h then inList:=True
|
|
else p:=p^.next;
|
|
|
|
if p^.next<>nil then begin
|
|
dispose(p^.next);
|
|
p^.next:=p^.next^.next;
|
|
end;
|
|
end;
|
|
|
|
|
|
{****************************************************************************
|
|
Low level File Routines
|
|
All these functions can set InOutRes on errors
|
|
****************************************************************************}
|
|
|
|
{ close a file from the handle value }
|
|
procedure do_close(handle : longint);
|
|
begin
|
|
if (handle<=0) then exit;
|
|
|
|
RemoveFromList(MOS_fileList,handle);
|
|
{ Do _NOT_ check CTRL_C on Close, because it will conflict
|
|
with System_Exit! }
|
|
if not dosClose(handle) then
|
|
dosError2InOut(IoErr);
|
|
end;
|
|
|
|
procedure do_erase(p : pchar);
|
|
begin
|
|
checkCTRLC;
|
|
if not dosDeleteFile(p) then
|
|
dosError2InOut(IoErr);
|
|
end;
|
|
|
|
procedure do_rename(p1,p2 : pchar);
|
|
begin
|
|
checkCTRLC;
|
|
if not dosRename(p1,p2) then
|
|
dosError2InOut(IoErr);
|
|
end;
|
|
|
|
function do_write(h:longint; addr: pointer; len: longint) : longint;
|
|
var dosResult: LongInt;
|
|
begin
|
|
checkCTRLC;
|
|
do_write:=0;
|
|
if (len<=0) or (h<=0) then exit;
|
|
|
|
dosResult:=dosWrite(h,addr,len);
|
|
if dosResult<0 then begin
|
|
dosError2InOut(IoErr);
|
|
end else begin
|
|
do_write:=dosResult;
|
|
end;
|
|
end;
|
|
|
|
function do_read(h:longint; addr: pointer; len: longint) : longint;
|
|
var dosResult: LongInt;
|
|
begin
|
|
checkCTRLC;
|
|
do_read:=0;
|
|
if (len<=0) or (h<=0) then exit;
|
|
|
|
dosResult:=dosRead(h,addr,len);
|
|
if dosResult<0 then begin
|
|
dosError2InOut(IoErr);
|
|
end else begin
|
|
do_read:=dosResult;
|
|
end
|
|
end;
|
|
|
|
function do_filepos(handle : longint) : longint;
|
|
var dosResult: LongInt;
|
|
begin
|
|
checkCTRLC;
|
|
do_filepos:=-1;
|
|
if (handle<=0) then exit;
|
|
|
|
{ Seeking zero from OFFSET_CURRENT to find out where we are }
|
|
dosResult:=dosSeek(handle,0,OFFSET_CURRENT);
|
|
if dosResult<0 then begin
|
|
dosError2InOut(IoErr);
|
|
end else begin
|
|
do_filepos:=dosResult;
|
|
end;
|
|
end;
|
|
|
|
procedure do_seek(handle,pos : longint);
|
|
begin
|
|
checkCTRLC;
|
|
if (handle<=0) then exit;
|
|
|
|
{ Seeking from OFFSET_BEGINNING }
|
|
if dosSeek(handle,pos,OFFSET_BEGINNING)<0 then
|
|
dosError2InOut(IoErr);
|
|
end;
|
|
|
|
function do_seekend(handle:longint):longint;
|
|
var dosResult: LongInt;
|
|
begin
|
|
checkCTRLC;
|
|
do_seekend:=-1;
|
|
if (handle<=0) then exit;
|
|
|
|
{ Seeking to OFFSET_END }
|
|
dosResult:=dosSeek(handle,0,OFFSET_END);
|
|
if dosResult<0 then begin
|
|
dosError2InOut(IoErr);
|
|
end else begin
|
|
do_seekend:=dosResult;
|
|
end
|
|
end;
|
|
|
|
function do_filesize(handle : longint) : longint;
|
|
var currfilepos: longint;
|
|
begin
|
|
checkCTRLC;
|
|
do_filesize:=-1;
|
|
if (handle<=0) then exit;
|
|
|
|
currfilepos:=do_filepos(handle);
|
|
{ We have to do this twice, because seek returns the OLD position }
|
|
do_filesize:=do_seekend(handle);
|
|
do_filesize:=do_seekend(handle);
|
|
do_seek(handle,currfilepos)
|
|
end;
|
|
|
|
{ truncate at a given position }
|
|
procedure do_truncate (handle,pos:longint);
|
|
begin
|
|
checkCTRLC;
|
|
if (handle<=0) then exit;
|
|
|
|
{ Seeking from OFFSET_BEGINNING }
|
|
if SetFileSize(handle,pos,OFFSET_BEGINNING)<0 then
|
|
dosError2InOut(IoErr);
|
|
end;
|
|
|
|
procedure do_open(var f;p:pchar;flags:longint);
|
|
{
|
|
filerec and textrec have both handle and mode as the first items so
|
|
they could use the same routine for opening/creating.
|
|
when (flags and $10) the file will be append
|
|
when (flags and $100) the file will be truncate/rewritten
|
|
when (flags and $1000) there is no check for close (needed for textfiles)
|
|
}
|
|
var
|
|
handle : LongInt;
|
|
openflags: LongInt;
|
|
tmpStr : array[0..255] of Char;
|
|
begin
|
|
tmpStr:=PathConv(strpas(p))+#0;
|
|
|
|
{ close first if opened }
|
|
if ((flags and $10000)=0) then begin
|
|
case filerec(f).mode of
|
|
fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
|
|
fmclosed : ;
|
|
else begin
|
|
inoutres:=102; {not assigned}
|
|
exit;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
{ reset file handle }
|
|
filerec(f).handle:=UnusedHandle;
|
|
|
|
{ convert filemode to filerec modes }
|
|
{ READ/WRITE on existing file }
|
|
{ RESET/APPEND }
|
|
openflags:=MODE_OLDFILE;
|
|
case (flags and 3) of
|
|
0 : filerec(f).mode:=fminput;
|
|
1 : filerec(f).mode:=fmoutput;
|
|
2 : filerec(f).mode:=fminout;
|
|
end;
|
|
|
|
{ rewrite (create a new file) }
|
|
if (flags and $1000)<>0 then openflags:=MODE_NEWFILE;
|
|
|
|
{ empty name is special }
|
|
if p[0]=#0 then begin
|
|
case filerec(f).mode of
|
|
fminput :
|
|
filerec(f).handle:=StdInputHandle;
|
|
fmappend,
|
|
fmoutput : begin
|
|
filerec(f).handle:=StdOutputHandle;
|
|
filerec(f).mode:=fmoutput; {fool fmappend}
|
|
end;
|
|
end;
|
|
exit;
|
|
end;
|
|
|
|
handle:=Open(@tmpStr,openflags);
|
|
if handle=0 then begin
|
|
dosError2InOut(IoErr);
|
|
end else begin
|
|
AddToList(MOS_fileList,handle);
|
|
filerec(f).handle:=handle;
|
|
end;
|
|
|
|
{ append mode }
|
|
if ((Flags and $100)<>0) and
|
|
(FileRec(F).Handle<>UnusedHandle) then begin
|
|
do_seekend(filerec(f).handle);
|
|
filerec(f).mode:=fmoutput; {fool fmappend}
|
|
end;
|
|
end;
|
|
|
|
function do_isdevice(handle:longint):boolean;
|
|
begin
|
|
if (handle=StdOutputHandle) or (handle=StdInputHandle) or
|
|
(handle=StdErrorHandle) then
|
|
do_isdevice:=True
|
|
else
|
|
do_isdevice:=False;
|
|
end;
|
|
|
|
|
|
{
|
|
$Log: sysfile.inc,v $
|
|
Revision 1.2 2005/02/14 17:13:30 peter
|
|
* truncate log
|
|
|
|
Revision 1.1 2005/02/07 21:30:12 peter
|
|
* system unit updated
|
|
|
|
Revision 1.1 2005/02/06 16:57:18 peter
|
|
* threads for go32v2,os,emx,netware
|
|
|
|
Revision 1.1 2005/02/06 13:06:20 peter
|
|
* moved file and dir functions to sysfile/sysdir
|
|
* win32 thread in systemunit
|
|
|
|
}
|
|
|