syslinux.pp 15 KB

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