osmain.inc 15 KB

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