1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2005 by Free Pascal development team
Low level 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.
**********************************************************************}
{ Enable this for file handling debug }
{DEFINE MOSFPC_FILEDEBUG}
{*****************************************************************************
AROS 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 : THandle; { Handle to file }
next : PFileList; { Next file in list }
buffered : boolean; { used buffered I/O? }
end;
var
AOS_fileList: PFileList; public name 'AOS_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 : THandle;
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: THandle); 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^.buffered:=False;
p^.next:=l^.next;
l^.next:=p;
end
{$IFDEF MOSFPC_FILEDEBUG}
else
RawDoFmt('FPC_FILE_DEBUG: Error! Trying add filehandle a filehandle twice: $%lx !'+#10,@h,pointer(1),nil);
{$ENDIF}
;
end;
{ Function to be called to remove a file from the list }
function RemoveFromList(var l: PFileList; h: THandle): boolean; alias: 'REMOVEFROMLIST'; [public];
var
p : PFileList;
inList : Boolean;
tmpList: PFileList;
begin
inList:=False;
if l=nil then begin
RemoveFromList:=inList;
exit;
end;
p:=l;
while (p^.next<>nil) and (not inList) do
if p^.next^.handle=h then inList:=True
else p:=p^.next;
if inList then begin
tmpList:=p^.next^.next;
dispose(p^.next);
p^.next:=tmpList;
end
{$IFDEF MOSFPC_FILEDEBUG}
else
RawDoFmt('FPC_FILE_DEBUG: Error! Trying to remove not existing filehandle: $%lx !'+#10,@h,pointer(1),nil);
{$ENDIF}
;
RemoveFromList:=inList;
end;
{ Function to check if file is in the list }
function CheckInList(var l: PFileList; h: THandle): pointer; alias: 'CHECKINLIST'; [public];
var
p : PFileList;
inList : Pointer;
begin
inList:=nil;
if l=nil then begin
CheckInList:=inList;
exit;
end;
p:=l;
while (p^.next<>nil) and (inList=nil) do
if p^.next^.handle=h then inList:=p^.next
else p:=p^.next;
{$IFDEF MOSFPC_FILEDEBUG}
if inList=nil then
RawDoFmt('FPC_FILE_DEBUG: Warning! Check for not existing filehandle: $%lx !'+#10,@h,pointer(1),nil);
{$ENDIF}
CheckInList:=inList;
end;
{****************************************************************************
Low level File Routines
All these functions can set InOutRes on errors
****************************************************************************}
{ close a file from the handle value }
procedure do_close(handle : THandle);
begin
if RemoveFromList(AOS_fileList,handle) then begin
{ Do _NOT_ check CTRL_C on Close, because it will conflict
with System_Exit! }
if not dosClose(handle) then
dosError2InOut(IoErr);
end;
end;
procedure do_erase(p : pchar);
var
tmpStr: array[0..255] of Char;
begin
tmpStr:=PathConv(strpas(p))+#0;
checkCTRLC;
if not dosDeleteFile(@tmpStr) then
dosError2InOut(IoErr);
end;
procedure do_rename(p1,p2 : pchar);
{ quite stack-effective code, huh? :) damn path conversions... (KB) }
var
tmpStr1: array[0..255] of Char;
tmpStr2: array[0..255] of Char;
begin
tmpStr1:=PathConv(strpas(p1))+#0;
tmpStr2:=PathConv(strpas(p2))+#0;
checkCTRLC;
if not dosRename(@tmpStr1,@tmpStr2) then
dosError2InOut(IoErr);
end;
function do_write(h: THandle; addr: pointer; len: longint) : longint;
var dosResult: LongInt;
begin
checkCTRLC;
do_write:=0;
if (len<=0) or (h=0) then exit;
{$IFDEF MOSFPC_FILEDEBUG}
if not ((h=StdOutputHandle) or (h=StdInputHandle) or
(h=StdErrorHandle)) then CheckInList(AOS_fileList,h);
{$ENDIF}
dosResult:=dosWrite(h,addr,len);
if dosResult<0 then begin
dosError2InOut(IoErr);
end else begin
do_write:=dosResult;
end;
end;
function do_read(h: THandle; addr: pointer; len: longint) : longint;
var dosResult: LongInt;
begin
checkCTRLC;
do_read:=0;
if (len<=0) or (h=0) then exit;
{$IFDEF MOSFPC_FILEDEBUG}
if not ((h=StdOutputHandle) or (h=StdInputHandle) or
(h=StdErrorHandle)) then CheckInList(AOS_fileList,h);
{$ENDIF}
dosResult:=dosRead(h,addr,len);
if dosResult<0 then begin
dosError2InOut(IoErr);
end else begin
do_read:=dosResult;
end
end;
function do_filepos(handle: THandle) : longint;
var dosResult: LongInt;
begin
checkCTRLC;
do_filepos:=-1;
if CheckInList(AOS_fileList,handle)<>nil then begin
{ 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;
end;
procedure do_seek(handle: Thandle; pos: longint);
begin
checkCTRLC;
if CheckInList(AOS_fileList,handle)<>nil then begin
{ Seeking from OFFSET_BEGINNING }
if dosSeek(handle,pos,OFFSET_BEGINNING)<0 then
dosError2InOut(IoErr);
end;
end;
function do_seekend(handle: longint):longint;
var dosResult: LongInt;
begin
checkCTRLC;
do_seekend:=-1;
if CheckInList(AOS_fileList,handle)<>nil then begin
{ 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;
end;
function do_filesize(handle : longint) : longint;
var currfilepos: longint;
begin
checkCTRLC;
do_filesize:=-1;
if CheckInList(AOS_fileList,handle)<>nil then begin
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);
Result := do_fileSize;
end;
end;
{ truncate at a given position }
procedure do_truncate(handle, pos: longint);
begin
checkCTRLC;
if CheckInList(AOS_fileList,handle)<>nil then begin
{ Seeking from OFFSET_BEGINNING }
if SetFileSize(handle,pos,OFFSET_BEGINNING)<0 then
dosError2InOut(IoErr);
end;
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 : THandle;
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(AOS_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;
|