syslinux.pp 15 KB

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