sysos.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 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. {$ifdef darwin}
  66. {$i sysmach.inc}
  67. {$endif}
  68. {$else}
  69. {$I syscallh.inc}
  70. {$I syscall.inc}
  71. {$I sysnr.inc}
  72. {$I ossysc.inc}
  73. {$I syscgen.inc}
  74. {$endif}
  75. {*****************************************************************************
  76. Error conversion
  77. *****************************************************************************}
  78. {
  79. The lowlevel file functions should take care of setting the InOutRes to the
  80. correct value if an error has occurred, else leave it untouched
  81. }
  82. Function PosixToRunError (PosixErrno : longint) : longint;
  83. {
  84. Convert ErrNo error to the correct Inoutres value
  85. }
  86. begin
  87. if PosixErrNo=0 then { Else it will go through all the cases }
  88. exit(0);
  89. case PosixErrNo of
  90. ESysENFILE,
  91. ESysEMFILE : Inoutres:=4;
  92. ESysENOENT : Inoutres:=2;
  93. ESysEBADF : Inoutres:=6;
  94. ESysENOMEM,
  95. ESysEFAULT : Inoutres:=217;
  96. ESysEINVAL : Inoutres:=218;
  97. ESysEPIPE,
  98. ESysEINTR,
  99. ESysEIO,
  100. ESysEAGAIN,
  101. ESysENOSPC : Inoutres:=101;
  102. ESysENAMETOOLONG : Inoutres := 3;
  103. ESysEROFS,
  104. ESysEEXIST,
  105. ESysENOTEMPTY,
  106. ESysEBusy,
  107. ESysENOTDIR, // busy, enotdir, mantis #25931
  108. ESysEACCES : Inoutres:=5;
  109. ESysEISDIR : InOutRes:=5;
  110. else
  111. begin
  112. InOutRes := Integer(PosixErrno);
  113. end;
  114. end;
  115. PosixToRunError:=InOutRes;
  116. end;
  117. Function Errno2InoutRes : longint;
  118. begin
  119. Errno2InoutRes:=PosixToRunError(getErrno);
  120. InoutRes:=Errno2InoutRes;
  121. end;
  122. {*****************************************************************************
  123. Low Level File Routines
  124. *****************************************************************************}
  125. Function Do_IsDevice(Handle:Longint):boolean;
  126. var
  127. StatRec: Stat;
  128. begin
  129. fpFStat (Handle, StatRec);
  130. case StatRec.st_Mode and S_IFMT of
  131. S_IFCHR, S_IFIFO, S_IFSOCK: Do_IsDevice := true
  132. else
  133. Do_IsDevice := false;
  134. end;
  135. end;