sysunix.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. member of the Free Pascal development team.
  6. This is the core of the system unit *nix systems (now FreeBSD
  7. and Unix).
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. { These things are set in the makefile, }
  15. { But you can override them here.}
  16. { If you use an aout system, set the conditional AOUT}
  17. { $Define AOUT}
  18. {$I system.inc}
  19. { used in syscall to report errors.}
  20. var
  21. Errno : longint;
  22. { Include constant and type definitions }
  23. {$i errno.inc } { Error numbers }
  24. {$i sysnr.inc } { System call numbers }
  25. {$i sysconst.inc } { Miscellaneous constants }
  26. {$i systypes.inc } { Types needed for system calls }
  27. { Read actual system call definitions. }
  28. {$i signal.inc}
  29. {$i syscalls.inc }
  30. {*****************************************************************************
  31. Misc. System Dependent Functions
  32. *****************************************************************************}
  33. procedure prthaltproc;external name '_haltproc';
  34. Procedure System_exit;
  35. Begin
  36. prthaltproc;
  37. End;
  38. Function ParamCount: Longint;
  39. Begin
  40. Paramcount:=argc-1;
  41. End;
  42. Function ParamStr(l: Longint): String;
  43. var
  44. link,
  45. hs : string;
  46. i : longint;
  47. begin
  48. if l=0 then
  49. begin
  50. str(sys_getpid,hs);
  51. {$ifdef FreeBSD}
  52. hs:='/proc/'+hs+'/file'#0;
  53. {$else}
  54. hs:='/proc/'+hs+'/exe'#0;
  55. {$endif}
  56. i:=Sys_readlink(@hs[1],@link[1],high(link));
  57. { it must also be an absolute filename, linux 2.0 points to a memory
  58. location so this will skip that }
  59. if (i>0) and (link[1]='/') then
  60. begin
  61. link[0]:=chr(i);
  62. paramstr:=link;
  63. end
  64. else
  65. paramstr:=strpas(argv[0]);
  66. end
  67. else
  68. if (l>0) and (l<argc) then
  69. paramstr:=strpas(argv[l])
  70. else
  71. paramstr:='';
  72. end;
  73. Procedure Randomize;
  74. Begin
  75. randseed:=sys_time;
  76. End;
  77. {*****************************************************************************
  78. Heap Management
  79. *****************************************************************************}
  80. var
  81. _HEAP : pointer;external name 'HEAP';
  82. _HEAPSIZE : longint;external name 'HEAPSIZE';
  83. function getheapstart:pointer;assembler;
  84. {$undef fpc_getheapstart_ok}
  85. {$ifdef cpui386}
  86. {$define fpc_getheapstart_ok}
  87. asm
  88. leal _HEAP,%eax
  89. end ['EAX'];
  90. {$endif cpui386}
  91. {$ifdef cpum68k}
  92. {$define fpc_getheapstart_ok}
  93. asm
  94. lea.l _HEAP,a0
  95. move.l a0,d0
  96. end['A0','D0'];
  97. {$endif cpum68k}
  98. {$ifdef cpupowerpc}
  99. {$define fpc_getheapstart_ok}
  100. asm
  101. lis r3,HEAP@ha
  102. la r3,HEAP@l(r3)
  103. end['R3'];
  104. {$endif cpupowerpc}
  105. {$ifndef fpc_getheapstart_ok}
  106. asm
  107. end;
  108. {$error Getheapstart code is not implemented }
  109. {$endif not fpc_getheapstart_ok}
  110. function getheapsize:longint;assembler;
  111. {$undef fpc_getheapsize_ok}
  112. {$ifdef cpui386}
  113. {$define fpc_getheapsize_ok}
  114. asm
  115. movl _HEAPSIZE,%eax
  116. end ['EAX'];
  117. {$endif cpui386}
  118. {$ifdef cpum68k}
  119. {$define fpc_getheapsize_ok}
  120. asm
  121. move.l _HEAPSIZE,d0
  122. end ['D0'];
  123. {$endif cpum68k}
  124. {$ifdef cpupowerpc}
  125. {$define fpc_getheapsize_ok}
  126. asm
  127. lis r9,HEAPSIZE@ha
  128. lwz r3,HEAPSIZE@l(r9)
  129. end ['R0','R9'];
  130. {$endif cpupowerpc}
  131. {$ifndef fpc_getheapsize_ok}
  132. asm
  133. end;
  134. {$error Getheapsize code is not implemented }
  135. {$endif not fpc_getheapsize_ok}
  136. Function sbrk(size : longint) : pointer;
  137. var address:longint;
  138. begin
  139. address:=Sys_mmap(0,Size,3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0);
  140. if addres=-1 then inc(address) else errno := 0;
  141. sbrk:=pointer(address);
  142. end;
  143. { include standard heap management }
  144. {$I heap.inc}
  145. {*****************************************************************************
  146. Low Level File Routines
  147. *****************************************************************************}
  148. {
  149. The lowlevel file functions should take care of setting the InOutRes to the
  150. correct value if an error has occured, else leave it untouched
  151. }
  152. Procedure Errno2Inoutres;
  153. {
  154. Convert ErrNo error to the correct Inoutres value
  155. }
  156. begin
  157. if ErrNo=0 then { Else it will go through all the cases }
  158. exit;
  159. If errno<0 then Errno:=-errno;
  160. case ErrNo of
  161. Sys_ENFILE,
  162. Sys_EMFILE : Inoutres:=4;
  163. Sys_ENOENT : Inoutres:=2;
  164. Sys_EBADF : Inoutres:=6;
  165. Sys_ENOMEM,
  166. Sys_EFAULT : Inoutres:=217;
  167. Sys_EINVAL : Inoutres:=218;
  168. Sys_EPIPE,
  169. Sys_EINTR,
  170. Sys_EIO,
  171. Sys_EAGAIN,
  172. Sys_ENOSPC : Inoutres:=101;
  173. Sys_ENAMETOOLONG,
  174. Sys_ELOOP,
  175. Sys_ENOTDIR : Inoutres:=3;
  176. Sys_EROFS,
  177. Sys_EEXIST,
  178. Sys_EISDIR,
  179. Sys_ENOTEMPTY,
  180. Sys_EACCES,
  181. Sys_ETXTBSY : Inoutres:=5;
  182. else
  183. InOutRes := Integer(Errno);
  184. end;
  185. end;
  186. Procedure Do_Close(Handle:Longint);
  187. Begin
  188. sys_close(Handle);
  189. {Errno2Inoutres;}
  190. End;
  191. Procedure Do_Erase(p:pchar);
  192. {$ifdef BSD}
  193. var FileInfo : Stat;
  194. {$endif}
  195. Begin
  196. {$ifdef BSD} {or POSIX}
  197. { verify if the filename is actually a directory }
  198. { if so return error and do nothing, as defined }
  199. { by POSIX }
  200. if sys_stat(p,fileinfo)<0 then
  201. begin
  202. Errno2Inoutres;
  203. exit;
  204. end;
  205. {$ifdef BSD}
  206. if (fileinfo.mode and STAT_IFMT)=STAT_IFDIR then
  207. {$else}
  208. if s_ISDIR(fileinfo.st_mode) then
  209. {$endif}
  210. begin
  211. InOutRes := 2;
  212. exit;
  213. end;
  214. {$endif}
  215. sys_unlink(p);
  216. Errno2Inoutres;
  217. {$ifdef Linux}
  218. { tp compatible result }
  219. if (Errno=Sys_EISDIR) then
  220. InOutRes:=2;
  221. {$endif}
  222. End;
  223. Procedure Do_Rename(p1,p2:pchar);
  224. Begin
  225. sys_rename(p1,p2);
  226. Errno2Inoutres;
  227. End;
  228. Function Do_Write(Handle,Addr,Len:Longint):longint;
  229. Begin
  230. repeat
  231. Do_Write:=sys_write(Handle,pchar(addr),len);
  232. until ErrNo<>Sys_EINTR;
  233. Errno2Inoutres;
  234. if Do_Write<0 then
  235. Do_Write:=0;
  236. End;
  237. Function Do_Read(Handle,Addr,Len:Longint):Longint;
  238. Begin
  239. repeat
  240. Do_Read:=sys_read(Handle,pchar(addr),len);
  241. until ErrNo<>Sys_EINTR;
  242. Errno2Inoutres;
  243. if Do_Read<0 then
  244. Do_Read:=0;
  245. End;
  246. {Function Do_Write(Handle,Addr,Len:Longint):longint;
  247. var
  248. total,
  249. res : longint;
  250. Begin
  251. total:=0;
  252. repeat
  253. res:=sys_write(Handle,pchar(pchar(addr)+total),len-total);
  254. if res>0 then
  255. inc(total,res);
  256. until ErrNo<>Sys_EINTR;
  257. Errno2Inoutres;
  258. if res<0 then
  259. Do_Write:=0
  260. else
  261. Do_Write:=total;
  262. End;
  263. Function Do_Read(Handle,Addr,Len:Longint):Longint;
  264. var
  265. total,
  266. res : longint;
  267. Begin
  268. total:=0;
  269. repeat
  270. res:=sys_read(Handle,pchar(pchar(addr)+total),len-total);
  271. if res>0 then
  272. inc(total,res);
  273. until ErrNo<>Sys_EINTR;
  274. Errno2Inoutres;
  275. if res<0 then
  276. Do_Read:=0
  277. else
  278. Do_Read:=total;
  279. End;
  280. }
  281. Function Do_FilePos(Handle: Longint): Longint;
  282. Begin
  283. Do_FilePos:=sys_lseek(Handle, 0, Seek_Cur);
  284. Errno2Inoutres;
  285. End;
  286. Procedure Do_Seek(Handle,Pos:Longint);
  287. Begin
  288. sys_lseek(Handle, pos, Seek_set);
  289. errno2inoutres;
  290. End;
  291. Function Do_SeekEnd(Handle:Longint): Longint;
  292. begin
  293. Do_SeekEnd:=sys_lseek(Handle,0,Seek_End);
  294. errno2inoutres;
  295. end;
  296. Function Do_FileSize(Handle:Longint): Longint;
  297. var
  298. Info : Stat;
  299. Begin
  300. if sys_fstat(handle,info)=0 then
  301. Do_FileSize:=Info.Size
  302. else
  303. Do_FileSize:=0;
  304. Errno2Inoutres;
  305. End;
  306. Procedure Do_Truncate(Handle,fPos:longint);
  307. begin
  308. sys_ftruncate(handle,fpos);
  309. Errno2Inoutres;
  310. end;
  311. Procedure Do_Open(var f;p:pchar;flags:longint);
  312. {
  313. FileRec and textrec have both Handle and mode as the first items so
  314. they could use the same routine for opening/creating.
  315. when (flags and $100) the file will be append
  316. when (flags and $1000) the file will be truncate/rewritten
  317. when (flags and $10000) there is no check for close (needed for textfiles)
  318. }
  319. var
  320. oflags : longint;
  321. Begin
  322. { close first if opened }
  323. if ((flags and $10000)=0) then
  324. begin
  325. case FileRec(f).mode of
  326. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  327. fmclosed : ;
  328. else
  329. begin
  330. inoutres:=102; {not assigned}
  331. exit;
  332. end;
  333. end;
  334. end;
  335. { reset file Handle }
  336. FileRec(f).Handle:=UnusedHandle;
  337. { We do the conversion of filemodes here, concentrated on 1 place }
  338. case (flags and 3) of
  339. 0 : begin
  340. oflags :=Open_RDONLY;
  341. FileRec(f).mode:=fminput;
  342. end;
  343. 1 : begin
  344. oflags :=Open_WRONLY;
  345. FileRec(f).mode:=fmoutput;
  346. end;
  347. 2 : begin
  348. oflags :=Open_RDWR;
  349. FileRec(f).mode:=fminout;
  350. end;
  351. end;
  352. if (flags and $1000)=$1000 then
  353. oflags:=oflags or (Open_CREAT or Open_TRUNC)
  354. else
  355. if (flags and $100)=$100 then
  356. oflags:=oflags or (Open_APPEND);
  357. { empty name is special }
  358. if p[0]=#0 then
  359. begin
  360. case FileRec(f).mode of
  361. fminput :
  362. FileRec(f).Handle:=StdInputHandle;
  363. fminout, { this is set by rewrite }
  364. fmoutput :
  365. FileRec(f).Handle:=StdOutputHandle;
  366. fmappend :
  367. begin
  368. FileRec(f).Handle:=StdOutputHandle;
  369. FileRec(f).mode:=fmoutput; {fool fmappend}
  370. end;
  371. end;
  372. exit;
  373. end;
  374. { real open call }
  375. FileRec(f).Handle:=sys_open(p,oflags,438);
  376. if (ErrNo=Sys_EROFS) and ((OFlags and Open_RDWR)<>0) then
  377. begin
  378. Oflags:=Oflags and not(Open_RDWR);
  379. FileRec(f).Handle:=sys_open(p,oflags,438);
  380. end;
  381. Errno2Inoutres;
  382. End;
  383. Function Do_IsDevice(Handle:Longint):boolean;
  384. {
  385. Interface to Unix ioctl call.
  386. Performs various operations on the filedescriptor Handle.
  387. Ndx describes the operation to perform.
  388. Data points to data needed for the Ndx function. The structure of this
  389. data is function-dependent.
  390. }
  391. var
  392. Data : array[0..255] of byte; {Large enough for termios info}
  393. begin
  394. Do_IsDevice:=(sys_ioctl(handle,IOCTL_TCGETS,@data)<>-1);
  395. end;
  396. {*****************************************************************************
  397. UnTyped File Handling
  398. *****************************************************************************}
  399. {$i file.inc}
  400. {*****************************************************************************
  401. Typed File Handling
  402. *****************************************************************************}
  403. {$i typefile.inc}
  404. {*****************************************************************************
  405. Text File Handling
  406. *****************************************************************************}
  407. {$DEFINE SHORT_LINEBREAK}
  408. {$DEFINE EXTENDED_EOF}
  409. {$i text.inc}
  410. {*****************************************************************************
  411. Directory Handling
  412. *****************************************************************************}
  413. Procedure MkDir(Const s: String);[IOCheck];
  414. Var
  415. Buffer: Array[0..255] of Char;
  416. Begin
  417. If (s='') or (InOutRes <> 0) then
  418. exit;
  419. Move(s[1], Buffer, Length(s));
  420. Buffer[Length(s)] := #0;
  421. sys_mkdir(@buffer, 511);
  422. Errno2Inoutres;
  423. End;
  424. Procedure RmDir(Const s: String);[IOCheck];
  425. Var
  426. Buffer: Array[0..255] of Char;
  427. Begin
  428. if (s ='.') then
  429. InOutRes := 16;
  430. If (s='') or (InOutRes <> 0) then
  431. exit;
  432. Move(s[1], Buffer, Length(s));
  433. Buffer[Length(s)] := #0;
  434. sys_rmdir(@buffer);
  435. {$ifdef BSD}
  436. if (Errno=Sys_EINVAL) Then
  437. InOutRes:=5
  438. Else
  439. {$endif}
  440. Errno2Inoutres;
  441. End;
  442. Procedure ChDir(Const s: String);[IOCheck];
  443. Var
  444. Buffer: Array[0..255] of Char;
  445. Begin
  446. If (s='') or (InOutRes <> 0) then
  447. exit;
  448. Move(s[1], Buffer, Length(s));
  449. Buffer[Length(s)] := #0;
  450. sys_chdir(@buffer);
  451. Errno2Inoutres;
  452. { file not exists is path not found under tp7 }
  453. if InOutRes=2 then
  454. InOutRes:=3;
  455. End;
  456. procedure GetDir (DriveNr: byte; var Dir: ShortString);
  457. var
  458. thisdir : stat;
  459. rootino,
  460. thisino,
  461. dotdotino : longint;
  462. rootdev,
  463. thisdev,
  464. dotdotdev : dev_t;
  465. thedir,dummy : string[255];
  466. dirstream : pdir;
  467. d : pdirent;
  468. mountpoint,validdir : boolean;
  469. predot : string[255];
  470. begin
  471. drivenr:=0;
  472. dir:='';
  473. thedir:='/'#0;
  474. if sys_stat(@thedir[1],thisdir)<0 then
  475. exit;
  476. rootino:=thisdir.ino;
  477. rootdev:=thisdir.dev;
  478. thedir:='.'#0;
  479. if sys_stat(@thedir[1],thisdir)<0 then
  480. exit;
  481. thisino:=thisdir.ino;
  482. thisdev:=thisdir.dev;
  483. { Now we can uniquely identify the current and root dir }
  484. thedir:='';
  485. predot:='';
  486. while not ((thisino=rootino) and (thisdev=rootdev)) do
  487. begin
  488. { Are we on a mount point ? }
  489. dummy:=predot+'..'#0;
  490. if sys_stat(@dummy[1],thisdir)<0 then
  491. exit;
  492. dotdotino:=thisdir.ino;
  493. dotdotdev:=thisdir.dev;
  494. mountpoint:=(thisdev<>dotdotdev);
  495. { Now, Try to find the name of this dir in the previous one }
  496. dirstream:=opendir (@dummy[1]);
  497. if dirstream=nil then
  498. exit;
  499. repeat
  500. d:=sys_readdir (dirstream);
  501. validdir:=false;
  502. if (d<>nil) and
  503. (not ((d^.name[0]='.') and ((d^.name[1]=#0) or ((d^.name[1]='.')
  504. and (d^.name[2]=#0))))) and
  505. (mountpoint or (d^.ino=thisino)) then
  506. begin
  507. dummy:=predot+'../'+strpas(@(d^.name[0]))+#0;
  508. validdir:=not (sys_stat (@(dummy[1]),thisdir)<0);
  509. end
  510. else
  511. validdir:=false;
  512. until (d=nil) or
  513. ((validdir) and (thisdir.dev=thisdev) and (thisdir.ino=thisino) );
  514. { At this point, d.name contains the name of the current dir}
  515. if (d<>nil) then
  516. thedir:='/'+strpas(@(d^.name[0]))+thedir;
  517. { closedir also makes d invalid }
  518. if (closedir(dirstream)<0) or (d=nil) then
  519. exit;
  520. thisdev:=dotdotdev;
  521. thisino:=dotdotino;
  522. predot:=predot+'../';
  523. end;
  524. { Now rootino=thisino and rootdev=thisdev so we've reached / }
  525. dir:=thedir
  526. end;
  527. {*****************************************************************************
  528. SystemUnit Initialization
  529. *****************************************************************************}
  530. {$ifdef BSD}
  531. procedure SignalToRunerror(Sig: longint; SigContext: SigContextRec;someptr:pointer); cdecl;
  532. {$else}
  533. {$ifdef Solaris}
  534. procedure SignalToRunerror(Sig: longint; SigContext: SigContextRec;someptr:pointer); cdecl;
  535. {$else}
  536. procedure SignalToRunerror(Sig: longint; SigContext: SigContextRec); cdecl;
  537. {$endif}
  538. {$ENDIF}
  539. var
  540. res,fpustate : word;
  541. begin
  542. res:=0;
  543. case sig of
  544. SIGFPE :
  545. begin
  546. { this is not allways necessary but I don't know yet
  547. how to tell if it is or not PM }
  548. {$ifdef cpui386}
  549. fpustate:=0;
  550. res:=200;
  551. {$ifndef FreeBSD}
  552. if assigned(SigContext.fpstate) then
  553. fpuState:=SigContext.fpstate^.sw;
  554. {$else}
  555. fpustate:=SigContext.en_sw;
  556. {$ifdef SYSTEM_DEBUG}
  557. writeln('xx:',sigcontext.en_tw,' ',sigcontext.en_cw);
  558. {$endif SYSTEM_DEBUG}
  559. {$endif}
  560. {$ifdef SYSTEM_DEBUG}
  561. Writeln(stderr,'FpuState = ',FpuState);
  562. {$endif SYSTEM_DEBUG}
  563. if (FpuState and $7f) <> 0 then
  564. begin
  565. { first check te more precise options }
  566. if (FpuState and FPU_DivisionByZero)<>0 then
  567. res:=200
  568. else if (FpuState and FPU_Overflow)<>0 then
  569. res:=205
  570. else if (FpuState and FPU_Underflow)<>0 then
  571. res:=206
  572. else if (FpuState and FPU_Denormal)<>0 then
  573. res:=216
  574. else if (FpuState and (FPU_StackOverflow or FPU_StackUnderflow))<>0 then
  575. res:=207
  576. else if (FpuState and FPU_Invalid)<>0 then
  577. res:=216
  578. else
  579. res:=207; {'Coprocessor Error'}
  580. end;
  581. {$endif cpui386}
  582. SysResetFPU;
  583. end;
  584. SIGILL,
  585. SIGBUS,
  586. SIGSEGV :
  587. res:=216;
  588. end;
  589. { give runtime error at the position where the signal was raised }
  590. if res<>0 then
  591. begin
  592. {$ifdef cpui386}
  593. {$ifdef FreeBSD}
  594. HandleErrorAddrFrame(res,SigContext.sc_eip,SigContext.sc_ebp);
  595. {$else}
  596. HandleErrorAddrFrame(res,SigContext.eip,SigContext.ebp);
  597. {$endif}
  598. {$else}
  599. HandleError(res);
  600. {$endif}
  601. end;
  602. end;
  603. Procedure InstallSignals;
  604. const
  605. {$Ifndef BSD}
  606. {$ifdef solaris}
  607. act: SigActionRec =(sa_flags:SA_SIGINFO;Handler:(sa:@signaltorunerror;sa_mask:0);
  608. {$else}
  609. act: SigActionRec = (handler:(Sa:@SignalToRunError);sa_mask:0;sa_flags:0;
  610. Sa_restorer: NIL);
  611. {$endif}
  612. {$ELSE}
  613. act: SigActionRec = (handler:(Sa:@SignalToRunError);sa_flags:SA_SIGINFO;
  614. sa_mask:0);
  615. {$endif}
  616. oldact: PSigActionRec = Nil; {Probably not necessary anymore, now
  617. VAR is removed}
  618. begin
  619. SysResetFPU;
  620. SigAction(SIGFPE,@act,oldact);
  621. {$ifndef Solaris}
  622. SigAction(SIGSEGV,@act,oldact);
  623. SigAction(SIGBUS,@act,oldact);
  624. SigAction(SIGILL,@act,oldact);
  625. {$endif}
  626. end;
  627. procedure SetupCmdLine;
  628. var
  629. bufsize,
  630. len,j,
  631. size,i : longint;
  632. found : boolean;
  633. buf : array[0..1026] of char;
  634. procedure AddBuf;
  635. begin
  636. reallocmem(cmdline,size+bufsize);
  637. move(buf,cmdline[size],bufsize);
  638. inc(size,bufsize);
  639. bufsize:=0;
  640. end;
  641. begin
  642. size:=0;
  643. bufsize:=0;
  644. i:=0;
  645. while (i<argc) do
  646. begin
  647. len:=strlen(argv[i]);
  648. if len>sizeof(buf)-2 then
  649. len:=sizeof(buf)-2;
  650. found:=false;
  651. for j:=1 to len do
  652. if argv[i][j]=' ' then
  653. begin
  654. found:=true;
  655. break;
  656. end;
  657. if bufsize+len>=sizeof(buf)-2 then
  658. AddBuf;
  659. if found then
  660. begin
  661. buf[bufsize]:='"';
  662. inc(bufsize);
  663. end;
  664. move(argv[i]^,buf[bufsize],len);
  665. inc(bufsize,len);
  666. if found then
  667. begin
  668. buf[bufsize]:='"';
  669. inc(bufsize);
  670. end;
  671. if i<argc then
  672. buf[bufsize]:=' '
  673. else
  674. buf[bufsize]:=#0;
  675. inc(bufsize);
  676. inc(i);
  677. end;
  678. AddBuf;
  679. end;
  680. procedure SysInitStdIO;
  681. begin
  682. OpenStdIO(Input,fmInput,StdInputHandle);
  683. OpenStdIO(Output,fmOutput,StdOutputHandle);
  684. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  685. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  686. end;
  687. Begin
  688. IsConsole := TRUE;
  689. IsLibrary := FALSE;
  690. { Setup stack checking variables }
  691. StackLength := InitialStkLen;
  692. StackBottom := Sptr - StackLength;
  693. { Set up signals handlers }
  694. InstallSignals;
  695. { Setup heap }
  696. InitHeap;
  697. SysInitExceptions;
  698. { Arguments }
  699. SetupCmdLine;
  700. { Setup stdin, stdout and stderr }
  701. SysInitStdIO;
  702. { Reset IO Error }
  703. InOutRes:=0;
  704. { Setup variant support }
  705. {$ifdef HASVARIANT}
  706. initvariantmanager;
  707. {$endif HASVARIANT}
  708. End.
  709. {
  710. $Log$
  711. Revision 1.36 2003-10-17 20:45:58 olle
  712. * Changed mapping of Sys_ETXTBSY to 5
  713. * Changed m68k to cpum68k, i386 to cpui386
  714. Revision 1.35 2003/10/13 21:24:04 hajny
  715. * sbrk error handling corrected
  716. Revision 1.34 2003/09/27 11:52:36 peter
  717. * sbrk returns pointer
  718. Revision 1.33 2003/09/03 14:09:37 florian
  719. * arm fixes to the common rtl code
  720. * some generic math code fixed
  721. * ...
  722. Revision 1.32 2003/08/21 22:21:00 olle
  723. - removed parameter from fpc_iocheck
  724. Revision 1.31 2002/10/14 19:39:17 peter
  725. * threads unit added for thread support
  726. Revision 1.30 2002/10/13 09:20:56 peter
  727. * added initvariantmanager
  728. Revision 1.29 2002/09/07 16:01:27 peter
  729. * old logs removed and tabs fixed
  730. Revision 1.28 2002/09/02 19:46:37 florian
  731. * fixed line breaks
  732. Revision 1.27 2002/08/31 21:29:57 florian
  733. * several PC related fixes
  734. Revision 1.26 2002/08/13 18:11:08 florian
  735. * heap stuff for powerpc fixed
  736. Revision 1.25 2002/08/03 20:05:13 florian
  737. + ppc implementation of heap functions added
  738. Revision 1.24 2002/07/29 21:28:17 florian
  739. * several fixes to get further with linux/ppc system unit compilation
  740. Revision 1.23 2002/07/28 20:43:49 florian
  741. * several fixes for linux/powerpc
  742. * several fixes to MT
  743. Revision 1.22 2002/05/31 13:37:24 marco
  744. * more Renamefest
  745. Revision 1.21 2002/04/21 15:55:00 carl
  746. + initialize some global variables
  747. Revision 1.20 2002/04/12 17:43:28 carl
  748. + generic stack checking
  749. Revision 1.19 2002/03/11 19:10:33 peter
  750. * Regenerated with updated fpcmake
  751. }