osmain.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. procedure getdir(drivenr : byte;var dir : shortstring);
  361. var
  362. {$ifndef usegetcwd}
  363. cwdinfo : stat;
  364. rootinfo : stat;
  365. thedir,dummy : string[255];
  366. dirstream : pdir;
  367. d : pdirent;
  368. name : string[255];
  369. thisdir : stat;
  370. tmp : string[255];
  371. {$else}
  372. tmp : array[0..4095] of char;
  373. {$endif}
  374. begin
  375. {$ifdef usegetcwd}
  376. if Fpgetcwd(@tmp,10240+512)<>NIL then
  377. dir:=pchar(@tmp)
  378. else
  379. begin
  380. dir:='';
  381. writeln(geterrno);
  382. end;
  383. {$else}
  384. dir:='';
  385. thedir:='';
  386. dummy:='';
  387. { get root directory information }
  388. tmp := '/'+#0;
  389. if Fpstat(@tmp[1],rootinfo)<0 then
  390. Exit;
  391. repeat
  392. tmp := dummy+'.'+#0;
  393. { get current directory information }
  394. if Fpstat(@tmp[1],cwdinfo)<0 then
  395. Exit;
  396. tmp:=dummy+'..'+#0;
  397. { open directory stream }
  398. { try to find the current inode number of the cwd }
  399. dirstream:=Fpopendir(@tmp[1]);
  400. if dirstream=nil then
  401. exit;
  402. repeat
  403. name:='';
  404. d:=Fpreaddir(dirstream);
  405. { no more entries to read ... }
  406. if not assigned(d) then
  407. break;
  408. tmp:=dummy+'../'+strpas(d^.d_name) + #0;
  409. if (Fpstat(@tmp[1],thisdir)=0) then
  410. begin
  411. { found the entry for this directory name }
  412. if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
  413. begin
  414. { are the filenames of type '.' or '..' ? }
  415. { then do not set the name. }
  416. if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
  417. ((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
  418. name:='/'+strpas(d^.d_name);
  419. end;
  420. end;
  421. until (name<>'');
  422. If Fpclosedir(dirstream)<0 THen
  423. Exit;
  424. thedir:=name+thedir;
  425. dummy:=dummy+'../';
  426. if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
  427. begin
  428. if thedir='' then
  429. dir:='/'
  430. else
  431. dir:=thedir;
  432. exit;
  433. end;
  434. until false;
  435. {$endif}
  436. end;
  437. {*****************************************************************************
  438. SystemUnit Initialization
  439. *****************************************************************************}
  440. // signal handler is arch dependant due to processorexception to language
  441. // exception translation
  442. {$i sighnd.inc}
  443. var
  444. act: SigActionRec;
  445. Procedure InstallSignals;
  446. var
  447. oldact: SigActionRec;
  448. begin
  449. { Initialize the sigaction structure }
  450. { all flags and information set to zero }
  451. FillChar(act, sizeof(SigActionRec),0);
  452. { initialize handler }
  453. act.sa_handler := signalhandler(@SignalToRunError);
  454. FpSigAction(SIGFPE,@act,@oldact);
  455. FpSigAction(SIGSEGV,@act,@oldact);
  456. FpSigAction(SIGBUS,@act,@oldact);
  457. FpSigAction(SIGILL,@act,@oldact);
  458. end;
  459. procedure SetupCmdLine;
  460. var
  461. bufsize,
  462. len,j,
  463. size,i : longint;
  464. found : boolean;
  465. buf : pchar;
  466. procedure AddBuf;
  467. begin
  468. reallocmem(cmdline,size+bufsize);
  469. move(buf^,cmdline[size],bufsize);
  470. inc(size,bufsize);
  471. bufsize:=0;
  472. end;
  473. begin
  474. GetMem(buf,ARG_MAX);
  475. size:=0;
  476. bufsize:=0;
  477. i:=0;
  478. while (i<argc) do
  479. begin
  480. len:=strlen(argv[i]);
  481. if len>ARG_MAX-2 then
  482. len:=ARG_MAX-2;
  483. found:=false;
  484. for j:=1 to len do
  485. if argv[i][j]=' ' then
  486. begin
  487. found:=true;
  488. break;
  489. end;
  490. if bufsize+len>=ARG_MAX-2 then
  491. AddBuf;
  492. if found then
  493. begin
  494. buf[bufsize]:='"';
  495. inc(bufsize);
  496. end;
  497. move(argv[i]^,buf[bufsize],len);
  498. inc(bufsize,len);
  499. if found then
  500. begin
  501. buf[bufsize]:='"';
  502. inc(bufsize);
  503. end;
  504. if i<argc then
  505. buf[bufsize]:=' '
  506. else
  507. buf[bufsize]:=#0;
  508. inc(bufsize);
  509. inc(i);
  510. end;
  511. AddBuf;
  512. FreeMem(buf,ARG_MAX);
  513. end;
  514. {
  515. $Log$
  516. Revision 1.12 2004-01-01 14:19:55 marco
  517. * use_getcwd updates because FPC_USE_LIBC uses that
  518. Revision 1.11 2003/12/30 16:26:10 marco
  519. * some more fixes. Testing on idefix
  520. Revision 1.10 2003/12/21 20:30:49 peter
  521. * don't exit in getdir when fpstat gives a failure
  522. Revision 1.9 2003/12/14 14:28:36 peter
  523. * only check errno if the syscall failed
  524. Revision 1.8 2003/11/01 01:58:11 marco
  525. * more small fixes.
  526. Revision 1.7 2003/10/31 20:36:01 marco
  527. * i386 specific fixes that hopefully fix texception4.
  528. Only the "generic" signal handler was ported to the unix rtl.
  529. Revision 1.6 2003/09/27 12:51:33 peter
  530. * fpISxxx macros renamed to C compliant fpS_ISxxx
  531. Revision 1.5 2003/05/01 08:05:23 florian
  532. * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
  533. Revision 1.4 2002/12/24 19:45:40 peter
  534. * Fix do_erase which was wrong with inoutres setting
  535. Revision 1.3 2002/12/23 22:23:43 peter
  536. * fixed Getdir to not set Inoutres
  537. * broken symlinks are now ignored in getdir instead of aborting
  538. the search
  539. Revision 1.2 2002/12/18 20:43:27 peter
  540. * removed stackcheck, the generic stackcheck is used
  541. * fixed return value for error conversion when no error was passed
  542. Revision 1.1 2002/12/18 16:43:26 marco
  543. * new unix rtl, linux part.....
  544. Revision 1.7 2002/11/14 12:18:03 marco
  545. * fixed Fptime call to (NIL)
  546. Revision 1.6 2002/10/27 17:21:29 marco
  547. * Only "difficult" functions + execvp + termios + rewinddir left to do
  548. Revision 1.5 2002/10/26 18:27:52 marco
  549. * First series POSIX calls commits. Including getcwd.
  550. Revision 1.4 2002/09/07 16:01:26 peter
  551. * old logs removed and tabs fixed
  552. Revision 1.3 2002/08/20 12:50:22 marco
  553. * New errno handling. Should be libc compatible.
  554. Revision 1.2 2002/08/10 13:42:36 marco
  555. * Fixes Posix dir copied to devel branch
  556. Revision 1.1.2.18 2002/03/10 11:45:02 carl
  557. * InOutRes := 16 with rmdir()
  558. * InOutRes := 5 more checking
  559. Revision 1.1.2.17 2002/03/03 15:11:51 carl
  560. * erase() bugfix (erasing a directory is done via rmdir() only!)
  561. Revision 1.1.2.16 2002/02/15 18:13:35 carl
  562. * bugfix for paramstr(0)
  563. }