sysunix.inc 21 KB

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