sysos.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. {$ifdef ver1_0}
  47. Var
  48. {$else}
  49. threadvar
  50. {$endif}
  51. Errno : longint;
  52. function geterrno:longint; [public, alias: 'FPC_SYS_GETERRNO'];
  53. begin
  54. GetErrno:=Errno;
  55. end;
  56. procedure seterrno(err:longint); [public, alias: 'FPC_SYS_SETERRNO'];
  57. begin
  58. Errno:=err;
  59. end;
  60. {$endif}
  61. { OS dependant parts }
  62. {$I errno.inc} // error numbers
  63. {$I ostypes.inc} // c-types, unix base types, unix base structures
  64. {$I osmacro.inc}
  65. {$ifdef FPC_USE_LIBC}
  66. {$Linklib c}
  67. {$i oscdeclh.inc}
  68. {$else}
  69. {$I syscallh.inc}
  70. {$I syscall.inc}
  71. {$I sysnr.inc}
  72. {$I ossysc.inc}
  73. {$endif}
  74. {*****************************************************************************
  75. Error conversion
  76. *****************************************************************************}
  77. {
  78. The lowlevel file functions should take care of setting the InOutRes to the
  79. correct value if an error has occured, else leave it untouched
  80. }
  81. Function PosixToRunError (PosixErrno : longint) : longint;
  82. {
  83. Convert ErrNo error to the correct Inoutres value
  84. }
  85. begin
  86. if PosixErrNo=0 then { Else it will go through all the cases }
  87. exit(0);
  88. case PosixErrNo of
  89. ESysENFILE,
  90. ESysEMFILE : Inoutres:=4;
  91. ESysENOENT : Inoutres:=2;
  92. ESysEBADF : Inoutres:=6;
  93. ESysENOMEM,
  94. ESysEFAULT : Inoutres:=217;
  95. ESysEINVAL : Inoutres:=218;
  96. ESysEPIPE,
  97. ESysEINTR,
  98. ESysEIO,
  99. ESysEAGAIN,
  100. ESysENOSPC : Inoutres:=101;
  101. ESysENAMETOOLONG : Inoutres := 3;
  102. ESysEROFS,
  103. ESysEEXIST,
  104. ESysENOTEMPTY,
  105. ESysEACCES : Inoutres:=5;
  106. ESysEISDIR : InOutRes:=5;
  107. else
  108. begin
  109. InOutRes := Integer(PosixErrno);
  110. end;
  111. end;
  112. PosixToRunError:=InOutRes;
  113. end;
  114. Function Errno2InoutRes : longint;
  115. begin
  116. Errno2InoutRes:=PosixToRunError(getErrno);
  117. InoutRes:=Errno2InoutRes;
  118. end;
  119. {*****************************************************************************
  120. Low Level File Routines
  121. *****************************************************************************}
  122. Function Do_IsDevice(Handle:Longint):boolean;
  123. {
  124. Interface to Unix ioctl call.
  125. Performs various operations on the filedescriptor Handle.
  126. Ndx describes the operation to perform.
  127. Data points to data needed for the Ndx function. The structure of this
  128. data is function-dependent.
  129. }
  130. CONST
  131. IOCtl_TCGETS=$5401;
  132. var
  133. Data : array[0..255] of byte; {Large enough for termios info}
  134. begin
  135. Do_IsDevice:=(Fpioctl(handle,IOCTL_TCGETS,@data)<>-1);
  136. end;
  137. {
  138. $Log: sysos.inc,v $
  139. Revision 1.4 2005/02/13 21:47:56 peter
  140. * include file cleanup part 2
  141. Revision 1.3 2005/02/07 22:04:55 peter
  142. * moved to unix
  143. Revision 1.2 2005/02/06 13:06:20 peter
  144. * moved file and dir functions to sysfile/sysdir
  145. * win32 thread in systemunit
  146. Revision 1.1 2005/02/06 12:16:52 peter
  147. * bsd thread updates
  148. }