syslinux.pp 14 KB

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