summaryrefslogtreecommitdiff
path: root/rtl/os2/sysfile.inc
blob: 1233333d14180459ac4e0a8b51842ba490bd9d7a (plain)
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
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2001 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.

 **********************************************************************}

{****************************************************************************

                          Low Level File Routines

****************************************************************************}

procedure do_close(h:thandle);
var
  RC: cardinal;
begin
{ Only three standard handles under real OS/2 }
  if h>2 then
   begin
    RC := DosClose (H);
    if RC <> 0 then
     begin
      InOutRes := longint (RC);
      OSErrorWatch (RC);
     end;
   end;
{$ifdef IODEBUG}
  writeln('do_close: handle=', H, ', InOutRes=', InOutRes);
{$endif}
end;

procedure do_erase(p:Pchar; pchangeable: boolean);
var
  oldp: pchar;
  RC: cardinal;
begin
  oldp:=p;
  DoDirSeparators(p,pchangeable);
  RC := DosDelete (P);
  if RC <> 0 then
   begin
    InOutRes := longint (RC);
    OSErrorWatch (RC);
   end;
  if p<>oldp then
    freemem(p);
end;

procedure do_rename(p1,p2:Pchar; p1changeable, p2changeable: boolean);
var
  oldp1, oldp2 : pchar;
  RC: cardinal;
begin
  oldp1:=p1;
  oldp2:=p2;
  DoDirSeparators(p1,p1changeable);
  DoDirSeparators(p2,p2changeable);
  RC := DosMove (p1, p2);
  if RC <> 0 then
   begin
    InOutRes := longint (RC);
    OSErrorWatch (RC);
   end;
  if p1<>oldp1 then
    freemem(p1);
  if p2<>oldp2 then
    freemem(p2);
end;

function do_read(h:thandle;addr:pointer;len:longint):longint;
Var
  T: cardinal;
  RC: cardinal;
begin
{$ifdef IODEBUG}
  write('do_read: handle=', h, ', addr=', ptrint(addr), ', length=', len);
{$endif}
  RC := DosRead(H, Addr, Len, T);
  if RC <> 0 then
   begin
    InOutRes := longint (RC);
    OSErrorWatch (RC);
   end;
  do_read:= longint (T);
{$ifdef IODEBUG}
  writeln(', actual_len=', t, ', InOutRes=', InOutRes);
{$endif}
end;

function do_write(h:thandle;addr:pointer;len:longint) : longint;
Var
  T: cardinal;
  RC: cardinal;
begin
{$ifdef IODEBUG}
  write('do_write: handle=', h, ', addr=', ptrint(addr), ', length=', len);
{$endif}
  RC := DosWrite(H, Addr, Len, T);
  if RC <> 0 then
   begin
    InOutRes := longint (RC);
    OSErrorWatch (RC);
   end;
  do_write:= longint (T);
{$ifdef IODEBUG}
  writeln(', actual_len=', t, ', InOutRes=', InOutRes);
{$endif}
end;

function Do_FilePos (Handle: THandle): int64;
var
  PosActual: int64;
  RC: cardinal;
begin
  RC := Sys_DosSetFilePtrL (Handle, 0, 1, PosActual);
  if RC <> 0 then
   begin
    InOutRes := longint (RC);
    OSErrorWatch (RC);
   end;
  Do_FilePos := PosActual;
{$ifdef IODEBUG}
  writeln('do_filepos: handle=', Handle, ', actual_pos=', PosActual, ', InOutRes=', InOutRes);
{$endif}
end;

procedure Do_Seek (Handle: THandle; Pos: int64);
var
  PosActual: int64;
  RC: cardinal;
begin
  RC := Sys_DosSetFilePtrL(Handle, Pos, 0 {ZeroBased}, PosActual);
  if RC <> 0 then
   begin
    InOutRes := longint (RC);
    OSErrorWatch (RC);
   end;
{$ifdef IODEBUG}
  writeln('do_seek: handle=', Handle, ', pos=', pos, ', actual_pos=', PosActual, ', InOutRes=', InOutRes);
{$endif}
end;

function Do_SeekEnd (Handle: THandle): int64;
var
  PosActual: int64;
  RC: cardinal;
begin
  RC := Sys_DosSetFilePtrL (Handle, 0, 2 {EndBased}, PosActual);
  if RC <> 0 then
   begin
    InOutRes := longint (RC);
    OSErrorWatch (RC);
    Do_SeekEnd := -1;
   end
  else
   Do_SeekEnd := PosActual;
{$ifdef IODEBUG}
  writeln('do_seekend: handle=', Handle, ', actual_pos=', PosActual, ', InOutRes=', InOutRes);
{$endif}
end;

