osmain.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. POSIX Interface to the system unit
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This is the core of the system unit *nix systems (now FreeBSD
  8. and Unix).
  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. {*****************************************************************************
  14. Misc. System Dependent Functions
  15. *****************************************************************************}
  16. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  17. procedure System_exit;
  18. begin
  19. haltproc(ExitCode);
  20. End;
  21. Function ParamCount: Longint;
  22. Begin
  23. Paramcount:=argc-1
  24. End;
  25. function BackPos(c:char; const s: shortstring): integer;
  26. var
  27. i: integer;
  28. Begin
  29. for i:=length(s) downto 0 do
  30. if s[i] = c then break;
  31. if i=0 then
  32. BackPos := 0
  33. else
  34. BackPos := i;
  35. end;
  36. { variable where full path and filename and executable is stored }
  37. { is setup by the startup of the system unit. }
  38. var
  39. execpathstr : shortstring;
  40. function paramstr(l: longint) : string;
  41. begin
  42. { stricly conforming POSIX applications }
  43. { have the executing filename as argv[0] }
  44. if l=0 then
  45. begin
  46. paramstr := execpathstr;
  47. end
  48. else
  49. paramstr:=strpas(argv[l]);
  50. end;
  51. Procedure Randomize;
  52. Begin
  53. randseed:=longint(Fptime(nil));
  54. End;
  55. {*****************************************************************************
  56. Low Level File Routines
  57. *****************************************************************************}
  58. {
  59. The lowlevel file functions should take care of setting the InOutRes to the
  60. correct value if an error has occured, else leave it untouched
  61. }
  62. Function PosixToRunError (PosixErrno : longint) : longint;
  63. {
  64. Convert ErrNo error to the correct Inoutres value
  65. }
  66. begin
  67. if PosixErrNo=0 then { Else it will go through all the cases }
  68. exit(0);
  69. case PosixErrNo of
  70. ESysENFILE,
  71. ESysEMFILE : Inoutres:=4;
  72. ESysENOENT : Inoutres:=2;
  73. ESysEBADF : Inoutres:=6;
  74. ESysENOMEM,
  75. ESysEFAULT : Inoutres:=217;
  76. ESysEINVAL : Inoutres:=218;
  77. ESysEPIPE,
  78. ESysEINTR,
  79. ESysEIO,
  80. ESysEAGAIN,
  81. ESysENOSPC : Inoutres:=101;
  82. ESysENAMETOOLONG : Inoutres := 3;
  83. ESysEROFS,
  84. ESysEEXIST,
  85. ESysENOTEMPTY,
  86. ESysEACCES : Inoutres:=5;
  87. ESysEISDIR : InOutRes:=5;
  88. else
  89. begin
  90. InOutRes := Integer(PosixErrno);
  91. end;
  92. end;
  93. PosixToRunError:=InOutRes;
  94. end;
  95. Function Errno2InoutRes : longint;
  96. begin
  97. Errno2InoutRes:=PosixToRunError(getErrno);
  98. InoutRes:=Errno2InoutRes;
  99. end;
  100. {*****************************************************************************
  101. SystemUnit Initialization
  102. *****************************************************************************}
  103. // signal handler is arch dependant due to processorexception to language
  104. // exception translation
  105. {$i sighnd.inc}
  106. var
  107. act: SigActionRec;
  108. Procedure InstallSignals;
  109. begin
  110. { Initialize the sigaction structure }
  111. { all flags and information set to zero }
  112. FillChar(act, sizeof(SigActionRec),0);
  113. { initialize handler }
  114. act.sa_handler := SigActionHandler(@SignalToRunError);
  115. act.sa_flags:=SA_SIGINFO
  116. {$ifdef cpux86_64}
  117. or $4000000
  118. {$endif cpux86_64}
  119. ;
  120. FpSigAction(SIGFPE,@act,nil);
  121. FpSigAction(SIGSEGV,@act,nil);
  122. FpSigAction(SIGBUS,@act,nil);
  123. FpSigAction(SIGILL,@act,nil);
  124. end;
  125. procedure SetupCmdLine;
  126. var
  127. bufsize,
  128. len,j,
  129. size,i : longint;
  130. found : boolean;
  131. buf : pchar;
  132. procedure AddBuf;
  133. begin
  134. reallocmem(cmdline,size+bufsize);
  135. move(buf^,cmdline[size],bufsize);
  136. inc(size,bufsize);
  137. bufsize:=0;
  138. end;
  139. begin
  140. GetMem(buf,ARG_MAX);
  141. size:=0;
  142. bufsize:=0;
  143. i:=0;
  144. while (i<argc) do
  145. begin
  146. len:=strlen(argv[i]);
  147. if len>ARG_MAX-2 then
  148. len:=ARG_MAX-2;
  149. found:=false;
  150. for j:=1 to len do
  151. if argv[i][j]=' ' then
  152. begin
  153. found:=true;
  154. break;
  155. end;
  156. if bufsize+len>=ARG_MAX-2 then
  157. AddBuf;
  158. if found then
  159. begin
  160. buf[bufsize]:='"';
  161. inc(bufsize);
  162. end;
  163. move(argv[i]^,buf[bufsize],len);
  164. inc(bufsize,len);
  165. if found then
  166. begin
  167. buf[bufsize]:='"';
  168. inc(bufsize);
  169. end;
  170. if i<argc then
  171. buf[bufsize]:=' '
  172. else
  173. buf[bufsize]:=#0;
  174. inc(bufsize);
  175. inc(i);
  176. end;
  177. AddBuf;
  178. FreeMem(buf,ARG_MAX);
  179. end;
  180. {
  181. $Log$
  182. Revision 1.27 2005-02-06 13:06:20 peter
  183. * moved file and dir functions to sysfile/sysdir
  184. * win32 thread in systemunit
  185. Revision 1.26 2005/02/05 22:53:43 peter
  186. * use typecasted sigactionhandler, needed for arm
  187. Revision 1.25 2005/02/03 21:42:17 peter
  188. * readded magic value $4000000 for sa_flags for x86_64
  189. Revision 1.24 2005/01/31 20:13:24 peter
  190. * rt_sigaction for all cpus
  191. Revision 1.23 2005/01/30 18:01:15 peter
  192. * signal cleanup for linux
  193. * sigactionhandler instead of tsigaction for bsds
  194. * sigcontext moved to cpu dir
  195. Revision 1.22 2004/11/02 14:49:48 florian
  196. * fixed baseunix.signal for CPU using rt_sigaction
  197. * fixed it for x86_64 too
  198. Revision 1.21 2004/10/25 15:38:59 peter
  199. * compiler defined HEAP and HEAPSIZE removed
  200. Revision 1.20 2004/08/04 19:27:09 florian
  201. * fixed floating point and integer exception handling on sparc/linux
  202. Revision 1.19 2004/05/31 20:25:04 peter
  203. * removed warnings
  204. Revision 1.18 2004/05/16 18:51:20 peter
  205. * use thandle in do_*
  206. Revision 1.17 2004/05/01 15:59:17 florian
  207. * x86_64 exception handling fixed
  208. Revision 1.16 2004/04/27 20:47:00 florian
  209. * tried to fix x86-64 signal handling
  210. Revision 1.15 2004/04/22 21:16:35 peter
  211. * do_write/do_read fix
  212. Revision 1.14 2004/03/27 14:33:45 florian
  213. * tell sigaction to pass siginfo on arm
  214. Revision 1.13 2004/03/10 20:35:33 peter
  215. * call _haltproc instead of exit(). This is required for libc linking
  216. Revision 1.12 2004/01/01 14:19:55 marco
  217. * use_getcwd updates because FPC_USE_LIBC uses that
  218. Revision 1.11 2003/12/30 16:26:10 marco
  219. * some more fixes. Testing on idefix
  220. Revision 1.10 2003/12/21 20:30:49 peter
  221. * don't exit in getdir when fpstat gives a failure
  222. Revision 1.9 2003/12/14 14:28:36 peter
  223. * only check errno if the syscall failed
  224. Revision 1.8 2003/11/01 01:58:11 marco
  225. * more small fixes.
  226. Revision 1.7 2003/10/31 20:36:01 marco
  227. * i386 specific fixes that hopefully fix texception4.
  228. Only the "generic" signal handler was ported to the unix rtl.
  229. Revision 1.6 2003/09/27 12:51:33 peter
  230. * fpISxxx macros renamed to C compliant fpS_ISxxx
  231. Revision 1.5 2003/05/01 08:05:23 florian
  232. * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
  233. Revision 1.4 2002/12/24 19:45:40 peter
  234. * Fix do_erase which was wrong with inoutres setting
  235. Revision 1.3 2002/12/23 22:23:43 peter
  236. * fixed Getdir to not set Inoutres
  237. * broken symlinks are now ignored in getdir instead of aborting
  238. the search
  239. Revision 1.2 2002/12/18 20:43:27 peter
  240. * removed stackcheck, the generic stackcheck is used
  241. * fixed return value for error conversion when no error was passed
  242. Revision 1.1 2002/12/18 16:43:26 marco
  243. * new unix rtl, linux part.....
  244. Revision 1.7 2002/11/14 12:18:03 marco
  245. * fixed Fptime call to (NIL)
  246. Revision 1.6 2002/10/27 17:21:29 marco
  247. * Only "difficult" functions + execvp + termios + rewinddir left to do
  248. Revision 1.5 2002/10/26 18:27:52 marco
  249. * First series POSIX calls commits. Including getcwd.
  250. Revision 1.4 2002/09/07 16:01:26 peter
  251. * old logs removed and tabs fixed
  252. Revision 1.3 2002/08/20 12:50:22 marco
  253. * New errno handling. Should be libc compatible.
  254. Revision 1.2 2002/08/10 13:42:36 marco
  255. * Fixes Posix dir copied to devel branch
  256. Revision 1.1.2.18 2002/03/10 11:45:02 carl
  257. * InOutRes := 16 with rmdir()
  258. * InOutRes := 5 more checking
  259. Revision 1.1.2.17 2002/03/03 15:11:51 carl
  260. * erase() bugfix (erasing a directory is done via rmdir() only!)
  261. Revision 1.1.2.16 2002/02/15 18:13:35 carl
  262. * bugfix for paramstr(0)
  263. }