syslinux.pp 16 KB

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