syslinux.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. Procedure Halt(ErrNum: Byte);
  68. Begin
  69. ExitCode:=Errnum;
  70. ErrorAddr:=nil;
  71. Do_Exit;
  72. {$ifdef i386}
  73. asm
  74. jmp _haltproc
  75. end;
  76. {$endif}
  77. End;
  78. Function ParamCount: Longint;
  79. Begin
  80. {$ifdef crtlib}
  81. ParamCount:=_rtl_paramcount;
  82. {$else}
  83. Paramcount:=argc-1
  84. {$endif}
  85. End;
  86. Function ParamStr(l: Longint): String;
  87. Var
  88. b : Array[0..255] of Char;
  89. {$ifndef crtlib}
  90. i : longint;
  91. pp : ppchar;
  92. {$endif}
  93. Begin
  94. {$ifdef crtlib}
  95. _rtl_paramstr(@b, l);
  96. {$else}
  97. if l>argc then
  98. begin
  99. paramstr:='';
  100. exit
  101. end;
  102. pp:=argv;
  103. i:=0;
  104. while (i<l) and (pp^<>nil) do
  105. begin
  106. pp:=pp+4;
  107. inc(i);
  108. end;
  109. if pp^<>nil then
  110. move (pp^^,b[0],255)
  111. else
  112. b[0]:=#0;
  113. {$endif}
  114. ParamStr:=StrPas(b);
  115. End;
  116. Procedure Randomize;
  117. Begin
  118. {$ifdef crtlib}
  119. _rtl_gettime(longint(@randseed));
  120. {$else}
  121. randseed:=sys_time;
  122. {$endif}
  123. End;
  124. {*****************************************************************************
  125. Heap Management
  126. *****************************************************************************}
  127. { ___brk_addr is defined and allocated in prt1.as }
  128. Function Get_Brk_addr : longint;assembler;
  129. {$ifdef i386}
  130. asm
  131. movl ___brk_addr,%eax
  132. end ['EAX'];
  133. {$endif}
  134. Procedure Set_brk_addr (NewAddr : longint);assembler;
  135. {$ifdef i386}
  136. asm
  137. movl NewAddr,%eax
  138. movl %eax,___brk_addr
  139. end ['EAX'];
  140. {$endif}
  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 : Inoutres:=150;
  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:=-1;
  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. Errno2Inoutres;
  391. {$endif}
  392. End;
  393. {*****************************************************************************
  394. UnTyped File Handling
  395. *****************************************************************************}
  396. {$i file.inc}
  397. {*****************************************************************************
  398. Typed File Handling
  399. *****************************************************************************}
  400. {$i typefile.inc}
  401. {*****************************************************************************
  402. Text File Handling
  403. *****************************************************************************}
  404. {$DEFINE SHORT_LINEBREAK}
  405. {$DEFINE EXTENDED_EOF}
  406. {$i text.inc}
  407. {*****************************************************************************
  408. Directory Handling
  409. *****************************************************************************}
  410. Procedure MkDir(Const s: String);
  411. Var
  412. Buffer: Array[0..255] of Char;
  413. Begin
  414. Move(s[1], Buffer, Length(s));
  415. Buffer[Length(s)] := #0;
  416. {$ifdef crtlib}
  417. _rtl_mkdir(@buffer);
  418. {$else}
  419. sys_mkdir(@buffer, 511);
  420. Errno2Inoutres;
  421. {$endif}
  422. End;
  423. Procedure RmDir(Const s: String);
  424. Var
  425. Buffer: Array[0..255] of Char;
  426. Begin
  427. Move(s[1], Buffer, Length(s));
  428. Buffer[Length(s)] := #0;
  429. {$ifdef crtlib}
  430. _rtl_rmdir(@buffer);
  431. {$else}
  432. sys_rmdir(@buffer);
  433. Errno2Inoutres;
  434. {$endif}
  435. End;
  436. Procedure ChDir(Const s: String);
  437. Var
  438. Buffer: Array[0..255] of Char;
  439. Begin
  440. Move(s[1], Buffer, Length(s));
  441. Buffer[Length(s)] := #0;
  442. {$ifdef crtlib}
  443. _rtl_chdir(@buffer);
  444. {$else}
  445. sys_chdir(@buffer);
  446. Errno2Inoutres;
  447. {$endif}
  448. End;
  449. procedure getdir(drivenr : byte;var dir : string);
  450. {$ifndef crtlib}
  451. var
  452. thisdir : stat;
  453. rootino,
  454. thisino,
  455. dotdotino : longint;
  456. rootdev,
  457. thisdev,
  458. dotdotdev : word;
  459. thedir,dummy : string[255];
  460. dirstream : pdir;
  461. d : pdirent;
  462. mountpoint : boolean;
  463. predot : string[255];
  464. procedure dodispose (p : pdir);
  465. begin
  466. dispose (p^.buf);
  467. dispose (p)
  468. end;
  469. {$endif}
  470. begin
  471. drivenr:=0;
  472. dir:='';
  473. {$ifndef crtlib}
  474. thedir:='/'#0;
  475. if sys_stat(@thedir[1],thisdir)<0 then
  476. exit;
  477. rootino:=thisdir.ino;
  478. rootdev:=thisdir.dev;
  479. thedir:='.'#0;
  480. if sys_stat(@thedir[1],thisdir)<0 then
  481. exit;
  482. thisino:=thisdir.ino;
  483. thisdev:=thisdir.dev;
  484. { Now we can uniquely identify the current and root dir }
  485. thedir:='';
  486. predot:='';
  487. while not ((thisino=rootino) and (thisdev=rootdev)) do
  488. begin
  489. { Are we on a mount point ? }
  490. dummy:=predot+'..'#0;
  491. if sys_stat(@dummy[1],thisdir)<0 then
  492. exit;
  493. dotdotino:=thisdir.ino;
  494. dotdotdev:=thisdir.dev;
  495. mountpoint:=(thisdev<>dotdotdev);
  496. { Now, Try to find the name of this dir in the previous one }
  497. dirstream:=opendir (@dummy[1]);
  498. if dirstream=nil then
  499. exit;
  500. repeat
  501. d:=sys_readdir (dirstream);
  502. if (d<>nil) and
  503. (not ((d^.name[0]='.') and ((d^.name[1]=#0) or ((d^.name[1]='.') and (d^.name[2]=#0))))) and
  504. (mountpoint or (d^.ino=thisino)) then
  505. begin
  506. dummy:=predot+'../'+strpas(@(d^.name[0]))+#0;
  507. if sys_stat (@(dummy[1]),thisdir)<0 then
  508. d:=nil;
  509. end;
  510. until (d=nil) or ((thisdir.dev=thisdev) and (thisdir.ino=thisino) );
  511. if (closedir (dirstream)<0) or (d=nil) then
  512. begin
  513. dodispose (dirstream);
  514. exit;
  515. end;
  516. { At this point, d.name contains the name of the current dir}
  517. thedir:='/'+strpas(@(d^.name[0]))+thedir;
  518. thisdev:=dotdotdev;
  519. thisino:=dotdotino;
  520. predot:=predot+'../';
  521. { We don't want to clutter op the heap with DIR records... }
  522. dodispose (dirstream);
  523. end;
  524. { Now rootino=thisino and rootdev=thisdev so we've reached / }
  525. dir:=thedir
  526. {$endif}
  527. end;
  528. {*****************************************************************************
  529. SystemUnit Initialization
  530. *****************************************************************************}
  531. Procedure SegFaultHandler (Sig : longint);
  532. begin
  533. if sig=11 then
  534. RunError (216);
  535. end;
  536. Procedure InstallSegFaultHandler;
  537. var
  538. sr : syscallregs;
  539. begin
  540. sr.reg2:=11;
  541. sr.reg3:=longint(@SegFaultHandler);
  542. syscall(syscall_nr_signal,sr);
  543. end;
  544. procedure OpenStdIO(var f:text;mode:word;const std:string;hdl:longint);
  545. begin
  546. Assign(f,std);
  547. TextRec(f).Handle:=hdl;
  548. TextRec(f).Mode:=mode;
  549. TextRec(f).InOutFunc:=@FileInOutFunc;
  550. TextRec(f).FlushFunc:=@FileInOutFunc;
  551. TextRec(f).Closefunc:=@fileclosefunc;
  552. end;
  553. Begin
  554. { Set up segfault Handler }
  555. InstallSegFaultHandler;
  556. { Setup heap }
  557. InitHeap;
  558. { Setup stdin, stdout and stderr }
  559. OpenStdIO(Input,fmInput,'stdin',StdInputHandle);
  560. OpenStdIO(Output,fmOutput,'stdout',StdOutputHandle);
  561. OpenStdIO(StdErr,fmOutput,'stderr',StdErrorHandle);
  562. { Reset IO Error }
  563. InOutRes:=0;
  564. End.
  565. {
  566. $Log$
  567. Revision 1.3 1998-05-12 10:42:48 peter
  568. * moved getopts to inc/, all supported OS's need argc,argv exported
  569. + strpas, strlen are now exported in the systemunit
  570. * removed logs
  571. * removed $ifdef ver_above
  572. Revision 1.2 1998/05/06 12:35:26 michael
  573. + Removed log from before restored version.
  574. }