unxfunc.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Marco van de Voort
  4. member of the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY;without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$ifndef HAS_LIBC_PIPING}
  12. Function PClose(Var F:file) : cint;
  13. var
  14. pl : ^cint;
  15. res : cint;
  16. begin
  17. fpclose(filerec(F).Handle);
  18. { closed our side, Now wait for the other - this appears to be needed ?? }
  19. pl:=@(filerec(f).userdata[2]);
  20. fpwaitpid(pl^,@res,0);
  21. pclose:=res shr 8;
  22. end;
  23. Function PClose(Var F:text) :cint;
  24. var
  25. pl : ^longint;
  26. res : longint;
  27. begin
  28. fpclose(Textrec(F).Handle);
  29. { closed our side, Now wait for the other - this appears to be needed ?? }
  30. pl:=@(textrec(f).userdata[2]);
  31. fpwaitpid(pl^,@res,0);
  32. pclose:=res shr 8;
  33. end;
  34. {$ENDIF}
  35. Function AssignPipe(var pipe_in,pipe_out:cint):cint; [public, alias : 'FPC_SYSC_ASSIGNPIPE'];
  36. {
  37. Sets up a pair of file variables, which act as a pipe. The first one can
  38. be read from, the second one can be written to.
  39. If the operation was unsuccesful, linuxerror is set.
  40. }
  41. var
  42. ret : longint;
  43. errn : cint;
  44. {$ifdef FPC_USE_LIBC}
  45. fdis : array[0..1] of cint;
  46. {$endif}
  47. begin
  48. {$ifndef FPC_USE_LIBC}
  49. ret:=intAssignPipe(pipe_in,pipe_out,errn);
  50. if ret=-1 Then
  51. fpseterrno(errn);
  52. {$ELSE}
  53. fdis[0]:=pipe_in;
  54. fdis[1]:=pipe_out;
  55. ret:=pipe(fdis);
  56. pipe_in:=fdis[0];
  57. pipe_out:=fdis[1];
  58. {$ENDIF}
  59. AssignPipe:=ret;
  60. end;
  61. Function fpFlock (fd,mode : longint) : cint;
  62. {
  63. var
  64. fl : flock;
  65. cmd : cint;
  66. }
  67. begin
  68. {
  69. { initialize the flock struct to set lock on entire file }
  70. fillchar(fl,sizeof(fl),0);
  71. { In non-blocking lock, use F_SETLK for cmd, F_SETLKW otherwise }
  72. if (operation and LOCK_NB)<>0 then
  73. begin
  74. cmd:=F_SETLK;
  75. { turn off this bit }
  76. operation:=operation and not(LOCK_NB);
  77. end
  78. else
  79. cmd:=F_SETLKW;
  80. case operation of
  81. LOCK_UN:
  82. fl.l_type:=fl.l_type or F_UNLCK;
  83. LOCK_SH:
  84. fl.l_type:=fl.l_type or F_RDLCK;
  85. LOCK_EX:
  86. fl.l_type:=fl.l_type or F_WRLCK;
  87. else
  88. begin
  89. errno:=EINVAL;
  90. result:=-1
  91. exit;
  92. end;
  93. end;
  94. result:=fpFcntl(fd,cmd,@fl);
  95. if (result=-1) and (errno=EACCES)
  96. errno:=EWOULDBLOCK;
  97. }
  98. end;