syslinux.pp 16 KB

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