system.pp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 FPC_IS_SYSTEM}
  20. {$i osdefs.inc}
  21. {$I sysunixh.inc}
  22. Implementation
  23. {$I system.inc}
  24. {*****************************************************************************
  25. Misc. System Dependent Functions
  26. *****************************************************************************}
  27. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  28. procedure System_exit;
  29. begin
  30. haltproc(ExitCode);
  31. End;
  32. Function ParamCount: Longint;
  33. Begin
  34. Paramcount:=argc-1
  35. End;
  36. function BackPos(c:char; const s: shortstring): integer;
  37. var
  38. i: integer;
  39. Begin
  40. for i:=length(s) downto 0 do
  41. if s[i] = c then break;
  42. if i=0 then
  43. BackPos := 0
  44. else
  45. BackPos := i;
  46. end;
  47. { variable where full path and filename and executable is stored }
  48. { is setup by the startup of the system unit. }
  49. var
  50. execpathstr : shortstring;
  51. function paramstr(l: longint) : string;
  52. begin
  53. { stricly conforming POSIX applications }
  54. { have the executing filename as argv[0] }
  55. if l=0 then
  56. begin
  57. paramstr := execpathstr;
  58. end
  59. else
  60. paramstr:=strpas(argv[l]);
  61. end;
  62. Procedure Randomize;
  63. Begin
  64. randseed:=longint(Fptime(nil));
  65. End;
  66. {*****************************************************************************
  67. SystemUnit Initialization
  68. *****************************************************************************}
  69. // signal handler is arch dependant due to processorexception to language
  70. // exception translation
  71. {$i sighnd.inc}
  72. var
  73. act: SigActionRec;
  74. Procedure InstallSignals;
  75. begin
  76. { Initialize the sigaction structure }
  77. { all flags and information set to zero }
  78. FillChar(act, sizeof(SigActionRec),0);
  79. { initialize handler }
  80. act.sa_handler := SigActionHandler(@SignalToRunError);
  81. act.sa_flags:=SA_SIGINFO
  82. {$ifdef cpux86_64}
  83. or $4000000
  84. {$endif cpux86_64}
  85. ;
  86. FpSigAction(SIGFPE,@act,nil);
  87. FpSigAction(SIGSEGV,@act,nil);
  88. FpSigAction(SIGBUS,@act,nil);
  89. FpSigAction(SIGILL,@act,nil);
  90. end;
  91. procedure SetupCmdLine;
  92. var
  93. bufsize,
  94. len,j,
  95. size,i : longint;
  96. found : boolean;
  97. buf : pchar;
  98. procedure AddBuf;
  99. begin
  100. reallocmem(cmdline,size+bufsize);
  101. move(buf^,cmdline[size],bufsize);
  102. inc(size,bufsize);
  103. bufsize:=0;
  104. end;
  105. begin
  106. GetMem(buf,ARG_MAX);
  107. size:=0;
  108. bufsize:=0;
  109. i:=0;
  110. while (i<argc) do
  111. begin
  112. len:=strlen(argv[i]);
  113. if len>ARG_MAX-2 then
  114. len:=ARG_MAX-2;
  115. found:=false;
  116. for j:=1 to len do
  117. if argv[i][j]=' ' then
  118. begin
  119. found:=true;
  120. break;
  121. end;
  122. if bufsize+len>=ARG_MAX-2 then
  123. AddBuf;
  124. if found then
  125. begin
  126. buf[bufsize]:='"';
  127. inc(bufsize);
  128. end;
  129. move(argv[i]^,buf[bufsize],len);
  130. inc(bufsize,len);
  131. if found then
  132. begin
  133. buf[bufsize]:='"';
  134. inc(bufsize);
  135. end;
  136. if i<argc then
  137. buf[bufsize]:=' '
  138. else
  139. buf[bufsize]:=#0;
  140. inc(bufsize);
  141. inc(i);
  142. end;
  143. AddBuf;
  144. FreeMem(buf,ARG_MAX);
  145. end;
  146. procedure SysInitStdIO;
  147. begin
  148. OpenStdIO(Input,fmInput,StdInputHandle);
  149. OpenStdIO(Output,fmOutput,StdOutputHandle);
  150. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  151. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  152. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  153. end;
  154. procedure SysInitExecPath;
  155. var
  156. i : longint;
  157. begin
  158. execpathstr[0]:=#0;
  159. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  160. { it must also be an absolute filename, linux 2.0 points to a memory
  161. location so this will skip that }
  162. if (i>0) and (execpathstr[1]='/') then
  163. execpathstr[0]:=char(i);
  164. end;
  165. function GetProcessID: SizeUInt;
  166. begin
  167. GetProcessID := SizeUInt (fpGetPID);
  168. end;
  169. Begin
  170. IsConsole := TRUE;
  171. IsLibrary := FALSE;
  172. StackLength := InitialStkLen;
  173. StackBottom := Sptr - StackLength;
  174. { Set up signals handlers }
  175. InstallSignals;
  176. { Setup heap }
  177. InitHeap;
  178. SysInitExceptions;
  179. { Arguments }
  180. SetupCmdLine;
  181. SysInitExecPath;
  182. { Setup stdin, stdout and stderr }
  183. SysInitStdIO;
  184. { Reset IO Error }
  185. InOutRes:=0;
  186. { threading }
  187. InitSystemThreads;
  188. {$ifdef HASVARIANT}
  189. initvariantmanager;
  190. {$endif HASVARIANT}
  191. {$ifdef HASWIDESTRING}
  192. initwidestringmanager;
  193. {$endif HASWIDESTRING}
  194. End.
  195. {
  196. $Log$
  197. Revision 1.24 2005-02-14 17:13:30 peter
  198. * truncate log
  199. Revision 1.23 2005/02/13 21:47:56 peter
  200. * include file cleanup part 2
  201. Revision 1.22 2005/02/06 11:20:52 peter
  202. * threading in system unit
  203. * removed systhrds unit
  204. Revision 1.21 2005/02/01 20:22:49 florian
  205. * improved widestring infrastructure manager
  206. }