osmain.inc 15 KB

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