sysos.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. {$ifdef darwin}
  62. {$i sysmach.inc}
  63. {$endif}
  64. {$else}
  65. {$I syscallh.inc}
  66. {$I syscall.inc}
  67. {$I sysnr.inc}
  68. {$I ossysc.inc}
  69. {$I syscgen.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 occurred, 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. ESysEBusy,
  103. ESysENOTDIR, // busy, enotdir, mantis #25931
  104. ESysEACCES : Inoutres:=5;
  105. ESysEISDIR : InOutRes:=5;
  106. else
  107. begin
  108. InOutRes := Integer(PosixErrno);
  109. end;
  110. end;
  111. PosixToRunError:=InOutRes;
  112. end;
  113. Function Errno2InoutRes : longint;
  114. begin
  115. Errno2InoutRes:=PosixToRunError(getErrno);
  116. InoutRes:=Errno2InoutRes;
  117. end;
  118. {*****************************************************************************
  119. Low Level File Routines
  120. *****************************************************************************}
  121. Function Do_IsDevice(Handle:Longint):boolean;
  122. var
  123. StatRec: Stat;
  124. begin
  125. fpFStat (Handle, StatRec);
  126. case StatRec.st_Mode and S_IFMT of
  127. S_IFCHR, S_IFIFO, S_IFSOCK: Do_IsDevice := true
  128. else
  129. Do_IsDevice := false;
  130. end;
  131. end;