sysos.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2001 by Free Pascal development team
  4. This file implements all the base types and limits required
  5. for a minimal POSIX compliant subset required to port the compiler
  6. to a new OS.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$ifdef FPC_USE_LIBC}
  14. const clib = 'c';
  15. type libcint=longint;
  16. plibcint=^libcint;
  17. {$ifdef FreeBSD} // tested on x86
  18. function geterrnolocation: Plibcint; cdecl;external clib name '__error';
  19. {$else}
  20. {$ifdef NetBSD} // from a sparc dump.
  21. function geterrnolocation: Plibcint; cdecl;external clib name '__errno';
  22. {$else}
  23. {$ifdef Darwin}
  24. function geterrnolocation: Plibcint; cdecl;external clib name '__error';
  25. {$else}
  26. {$ifdef OpenBSD}
  27. var libcerrno : libcint; cvar;
  28. function geterrnolocation: Plibcint; cdecl;
  29. begin
  30. geterrnolocation:=@libcerrno;
  31. end;
  32. {$else}
  33. {$endif}
  34. {$endif}
  35. {$endif}
  36. {$endif}
  37. function geterrno:libcint; [public, alias: 'FPC_SYS_GETERRNO'];
  38. begin
  39. geterrno:=geterrnolocation^;
  40. end;
  41. procedure seterrno(err:libcint); [public, alias: 'FPC_SYS_SETERRNO'];
  42. begin
  43. geterrnolocation^:=err;
  44. end;
  45. {$else}
  46. threadvar
  47. Errno : longint;
  48. function geterrno:longint; [public, alias: 'FPC_SYS_GETERRNO'];
  49. begin
  50. GetErrno:=Errno;
  51. end;
  52. procedure seterrno(err:longint); [public, alias: 'FPC_SYS_SETERRNO'];
  53. begin
  54. Errno:=err;
  55. end;
  56. {$endif}
  57. { OS dependant parts }
  58. {$I errno.inc} // error numbers
  59. {$I ostypes.inc} // c-types, unix base types, unix base structures
  60. {$I osmacro.inc}
  61. {$ifdef FPC_USE_LIBC}
  62. {$Linklib c}
  63. {$i oscdeclh.inc}
  64. {$i oscdecl.inc}
  65. {$else}
  66. {$I syscallh.inc}
  67. {$I syscall.inc}
  68. {$I sysnr.inc}
  69. {$I ossysc.inc}
  70. {$endif}
  71. {*****************************************************************************
  72. Error conversion
  73. *****************************************************************************}
  74. {
  75. The lowlevel file functions should take care of setting the InOutRes to the
  76. correct value if an error has occured, else leave it untouched
  77. }
  78. Function PosixToRunError (PosixErrno : longint) : longint;
  79. {
  80. Convert ErrNo error to the correct Inoutres value
  81. }
  82. begin
  83. if PosixErrNo=0 then { Else it will go through all the cases }
  84. exit(0);
  85. case PosixErrNo of
  86. ESysENFILE,
  87. ESysEMFILE : Inoutres:=4;
  88. ESysENOENT : Inoutres:=2;
  89. ESysEBADF : Inoutres:=6;
  90. ESysENOMEM,
  91. ESysEFAULT : Inoutres:=217;
  92. ESysEINVAL : Inoutres:=218;
  93. ESysEPIPE,
  94. ESysEINTR,
  95. ESysEIO,
  96. ESysEAGAIN,
  97. ESysENOSPC : Inoutres:=101;
  98. ESysENAMETOOLONG : Inoutres := 3;
  99. ESysEROFS,
  100. ESysEEXIST,
  101. ESysENOTEMPTY,
  102. ESysEACCES : Inoutres:=5;
  103. ESysEISDIR : InOutRes:=5;
  104. else
  105. begin
  106. InOutRes := Integer(PosixErrno);
  107. end;
  108. end;
  109. PosixToRunError:=InOutRes;
  110. end;
  111. Function Errno2InoutRes : longint;
  112. begin
  113. Errno2InoutRes:=PosixToRunError(getErrno);
  114. InoutRes:=Errno2InoutRes;
  115. end;
  116. {*****************************************************************************
  117. Low Level File Routines
  118. *****************************************************************************}
  119. Function Do_IsDevice(Handle:Longint):boolean;
  120. {
  121. Interface to Unix ioctl call.
  122. Performs various operations on the filedescriptor Handle.
  123. Ndx describes the operation to perform.
  124. Data points to data needed for the Ndx function. The structure of this
  125. data is function-dependent.
  126. }
  127. CONST
  128. IOCtl_TCGETS=$40000000+$2C7400+ 19;
  129. var
  130. Data : array[0..255] of byte; {Large enough for termios info}
  131. begin
  132. Do_IsDevice:=(Fpioctl(handle,IOCTL_TCGETS,@data)<>-1);
  133. end;