unxfunc.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. Function PClose(Var F:file) : cint;
  12. var
  13. pl : ^cint;
  14. res: cint;
  15. begin
  16. repeat
  17. res:=fpclose(filerec(F).Handle);
  18. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  19. { closed our side, Now wait for the other - this appears to be needed ?? }
  20. pl:=@(filerec(f).userdata[2]);
  21. pclose := WaitProcess(pl^);
  22. end;
  23. Function PClose(Var F:text) :cint;
  24. var
  25. pl : ^cint;
  26. res : cint;
  27. begin
  28. repeat
  29. res:=fpclose(Textrec(F).Handle);
  30. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  31. { closed our side, Now wait for the other - this appears to be needed ?? }
  32. pl:=@(textrec(f).userdata[2]);
  33. pclose:= WaitProcess(pl^);
  34. end;
  35. // can't have oldfpccall here, linux doesn't need it.
  36. Function AssignPipe(var pipe_in,pipe_out:cint):cint; [public, alias : 'FPC_SYSC_ASSIGNPIPE'];
  37. {
  38. Sets up a pair of file variables, which act as a pipe. The first one can
  39. be read from, the second one can be written to.
  40. If the operation was unsuccesful, linuxerror is set.
  41. }
  42. var
  43. ret : longint;
  44. fdis : array[0..1] of cint;
  45. begin
  46. fdis[0]:=pipe_in;
  47. fdis[1]:=pipe_out;
  48. ret:=pipe(fdis);
  49. pipe_in:=fdis[0];
  50. pipe_out:=fdis[1];
  51. AssignPipe:=ret;
  52. end;
  53. {$define FPC_HAS_GETTIMEZONEFILE}
  54. function GetTimezoneFile:shortstring;
  55. var
  56. tzenv : PAnsiChar;
  57. l: longint;
  58. s: shortstring;
  59. ft : text;
  60. begin
  61. GetTimeZoneFile:='';
  62. { the TZ variable holds the name of the timezone (possibly followed by a
  63. comma and rules), and the timezone files themselves are stored in
  64. /usr/share/lib/zoneinfo }
  65. tzenv:=fpgetenv('TZ');
  66. if assigned(tzenv) then
  67. begin
  68. s:=strpas(tzenv);
  69. l:=pos(',',s);
  70. if l<>0 then
  71. s:=copy(s,1,l-1);
  72. GetTimeZoneFile:='/usr/share/lib/zoneinfo/'+s;
  73. end
  74. else
  75. exit;
  76. assign(ft,GetTimeZoneFile);
  77. {$push}
  78. {$I-}
  79. reset(ft);
  80. if IOResult=0 then
  81. close(ft)
  82. else
  83. GetTimeZoneFile:='';
  84. {$pop}
  85. end;
  86. { should probably be defined in ostypes.inc for all OSes }
  87. const
  88. F_RDLCK = 01; (* Read lock *)
  89. F_WRLCK = 02; (* Write lock *)
  90. F_UNLCK = 03; (* Remove lock(s) *)
  91. Function fpFlock (fd,mode : longint) : cint;
  92. var
  93. fl : flock;
  94. cmd : cint;
  95. begin
  96. { initialize the flock struct to set lock on entire file }
  97. fillchar(fl,sizeof(fl),0);
  98. { In non-blocking lock, use F_SETLK for cmd, F_SETLKW otherwise }
  99. if (mode and LOCK_NB)<>0 then
  100. begin
  101. cmd:=F_SETLK;
  102. { turn off this bit }
  103. mode:=mode and not(LOCK_NB);
  104. end
  105. else
  106. cmd:=F_SETLKW;
  107. case mode of
  108. LOCK_UN:
  109. fl.l_type:=fl.l_type or F_UNLCK;
  110. LOCK_SH:
  111. fl.l_type:=fl.l_type or F_RDLCK;
  112. LOCK_EX:
  113. fl.l_type:=fl.l_type or F_WRLCK;
  114. else
  115. begin
  116. errno:=ESysEINVAL;
  117. fpFlock:=-1;
  118. exit;
  119. end;
  120. end;
  121. fpFlock:=fpFcntl(fd,cmd,fl);
  122. if (fpFlock=-1) and (errno=ESysEACCES) then
  123. errno:=ESysEWOULDBLOCK;
  124. end;