sysunix.inc 19 KB

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