function Do_FileSize (Handle: THandle): int64;
var
  AktFilePos: int64;
begin
  AktFilePos := Do_FilePos (Handle);
  if InOutRes = 0 then
   begin
    Do_FileSize := Do_SeekEnd (Handle);
    Do_Seek (Handle, AktFilePos);
   end;
end;

procedure Do_Truncate (Handle: THandle; Pos: int64);
var
  RC: cardinal;
begin
  RC := Sys_DosSetFileSizeL (Handle, Pos);
  if RC <> 0 then
   begin
    InOutRes := longint (RC);
    OSErrorWatch (RC);
   end
  else
   Do_SeekEnd (Handle);
end;


const
    FileHandleCount: cardinal = 20;

function Increase_File_Handle_Count: boolean;
var L1: longint;
    L2: cardinal;
    RC: cardinal;
begin
  L1 := 10;
  RC := DosSetRelMaxFH (L1, L2);
  if RC <> 0 then
   begin
    Increase_File_Handle_Count := false;
    OSErrorWatch (RC);
   end
  else
   if L2 > FileHandleCount then
    begin
      FileHandleCount := L2;
      Increase_File_Handle_Count := true;
    end
   else
    Increase_File_Handle_Count := false;
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
  Action, Attrib, OpenFlags, FM: Cardinal;
  oldp : pchar;
  RC: cardinal;
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
        inoutres:=102; {not assigned}
        exit;
      end;
    end;
  end;

  // reset file handle
  filerec(f).handle := UnusedHandle;

  Attrib:=0;
  OpenFlags:=0;

  // convert filesharing
  FM := Flags and $FF and not (8);
(* DenyNone if sharing not specified. *)
  if FM and 112 = 0 then
    FM := FM or 64;
  // convert filemode to filerec modes and access mode
  case (FM and 3) of
    0: filerec(f).mode:=fminput;
    1: filerec(f).mode:=fmoutput;
    2: filerec(f).mode:=fminout;
  end;

  if (flags and $1000)<>0 then
    OpenFlags:=OpenFlags or 2 {doOverwrite} or 16 {doCreate} // Create/overwrite
  else
    OpenFlags:=OpenFlags or 1 {doOpen}; // Open existing

  // Handle Std I/O
  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;

  oldp:=p;
  // convert unix slashes to normal slashes
  DoDirSeparators(p,pchangeable);
  Attrib:=32 {faArchive};

  RC := Sys_DosOpenL(p, FileRec(F).Handle, Action, 0, Attrib, OpenFlags, FM, nil);
  if RC <> 0 then
   begin
    InOutRes := longint (RC);
    OSErrorWatch (RC);
   end;

  // If too many open files try to set more file handles and open again
  if (InOutRes = 4) then
   if Increase_File_Handle_Count then
    begin
     RC := Sys_DosOpenL(p, FileRec(F).Handle, Action, 0, Attrib, OpenFlags, FM, nil);
     if RC <> 0 then
      begin
       InOutRes := longint (RC);
       OSErrorWatch (RC);
      end;
    end;
  if RC <> 0 then
   FileRec(F).Handle:=UnusedHandle;

  // If Handle created -> make some things
  if (FileRec(F).Handle <> UnusedHandle) then
  begin

    // Move to end of file for Append command
    if ((Flags and $100) <> 0) then
    begin
      if not (Do_IsDevice (FileRec (F).Handle)) then
       Do_SeekEnd (FileRec (F).Handle);
      FileRec(F).Mode := fmOutput;
    end;

  end
  else
    FileRec(f).mode:=fmclosed;

  if oldp<>p then
    freemem(p);

{$ifdef IODEBUG}
  writeln('do_open,', filerec(f).handle, ',', filerec(f).name, ',', filerec(f).mode, ', InOutRes=', InOutRes);
{$endif}
end;

function do_isdevice (Handle: THandle): boolean;
var
  HT, Attr: cardinal;
  RC: cardinal;
const
  dhDevice = 1;
  dhPipe = 2;
begin
  do_isdevice:=false;
  RC := DosQueryHType(Handle, HT, Attr);
  if RC <> 0 then
   begin
    OSErrorWatch (RC);
    Exit;
   end;
  if (HT = dhDevice) or (HT = dhPipe) then
   do_isdevice:=true;
end;
{$ASMMODE ATT}