syslinux.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. {*****************************************************************************
  401. UnTyped File Handling
  402. *****************************************************************************}
  403. {$i file.inc}
  404. {*****************************************************************************
  405. Typed File Handling
  406. *****************************************************************************}
  407. {$i typefile.inc}
  408. {*****************************************************************************
  409. Text File Handling
  410. *****************************************************************************}
  411. {$DEFINE SHORT_LINEBREAK}
  412. {$DEFINE EXTENDED_EOF}
  413. {$i text.inc}
  414. {*****************************************************************************
  415. Directory Handling
  416. *****************************************************************************}
  417. Procedure MkDir(Const s: String);
  418. Var
  419. Buffer: Array[0..255] of Char;
  420. Begin
  421. Move(s[1], Buffer, Length(s));
  422. Buffer[Length(s)] := #0;
  423. {$ifdef crtlib}
  424. _rtl_mkdir(@buffer);
  425. {$else}
  426. sys_mkdir(@buffer, 511);
  427. Errno2Inoutres;
  428. {$endif}
  429. End;
  430. Procedure RmDir(Const s: String);
  431. Var
  432. Buffer: Array[0..255] of Char;
  433. Begin
  434. Move(s[1], Buffer, Length(s));
  435. Buffer[Length(s)] := #0;
  436. {$ifdef crtlib}
  437. _rtl_rmdir(@buffer);
  438. {$else}
  439. sys_rmdir(@buffer);
  440. Errno2Inoutres;
  441. {$endif}
  442. End;
  443. Procedure ChDir(Const s: String);
  444. Var
  445. Buffer: Array[0..255] of Char;
  446. Begin
  447. Move(s[1], Buffer, Length(s));
  448. Buffer[Length(s)] := #0;
  449. {$ifdef crtlib}
  450. _rtl_chdir(@buffer);
  451. {$else}
  452. sys_chdir(@buffer);
  453. Errno2Inoutres;
  454. {$endif}
  455. End;
  456. procedure getdir(drivenr : byte;var dir : string);
  457. {$ifndef crtlib}
  458. var
  459. thisdir : stat;
  460. rootino,
  461. thisino,
  462. dotdotino : longint;
  463. rootdev,
  464. thisdev,
  465. dotdotdev : word;
  466. thedir,dummy : string[255];
  467. dirstream : pdir;
  468. d : pdirent;
  469. mountpoint : boolean;
  470. predot : string[255];
  471. procedure dodispose (p : pdir);
  472. begin
  473. dispose (p^.buf);
  474. dispose (p)
  475. end;
  476. {$endif}
  477. begin
  478. drivenr:=0;
  479. dir:='';
  480. {$ifndef crtlib}
  481. thedir:='/'#0;
  482. if sys_stat(@thedir[1],thisdir)<0 then
  483. exit;
  484. rootino:=thisdir.ino;
  485. rootdev:=thisdir.dev;
  486. thedir:='.'#0;
  487. if sys_stat(@thedir[1],thisdir)<0 then
  488. exit;
  489. thisino:=thisdir.ino;
  490. thisdev:=thisdir.dev;
  491. { Now we can uniquely identify the current and root dir }
  492. thedir:='';
  493. predot:='';
  494. while not ((thisino=rootino) and (thisdev=rootdev)) do
  495. begin
  496. { Are we on a mount point ? }
  497. dummy:=predot+'..'#0;
  498. if sys_stat(@dummy[1],thisdir)<0 then
  499. exit;
  500. dotdotino:=thisdir.ino;
  501. dotdotdev:=thisdir.dev;
  502. mountpoint:=(thisdev<>dotdotdev);
  503. { Now, Try to find the name of this dir in the previous one }
  504. dirstream:=opendir (@dummy[1]);
  505. if dirstream=nil then
  506. exit;
  507. repeat
  508. d:=sys_readdir (dirstream);
  509. if (d<>nil) and
  510. (not ((d^.name[0]='.') and ((d^.name[1]=#0) or ((d^.name[1]='.') and (d^.name[2]=#0))))) and
  511. (mountpoint or (d^.ino=thisino)) then
  512. begin
  513. dummy:=predot+'../'+strpas(@(d^.name[0]))+#0;
  514. if sys_stat (@(dummy[1]),thisdir)<0 then
  515. d:=nil;
  516. end;
  517. until (d=nil) or ((thisdir.dev=thisdev) and (thisdir.ino=thisino) );
  518. if (closedir (dirstream)<0) or (d=nil) then
  519. begin
  520. dodispose (dirstream);
  521. exit;
  522. end;
  523. { At this point, d.name contains the name of the current dir}
  524. thedir:='/'+strpas(@(d^.name[0]))+thedir;
  525. thisdev:=dotdotdev;
  526. thisino:=dotdotino;
  527. predot:=predot+'../';
  528. { We don't want to clutter op the heap with DIR records... }
  529. dodispose (dirstream);
  530. end;
  531. { Now rootino=thisino and rootdev=thisdev so we've reached / }
  532. dir:=thedir
  533. {$endif}
  534. end;
  535. {*****************************************************************************
  536. SystemUnit Initialization
  537. *****************************************************************************}
  538. Procedure SegFaultHandler (Sig : longint);
  539. begin
  540. if sig=11 then
  541. RunError (216);
  542. end;
  543. Procedure InstallSegFaultHandler;
  544. var
  545. sr : syscallregs;
  546. begin
  547. sr.reg2:=11;
  548. sr.reg3:=longint(@SegFaultHandler);
  549. syscall(syscall_nr_signal,sr);
  550. end;
  551. procedure OpenStdIO(var f:text;mode:word;const std:string;hdl:longint);
  552. begin
  553. Assign(f,std);
  554. TextRec(f).Handle:=hdl;
  555. TextRec(f).Mode:=mode;
  556. TextRec(f).InOutFunc:=@FileInOutFunc;
  557. TextRec(f).FlushFunc:=@FileInOutFunc;
  558. TextRec(f).Closefunc:=@fileclosefunc;
  559. end;
  560. Begin
  561. { Set up segfault Handler }
  562. InstallSegFaultHandler;
  563. { Setup heap }
  564. InitHeap;
  565. { Setup stdin, stdout and stderr }
  566. OpenStdIO(Input,fmInput,'stdin',StdInputHandle);
  567. OpenStdIO(Output,fmOutput,'stdout',StdOutputHandle);
  568. OpenStdIO(StdErr,fmOutput,'stderr',StdErrorHandle);
  569. { Reset IO Error }
  570. InOutRes:=0;
  571. End.
  572. {
  573. $Log$
  574. Revision 1.5 1998-06-23 16:57:17 peter
  575. * fixed the filesize() problems under linux and filerec.size=0 error
  576. Revision 1.4 1998/05/30 14:18:43 peter
  577. * fixed to remake with -Rintel in the ppc386.cfg
  578. Revision 1.3 1998/05/12 10:42:48 peter
  579. * moved getopts to inc/, all supported OS's need argc,argv exported
  580. + strpas, strlen are now exported in the systemunit
  581. * removed logs
  582. * removed $ifdef ver_above
  583. Revision 1.2 1998/05/06 12:35:26 michael
  584. + Removed log from before restored version.
  585. }