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
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2001-2005 by Free Pascal development team
Low level file functions for MacOS
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.
**********************************************************************}
{*****************************************************************************
Low Level File Routines
****************************************************************************}
function do_isdevice(handle:longint):boolean;
begin
do_isdevice:= (handle=StdInputHandle) or
(handle=StdOutputHandle) or
(handle=StdErrorHandle);
end;
{ close a file from the handle value }
procedure do_close(h : longint);
var
err: OSErr;
{Ignore error handling, according to the other targets, which seems reasonable,
because close might be used to clean up after an error.}
begin
{$ifdef MACOS_USE_STDCLIB}
c_close(h);
errno:= 0;
{$else}
err:= FSClose(h);
// OSErr2InOutRes(err);
{$endif}
end;
procedure do_erase(p : pchar; pchangeable: boolean);
var
spec: FSSpec;
err: OSErr;
res: Integer;
begin
res:= PathArgToFSSpec(p, spec);
if (res = 0) then
begin
if not IsDirectory(spec) then
begin
err:= FSpDelete(spec);
OSErr2InOutRes(err);
end
else
InOutRes:= 2;
end
else
InOutRes:=res;
end;
procedure do_rename(p1,p2 : pchar; p1changeable, p2changeable: boolean);
var
s1,s2: RawByteString;
begin
s1:=''; { to fix warnings }
s2:='';
{$ifdef MACOS_USE_STDCLIB}
InOutRes:= PathArgToFullPath(p1, s1);
if InOutRes <> 0 then
exit;
InOutRes:= PathArgToFullPath(p2, s2);
if InOutRes <> 0 then
exit;
c_rename(PChar(s1),PChar(s2));
Errno2InoutRes;
{$else}
InOutRes:=1;
{$endif}
end;
function do_write(h:longint;addr:pointer;len : longint) : longint;
begin
{$ifdef MACOS_USE_STDCLIB}
do_write:= c_write(h, addr, len);
Errno2InoutRes;
{$else}
InOutRes:=1;
if FSWrite(h, len, Mac_Ptr(addr)) = noErr then
InOutRes:=0;
do_write:= len;
{$endif}
end;
function do_read(h:longint;addr:pointer;len : longint) : longint;
var
i: Longint;
begin
{$ifdef MACOS_USE_STDCLIB}
len:= c_read(h, addr, len);
Errno2InoutRes;
do_read:= len;
{$else}
InOutRes:=1;
if FSread(h, len, Mac_Ptr(addr)) = noErr then
InOutRes:=0;
do_read:= len;
{$endif}
end;
function do_filepos(handle : longint) : longint;
var
pos: Longint;
begin
{$ifdef MACOS_USE_STDCLIB}
{This returns the filepos without moving it.}
do_filepos := lseek(handle, 0, SEEK_CUR);
Errno2InoutRes;
{$else}
InOutRes:=1;
if GetFPos(handle, pos) = noErr then
InOutRes:=0;
do_filepos:= pos;
{$endif}
end;
procedure do_seek(handle,pos : longint);
begin
{$ifdef MACOS_USE_STDCLIB}
lseek(handle, pos, SEEK_SET);
Errno2InoutRes;
{$else}
InOutRes:=1;
if SetFPos(handle, fsFromStart, pos) = noErr then
InOutRes:=0;
{$endif}
end;
function do_seekend(handle:longint):longint;
begin
{$ifdef MACOS_USE_STDCLIB}
do_seekend:= lseek(handle, 0, SEEK_END);
Errno2InoutRes;
{$else}
InOutRes:=1;
if SetFPos(handle, fsFromLEOF, 0) = noErr then
InOutRes:=0;
{TODO Resulting file position is to be returned.}
{$endif}
end;
function do_filesize(handle : longint) : longint;
var
aktfilepos: Longint;
begin
{$ifdef MACOS_USE_STDCLIB}
aktfilepos:= lseek(handle, 0, SEEK_CUR);
if errno = 0 then
begin
do_filesize := lseek(handle, 0, SEEK_END);
Errno2InOutRes; {Report the error from this operation.}
lseek(handle, aktfilepos, SEEK_SET); {Always try to move back,
even in presence of error.}
end
else
Errno2InOutRes;
{$else}
InOutRes:=1;
if GetEOF(handle, pos) = noErr then
InOutRes:=0;
do_filesize:= pos;
{$endif}
end;
{ truncate at a given position }
procedure do_truncate (handle,pos:longint);
begin
{$ifdef MACOS_USE_STDCLIB}
ioctl(handle, FIOSETEOF, pointer(pos));
Errno2InoutRes;
{$else}
InOutRes:=1;
do_seek(handle,pos); //TODO: Is this needed (Does the user anticipate the filemarker is at the end?)
if SetEOF(handle, pos) = noErr then
InOutRes:=0;
{$endif}
end;
procedure do_open(var f;p:pchar;flags:longint; pchangeable: boolean);
{
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 $100) the file will be append
when (flags and $1000) the file will be truncate/rewritten
when (flags and $10000) there is no check for close (needed for textfiles)
}
var
scriptTag: ScriptCode;
refNum: Integer;
err: OSErr;
res: Integer;
spec: FSSpec;
fh: Longint;
oflags : longint;
fullPath: RawByteString;
finderInfo: FInfo;
begin
{ 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
{not assigned}
inoutres:=102;
exit;
end;
end;
end;
{ reset file handle }
filerec(f).handle:=UnusedHandle;
{$ifdef MACOS_USE_STDCLIB}
{ We do the conversion of filemodes here, concentrated on 1 place }
case (flags and 3) of
0 : begin
oflags :=O_RDONLY;
filerec(f).mode:=fminput;
end;
1 : begin
oflags :=O_WRONLY;
filerec(f).mode:=fmoutput;
end;
2 : begin
oflags :=O_RDWR;
filerec(f).mode:=fminout;
end;
end;
if (flags and $1000)=$1000 then
oflags:=oflags or (O_CREAT or O_TRUNC)
else if (flags and $100)=$100 then
oflags:=oflags or (O_APPEND);
{ empty name is special }
if p[0]=#0 then
begin
case FileRec(f).mode of
fminput :
FileRec(f).Handle:=StdInputHandle;
fminout, { this is set by rewrite }
fmoutput :
FileRec(f).Handle:=StdOutputHandle;
fmappend :
begin
FileRec(f).Handle:=StdOutputHandle;
FileRec(f).mode:=fmoutput; {fool fmappend}
end;
end;
exit;
end
else
begin
InOutRes:= PathArgToFSSpec(p, spec);
if (InOutRes = 0) or (InOutRes = 2) then
begin
err:= FSpGetFullPath(spec, fullPath, false);
InOutRes:= MacOSErr2RTEerr(err);
end;
if InOutRes <> 0 then
begin
FileRec(f).mode:=fmclosed;
exit;
end;
p:= PChar(fullPath);
end;
fh:= c_open(p, oflags);
if (fh = -1) and (errno = Sys_EROFS) and ((oflags and O_RDWR)<>0) then
begin
oflags:=oflags and not(O_RDWR);
fh:= c_open(p, oflags);
end;
Errno2InOutRes;
if fh <> -1 then
begin
if (FileRec(f).mode = fmOutput) or
(FileRec(f).mode = fmInout) or
(FileRec(f).mode = fmAppend) then
begin
{Change of filetype and creator is always done when a file is opened
for some kind of writing. This ensures overwritten Darwin files will
get apropriate filetype. It must be done after file is opened,
in the case the file did not previously exist.}
FSpGetFInfo(spec, finderInfo);
finderInfo.fdType:= defaultFileType;
finderInfo.fdCreator:= defaultCreator;
FSpSetFInfo(spec, finderInfo);
end;
filerec(f).handle:= fh;
end
else
begin
filerec(f).handle:= UnusedHandle;
FileRec(f).mode:=fmclosed;
end;
{$else}
InOutRes:=1;
{ reset file handle }
filerec(f).handle:=UnusedHandle;
res:= FSpLocationFromFullPath(StrLen(p), p, spec);
if (res = noErr) or (res = fnfErr) then
begin
if FSpCreate(spec, defaultCreator, defaultFileType, smSystemScript) = noErr then
;
if FSpOpenDF(spec, fsCurPerm, refNum) = noErr then
begin
filerec(f).handle:= refNum;
InOutRes:=0;
end;
end;
if (filerec(f).handle=UnusedHandle) then
begin
FileRec(f).mode:=fmclosed;
//errno:=GetLastError;
//Errno2InoutRes;
end;
{$endif}
end;
|