summaryrefslogtreecommitdiff
path: root/rtl/unix/bunxovl.inc
blob: c5774ea3a391e8a6692b68743a562157c53b71d4 (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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2002 by Marco van de Voort

    Some generic overloads for stringfunctions in the baseunix unit.

    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.

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

Function  FpLink (const existing : RawByteString; const newone : RawByteString): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemExistingFileName, SystemNewOneFileName: RawByteString;
Begin
  SystemExistingFileName:=ToSingleByteFileSystemEncodedFileName(existing);
  SystemNewOneFileName:=ToSingleByteFileSystemEncodedFileName(newone);
  FpLink:=FpLink(pchar(SystemExistingFileName),pchar(SystemNewOneFileName));
End;

Function  FpMkfifo (const path : RawByteString; Mode : TMode): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpMkfifo:=FpMkfifo(pchar(SystemPath),mode);
End;

Function  FpChmod (const path : RawByteString; Mode : TMode): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpChmod:=FpChmod(pchar(SystemPath),mode);
End;

Function  FpChown (const path : RawByteString; owner : TUid; group : TGid): cInt;{$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpChown:=FpChown(pchar(SystemPath),owner,group);
End;

Function  FpUtime (const path : RawByteString; times : putimbuf): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpUtime:=FpUtime(pchar(SystemPath),times);
End;

{
Function  FpGetcwd (const path:RawByteString; siz:TSize):RawByteString; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpGetcwd:=RawByteString(pchar(FpGetcwd(pchar(SystemPath),siz)));
  SetCodePage(FpGetcwd,DefaultFileSystemCodePage,false);
End;
}
Function  FpGetcwd: RawByteString;

Var
  Buf : Array[0..PATH_MAX+1]  of char;
Begin
  Buf[PATH_MAX+1]:=#0;
  If FpGetcwd(@Buf[0],PATH_MAX)=Nil then
    FpGetcwd:=''
  else
    begin
      FpGetcwd:=Buf;
      SetCodePage(FpGetcwd,DefaultFileSystemCodePage,false);
    end;
End;

Function  FpExecve (const path : RawByteString; argv : ppchar; envp: ppchar): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpExecve:=FpExecve (pchar(SystemPath),argv,envp);
End;

Function  FpExecv (const path : RawByteString; argv : ppchar): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpExecv:=FpExecve (pchar(SystemPath),argv,envp);
End;


Function  FpChdir (const path : RawByteString): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpChDir:=FpChdir(pchar(SystemPath));
End;

Function  FpOpen (const path : RawByteString; flags : cInt; Mode: TMode):cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpOpen:=FpOpen(pchar(SystemPath),flags,mode);
End;


Function  FpMkdir (const path : RawByteString; Mode: TMode):cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpMkdir:=FpMkdir(pchar(SystemPath),mode);
End;

Function  FpUnlink (const path : RawByteString): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpUnlink:=FpUnlink(pchar(SystemPath));
End;

Function  FpRmdir (const path : RawByteString): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpRmdir:=FpRmdir(pchar(SystemPath));
End;

Function  FpRename (const old  : RawByteString; const newpath: RawByteString): cInt; {$ifdef VER2_0}inline;{$endif}
var
  OldSystemPath, NewSystemPath: RawByteString;
Begin
  OldSystemPath:=ToSingleByteFileSystemEncodedFileName(old);
  NewSystemPath:=ToSingleByteFileSystemEncodedFileName(newpath);
  FpRename:=FpRename(pchar(OldSystemPath),pchar(NewSystemPath));
End;

Function  FpStat (const path: RawByteString; var buf : stat): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpStat:=FpStat(pchar(SystemPath),buf);
End;

Function  fpLstat   (const path: RawByteString; Info: pstat):cint; inline;
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  fplstat:=fplstat(pchar(SystemPath), info);
end;

Function  fpLstat   (path:pchar;var Info:stat):cint; inline;

begin
  fpLstat:=fplstat(path,@info);
end;

Function  fpLstat   (const Filename: RawByteString;var Info:stat):cint; inline;
Begin
  fpLstat:=fplstat(filename,@info);
end;

Function FpAccess (const pathname : RawByteString; aMode : cInt): cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPathName: RawByteString;
Begin
  SystemPathName:=ToSingleByteFileSystemEncodedFileName(pathname);
  FpAccess:=FpAccess(pchar(SystemPathName),amode);
End;

Function  FPFStat(var F:Text;Var Info:stat):Boolean; {$ifdef VER2_0}inline;{$endif}
{
  Get all information on a text file, and return it in info.
}
begin
  FPFStat:=FPFstat(TextRec(F).Handle,INfo)=0;
end;

Function  FPFStat(var F:File;Var Info:stat):Boolean; {$ifdef VER2_0}inline;{$endif}
{
  Get all information on a untyped file, and return it in info.
}
begin
  FPFStat:=FPFstat(FileRec(F).Handle,Info)=0;
end;

Function FpSignal(signum:longint;Handler:signalhandler):signalhandler;
// should be moved out of generic files. Too specific.

var sa,osa : sigactionrec;

begin
     sa.sa_handler:=SigActionHandler(handler);
     FillChar(sa.sa_mask,sizeof(sa.sa_mask),#0);
     sa.sa_flags := 0;
{     if (sigintr and signum) =0 then
 {restart behaviour needs libc}
      sa.sa_flags :=sa.sa_flags or SA_RESTART;
}
     FPSigaction(signum,@sa,@osa);
     if fpgetErrNo<>0 then
       fpsignal:=NIL
     else
       fpsignal:=signalhandler(osa.sa_handler);
end;

{$ifdef FPC_USE_LIBC} // can't remember why this is the case. Might be legacy.
function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; cdecl; external clib name 'read';
{$else}
function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; external name 'FPC_SYSC_READ';
{$endif}

Function  FpRead           (fd : cInt;var buf; nbytes : TSize): TSsize; {$ifdef VER2_0}inline;{$endif}

begin
  FPRead:=xFpRead(fd,pchar(@buf),nbytes);
end;

Function  FpWrite          (fd : cInt;const buf; nbytes : TSize): TSsize; {$ifdef VER2_0}inline;{$endif}
begin
 FpWrite:=FpWrite(fd,pchar(@buf),nbytes);
end;

{$ifdef linux}
function  FppRead           (fd : cInt;var buf; nbytes : TSize; offset:Toff): TSsize; {$ifdef VER2_0}inline;{$endif}

begin
  FppRead:=FppRead(fd,pchar(@buf),nbytes,offset);
end;

function  FppWrite          (fd : cInt;const buf; nbytes : TSize; offset:Toff): TSsize; {$ifdef VER2_0}inline;{$endif}

begin
  FppWrite:=FppWrite(fd,pchar(@buf),nbytes,offset);
end;
{$endif}

const
  { read/write permission for everyone }
  MODE_FPOPEN = S_IWUSR OR S_IRUSR OR
                S_IWGRP OR S_IRGRP OR
                S_IWOTH OR S_IROTH;

Function  FpOpen    (path : pChar; flags : cInt):cInt; {$ifdef VER2_0}inline;{$endif}

begin
 FpOpen:=FpOpen(path,flags,MODE_FPOPEN);
end;

Function  FpOpen    (const path : RawByteString; flags : cInt):cInt; {$ifdef VER2_0}inline;{$endif}
var
  SystemPath: RawByteString;
Begin
  SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  FpOpen:=FpOpen(pchar(SystemPath),flags,MODE_FPOPEN);
end;

Function  FpOpen    (path : String; flags : cInt):cInt;

begin
 path:=path+#0;
 FpOpen:=FpOpen(@path[1],flags,MODE_FPOPEN);
end;

Function  FpOpen    (path : String; flags : cInt; Mode: TMode):cInt;

begin
 path:=path+#0;
 FpOpen:=FpOpen(@path[1],flags,Mode);
end;

Function  FpOpendir (const dirname : RawByteString): pDir; {$ifdef VER2_0}inline;{$endif}
var
  SystemDirName: RawByteString;
Begin
  SystemDirName:=ToSingleByteFileSystemEncodedFileName(dirname);
  FpOpenDir:=FpOpenDir(pchar(SystemDirName));
End;


Function  FpOpendir (dirname : shortString): pDir; {$ifdef VER2_0}inline;{$endif}
Begin
  dirname:=dirname+#0;
  FpOpenDir:=FpOpenDir(pchar(@dirname[1]));
End;


Function  FpStat (path: String; var buf : stat): cInt;

begin
 path:=path+#0;
 FpStat:=FpStat(pchar(@path[1]),buf);
end;

Function fpDup(var oldfile,newfile:text):cint;
{
  Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  oldfile.
  After which the two textfiles are, in effect, the same, except
  that they don't share the same buffer, and don't share the same
  close_on_exit flag.
}
begin
  flush(oldfile);{ We cannot share buffers, so we flush them. }
  textrec(newfile):=textrec(oldfile);
  textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  textrec(newfile).handle:=fpDup(textrec(oldfile).handle);
  fpdup:=textrec(newfile).handle;
end;

Function fpDup(var oldfile,newfile:file):cint;
{
  Copies the filedescriptor oldfile to newfile
}
begin
  filerec(newfile):=filerec(oldfile);
  filerec(newfile).handle:=fpDup(filerec(oldfile).handle);
  fpdup:=  filerec(newfile).handle;
end;


Function FpDup2(var oldfile,newfile:text):cint;
{
  Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  oldfile. It closes newfile if it was still open.
  After which the two textfiles are, in effect, the same, except
  that they don't share the same buffer, and don't share the same
  close_on_exit flag.
}
var
  tmphandle : word;
begin
  case TextRec(oldfile).mode of
    fmOutput, fmInOut, fmAppend :
      flush(oldfile);{ We cannot share buffers, so we flush them. }
  end;
  case TextRec(newfile).mode of
    fmOutput, fmInOut, fmAppend :
      flush(newfile);
  end;
  tmphandle:=textrec(newfile).handle;
  textrec(newfile):=textrec(oldfile);
  textrec(newfile).handle:=tmphandle;
  textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  fpDup2:=fpDup2(textrec(oldfile).handle,textrec(newfile).handle);
end;

Function FpDup2(var oldfile,newfile:file):cint;
{
  Copies the filedescriptor oldfile to newfile
}
var
  tmphandle : word;
begin
  tmphandle := filerec(newfile).handle;
  filerec(newfile):=filerec(oldfile);
  filerec(newfile).handle := tmphandle;
  fpDup2:=fpDup2(filerec(oldfile).handle,filerec(newfile).handle);
end;

function  fptime    :time_t;  {$ifdef VER2_0}inline;{$endif}
var t:time_t;
begin
  fptime:=fptime(t);
end;

Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdset;TimeOut:cint):cint;
{
  Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  have changed.
  This function allows specification of a timeout as a longint.
}
var
  p  : PTimeVal;
  tv : TimeVal;
begin
  if TimeOut=-1 then
   p:=nil
  else
   begin
     tv.tv_Sec:=Timeout div 1000;
     tv.tv_Usec:=(Timeout mod 1000)*1000;
     p:=@tv;
   end;
  fpSelect:=fpSelect(N,Readfds,WriteFds,ExceptFds,p);
end;

Function fpSelect(var T:Text;TimeOut :PTimeval):cint;
Var
  F:TfdSet;
begin
  if textrec(t).mode=fmclosed then
   begin
     fpSetErrNo(ESysEBADF);
     exit(-1);
   end;
  FpFD_ZERO(f);
  fpFD_SET(textrec(T).handle,f);
  if textrec(T).mode=fminput then
   fpselect:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  else
   fpSelect:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
end;

Function fpSelect(var T:Text;TimeOut :time_t):cint;
var
  p  : PTimeVal;
  tv : TimeVal;
begin
  if TimeOut=-1 then
   p:=nil
  else
   begin
     tv.tv_Sec:=Timeout div 1000;
     tv.tv_Usec:=(Timeout mod 1000)*1000;
     p:=@tv;
   end;
  fpSelect:=fpSelect(T,p);
end;

function  FpWaitPid (pid : TPid; Var Status : cInt; Options : cint) : TPid;

begin
  fpWaitPID:=fpWaitPID(Pid,@Status,Options);
end;

Function fpReadLink(const Name: RawByteString): RawByteString;
{
  Read a link (where it points to)
}
var
  SystemFileName : RawByteString;
  i : cint;
begin
  SetLength(fpReadLink,PATH_MAX);
  SystemFileName:=ToSingleByteFileSystemEncodedFileName(Name);
  i:=fpReadLink(pchar(SystemFileName),pchar(fpReadLink),PATH_MAX);
  if i>0 then
   begin
     SetLength(fpReadLink,i);
     SetCodePage(fpReadLink,DefaultFileSystemCodePage,false);
   end
  else
    fpReadLink:='';
end;