2
$Id: sysfile.inc,v 1.2 2005/02/14 17:13:30 peter Exp $
3
2
This file is part of the Free Pascal run time library.
4
Copyright (c) 2001 by Free Pascal development team
3
Copyright (c) 2005 by Free Pascal development team
6
Low leve file functions
5
Low level file functions
8
7
See the file COPYING.FPC, included in this distribution,
9
8
for details about the copyright.
15
14
**********************************************************************}
16
{ Enable this for file handling debug }
17
{DEFINE MOSFPC_FILEDEBUG}
17
19
{*****************************************************************************
18
20
MorphOS File-handling Support Functions
19
21
*****************************************************************************}
23
25
{ manually on exit. }
24
26
PFileList = ^TFileList;
25
27
TFileList = record { no packed, must be correctly aligned }
26
handle : LongInt; { Handle to file }
27
next : PFileList; { Next file in list }
28
handle : LongInt; { Handle to file }
29
next : PFileList; { Next file in list }
30
buffered : boolean; { used buffered I/O? }
81
84
if not inList then begin
91
{$IFDEF MOSFPC_FILEDEBUG}
93
RawDoFmt('FPC_FILE_DEBUG: Error! Trying add filehandle a filehandle twice: $%lx !'+#10,@h,pointer(1),nil);
89
98
{ Function to be called to remove a file from the list }
90
procedure RemoveFromList(var l: PFileList; h: LongInt); alias: 'REMOVEFROMLIST'; [public];
99
function RemoveFromList(var l: PFileList; h: LongInt): boolean; alias: 'REMOVEFROMLIST'; [public];
107
RemoveFromList:=inList;
99
112
while (p^.next<>nil) and (not inList) do
100
113
if p^.next^.handle=h then inList:=True
103
if p^.next<>nil then begin
117
tmpList:=p^.next^.next;
104
118
dispose(p^.next);
105
p^.next:=p^.next^.next;
121
{$IFDEF MOSFPC_FILEDEBUG}
123
RawDoFmt('FPC_FILE_DEBUG: Error! Trying to remove not existing filehandle: $%lx !'+#10,@h,pointer(1),nil);
127
RemoveFromList:=inList;
130
{ Function to check if file is in the list }
131
function CheckInList(var l: PFileList; h: LongInt): pointer; alias: 'CHECKINLIST'; [public];
145
while (p^.next<>nil) and (inList=nil) do
146
if p^.next^.handle=h then inList:=p^.next
149
{$IFDEF MOSFPC_FILEDEBUG}
151
RawDoFmt('FPC_FILE_DEBUG: Warning! Check for not existing filehandle: $%lx !'+#10,@h,pointer(1),nil);
115
163
{ close a file from the handle value }
116
164
procedure do_close(handle : longint);
118
if (handle<=0) then exit;
120
RemoveFromList(MOS_fileList,handle);
121
{ Do _NOT_ check CTRL_C on Close, because it will conflict
123
if not dosClose(handle) then
124
dosError2InOut(IoErr);
166
if RemoveFromList(MOS_fileList,handle) then begin
167
{ Do _NOT_ check CTRL_C on Close, because it will conflict
169
if not dosClose(handle) then
170
dosError2InOut(IoErr);
127
174
procedure do_erase(p : pchar);
176
tmpStr: array[0..255] of Char;
178
tmpStr:=PathConv(strpas(p))+#0;
130
if not dosDeleteFile(p) then
180
if not dosDeleteFile(@tmpStr) then
131
181
dosError2InOut(IoErr);
134
184
procedure do_rename(p1,p2 : pchar);
185
{ quite stack-effective code, huh? :) damn path conversions... (KB) }
187
tmpStr1: array[0..255] of Char;
188
tmpStr2: array[0..255] of Char;
190
tmpStr1:=PathConv(strpas(p1))+#0;
191
tmpStr2:=PathConv(strpas(p2))+#0;
137
if not dosRename(p1,p2) then
193
if not dosRename(@tmpStr1,@tmpStr2) then
138
194
dosError2InOut(IoErr);
141
function do_write(h:longint; addr: pointer; len: longint) : longint;
197
function do_write(h: longint; addr: pointer; len: longint) : longint;
142
198
var dosResult: LongInt;
146
202
if (len<=0) or (h<=0) then exit;
204
{$IFDEF MOSFPC_FILEDEBUG}
205
if not ((h=StdOutputHandle) or (h=StdInputHandle) or
206
(h=StdErrorHandle)) then CheckInList(MOS_fileList,h);
148
209
dosResult:=dosWrite(h,addr,len);
149
210
if dosResult<0 then begin
150
211
dosError2InOut(IoErr);
156
function do_read(h:longint; addr: pointer; len: longint) : longint;
217
function do_read(h: longint; addr: pointer; len: longint) : longint;
157
218
var dosResult: LongInt;
161
222
if (len<=0) or (h<=0) then exit;
224
{$IFDEF MOSFPC_FILEDEBUG}
225
if not ((h=StdOutputHandle) or (h=StdInputHandle) or
226
(h=StdErrorHandle)) then CheckInList(MOS_fileList,h);
163
229
dosResult:=dosRead(h,addr,len);
164
230
if dosResult<0 then begin
165
231
dosError2InOut(IoErr);
171
function do_filepos(handle : longint) : longint;
237
function do_filepos(handle: longint) : longint;
172
238
var dosResult: LongInt;
176
if (handle<=0) then exit;
178
{ Seeking zero from OFFSET_CURRENT to find out where we are }
179
dosResult:=dosSeek(handle,0,OFFSET_CURRENT);
180
if dosResult<0 then begin
181
dosError2InOut(IoErr);
183
do_filepos:=dosResult;
242
if CheckInList(MOS_fileList,handle)<>nil then begin
244
{ Seeking zero from OFFSET_CURRENT to find out where we are }
245
dosResult:=dosSeek(handle,0,OFFSET_CURRENT);
246
if dosResult<0 then begin
247
dosError2InOut(IoErr);
249
do_filepos:=dosResult;
187
procedure do_seek(handle,pos : longint);
255
procedure do_seek(handle, pos: longint);
190
if (handle<=0) then exit;
192
{ Seeking from OFFSET_BEGINNING }
193
if dosSeek(handle,pos,OFFSET_BEGINNING)<0 then
194
dosError2InOut(IoErr);
258
if CheckInList(MOS_fileList,handle)<>nil then begin
260
{ Seeking from OFFSET_BEGINNING }
261
if dosSeek(handle,pos,OFFSET_BEGINNING)<0 then
262
dosError2InOut(IoErr);
197
function do_seekend(handle:longint):longint;
267
function do_seekend(handle: longint):longint;
198
268
var dosResult: LongInt;
202
if (handle<=0) then exit;
204
{ Seeking to OFFSET_END }
205
dosResult:=dosSeek(handle,0,OFFSET_END);
206
if dosResult<0 then begin
207
dosError2InOut(IoErr);
209
do_seekend:=dosResult;
272
if CheckInList(MOS_fileList,handle)<>nil then begin
274
{ Seeking to OFFSET_END }
275
dosResult:=dosSeek(handle,0,OFFSET_END);
276
if dosResult<0 then begin
277
dosError2InOut(IoErr);
279
do_seekend:=dosResult;
213
285
function do_filesize(handle : longint) : longint;
218
if (handle<=0) then exit;
220
currfilepos:=do_filepos(handle);
221
{ We have to do this twice, because seek returns the OLD position }
222
do_filesize:=do_seekend(handle);
223
do_filesize:=do_seekend(handle);
224
do_seek(handle,currfilepos)
290
if CheckInList(MOS_fileList,handle)<>nil then begin
292
currfilepos:=do_filepos(handle);
293
{ We have to do this twice, because seek returns the OLD position }
294
do_filesize:=do_seekend(handle);
295
do_filesize:=do_seekend(handle);
296
do_seek(handle,currfilepos);
227
301
{ truncate at a given position }
228
procedure do_truncate (handle,pos:longint);
302
procedure do_truncate(handle, pos: longint);
231
if (handle<=0) then exit;
233
{ Seeking from OFFSET_BEGINNING }
234
if SetFileSize(handle,pos,OFFSET_BEGINNING)<0 then
235
dosError2InOut(IoErr);
305
if CheckInList(MOS_fileList,handle)<>nil then begin
307
{ Seeking from OFFSET_BEGINNING }
308
if SetFileSize(handle,pos,OFFSET_BEGINNING)<0 then
309
dosError2InOut(IoErr);
238
314
procedure do_open(var f;p:pchar;flags:longint);
317
393
do_isdevice:=False;
322
$Log: sysfile.inc,v $
323
Revision 1.2 2005/02/14 17:13:30 peter
326
Revision 1.1 2005/02/07 21:30:12 peter
327
* system unit updated
329
Revision 1.1 2005/02/06 16:57:18 peter
330
* threads for go32v2,os,emx,netware
332
Revision 1.1 2005/02/06 13:06:20 peter
333
* moved file and dir functions to sysfile/sysdir
334
* win32 thread in systemunit