osmain.inc 15 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(getErrno);
  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 FpS_ISDIR(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:SizeInt):longint;
  173. Begin
  174. repeat
  175. Do_Write:=Fpwrite(Handle,pchar(addr),len);
  176. until (Do_Write>=0) or (getErrNo<>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:SizeInt):Longint;
  186. Begin
  187. repeat
  188. Do_Read:=Fpread(Handle,pchar(addr),len);
  189. until (Do_Read>=0) or (getErrNo<>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 (FileRec(f).Handle<0) and
  302. (getErrNo=ESysEROFS) and
  303. ((OFlags and O_RDWR)<>0) then
  304. begin
  305. Oflags:=Oflags and not(O_RDWR);
  306. FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
  307. end;
  308. If Filerec(f).Handle<0 Then
  309. Errno2Inoutres
  310. else
  311. InOutRes:=0;
  312. End;
  313. {*****************************************************************************
  314. Directory Handling
  315. *****************************************************************************}
  316. Procedure MkDir(Const s: String);[IOCheck];
  317. Var
  318. Buffer: Array[0..255] of Char;
  319. Begin
  320. If (s='') or (InOutRes <> 0) then
  321. exit;
  322. Move(s[1], Buffer, Length(s));
  323. Buffer[Length(s)] := #0;
  324. If Fpmkdir(@buffer, MODE_MKDIR)<0 Then
  325. Errno2Inoutres
  326. Else
  327. InOutRes:=0;
  328. End;
  329. Procedure RmDir(Const s: String);[IOCheck];
  330. Var
  331. Buffer: Array[0..255] of Char;
  332. Begin
  333. if (s = '.') then
  334. InOutRes := 16;
  335. If (s='') or (InOutRes <> 0) then
  336. exit;
  337. Move(s[1], Buffer, Length(s));
  338. Buffer[Length(s)] := #0;
  339. If Fprmdir(@buffer)<0 Then
  340. Errno2Inoutres
  341. Else
  342. InOutRes:=0;
  343. End;
  344. Procedure ChDir(Const s: String);[IOCheck];
  345. Var
  346. Buffer: Array[0..255] of Char;
  347. Begin
  348. If (s='') or (InOutRes <> 0) then
  349. exit;
  350. Move(s[1], Buffer, Length(s));
  351. Buffer[Length(s)] := #0;
  352. If Fpchdir(@buffer)<0 Then
  353. Errno2Inoutres
  354. Else
  355. InOutRes:=0;
  356. { file not exists is path not found under tp7 }
  357. if InOutRes=2 then
  358. InOutRes:=3;
  359. End;
  360. { // $define usegetcwd}
  361. procedure getdir(drivenr : byte;var dir : shortstring);
  362. var
  363. {$ifndef usegetcwd}
  364. cwdinfo : stat;
  365. rootinfo : stat;
  366. thedir,dummy : string[255];
  367. dirstream : pdir;
  368. d : pdirent;
  369. name : string[255];
  370. thisdir : stat;
  371. {$endif}
  372. tmp : string[255];
  373. begin
  374. {$ifdef usegetcwd}
  375. Fpgetcwd(@tmp[1],255);
  376. dir:=tmp;
  377. {$else}
  378. dir:='';
  379. thedir:='';
  380. dummy:='';
  381. { get root directory information }
  382. tmp := '/'+#0;
  383. if Fpstat(@tmp[1],rootinfo)<0 then
  384. Exit;
  385. repeat
  386. tmp := dummy+'.'+#0;
  387. { get current directory information }
  388. if Fpstat(@tmp[1],cwdinfo)<0 then
  389. Exit;
  390. tmp:=dummy+'..'+#0;
  391. { open directory stream }
  392. { try to find the current inode number of the cwd }
  393. dirstream:=Fpopendir(@tmp[1]);
  394. if dirstream=nil then
  395. exit;
  396. repeat
  397. name:='';
  398. d:=Fpreaddir(dirstream);
  399. { no more entries to read ... }
  400. if not assigned(d) then
  401. break;
  402. tmp:=dummy+'../'+strpas(d^.d_name) + #0;
  403. if (Fpstat(@tmp[1],thisdir)=0) then
  404. begin
  405. { found the entry for this directory name }
  406. if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
  407. begin
  408. { are the filenames of type '.' or '..' ? }
  409. { then do not set the name. }
  410. if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
  411. ((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
  412. name:='/'+strpas(d^.d_name);
  413. end;
  414. end;
  415. until (name<>'');
  416. If Fpclosedir(dirstream)<0 THen
  417. Exit;
  418. thedir:=name+thedir;
  419. dummy:=dummy+'../';
  420. if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
  421. begin
  422. if thedir='' then
  423. dir:='/'
  424. else
  425. dir:=thedir;
  426. exit;
  427. end;
  428. until false;
  429. {$endif}
  430. end;
  431. {*****************************************************************************
  432. SystemUnit Initialization
  433. *****************************************************************************}
  434. // signal handler is arch dependant due to processorexception to language
  435. // exception translation
  436. {$i sighnd.inc}
  437. var
  438. act: SigActionRec;
  439. Procedure InstallSignals;
  440. var
  441. oldact: SigActionRec;
  442. begin
  443. { Initialize the sigaction structure }
  444. { all flags and information set to zero }
  445. FillChar(act, sizeof(SigActionRec),0);
  446. { initialize handler }
  447. act.sa_handler := signalhandler(@SignalToRunError);
  448. FpSigAction(SIGFPE,@act,@oldact);
  449. FpSigAction(SIGSEGV,@act,@oldact);
  450. FpSigAction(SIGBUS,@act,@oldact);
  451. FpSigAction(SIGILL,@act,@oldact);
  452. end;
  453. procedure SetupCmdLine;
  454. var
  455. bufsize,
  456. len,j,
  457. size,i : longint;
  458. found : boolean;
  459. buf : pchar;
  460. procedure AddBuf;
  461. begin
  462. reallocmem(cmdline,size+bufsize);
  463. move(buf^,cmdline[size],bufsize);
  464. inc(size,bufsize);
  465. bufsize:=0;
  466. end;
  467. begin
  468. GetMem(buf,ARG_MAX);
  469. size:=0;
  470. bufsize:=0;
  471. i:=0;
  472. while (i<argc) do
  473. begin
  474. len:=strlen(argv[i]);
  475. if len>ARG_MAX-2 then
  476. len:=ARG_MAX-2;
  477. found:=false;
  478. for j:=1 to len do
  479. if argv[i][j]=' ' then
  480. begin
  481. found:=true;
  482. break;
  483. end;
  484. if bufsize+len>=ARG_MAX-2 then
  485. AddBuf;
  486. if found then
  487. begin
  488. buf[bufsize]:='"';
  489. inc(bufsize);
  490. end;
  491. move(argv[i]^,buf[bufsize],len);
  492. inc(bufsize,len);
  493. if found then
  494. begin
  495. buf[bufsize]:='"';
  496. inc(bufsize);
  497. end;
  498. if i<argc then
  499. buf[bufsize]:=' '
  500. else
  501. buf[bufsize]:=#0;
  502. inc(bufsize);
  503. inc(i);
  504. end;
  505. AddBuf;
  506. FreeMem(buf,ARG_MAX);
  507. end;
  508. (*
  509. Begin
  510. { Set up signals handlers }
  511. InstallSignals;
  512. { Setup heap }
  513. InitHeap;
  514. InitExceptions;
  515. { Arguments }
  516. SetupCmdLine;
  517. { Setup stdin, stdout and stderr }
  518. OpenStdIO(Input,fmInput,StdInputHandle);
  519. OpenStdIO(Output,fmOutput,StdOutputHandle);
  520. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  521. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  522. { Reset IO Error }
  523. InOutRes:=0;
  524. End.
  525. *)
  526. {
  527. $Log$
  528. Revision 1.11 2003-12-30 16:26:10 marco
  529. * some more fixes. Testing on idefix
  530. Revision 1.10 2003/12/21 20:30:49 peter
  531. * don't exit in getdir when fpstat gives a failure
  532. Revision 1.9 2003/12/14 14:28:36 peter
  533. * only check errno if the syscall failed
  534. Revision 1.8 2003/11/01 01:58:11 marco
  535. * more small fixes.
  536. Revision 1.7 2003/10/31 20:36:01 marco
  537. * i386 specific fixes that hopefully fix texception4.
  538. Only the "generic" signal handler was ported to the unix rtl.
  539. Revision 1.6 2003/09/27 12:51:33 peter
  540. * fpISxxx macros renamed to C compliant fpS_ISxxx
  541. Revision 1.5 2003/05/01 08:05:23 florian
  542. * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
  543. Revision 1.4 2002/12/24 19:45:40 peter
  544. * Fix do_erase which was wrong with inoutres setting
  545. Revision 1.3 2002/12/23 22:23:43 peter
  546. * fixed Getdir to not set Inoutres
  547. * broken symlinks are now ignored in getdir instead of aborting
  548. the search
  549. Revision 1.2 2002/12/18 20:43:27 peter
  550. * removed stackcheck, the generic stackcheck is used
  551. * fixed return value for error conversion when no error was passed
  552. Revision 1.1 2002/12/18 16:43:26 marco
  553. * new unix rtl, linux part.....
  554. Revision 1.7 2002/11/14 12:18:03 marco
  555. * fixed Fptime call to (NIL)
  556. Revision 1.6 2002/10/27 17:21:29 marco
  557. * Only "difficult" functions + execvp + termios + rewinddir left to do
  558. Revision 1.5 2002/10/26 18:27:52 marco
  559. * First series POSIX calls commits. Including getcwd.
  560. Revision 1.4 2002/09/07 16:01:26 peter
  561. * old logs removed and tabs fixed
  562. Revision 1.3 2002/08/20 12:50:22 marco
  563. * New errno handling. Should be libc compatible.
  564. Revision 1.2 2002/08/10 13:42:36 marco
  565. * Fixes Posix dir copied to devel branch
  566. Revision 1.1.2.18 2002/03/10 11:45:02 carl
  567. * InOutRes := 16 with rmdir()
  568. * InOutRes := 5 more checking
  569. Revision 1.1.2.17 2002/03/03 15:11:51 carl
  570. * erase() bugfix (erasing a directory is done via rmdir() only!)
  571. Revision 1.1.2.16 2002/02/15 18:13:35 carl
  572. * bugfix for paramstr(0)
  573. }