syslinux.pp 16 KB

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