syslinux.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,97 by Michael Van Canneyt,
  5. member of the Free Pascal development team.
  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. { These things are set in the makefile, }
  13. { But you can override them here.}
  14. { If you want to link to the C library, set the conditional crtlib }
  15. { $define crtlib}
  16. { If you use an aout system, set the conditional AOUT}
  17. { $Define AOUT}
  18. Unit SysLinux;
  19. {$I os.inc}
  20. Interface
  21. {$I systemh.inc}
  22. {$I heaph.inc}
  23. const
  24. UnusedHandle = $ffff;
  25. StdInputHandle = 0;
  26. StdOutputHandle = 1;
  27. StdErrorHandle = 2;
  28. var
  29. argc : longint;
  30. argv : ppchar;
  31. envp : ppchar;
  32. Implementation
  33. {$I system.inc}
  34. {$ifdef crtlib}
  35. Procedure _rtl_exit(l: longint); cdecl;
  36. Function _rtl_paramcount: longint; cdecl;
  37. Procedure _rtl_paramstr(st: pchar; l: longint); cdecl;
  38. Function _rtl_open(f: pchar; flags: longint): longint; cdecl;
  39. Procedure _rtl_close(h: longint); cdecl;
  40. Procedure _rtl_write(h: longint; addr: longInt; len : longint); cdecl;
  41. Procedure _rtl_erase(p: pchar); cdecl;
  42. Procedure _rtl_rename(p1: pchar; p2 : pchar); cdecl;
  43. Function _rtl_read(h: longInt; addr: longInt; len : longint) : longint; cdecl;
  44. Function _rtl_filepos(Handle: longint): longint; cdecl;
  45. Procedure _rtl_seek(Handle: longint; pos:longint); cdecl;
  46. Function _rtl_filesize(Handle:longint): longInt; cdecl;
  47. Procedure _rtl_rmdir(buffer: pchar); cdecl;
  48. Procedure _rtl_mkdir(buffer: pchar); cdecl;
  49. Procedure _rtl_chdir(buffer: pchar); cdecl;
  50. {$else}
  51. { used in syscall to report errors.}
  52. var
  53. Errno : longint;
  54. { Include constant and type definitions }
  55. {$i errno.inc } { Error numbers }
  56. {$i sysnr.inc } { System call numbers }
  57. {$i sysconst.inc } { Miscellaneous constants }
  58. {$i systypes.inc } { Types needed for system calls }
  59. { Read actual system call definitions. }
  60. {$i syscalls.inc }
  61. {$endif}
  62. {*****************************************************************************
  63. Misc. System Dependent Functions
  64. *****************************************************************************}
  65. {$ASMMODE DIRECT}
  66. Procedure Halt(ErrNum: Byte);
  67. Begin
  68. ExitCode:=Errnum;
  69. ErrorAddr:=nil;
  70. Do_Exit;
  71. {$ifdef i386}
  72. asm
  73. jmp _haltproc
  74. end;
  75. {$endif}
  76. End;
  77. Function ParamCount: Longint;
  78. Begin
  79. {$ifdef crtlib}
  80. ParamCount:=_rtl_paramcount;
  81. {$else}
  82. Paramcount:=argc-1
  83. {$endif}
  84. End;
  85. Function ParamStr(l: Longint): String;
  86. Var
  87. {$ifndef crtlib}
  88. i : longint;
  89. pp : ppchar;
  90. {$else}
  91. b : Array[0..255] of Char;
  92. {$endif}
  93. Begin
  94. {$ifdef crtlib}
  95. _rtl_paramstr(@b, l);
  96. ParamStr:=StrPas(b);
  97. {$else}
  98. if l>argc then
  99. begin
  100. paramstr:='';
  101. exit
  102. end;
  103. pp:=argv;
  104. i:=0;
  105. while (i<l) and (pp^<>nil) do
  106. begin
  107. pp:=pp+4;
  108. inc(i);
  109. end;
  110. if pp^<>nil then
  111. Paramstr:=StrPas(pp^)
  112. else
  113. ParamStr:='';
  114. {$endif}
  115. End;
  116. Procedure Randomize;
  117. Begin
  118. {$ifdef crtlib}
  119. _rtl_gettime(longint(@randseed));
  120. {$else}
  121. randseed:=sys_time;
  122. {$endif}
  123. End;
  124. {*****************************************************************************
  125. Heap Management
  126. *****************************************************************************}
  127. { ___brk_addr is defined and allocated in prt1.as }
  128. Function Get_Brk_addr : longint;assembler;
  129. {$ifdef i386}
  130. asm
  131. movl ___fpc_brk_addr,%eax
  132. end ['EAX'];
  133. {$endif}
  134. Procedure Set_brk_addr (NewAddr : longint);assembler;
  135. {$ifdef i386}
  136. asm
  137. movl NewAddr,%eax
  138. movl %eax,___fpc_brk_addr
  139. end ['EAX'];
  140. {$endif}
  141. {$ASMMODE ATT}
  142. Function brk(Location : longint) : Longint;
  143. { set end of data segment to location }
  144. var
  145. t : syscallregs;
  146. dummy : longint;
  147. begin
  148. t.reg2:=Location;
  149. dummy:=syscall(syscall_nr_brk,t);
  150. set_brk_addr(dummy);
  151. brk:=dummy;
  152. end;
  153. Function init_brk : longint;
  154. begin
  155. if Get_Brk_addr=0 then
  156. begin
  157. Set_brk_addr(brk(0));
  158. if Get_brk_addr=0 then
  159. exit(-1);
  160. end;
  161. init_brk:=0;
  162. end;
  163. Function sbrk(size : longint) : Longint;
  164. var
  165. Temp : longint;
  166. begin
  167. if init_brk=0 then
  168. begin
  169. Temp:=Get_Brk_Addr+size;
  170. if brk(temp)=-1 then
  171. exit(-1);
  172. if Get_brk_addr=temp then
  173. exit(temp-size);
  174. end;
  175. exit(-1);
  176. end;
  177. { include standard heap management }
  178. {$I heap.inc}
  179. {*****************************************************************************
  180. Low Level File Routines
  181. *****************************************************************************}
  182. {
  183. The lowlevel file functions should take care of setting the InOutRes to the
  184. correct value if an error has occured, else leave it untouched
  185. }
  186. Procedure Errno2Inoutres;
  187. {
  188. Convert ErrNo error to the correct Inoutres value
  189. }
  190. begin
  191. if ErrNo=0 then { Else it will go through all the cases }
  192. exit;
  193. case ErrNo of
  194. Sys_ENFILE,
  195. Sys_EMFILE : Inoutres:=4;
  196. Sys_ENOENT : Inoutres:=2;
  197. Sys_EBADF : Inoutres:=6;
  198. Sys_ENOMEM,
  199. Sys_EFAULT : Inoutres:=217;
  200. Sys_EINVAL : Inoutres:=218;
  201. Sys_EPIPE,
  202. Sys_EINTR,
  203. Sys_EIO,
  204. Sys_EAGAIN,
  205. Sys_ENOSPC : Inoutres:=101;
  206. Sys_ENAMETOOLONG,
  207. Sys_ELOOP,
  208. Sys_ENOTDIR : Inoutres:=3;
  209. Sys_EROFS,
  210. Sys_EEXIST,
  211. Sys_EACCES : Inoutres:=5;
  212. Sys_ETXTBSY : Inoutres:=162;
  213. end;
  214. end;
  215. Procedure Do_Close(Handle:Longint);
  216. Begin
  217. {$ifdef crtlib}
  218. _rtl_close(Handle);
  219. {$else}
  220. sys_close(Handle);
  221. {$endif}
  222. End;
  223. Procedure Do_Erase(p:pchar);
  224. Begin
  225. {$ifdef crtlib}
  226. _rtl_erase(p);
  227. {$else}
  228. sys_unlink(p);
  229. Errno2Inoutres;
  230. {$endif}
  231. End;
  232. Procedure Do_Rename(p1,p2:pchar);
  233. Begin
  234. {$ifdef crtlib}
  235. _rtl_rename(p1,p2);
  236. {$else }
  237. sys_rename(p1,p2);
  238. Errno2Inoutres;
  239. {$endif}
  240. End;
  241. Function Do_Write(Handle,Addr,Len:Longint):longint;
  242. Begin
  243. {$ifdef crtlib}
  244. _rtl_write(Handle,addr,len);
  245. Do_Write:=Len;
  246. {$else}
  247. Do_Write:=sys_write(Handle,pchar(addr),len);
  248. Errno2Inoutres;
  249. {$endif}
  250. if Do_Write<0 then
  251. Do_Write:=0;
  252. End;
  253. Function Do_Read(Handle,Addr,Len:Longint):Longint;
  254. Begin
  255. {$ifdef crtlib}
  256. Do_Read:=_rtl_read(Handle,addr,len);
  257. {$else}
  258. Do_Read:=sys_read(Handle,pchar(addr),len);
  259. Errno2Inoutres;
  260. {$endif}
  261. if Do_Read<0 then
  262. Do_Read:=0;
  263. End;
  264. Function Do_FilePos(Handle: Longint): Longint;
  265. Begin
  266. {$ifdef crtlib}
  267. Do_FilePos:=_rtl_filepos(Handle);
  268. {$else}
  269. Do_FilePos:=sys_lseek(Handle, 0, Seek_Cur);
  270. Errno2Inoutres;
  271. {$endif}
  272. End;
  273. Procedure Do_Seek(Handle,Pos:Longint);
  274. Begin
  275. {$ifdef crtlib}
  276. _rtl_seek(Handle, Pos);
  277. {$else}
  278. sys_lseek(Handle, pos, Seek_set);
  279. {$endif}
  280. End;
  281. Function Do_SeekEnd(Handle:Longint): Longint;
  282. begin
  283. {$ifdef crtlib}
  284. Do_SeekEnd:=_rtl_filesize(Handle);
  285. {$else}
  286. Do_SeekEnd:=sys_lseek(Handle,0,Seek_End);
  287. {$endif}
  288. end;
  289. Function Do_FileSize(Handle:Longint): Longint;
  290. {$ifndef crtlib}
  291. var
  292. regs : Syscallregs;
  293. Info : Stat;
  294. {$endif}
  295. Begin
  296. {$ifdef crtlib}
  297. Do_FileSize:=_rtl_filesize(Handle);
  298. {$else}
  299. regs.reg2:=Handle;
  300. regs.reg3:=longint(@Info);
  301. if SysCall(SysCall_nr_fstat,regs)=0 then
  302. Do_FileSize:=Info.Size
  303. else
  304. Do_FileSize:=0;
  305. Errno2Inoutres;
  306. {$endif}
  307. End;
  308. Procedure Do_Truncate(Handle,Pos:longint);
  309. {$ifndef crtlib}
  310. var
  311. sr : syscallregs;
  312. {$endif}
  313. begin
  314. {$ifndef crtlib}
  315. sr.reg2:=Handle;
  316. sr.reg3:=Pos;
  317. syscall(syscall_nr_ftruncate,sr);
  318. Errno2Inoutres;
  319. {$endif}
  320. end;
  321. Procedure Do_Open(var f;p:pchar;flags:longint);
  322. {
  323. FileRec and textrec have both Handle and mode as the first items so
  324. they could use the same routine for opening/creating.
  325. when (flags and $10) the file will be append
  326. when (flags and $100) the file will be truncate/rewritten
  327. when (flags and $1000) there is no check for close (needed for textfiles)
  328. }
  329. var
  330. {$ifndef crtlib}
  331. oflags : longint;
  332. {$endif}
  333. Begin
  334. { close first if opened }
  335. if ((flags and $1000)=0) then
  336. begin
  337. case FileRec(f).mode of
  338. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  339. fmclosed : ;
  340. else
  341. begin
  342. inoutres:=102; {not assigned}
  343. exit;
  344. end;
  345. end;
  346. end;
  347. { reset file Handle }
  348. FileRec(f).Handle:=UnusedHandle;
  349. { We do the conversion of filemodes here, concentrated on 1 place }
  350. case (flags and 3) of
  351. 0 : begin
  352. oflags :=Open_RDONLY;
  353. FileRec(f).mode:=fminput;
  354. end;
  355. 1 : begin
  356. oflags :=Open_WRONLY;
  357. FileRec(f).mode:=fmoutput;
  358. end;
  359. 2 : begin
  360. oflags :=Open_RDWR;
  361. FileRec(f).mode:=fminout;
  362. end;
  363. end;
  364. if (flags and $100)=$100 then
  365. oflags:=oflags or (Open_CREAT or Open_TRUNC)
  366. else
  367. if (flags and $10)=$10 then
  368. oflags:=oflags or (Open_APPEND);
  369. { empty name is special }
  370. if p[0]=#0 then
  371. begin
  372. case FileRec(f).mode of
  373. fminput : FileRec(f).Handle:=StdInputHandle;
  374. fmoutput,
  375. fmappend : begin
  376. FileRec(f).Handle:=StdOutputHandle;
  377. FileRec(f).mode:=fmoutput; {fool fmappend}
  378. end;
  379. end;
  380. exit;
  381. end;
  382. { real open call }
  383. {$ifdef crtlib}
  384. FileRec(f).Handle:=_rtl_open(p, oflags);
  385. if FileRec(f).Handle<0 then
  386. InOutRes:=2
  387. else
  388. InOutRes:=0;
  389. {$else}
  390. FileRec(f).Handle:=sys_open(p,oflags,438);
  391. if (ErrNo=Sys_EROFS) and ((OFlags and Open_RDWR)<>0) then
  392. begin
  393. Oflags:=Oflags and not(Open_RDWR);
  394. FileRec(f).Handle:=sys_open(p,oflags,438);
  395. end;
  396. Errno2Inoutres;
  397. {$endif}
  398. End;
  399. Function Do_IsDevice(Handle:Longint):boolean;
  400. {
  401. Interface to Unix ioctl call.
  402. Performs various operations on the filedescriptor Handle.
  403. Ndx describes the operation to perform.
  404. Data points to data needed for the Ndx function. The structure of this
  405. data is function-dependent.
  406. }
  407. var
  408. sr: SysCallRegs;
  409. Data : array[0..255] of byte; {Large enough for termios info}
  410. begin
  411. sr.reg2:=Handle;
  412. sr.reg3:=$5401; {=TCGETS}
  413. sr.reg4:=Longint(@Data);
  414. Do_IsDevice:=(SysCall(Syscall_nr_ioctl,sr)=0);
  415. end;
  416. {*****************************************************************************
  417. UnTyped File Handling
  418. *****************************************************************************}
  419. {$i file.inc}
  420. {*****************************************************************************
  421. Typed File Handling
  422. *****************************************************************************}
  423. {$i typefile.inc}
  424. {*****************************************************************************
  425. Text File Handling
  426. *****************************************************************************}
  427. {$DEFINE SHORT_LINEBREAK}
  428. {$DEFINE EXTENDED_EOF}
  429. {$i text.inc}
  430. {*****************************************************************************
  431. Directory Handling
  432. *****************************************************************************}
  433. Procedure MkDir(Const s: String);[IOCheck];
  434. Var
  435. Buffer: Array[0..255] of Char;
  436. Begin
  437. If InOutRes <> 0 then exit;
  438. Move(s[1], Buffer, Length(s));
  439. Buffer[Length(s)] := #0;
  440. {$ifdef crtlib}
  441. _rtl_mkdir(@buffer);
  442. {$else}
  443. sys_mkdir(@buffer, 511);
  444. Errno2Inoutres;
  445. {$endif}
  446. End;
  447. Procedure RmDir(Const s: String);[IOCheck];
  448. Var
  449. Buffer: Array[0..255] of Char;
  450. Begin
  451. If InOutRes <> 0 then exit;
  452. Move(s[1], Buffer, Length(s));
  453. Buffer[Length(s)] := #0;
  454. {$ifdef crtlib}
  455. _rtl_rmdir(@buffer);
  456. {$else}
  457. sys_rmdir(@buffer);
  458. Errno2Inoutres;
  459. {$endif}
  460. End;
  461. Procedure ChDir(Const s: String);[IOCheck];
  462. Var
  463. Buffer: Array[0..255] of Char;
  464. Begin
  465. If InOutRes <> 0 then exit;
  466. Move(s[1], Buffer, Length(s));
  467. Buffer[Length(s)] := #0;
  468. {$ifdef crtlib}
  469. _rtl_chdir(@buffer);
  470. {$else}
  471. sys_chdir(@buffer);
  472. Errno2Inoutres;
  473. {$endif}
  474. End;
  475. procedure getdir(drivenr : byte;var dir : string);
  476. {$ifndef crtlib}
  477. var
  478. thisdir : stat;
  479. rootino,
  480. thisino,
  481. dotdotino : longint;
  482. rootdev,
  483. thisdev,
  484. dotdotdev : word;
  485. thedir,dummy : string[255];
  486. dirstream : pdir;
  487. d : pdirent;
  488. mountpoint : boolean;
  489. predot : string[255];
  490. procedure dodispose (p : pdir);
  491. begin
  492. dispose (p^.buf);
  493. dispose (p)
  494. end;
  495. {$endif}
  496. begin
  497. drivenr:=0;
  498. dir:='';
  499. {$ifndef crtlib}
  500. thedir:='/'#0;
  501. if sys_stat(@thedir[1],thisdir)<0 then
  502. exit;
  503. rootino:=thisdir.ino;
  504. rootdev:=thisdir.dev;
  505. thedir:='.'#0;
  506. if sys_stat(@thedir[1],thisdir)<0 then
  507. exit;
  508. thisino:=thisdir.ino;
  509. thisdev:=thisdir.dev;
  510. { Now we can uniquely identify the current and root dir }
  511. thedir:='';
  512. predot:='';
  513. while not ((thisino=rootino) and (thisdev=rootdev)) do
  514. begin
  515. { Are we on a mount point ? }
  516. dummy:=predot+'..'#0;
  517. if sys_stat(@dummy[1],thisdir)<0 then
  518. exit;
  519. dotdotino:=thisdir.ino;
  520. dotdotdev:=thisdir.dev;
  521. mountpoint:=(thisdev<>dotdotdev);
  522. { Now, Try to find the name of this dir in the previous one }
  523. dirstream:=opendir (@dummy[1]);
  524. if dirstream=nil then
  525. exit;
  526. repeat
  527. d:=sys_readdir (dirstream);
  528. if (d<>nil) and
  529. (not ((d^.name[0]='.') and ((d^.name[1]=#0) or ((d^.name[1]='.') and (d^.name[2]=#0))))) and
  530. (mountpoint or (d^.ino=thisino)) then
  531. begin
  532. dummy:=predot+'../'+strpas(@(d^.name[0]))+#0;
  533. if sys_stat (@(dummy[1]),thisdir)<0 then
  534. d:=nil;
  535. end;
  536. until (d=nil) or ((thisdir.dev=thisdev) and (thisdir.ino=thisino) );
  537. if (closedir (dirstream)<0) or (d=nil) then
  538. begin
  539. dodispose (dirstream);
  540. exit;
  541. end;
  542. { At this point, d.name contains the name of the current dir}
  543. thedir:='/'+strpas(@(d^.name[0]))+thedir;
  544. thisdev:=dotdotdev;
  545. thisino:=dotdotino;
  546. predot:=predot+'../';
  547. { We don't want to clutter op the heap with DIR records... }
  548. dodispose (dirstream);
  549. end;
  550. { Now rootino=thisino and rootdev=thisdev so we've reached / }
  551. dir:=thedir
  552. {$endif}
  553. end;
  554. {*****************************************************************************
  555. SystemUnit Initialization
  556. *****************************************************************************}
  557. Procedure SegFaultHandler (Sig : longint);
  558. begin
  559. if sig=11 then
  560. HandleError (216);
  561. end;
  562. Procedure InstallSegFaultHandler;
  563. var
  564. sr : syscallregs;
  565. begin
  566. sr.reg2:=11;
  567. sr.reg3:=longint(@SegFaultHandler);
  568. syscall(syscall_nr_signal,sr);
  569. end;
  570. Begin
  571. { Set up segfault Handler }
  572. InstallSegFaultHandler;
  573. { Setup heap }
  574. InitHeap;
  575. { Setup stdin, stdout and stderr }
  576. OpenStdIO(Input,fmInput,StdInputHandle);
  577. OpenStdIO(Output,fmOutput,StdOutputHandle);
  578. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  579. { Reset IO Error }
  580. InOutRes:=0;
  581. End.
  582. {
  583. $Log$
  584. Revision 1.11 1998-08-11 08:30:37 michael
  585. + Fixed paramstr() - sometimes there are no 255 characters available.
  586. Revision 1.10 1998/07/30 13:26:15 michael
  587. + Added support for ErrorProc variable. All internal functions are required
  588. to call HandleError instead of runerror from now on.
  589. This is necessary for exception support.
  590. Revision 1.9 1998/07/20 23:40:20 michael
  591. changed sbrk to fc_sbrk, to avoid conflicts with C library.
  592. Revision 1.8 1998/07/13 21:19:14 florian
  593. * some problems with ansi string support fixed
  594. Revision 1.7 1998/07/02 12:36:21 carl
  595. * IOCheck/InOutRes check for mkdir, chdir and rmdir as in TP
  596. Revision 1.6 1998/07/01 15:30:01 peter
  597. * better readln/writeln
  598. Revision 1.4 1998/05/30 14:18:43 peter
  599. * fixed to remake with -Rintel in the ppc386.cfg
  600. Revision 1.3 1998/05/12 10:42:48 peter
  601. * moved getopts to inc/, all supported OS's need argc,argv exported
  602. + strpas, strlen are now exported in the systemunit
  603. * removed logs
  604. * removed $ifdef ver_above
  605. Revision 1.2 1998/05/06 12:35:26 michael
  606. + Removed log from before restored version.
  607. }