sysos.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. {$if defined(FreeBSD) or defined(DragonFly)} // 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. function geterrnolocation: Plibcint; cdecl;external clib name '__errno';
  28. {$else}
  29. {$endif}
  30. {$endif}
  31. {$endif}
  32. {$endif}
  33. function geterrno:libcint; [public, alias: 'FPC_SYS_GETERRNO'];
  34. begin
  35. geterrno:=geterrnolocation^;
  36. end;
  37. procedure seterrno(err:libcint); [public, alias: 'FPC_SYS_SETERRNO'];
  38. begin
  39. geterrnolocation^:=err;
  40. end;
  41. {$else}
  42. threadvar
  43. Errno : longint;
  44. function geterrno:longint; [public, alias: 'FPC_SYS_GETERRNO'];
  45. begin
  46. GetErrno:=Errno;
  47. end;
  48. procedure seterrno(err:longint); [public, alias: 'FPC_SYS_SETERRNO'];
  49. begin
  50. Errno:=err;
  51. end;
  52. {$endif}
  53. { OS dependant parts }
  54. {$I errno.inc} // error numbers
  55. {$I ostypes.inc} // c-types, unix base types, unix base structures
  56. {$I osmacro.inc}
  57. {$ifdef FPC_USE_LIBC}
  58. {$Linklib c}
  59. {$i oscdeclh.inc}
  60. {$i oscdecl.inc}
  61. {$else}
  62. {$I syscallh.inc}
  63. {$I syscall.inc}
  64. {$I sysnr.inc}
  65. {$I ossysc.inc}
  66. {$I syscgen.inc}
  67. {$endif}
  68. {*****************************************************************************
  69. Error conversion
  70. *****************************************************************************}
  71. {
  72. The lowlevel file functions should take care of setting the InOutRes to the
  73. correct value if an error has occurred, else leave it untouched
  74. }
  75. Function PosixToRunError (PosixErrno : longint) : longint;
  76. {
  77. Convert ErrNo error to the correct Inoutres value
  78. }
  79. begin
  80. if PosixErrNo=0 then { Else it will go through all the cases }
  81. exit(0);
  82. case PosixErrNo of
  83. ESysENFILE,
  84. ESysEMFILE : Inoutres:=4;
  85. ESysENOENT : Inoutres:=2;
  86. ESysEBADF : Inoutres:=6;
  87. ESysENOMEM,
  88. ESysEFAULT : Inoutres:=217;
  89. ESysEINVAL : Inoutres:=218;
  90. ESysEPIPE,
  91. ESysEINTR,
  92. ESysEIO,
  93. ESysEAGAIN,
  94. ESysENOSPC : Inoutres:=101;
  95. ESysENAMETOOLONG : Inoutres := 3;
  96. ESysEROFS,
  97. ESysEEXIST,
  98. ESysENOTEMPTY,
  99. ESysEBusy,
  100. ESysENOTDIR, // busy, enotdir, mantis #25931
  101. ESysEACCES : Inoutres:=5;
  102. ESysEISDIR : InOutRes:=5;
  103. else
  104. begin
  105. InOutRes := Integer(PosixErrno);
  106. end;
  107. end;
  108. PosixToRunError:=InOutRes;
  109. end;
  110. Function Errno2InoutRes : longint;
  111. begin
  112. Errno2InoutRes:=PosixToRunError(getErrno);
  113. InoutRes:=Errno2InoutRes;
  114. end;
  115. {*****************************************************************************
  116. Low Level File Routines
  117. *****************************************************************************}
  118. Function Do_IsDevice(Handle:Longint):boolean;
  119. {
  120. Interface to Unix ioctl call.
  121. Performs various operations on the filedescriptor Handle.
  122. Ndx describes the operation to perform.
  123. Data points to data needed for the Ndx function. The structure of this
  124. data is function-dependent.
  125. }
  126. CONST
  127. IOCtl_TCGETS=$40000000+$2C7400+ 19;
  128. var
  129. Data : array[0..255] of byte; {Large enough for termios info}
  130. begin
  131. Do_IsDevice:=(Fpioctl(handle,IOCTL_TCGETS,@data)<>-1);
  132. end;