osmain.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. POSIX Interface to the system unit
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This is the core of the system unit *nix systems (now FreeBSD
  8. and Unix).
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. const
  14. { Default creation mode for directories and files }
  15. { read/write permission for everyone }
  16. MODE_OPEN = S_IWUSR OR S_IRUSR OR
  17. S_IWGRP OR S_IRGRP OR
  18. S_IWOTH OR S_IROTH;
  19. { read/write search permission for everyone }
  20. MODE_MKDIR = MODE_OPEN OR
  21. S_IXUSR OR S_IXGRP OR S_IXOTH;
  22. {*****************************************************************************
  23. Misc. System Dependent Functions
  24. *****************************************************************************}
  25. procedure System_exit;
  26. begin
  27. Fpexit(cint(ExitCode));
  28. End;
  29. Function ParamCount: Longint;
  30. Begin
  31. Paramcount:=argc-1
  32. End;
  33. function BackPos(c:char; const s: shortstring): integer;
  34. var
  35. i: integer;
  36. Begin
  37. for i:=length(s) downto 0 do
  38. if s[i] = c then break;
  39. if i=0 then
  40. BackPos := 0
  41. else
  42. BackPos := i;
  43. end;
  44. { variable where full path and filename and executable is stored }
  45. { is setup by the startup of the system unit. }
  46. var
  47. execpathstr : shortstring;
  48. function paramstr(l: longint) : string;
  49. var
  50. s: string;
  51. s1: string;
  52. begin
  53. { stricly conforming POSIX applications }
  54. { have the executing filename as argv[0] }
  55. if l=0 then
  56. begin
  57. paramstr := execpathstr;
  58. end
  59. else
  60. paramstr:=strpas(argv[l]);
  61. end;
  62. Procedure Randomize;
  63. Begin
  64. randseed:=longint(Fptime(nil));
  65. End;
  66. {*****************************************************************************
  67. Heap Management
  68. *****************************************************************************}
  69. var
  70. _HEAP : longint;external name 'HEAP';
  71. _HEAPSIZE : longint;external name 'HEAPSIZE';
  72. {$ifndef SYSTEM_HAS_GETHEAPSTART}
  73. function getheapstart:pointer;
  74. begin
  75. getheapstart := @_HEAP;
  76. end;
  77. {$endif}
  78. {$ifndef SYSTEM_HAS_GETHEAPSIZE}
  79. function getheapsize:longint;
  80. begin
  81. getheapsize := _HEAPSIZE;
  82. end;
  83. {$endif}
  84. {*****************************************************************************
  85. Low Level File Routines
  86. *****************************************************************************}
  87. {
  88. The lowlevel file functions should take care of setting the InOutRes to the
  89. correct value if an error has occured, else leave it untouched
  90. }
  91. Function PosixToRunError (PosixErrno : longint) : longint;
  92. {
  93. Convert ErrNo error to the correct Inoutres value
  94. }
  95. begin
  96. if PosixErrNo=0 then { Else it will go through all the cases }
  97. exit(0);
  98. case PosixErrNo of
  99. ESysENFILE,
  100. ESysEMFILE : Inoutres:=4;
  101. ESysENOENT : Inoutres:=2;
  102. ESysEBADF : Inoutres:=6;
  103. ESysENOMEM,
  104. ESysEFAULT : Inoutres:=217;
  105. ESysEINVAL : Inoutres:=218;
  106. ESysEPIPE,
  107. ESysEINTR,
  108. ESysEIO,
  109. ESysEAGAIN,
  110. ESysENOSPC : Inoutres:=101;
  111. ESysENAMETOOLONG : Inoutres := 3;
  112. ESysEROFS,
  113. ESysEEXIST,
  114. ESysENOTEMPTY,
  115. ESysEACCES : Inoutres:=5;
  116. ESysEISDIR : InOutRes:=5;
  117. else
  118. begin
  119. InOutRes := Integer(PosixErrno);
  120. end;
  121. end;
  122. PosixToRunError:=InOutRes;
  123. end;
  124. Function Errno2InoutRes : longint;
  125. begin
  126. Errno2InoutRes:=PosixToRunError(Errno);
  127. InoutRes:=Errno2InoutRes;
  128. end;
  129. Procedure Do_Close(Handle:Longint);
  130. Begin
  131. Fpclose(cint(Handle));
  132. End;
  133. Procedure Do_Erase(p:pchar);
  134. var
  135. fileinfo : stat;
  136. Begin
  137. { verify if the filename is actually a directory }
  138. { if so return error and do nothing, as defined }
  139. { by POSIX }
  140. if Fpstat(p,fileinfo)<0 then
  141. begin
  142. Errno2Inoutres;
  143. exit;
  144. end;
  145. if FpISDIR(fileinfo.st_mode) then
  146. begin
  147. InOutRes := 2;
  148. exit;
  149. end;
  150. if Fpunlink(p)<0 then
  151. Errno2Inoutres
  152. Else
  153. InOutRes:=0;
  154. End;
  155. { truncate at a given position }
  156. procedure do_truncate (handle,fpos:longint);
  157. begin
  158. { should be simulated in cases where it is not }
  159. { available. }
  160. If Fpftruncate(handle,fpos)<0 Then
  161. Errno2Inoutres
  162. Else
  163. InOutRes:=0;
  164. end;
  165. Procedure Do_Rename(p1,p2:pchar);
  166. Begin
  167. If Fprename(p1,p2)<0 Then
  168. Errno2Inoutres
  169. Else
  170. InOutRes:=0;
  171. End;
  172. Function Do_Write(Handle,Addr,Len:Longint):longint;
  173. Begin
  174. repeat
  175. Do_Write:=Fpwrite(Handle,pchar(addr),len);
  176. until ErrNo<>ESysEINTR;
  177. If Do_Write<0 Then
  178. Begin
  179. Errno2InOutRes;
  180. Do_Write:=0;
  181. End
  182. else
  183. InOutRes:=0;
  184. End;
  185. Function Do_Read(Handle,Addr,Len:Longint):Longint;
  186. Begin
  187. repeat
  188. Do_Read:=Fpread(Handle,pchar(addr),len);
  189. until ErrNo<>ESysEINTR;
  190. If Do_Read<0 Then
  191. Begin
  192. Errno2InOutRes;
  193. Do_Read:=0;
  194. End
  195. else
  196. InOutRes:=0;
  197. End;
  198. function Do_FilePos(Handle: Longint):longint;
  199. Begin
  200. do_FilePos:=Fplseek(Handle, 0, SEEK_CUR);
  201. If Do_FilePos<0 Then
  202. Errno2InOutRes
  203. else
  204. InOutRes:=0;
  205. End;
  206. Procedure Do_Seek(Handle,Pos:Longint);
  207. Begin
  208. If Fplseek(Handle, pos, SEEK_SET)<0 Then
  209. Errno2Inoutres
  210. Else
  211. InOutRes:=0;
  212. End;
  213. Function Do_SeekEnd(Handle:Longint): Longint;
  214. begin
  215. Do_SeekEnd:=Fplseek(Handle,0,SEEK_END);
  216. If Do_SeekEnd<0 Then
  217. Errno2Inoutres
  218. Else
  219. InOutRes:=0;
  220. end;
  221. Function Do_FileSize(Handle:Longint): Longint;
  222. var
  223. Info : Stat;
  224. Ret : Longint;
  225. Begin
  226. Ret:=Fpfstat(handle,info);
  227. If Ret=0 Then
  228. Do_FileSize:=Info.st_size
  229. else
  230. Do_FileSize:=0;
  231. If Ret<0 Then
  232. Errno2InOutRes
  233. Else
  234. InOutRes:=0;
  235. End;
  236. Procedure Do_Open(var f;p:pchar;flags:longint);
  237. {
  238. FileRec and textrec have both Handle and mode as the first items so
  239. they could use the same routine for opening/creating.
  240. when (flags and $100) the file will be append
  241. when (flags and $1000) the file will be truncate/rewritten
  242. when (flags and $10000) there is no check for close (needed for textfiles)
  243. }
  244. var
  245. oflags : cint;
  246. Begin
  247. { close first if opened }
  248. if ((flags and $10000)=0) then
  249. begin
  250. case FileRec(f).mode of
  251. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  252. fmclosed : ;
  253. else
  254. begin
  255. inoutres:=102; {not assigned}
  256. exit;
  257. end;
  258. end;
  259. end;
  260. { reset file Handle }
  261. FileRec(f).Handle:=UnusedHandle;
  262. { We do the conversion of filemodes here, concentrated on 1 place }
  263. case (flags and 3) of
  264. 0 : begin
  265. oflags :=O_RDONLY;
  266. FileRec(f).mode:=fminput;
  267. end;
  268. 1 : begin
  269. oflags :=O_WRONLY;
  270. FileRec(f).mode:=fmoutput;
  271. end;
  272. 2 : begin
  273. oflags :=O_RDWR;
  274. FileRec(f).mode:=fminout;
  275. end;
  276. end;
  277. if (flags and $1000)=$1000 then
  278. oflags:=oflags or (O_CREAT or O_TRUNC)
  279. else
  280. if (flags and $100)=$100 then
  281. oflags:=oflags or (O_APPEND);
  282. { empty name is special }
  283. if p[0]=#0 then
  284. begin
  285. case FileRec(f).mode of
  286. fminput :
  287. FileRec(f).Handle:=StdInputHandle;
  288. fminout, { this is set by rewrite }
  289. fmoutput :
  290. FileRec(f).Handle:=StdOutputHandle;
  291. fmappend :
  292. begin
  293. FileRec(f).Handle:=StdOutputHandle;
  294. FileRec(f).mode:=fmoutput; {fool fmappend}
  295. end;
  296. end;
  297. exit;
  298. end;
  299. { real open call }
  300. FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
  301. if (ErrNo=ESysEROFS) and ((OFlags and O_RDWR)<>0) then
  302. begin
  303. Oflags:=Oflags and not(O_RDWR);
  304. FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
  305. end;
  306. If Filerec(f).Handle<0 Then
  307. Errno2Inoutres
  308. else
  309. InOutRes:=0;
  310. End;
  311. {*****************************************************************************
  312. Directory Handling
  313. *****************************************************************************}
  314. Procedure MkDir(Const s: String);[IOCheck];
  315. Var
  316. Buffer: Array[0..255] of Char;
  317. Begin
  318. If (s='') or (InOutRes <> 0) then
  319. exit;
  320. Move(s[1], Buffer, Length(s));
  321. Buffer[Length(s)] := #0;
  322. If Fpmkdir(@buffer, MODE_MKDIR)<0 Then
  323. Errno2Inoutres
  324. Else
  325. InOutRes:=0;
  326. End;
  327. Procedure RmDir(Const s: String);[IOCheck];
  328. Var
  329. Buffer: Array[0..255] of Char;
  330. Begin
  331. if (s = '.') then
  332. InOutRes := 16;
  333. If (s='') or (InOutRes <> 0) then
  334. exit;
  335. Move(s[1], Buffer, Length(s));
  336. Buffer[Length(s)] := #0;
  337. If Fprmdir(@buffer)<0 Then
  338. Errno2Inoutres
  339. Else
  340. InOutRes:=0;
  341. End;
  342. Procedure ChDir(Const s: String);[IOCheck];
  343. Var
  344. Buffer: Array[0..255] of Char;
  345. Begin
  346. If (s='') or (InOutRes <> 0) then
  347. exit;
  348. Move(s[1], Buffer, Length(s));
  349. Buffer[Length(s)] := #0;
  350. If Fpchdir(@buffer)<0 Then
  351. Errno2Inoutres
  352. Else
  353. InOutRes:=0;
  354. { file not exists is path not found under tp7 }
  355. if InOutRes=2 then
  356. InOutRes:=3;
  357. End;
  358. { // $define usegetcwd}
  359. procedure getdir(drivenr : byte;var dir : shortstring);
  360. var
  361. {$ifndef usegetcwd}
  362. cwdinfo : stat;
  363. rootinfo : stat;
  364. thedir,dummy : string[255];
  365. dirstream : pdir;
  366. d : pdirent;
  367. name : string[255];
  368. thisdir : stat;
  369. {$endif}
  370. tmp : string[255];
  371. begin
  372. {$ifdef usegetcwd}
  373. Fpgetcwd(@tmp[1],255);
  374. dir:=tmp;
  375. {$else}
  376. dir:='';
  377. thedir:='';
  378. dummy:='';
  379. { get root directory information }
  380. tmp := '/'+#0;
  381. if Fpstat(@tmp[1],rootinfo)<0 then
  382. Exit;
  383. repeat
  384. tmp := dummy+'.'+#0;
  385. { get current directory information }
  386. if Fpstat(@tmp[1],cwdinfo)<0 then
  387. Exit;
  388. tmp:=dummy+'..'+#0;
  389. { open directory stream }
  390. { try to find the current inode number of the cwd }
  391. dirstream:=Fpopendir(@tmp[1]);
  392. if dirstream=nil then
  393. exit;
  394. repeat
  395. name:='';
  396. d:=Fpreaddir(dirstream);
  397. { no more entries to read ... }
  398. if not assigned(d) then
  399. break;
  400. tmp:=dummy+'../'+strpas(d^.d_name) + #0;
  401. if (Fpstat(@tmp[1],thisdir)=0) then
  402. begin
  403. { found the entry for this directory name }
  404. if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
  405. begin
  406. { are the filenames of type '.' or '..' ? }
  407. { then do not set the name. }
  408. if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
  409. ((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
  410. name:='/'+strpas(d^.d_name);
  411. end;
  412. end
  413. else
  414. begin
  415. { Ignore possible broken symlinks }
  416. if (Errno<>ESysENOENT) then
  417. Exit;
  418. end;
  419. until (name<>'');
  420. If Fpclosedir(dirstream)<0 THen
  421. Exit;
  422. thedir:=name+thedir;
  423. dummy:=dummy+'../';
  424. if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
  425. begin
  426. if thedir='' then
  427. dir:='/'
  428. else
  429. dir:=thedir;
  430. exit;
  431. end;
  432. until false;
  433. {$endif}
  434. end;
  435. {*****************************************************************************
  436. SystemUnit Initialization
  437. *****************************************************************************}
  438. procedure SignalToRunerror(signo: cint); cdecl;
  439. var
  440. res : word;
  441. begin
  442. res:=0;
  443. if signo = SIGFPE then
  444. begin
  445. res := 200;
  446. end
  447. else
  448. if (signo = SIGILL) or (signo = SIGBUS) or (signo = SIGSEGV) then
  449. begin
  450. res := 216;
  451. end;
  452. { give runtime error at the position where the signal was raised }
  453. if res<>0 then
  454. begin
  455. HandleError(res);
  456. end;
  457. end;
  458. var
  459. act: SigActionRec;
  460. Procedure InstallSignals;
  461. var
  462. oldact: SigActionRec;
  463. begin
  464. { Initialize the sigaction structure }
  465. { all flags and information set to zero }
  466. FillChar(act, sizeof(SigActionRec),0);
  467. { initialize handler }
  468. act.sa_handler := @SignalToRunError;
  469. FpSigAction(SIGFPE,@act,@oldact);
  470. FpSigAction(SIGSEGV,@act,@oldact);
  471. FpSigAction(SIGBUS,@act,@oldact);
  472. FpSigAction(SIGILL,@act,@oldact);
  473. end;
  474. procedure SetupCmdLine;
  475. var
  476. bufsize,
  477. len,j,
  478. size,i : longint;
  479. found : boolean;
  480. buf : pchar;
  481. procedure AddBuf;
  482. begin
  483. reallocmem(cmdline,size+bufsize);
  484. move(buf^,cmdline[size],bufsize);
  485. inc(size,bufsize);
  486. bufsize:=0;
  487. end;
  488. begin
  489. GetMem(buf,ARG_MAX);
  490. size:=0;
  491. bufsize:=0;
  492. i:=0;
  493. while (i<argc) do
  494. begin
  495. len:=strlen(argv[i]);
  496. if len>ARG_MAX-2 then
  497. len:=ARG_MAX-2;
  498. found:=false;
  499. for j:=1 to len do
  500. if argv[i][j]=' ' then
  501. begin
  502. found:=true;
  503. break;
  504. end;
  505. if bufsize+len>=ARG_MAX-2 then
  506. AddBuf;
  507. if found then
  508. begin
  509. buf[bufsize]:='"';
  510. inc(bufsize);
  511. end;
  512. move(argv[i]^,buf[bufsize],len);
  513. inc(bufsize,len);
  514. if found then
  515. begin
  516. buf[bufsize]:='"';
  517. inc(bufsize);
  518. end;
  519. if i<argc then
  520. buf[bufsize]:=' '
  521. else
  522. buf[bufsize]:=#0;
  523. inc(bufsize);
  524. inc(i);
  525. end;
  526. AddBuf;
  527. FreeMem(buf,ARG_MAX);
  528. end;
  529. (*
  530. Begin
  531. { Set up signals handlers }
  532. InstallSignals;
  533. { Setup heap }
  534. InitHeap;
  535. InitExceptions;
  536. { Arguments }
  537. SetupCmdLine;
  538. { Setup stdin, stdout and stderr }
  539. OpenStdIO(Input,fmInput,StdInputHandle);
  540. OpenStdIO(Output,fmOutput,StdOutputHandle);
  541. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  542. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  543. { Reset IO Error }
  544. InOutRes:=0;
  545. End.
  546. *)
  547. {
  548. $Log$
  549. Revision 1.4 2002-12-24 19:45:40 peter
  550. * Fix do_erase which was wrong with inoutres setting
  551. Revision 1.3 2002/12/23 22:23:43 peter
  552. * fixed Getdir to not set Inoutres
  553. * broken symlinks are now ignored in getdir instead of aborting
  554. the search
  555. Revision 1.2 2002/12/18 20:43:27 peter
  556. * removed stackcheck, the generic stackcheck is used
  557. * fixed return value for error conversion when no error was passed
  558. Revision 1.1 2002/12/18 16:43:26 marco
  559. * new unix rtl, linux part.....
  560. Revision 1.7 2002/11/14 12:18:03 marco
  561. * fixed Fptime call to (NIL)
  562. Revision 1.6 2002/10/27 17:21:29 marco
  563. * Only "difficult" functions + execvp + termios + rewinddir left to do
  564. Revision 1.5 2002/10/26 18:27:52 marco
  565. * First series POSIX calls commits. Including getcwd.
  566. Revision 1.4 2002/09/07 16:01:26 peter
  567. * old logs removed and tabs fixed
  568. Revision 1.3 2002/08/20 12:50:22 marco
  569. * New errno handling. Should be libc compatible.
  570. Revision 1.2 2002/08/10 13:42:36 marco
  571. * Fixes Posix dir copied to devel branch
  572. Revision 1.1.2.18 2002/03/10 11:45:02 carl
  573. * InOutRes := 16 with rmdir()
  574. * InOutRes := 5 more checking
  575. Revision 1.1.2.17 2002/03/03 15:11:51 carl
  576. * erase() bugfix (erasing a directory is done via rmdir() only!)
  577. Revision 1.1.2.16 2002/02/15 18:13:35 carl
  578. * bugfix for paramstr(0)
  579. }