system.pp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time librar~y.
  4. Copyright (c) 2000 by Marco van de Voort
  5. member of the Free Pascal development team.
  6. System unit for Linux.
  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. { These things are set in the makefile, }
  14. { But you can override them here.}
  15. { If you use an aout system, set the conditional AOUT}
  16. { $Define AOUT}
  17. Unit {$ifdef VER1_0}Syslinux{$else}System{$endif};
  18. Interface
  19. {$define oldreaddir}
  20. {$define usedomain}
  21. {$define posixworkaround}
  22. {$define FPC_IS_SYSTEM}
  23. {$ifdef FPC_USE_LIBC}
  24. {$define usegetcwd}
  25. {$endif}
  26. {$I sysunixh.inc}
  27. Implementation
  28. {$ifdef FPC_USE_LIBC}
  29. const clib = 'c';
  30. type libcint=longint;
  31. plibcint=^libcint;
  32. function geterrnolocation: Plibcint; cdecl;external clib name'__errno_location';
  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. {$ifdef ver1_0}
  43. Var
  44. {$else}
  45. ThreadVar
  46. {$endif}
  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. {$I system.inc}
  58. { OS dependant parts }
  59. {$I errno.inc} // error numbers
  60. {$I bunxtype.inc} // c-types, unix base types, unix
  61. // base structures
  62. {*****************************************************************************
  63. Extra cdecl declarations for FPC_USE_LIBC for this os
  64. *****************************************************************************}
  65. {$ifdef FPC_USE_LIBC}
  66. Function fpReadLink(name,linkname:pchar;maxlen:cint):cint; cdecl; external name 'readlink';
  67. function fpgetcwd(buf:pchar;_size:size_t):pchar; cdecl; external name 'getcwd';
  68. {$endif}
  69. {$I ossysc.inc} // base syscalls
  70. {$I osmain.inc} // base wrappers *nix RTL (derivatives)
  71. {*****************************************************************************
  72. OS Memory allocation / deallocation
  73. ****************************************************************************}
  74. function SysOSAlloc(size: ptrint): pointer;
  75. begin
  76. result := sbrk(size);
  77. end;
  78. {$define HAS_SYSOSFREE}
  79. procedure SysOSFree(p: pointer; size: ptrint);
  80. begin
  81. fpmunmap(p, size);
  82. end;
  83. { more OS independant parts}
  84. {$I text.inc}
  85. {$I heap.inc}
  86. {*****************************************************************************
  87. UnTyped File Handling
  88. *****************************************************************************}
  89. {$i file.inc}
  90. {*****************************************************************************
  91. Typed File Handling
  92. *****************************************************************************}
  93. {$i typefile.inc}
  94. procedure SysInitStdIO;
  95. begin
  96. OpenStdIO(Input,fmInput,StdInputHandle);
  97. OpenStdIO(Output,fmOutput,StdOutputHandle);
  98. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  99. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  100. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  101. end;
  102. procedure SysInitExecPath;
  103. var
  104. i : longint;
  105. begin
  106. execpathstr[0]:=#0;
  107. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  108. { it must also be an absolute filename, linux 2.0 points to a memory
  109. location so this will skip that }
  110. if (i>0) and (execpathstr[1]='/') then
  111. execpathstr[0]:=char(i);
  112. end;
  113. function GetProcessID: SizeUInt;
  114. begin
  115. GetProcessID := SizeUInt (fpGetPID);
  116. end;
  117. Begin
  118. IsConsole := TRUE;
  119. IsLibrary := FALSE;
  120. StackLength := InitialStkLen;
  121. StackBottom := Sptr - StackLength;
  122. { Set up signals handlers }
  123. InstallSignals;
  124. { Setup heap }
  125. InitHeap;
  126. SysInitExceptions;
  127. { Arguments }
  128. SetupCmdLine;
  129. SysInitExecPath;
  130. { Setup stdin, stdout and stderr }
  131. SysInitStdIO;
  132. { Reset IO Error }
  133. InOutRes:=0;
  134. (* This should be changed to a real value during *)
  135. (* thread driver initialization if appropriate. *)
  136. ThreadID := 1;
  137. {$ifdef HASVARIANT}
  138. initvariantmanager;
  139. {$endif HASVARIANT}
  140. End.
  141. {
  142. $Log$
  143. Revision 1.20 2004-12-05 14:36:37 hajny
  144. + GetProcessID added
  145. Revision 1.19 2004/11/04 09:32:31 peter
  146. ErrOutput added
  147. Revision 1.18 2004/07/09 22:31:22 peter
  148. * fixed execpathstr setting
  149. Revision 1.17 2004/07/08 21:22:15 daniel
  150. * Tweaking...
  151. Revision 1.16 2004/07/08 19:45:42 daniel
  152. * Use /proc/self/exe instead of /proc/[getpid]/exe
  153. Revision 1.15 2004/06/17 16:16:13 peter
  154. * New heapmanager that releases memory back to the OS, donated
  155. by Micha Nelissen
  156. Revision 1.14 2004/01/20 23:09:14 hajny
  157. * ExecuteProcess fixes, ProcessID and ThreadID added
  158. Revision 1.13 2004/01/01 14:16:55 marco
  159. * getcwd missed cdecl
  160. Revision 1.12 2003/12/31 20:20:57 marco
  161. * small additions for getcwd what we need for FPC_USE_LIBC
  162. Revision 1.11 2003/12/30 16:26:10 marco
  163. * some more fixes. Testing on idefix
  164. Revision 1.10 2003/12/30 15:43:20 marco
  165. * linux now compiles with FPC_USE_LIBC
  166. Revision 1.9 2003/12/30 12:36:56 marco
  167. * FPC_USE_LIBC
  168. Revision 1.8 2003/09/14 20:15:01 marco
  169. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  170. Revision 1.7 2003/04/30 22:11:06 florian
  171. + for a lot of x86-64 dependend files mostly dummies added
  172. Revision 1.6 2002/12/27 18:36:16 peter
  173. * Setup ExecPathStr for ParamStr(0)
  174. Revision 1.5 2002/12/18 20:42:29 peter
  175. * initial stacklen setup
  176. Revision 1.3 2002/12/18 16:44:09 marco
  177. * more new RTL
  178. Revision 1.7 2002/11/12 14:57:48 marco
  179. * Ugly hack to temporarily be able to use system.pp for Linux too
  180. Revision 1.6 2002/10/27 11:58:30 marco
  181. * Modifications from Saturday.
  182. Revision 1.5 2002/10/26 18:27:51 marco
  183. * First series POSIX calls commits. Including getcwd.
  184. Revision 1.4 2002/10/18 12:19:58 marco
  185. * Fixes to get the generic *BSD RTL compiling again + fixes for thread
  186. support. Still problems left in fexpand. (inoutres?) Therefore fixed
  187. sysposix not yet commited
  188. Revision 1.3 2002/10/13 09:25:39 florian
  189. + call to initvariantmanager inserted
  190. Revision 1.2 2002/09/07 16:01:17 peter
  191. * old logs removed and tabs fixed
  192. Revision 1.1 2002/08/19 12:29:11 marco
  193. * First working POSIX *BSD system unit.
  194. }