sysposix.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. Stack check code
  24. *****************************************************************************}
  25. {
  26. {$IFOPT S+}
  27. {$DEFINE STACKCHECK}
  28. {$ENDIF}
  29. {$S-}
  30. procedure int_stackcheck(stack_size:longint);[public,alias:'FPC_STACKCHECK'];
  31. var
  32. c: cardinal;
  33. begin
  34. c := cardinal(Sptr) - cardinal(stack_size) - STACK_MARGIN;
  35. if (c <= cardinal(StackBottom)) then
  36. HandleError(202);
  37. end;
  38. {$IFDEF STACKCHECK}
  39. {$S+}
  40. {$ENDIF}
  41. {$UNDEF STACKCHECK}
  42. }
  43. {*****************************************************************************
  44. Misc. System Dependent Functions
  45. *****************************************************************************}
  46. procedure System_exit;
  47. begin
  48. sys_exit(cint(ExitCode));
  49. End;
  50. Function ParamCount: Longint;
  51. Begin
  52. Paramcount:=argc-1
  53. End;
  54. function BackPos(c:char; const s: shortstring): integer;
  55. var
  56. i: integer;
  57. Begin
  58. for i:=length(s) downto 0 do
  59. if s[i] = c then break;
  60. if i=0 then
  61. BackPos := 0
  62. else
  63. BackPos := i;
  64. end;
  65. { variable where full path and filename and executable is stored }
  66. { is setup by the startup of the system unit. }
  67. var
  68. execpathstr : shortstring;
  69. function paramstr(l: longint) : string;
  70. var
  71. s: string;
  72. s1: string;
  73. begin
  74. { stricly conforming POSIX applications }
  75. { have the executing filename as argv[0] }
  76. if l=0 then
  77. begin
  78. paramstr := execpathstr;
  79. end
  80. else
  81. paramstr:=strpas(argv[l]);
  82. end;
  83. Procedure Randomize;
  84. Begin
  85. randseed:=longint(sys_time(nil));
  86. End;
  87. {*****************************************************************************
  88. Low Level File Routines
  89. *****************************************************************************}
  90. {
  91. The lowlevel file functions should take care of setting the InOutRes to the
  92. correct value if an error has occured, else leave it untouched
  93. }
  94. Function PosixToRunError (PosixErrno : longint) : longint;
  95. {
  96. Convert ErrNo error to the correct Inoutres value
  97. }
  98. begin
  99. if PosixErrNo=0 then { Else it will go through all the cases }
  100. exit;
  101. case PosixErrNo of
  102. Sys_ENFILE,
  103. Sys_EMFILE : Inoutres:=4;
  104. Sys_ENOENT : Inoutres:=2;
  105. Sys_EBADF : Inoutres:=6;
  106. Sys_ENOMEM,
  107. Sys_EFAULT : Inoutres:=217;
  108. Sys_EINVAL : Inoutres:=218;
  109. Sys_EPIPE,
  110. Sys_EINTR,
  111. Sys_EIO,
  112. Sys_EAGAIN,
  113. Sys_ENOSPC : Inoutres:=101;
  114. Sys_ENAMETOOLONG : Inoutres := 3;
  115. Sys_EROFS,
  116. Sys_EEXIST,
  117. Sys_ENOTEMPTY,
  118. Sys_EACCES : Inoutres:=5;
  119. Sys_EISDIR : InOutRes:=5;
  120. else
  121. begin
  122. InOutRes := Integer(PosixErrno);
  123. end;
  124. end;
  125. end;
  126. Function Errno2InoutRes : longint;
  127. begin
  128. Errno2InoutRes:=PosixToRunError(Errno);
  129. InoutRes:=Errno2InoutRes;
  130. end;
  131. Procedure Do_Close(Handle:Longint);
  132. Begin
  133. sys_close(cint(Handle));
  134. End;
  135. Procedure Do_Erase(p:pchar);
  136. var
  137. fileinfo : stat;
  138. Begin
  139. { verify if the filename is actually a directory }
  140. { if so return error and do nothing, as defined }
  141. { by POSIX }
  142. if sys_stat(p,fileinfo)<0 then
  143. begin
  144. Errno2Inoutres;
  145. exit;
  146. end;
  147. if S_ISDIR(fileinfo.st_mode) then
  148. begin
  149. InOutRes := 2;
  150. exit;
  151. end;
  152. sys_unlink(p);
  153. Errno2Inoutres;
  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 sys_ftruncate(handle,fpos)<0 Then
  161. Errno2Inoutres
  162. Else
  163. InOutRes:=0;
  164. end;
  165. Procedure Do_Rename(p1,p2:pchar);
  166. Begin
  167. If sys_rename(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:=sys_write(Handle,pchar(addr),len);
  176. until ErrNo<>Sys_EINTR;
  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:=sys_read(Handle,pchar(addr),len);
  189. until ErrNo<>Sys_EINTR;
  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:=sys_lseek(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 sys_lseek(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:=sys_lseek(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:=sys_fstat(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:=sys_open(p,oflags,MODE_OPEN);
  301. if (ErrNo=Sys_EROFS) and ((OFlags and O_RDWR)<>0) then
  302. begin
  303. Oflags:=Oflags and not(O_RDWR);
  304. FileRec(f).Handle:=sys_open(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 sys_mkdir(@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 sys_rmdir(@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 sys_chdir(@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. sys_getcwd(@tmp[1],255);
  374. dir:=tmp;
  375. {$else}
  376. dir:='';
  377. thedir:='';
  378. dummy:='';
  379. { get root directory information }
  380. tmp := '/'+#0;
  381. if sys_stat(@tmp[1],rootinfo)<0 then
  382. Begin
  383. Errno2Inoutres;
  384. Exit
  385. End
  386. Else
  387. InOutRes:=0;
  388. repeat
  389. tmp := dummy+'.'+#0;
  390. { get current directory information }
  391. if sys_stat(@tmp[1],cwdinfo)<0 then
  392. Begin
  393. Errno2Inoutres;
  394. Exit
  395. End
  396. Else
  397. InOutRes:=0;
  398. tmp:=dummy+'..'+#0;
  399. { open directory stream }
  400. { try to find the current inode number of the cwd }
  401. dirstream:=sys_opendir(@tmp[1]);
  402. if dirstream=nil then
  403. exit;
  404. repeat
  405. name:='';
  406. d:=sys_readdir(dirstream);
  407. { no more entries to read ... }
  408. if not assigned(d) then
  409. begin
  410. break;
  411. end;
  412. tmp:=dummy+'../'+strpas(d^.d_name) + #0;
  413. if sys_stat(@tmp[1],thisdir)<0 then
  414. begin
  415. Errno2Inoutres;
  416. Exit
  417. End
  418. Else
  419. InOutRes:=0;
  420. { found the entry for this directory name }
  421. if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
  422. begin
  423. { are the filenames of type '.' or '..' ? }
  424. { then do not set the name. }
  425. if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
  426. ((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
  427. begin
  428. name:='/'+strpas(d^.d_name);
  429. end;
  430. end
  431. until (name<>'');
  432. If sys_closedir(dirstream)<0 THen
  433. Begin
  434. Errno2Inoutres;
  435. Exit
  436. End
  437. Else
  438. InOutRes:=0;
  439. thedir:=name+thedir;
  440. dummy:=dummy+'../';
  441. if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
  442. begin
  443. if thedir='' then
  444. dir:='/'
  445. else
  446. dir:=thedir;
  447. exit;
  448. end;
  449. until false;
  450. {$endif}
  451. end;
  452. {*****************************************************************************
  453. SystemUnit Initialization
  454. *****************************************************************************}
  455. procedure SignalToRunerror(signo: cint); cdecl;
  456. var
  457. res : word;
  458. begin
  459. res:=0;
  460. if signo = SIGFPE then
  461. begin
  462. res := 200;
  463. end
  464. else
  465. if (signo = SIGILL) or (signo = SIGBUS) or (signo = SIGSEGV) then
  466. begin
  467. res := 216;
  468. end;
  469. { give runtime error at the position where the signal was raised }
  470. if res<>0 then
  471. begin
  472. HandleError(res);
  473. end;
  474. end;
  475. var
  476. act: SigActionRec;
  477. Procedure InstallSignals;
  478. var
  479. oldact: SigActionRec;
  480. begin
  481. { Initialize the sigaction structure }
  482. { all flags and information set to zero }
  483. FillChar(act, sizeof(SigActionRec),0);
  484. { initialize handler }
  485. act.sa_handler := @SignalToRunError;
  486. sys_SigAction(SIGFPE,act,oldact);
  487. sys_SigAction(SIGSEGV,act,oldact);
  488. sys_SigAction(SIGBUS,act,oldact);
  489. sys_SigAction(SIGILL,act,oldact);
  490. end;
  491. procedure SetupCmdLine;
  492. var
  493. bufsize,
  494. len,j,
  495. size,i : longint;
  496. found : boolean;
  497. buf : pchar;
  498. procedure AddBuf;
  499. begin
  500. reallocmem(cmdline,size+bufsize);
  501. move(buf^,cmdline[size],bufsize);
  502. inc(size,bufsize);
  503. bufsize:=0;
  504. end;
  505. begin
  506. GetMem(buf,ARG_MAX);
  507. size:=0;
  508. bufsize:=0;
  509. i:=0;
  510. while (i<argc) do
  511. begin
  512. len:=strlen(argv[i]);
  513. if len>ARG_MAX-2 then
  514. len:=ARG_MAX-2;
  515. found:=false;
  516. for j:=1 to len do
  517. if argv[i][j]=' ' then
  518. begin
  519. found:=true;
  520. break;
  521. end;
  522. if bufsize+len>=ARG_MAX-2 then
  523. AddBuf;
  524. if found then
  525. begin
  526. buf[bufsize]:='"';
  527. inc(bufsize);
  528. end;
  529. move(argv[i]^,buf[bufsize],len);
  530. inc(bufsize,len);
  531. if found then
  532. begin
  533. buf[bufsize]:='"';
  534. inc(bufsize);
  535. end;
  536. if i<argc then
  537. buf[bufsize]:=' '
  538. else
  539. buf[bufsize]:=#0;
  540. inc(bufsize);
  541. inc(i);
  542. end;
  543. AddBuf;
  544. FreeMem(buf,ARG_MAX);
  545. end;
  546. (*
  547. Begin
  548. { Set up signals handlers }
  549. InstallSignals;
  550. { Setup heap }
  551. InitHeap;
  552. InitExceptions;
  553. { Arguments }
  554. SetupCmdLine;
  555. { Setup stdin, stdout and stderr }
  556. OpenStdIO(Input,fmInput,StdInputHandle);
  557. OpenStdIO(Output,fmOutput,StdOutputHandle);
  558. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  559. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  560. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  561. { Reset IO Error }
  562. InOutRes:=0;
  563. End.
  564. *)
  565. {
  566. $Log$
  567. Revision 1.9 2004-11-04 09:32:31 peter
  568. ErrOutput added
  569. Revision 1.8 2004/10/25 15:38:59 peter
  570. * compiler defined HEAP and HEAPSIZE removed
  571. Revision 1.7 2002/11/14 12:18:03 marco
  572. * fixed sys_time call to (NIL)
  573. Revision 1.6 2002/10/27 17:21:29 marco
  574. * Only "difficult" functions + execvp + termios + rewinddir left to do
  575. Revision 1.5 2002/10/26 18:27:52 marco
  576. * First series POSIX calls commits. Including getcwd.
  577. Revision 1.4 2002/09/07 16:01:26 peter
  578. * old logs removed and tabs fixed
  579. Revision 1.3 2002/08/20 12:50:22 marco
  580. * New errno handling. Should be libc compatible.
  581. Revision 1.2 2002/08/10 13:42:36 marco
  582. * Fixes Posix dir copied to devel branch
  583. Revision 1.1.2.18 2002/03/10 11:45:02 carl
  584. * InOutRes := 16 with rmdir()
  585. * InOutRes := 5 more checking
  586. Revision 1.1.2.17 2002/03/03 15:11:51 carl
  587. * erase() bugfix (erasing a directory is done via rmdir() only!)
  588. Revision 1.1.2.16 2002/02/15 18:13:35 carl
  589. * bugfix for paramstr(0)
  590. }