osmain.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Main OS dependant body of the system unit, loosely modelled
  5. after POSIX. *BSD version (Linux version is near identical)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {*****************************************************************************
  13. Misc. System Dependent Functions
  14. *****************************************************************************}
  15. procedure System_exit;
  16. begin
  17. Fpexit(cint(ExitCode));
  18. End;
  19. Function ParamCount: Longint;
  20. Begin
  21. Paramcount:=argc-1
  22. End;
  23. function BackPos(c:char; const s: shortstring): integer;
  24. var
  25. i: integer;
  26. Begin
  27. for i:=length(s) downto 0 do
  28. if s[i] = c then break;
  29. if i=0 then
  30. BackPos := 0
  31. else
  32. BackPos := i;
  33. end;
  34. { variable where full path and filename and executable is stored }
  35. { is setup by the startup of the system unit. }
  36. var
  37. execpathstr : shortstring;
  38. function paramstr(l: longint) : string;
  39. var
  40. s: string;
  41. s1: string;
  42. begin
  43. { stricly conforming POSIX applications }
  44. { have the executing filename as argv[0] }
  45. // if l=0 then
  46. // begin
  47. // paramstr := execpathstr;
  48. // end
  49. // else
  50. paramstr:=strpas(argv[l]);
  51. end;
  52. Procedure Randomize;
  53. Begin
  54. randseed:=longint(Fptime(nil));
  55. End;
  56. {*****************************************************************************
  57. Low Level File Routines
  58. *****************************************************************************}
  59. {
  60. The lowlevel file functions should take care of setting the InOutRes to the
  61. correct value if an error has occured, else leave it untouched
  62. }
  63. Function PosixToRunError (PosixErrno : longint) : longint;
  64. {
  65. Convert ErrNo error to the correct Inoutres value
  66. }
  67. begin
  68. if PosixErrNo=0 then { Else it will go through all the cases }
  69. exit(0);
  70. case PosixErrNo of
  71. ESysENFILE,
  72. ESysEMFILE : Inoutres:=4;
  73. ESysENOENT : Inoutres:=2;
  74. ESysEBADF : Inoutres:=6;
  75. ESysENOMEM,
  76. ESysEFAULT : Inoutres:=217;
  77. ESysEINVAL : Inoutres:=218;
  78. ESysEPIPE,
  79. ESysEINTR,
  80. ESysEIO,
  81. ESysEAGAIN,
  82. ESysENOSPC : Inoutres:=101;
  83. ESysENAMETOOLONG : Inoutres := 3;
  84. ESysEROFS,
  85. ESysEEXIST,
  86. ESysENOTEMPTY,
  87. ESysEACCES : Inoutres:=5;
  88. ESysEISDIR : InOutRes:=5;
  89. else
  90. begin
  91. InOutRes := Integer(PosixErrno);
  92. end;
  93. end;
  94. PosixToRunError:=InOutRes;
  95. end;
  96. Function Errno2InoutRes : longint;
  97. begin
  98. Errno2InoutRes:=PosixToRunError(getErrno);
  99. InoutRes:=Errno2InoutRes;
  100. end;
  101. {*****************************************************************************
  102. SystemUnit Initialization
  103. *****************************************************************************}
  104. function reenable_signal(sig : longint) : boolean;
  105. var
  106. e,oe : TSigSet;
  107. i,j : byte;
  108. begin
  109. fillchar(e,sizeof(e),#0);
  110. fillchar(oe,sizeof(oe),#0);
  111. { set is 1 based PM }
  112. dec(sig);
  113. i:=sig mod 32;
  114. j:=sig div 32;
  115. e[j]:=1 shl i;
  116. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  117. reenable_signal:=geterrno=0;
  118. end;
  119. {$i sighnd.inc}
  120. var
  121. act: SigActionRec;
  122. Procedure InstallSignals;
  123. var
  124. oldact: SigActionRec;
  125. begin
  126. { Initialize the sigaction structure }
  127. { all flags and information set to zero }
  128. FillChar(act, sizeof(SigActionRec),0);
  129. { initialize handler }
  130. act.sa_handler :=@SignalToRunError;
  131. act.sa_flags:=SA_SIGINFO;
  132. FpSigAction(SIGFPE,act,oldact);
  133. FpSigAction(SIGSEGV,act,oldact);
  134. FpSigAction(SIGBUS,act,oldact);
  135. FpSigAction(SIGILL,act,oldact);
  136. end;
  137. procedure SetupCmdLine;
  138. var
  139. bufsize,
  140. len,j,
  141. size,i : longint;
  142. found : boolean;
  143. buf : pchar;
  144. procedure AddBuf;
  145. begin
  146. reallocmem(cmdline,size+bufsize);
  147. move(buf^,cmdline[size],bufsize);
  148. inc(size,bufsize);
  149. bufsize:=0;
  150. end;
  151. begin
  152. GetMem(buf,ARG_MAX);
  153. size:=0;
  154. bufsize:=0;
  155. i:=0;
  156. while (i<argc) do
  157. begin
  158. len:=strlen(argv[i]);
  159. if len>ARG_MAX-2 then
  160. len:=ARG_MAX-2;
  161. found:=false;
  162. for j:=1 to len do
  163. if argv[i][j]=' ' then
  164. begin
  165. found:=true;
  166. break;
  167. end;
  168. if bufsize+len>=ARG_MAX-2 then
  169. AddBuf;
  170. if found then
  171. begin
  172. buf[bufsize]:='"';
  173. inc(bufsize);
  174. end;
  175. move(argv[i]^,buf[bufsize],len);
  176. inc(bufsize,len);
  177. if found then
  178. begin
  179. buf[bufsize]:='"';
  180. inc(bufsize);
  181. end;
  182. if i<argc then
  183. buf[bufsize]:=' '
  184. else
  185. buf[bufsize]:=#0;
  186. inc(bufsize);
  187. inc(i);
  188. end;
  189. AddBuf;
  190. FreeMem(buf,ARG_MAX);
  191. end;
  192. {
  193. $Log$
  194. Revision 1.17 2005-02-06 13:06:20 peter
  195. * moved file and dir functions to sysfile/sysdir
  196. * win32 thread in systemunit
  197. Revision 1.16 2004/10/25 15:38:59 peter
  198. * compiler defined HEAP and HEAPSIZE removed
  199. Revision 1.15 2004/07/17 15:20:55 jonas
  200. * don't use O_CREATE when opening a file for appending (fixes tw1744)
  201. Revision 1.14 2004/05/16 18:51:20 peter
  202. * use thandle in do_*
  203. Revision 1.13 2004/04/22 21:10:56 peter
  204. * do_read/do_write addr argument changed to pointer
  205. Revision 1.12 2004/01/06 15:42:05 marco
  206. * o_creat added when o_append
  207. Revision 1.11 2004/01/03 14:56:10 marco
  208. * typo fix
  209. Revision 1.10 2004/01/03 12:35:39 marco
  210. * sighnd to separate file, like linux. Some comments removed
  211. Revision 1.9 2003/12/30 12:26:21 marco
  212. * FPC_USE_LIBC
  213. Revision 1.8 2003/12/21 20:31:50 peter
  214. * fix getdir when directory contains files that give EACCESS
  215. Revision 1.7 2003/12/14 14:47:02 marco
  216. * fix for repeating 'x' bug
  217. Revision 1.6 2003/11/18 10:12:25 marco
  218. * Small fixes for EAGAIN. bunxfunc only has comments added.
  219. Revision 1.5 2003/10/27 17:12:45 marco
  220. * fixes for signal handling.
  221. Revision 1.4 2003/10/26 17:01:04 marco
  222. * moved sigprocmask to system
  223. Revision 1.3 2003/09/27 13:04:58 peter
  224. * fpISxxx renamed
  225. Revision 1.2 2003/05/29 20:54:09 marco
  226. * progname fix.
  227. Revision 1.1 2003/01/05 19:01:28 marco
  228. * FreeBSD compiles now with baseunix mods.
  229. }