ossysc.inc 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. {
  2. Copyright (c) 2002 by Marco van de Voort
  3. The base *BSD syscalls required to implement the system unit. These
  4. are aliased for use in other units (to avoid poluting the system units
  5. interface)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. ****************************************************************************
  12. }
  13. {$i ostypes.inc}
  14. {$ifdef FPC_USE_LIBC}
  15. {$Linklib root}
  16. // Out of date atm.
  17. const clib = 'root';
  18. const netlib = 'network';
  19. {$ifdef FPC_IS_SYSTEM}
  20. {$i oscdeclh.inc}
  21. {$endif}
  22. {$I osmacro.inc}
  23. { var
  24. Errno : cint; external name 'errno';
  25. function Fptime(tloc:ptime_t): time_t; cdecl; external name 'time';
  26. function Fpopen(const path: pchar; flags : cint; mode: mode_t):cint; cdecl; external name 'open';
  27. function Fpclose(fd : cint): cint; cdecl; external name 'close';
  28. function Fplseek(fd : cint; offset : off_t; whence : cint): off_t; cdecl; external name 'lseek';
  29. function Fpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; cdecl; external name 'read';
  30. function Fpwrite(fd: cint;const buf:pchar; nbytes : size_t): ssize_t; cdecl; external name 'write';
  31. function Fpunlink(const path: pchar): cint; cdecl; external name 'unlink';
  32. function Fprename(const old : pchar; const newpath: pchar): cint; cdecl;external name 'rename';
  33. function Fpstat(const path: pchar; var buf : stat): cint; cdecl; external name 'stat';
  34. function Fpchdir(const path : pchar): cint; cdecl; external name 'chdir';
  35. function Fpmkdir(const path : pchar; mode: mode_t):cint; cdecl; external name 'mkdir';
  36. function Fprmdir(const path : pchar): cint; cdecl; external name 'rmdir';
  37. function Fpopendir(const dirname : pchar): pdir; cdecl; external name 'opendir';
  38. function Fpreaddir(var dirp : dir) : pdirent;cdecl; external name 'readdir';
  39. function Fpclosedir(var dirp : dir): cint; cdecl; external name 'closedir';
  40. procedure Fpexit(status : cint); cdecl; external name '_exit';
  41. function Fpsigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint; cdecl; external name 'sigaction';
  42. function Fpftruncate(fd : cint; flength : off_t): cint; cdecl; external name 'ftruncate';
  43. function Fprename(const old : pchar; const newpath: pchar): cint; cdecl;external name 'rename';
  44. function Fpfstat(fd : cint; var sb : stat): cint; cdecl; external name 'fstat';
  45. function Fpfork : pid_t; cdecl; external name 'fork';
  46. function Fpexecve(const path : pchar; const argv : ppchar; const envp: ppchar): cint; cdecl; external name 'execve';
  47. function Fpwaitpid(pid : pid_t; tat_loc : pcint; options: cint): pid_t; cdecl; external name 'waitpid';
  48. function Fpaccess(const pathname : pchar; amode : cint): cint; cdecl; external name 'access';
  49. function Fpuname(var name: utsname): cint; cdecl; external name 'uname';
  50. function FpDup(oldd:cint):cint; cdecl; external name 'dup';
  51. function FpDup2(oldd:cint;newd:cint):cint; cdecl; external name 'dup2';
  52. }
  53. {$else}
  54. {*****************************************************************************
  55. --- Main:The System Call Self ---
  56. *****************************************************************************}
  57. { The system designed for Linux can't be used for *BSD so easily, since
  58. *BSD pushes arguments, instead of loading them to registers.}
  59. // Var ErrNo : Longint;
  60. {$I syscallh.inc}
  61. {$I syscall.inc}
  62. {$I sysnr.inc}
  63. {$I osmacro.inc}
  64. // Should be moved to a FreeBSD specific unit in the future.
  65. function Fptime( tloc:ptime): time_t; [public, alias : 'FPC_SYSC_TIME'];
  66. {VAR tv : timeval;
  67. tz : timezone;
  68. retval : longint;
  69. }
  70. var
  71. args : SysCallArgs;
  72. begin
  73. { don't treat errno, since there is never any }
  74. tloc^ := Do_Syscall(syscall_nr_time,args);
  75. fptime := tloc^;
  76. {begin
  77. // Retval:=do_syscall(syscall_nr_gettimeofday,TSysParam(@tv),TSysParam(@tz));
  78. If retval=-1 then
  79. Fptime:=-1
  80. else
  81. Begin
  82. If Assigned(tloc) Then
  83. TLoc^:=tv.tv_sec;
  84. Fptime:=tv.tv_sec;
  85. End;
  86. }
  87. End;
  88. {*****************************************************************************
  89. --- File:File handling related calls ---
  90. *****************************************************************************}
  91. function Fpopen(path: pchar; flags : cint; mode: mode_t):cint; [public, alias : 'FPC_SYSC_OPEN'];
  92. var
  93. args: SysCallArgs;
  94. begin
  95. args.param[1] := $FFFFFFFF;
  96. args.param[2] := cint(path);
  97. args.param[3] := flags;
  98. args.param[4] := cint(mode);
  99. args.param[5] := 0; { close on execute flag }
  100. fpopen:= SysCall(syscall_nr_open, args);
  101. {Begin
  102. Fpopen:=do_syscall(syscall_nr_open,TSysParam(path),TSysParam(flags),TSysParam(mode));
  103. }
  104. End;
  105. function Fpclose(fd : cint): cint; [public, alias : 'FPC_SYSC_CLOSE'];
  106. var
  107. args : SysCallArgs;
  108. begin
  109. args.param[1] := fd;
  110. fpclose:=SysCall(syscall_nr_close,args);
  111. {begin
  112. Fpclose:=do_syscall(syscall_nr_close,fd);
  113. }
  114. end;
  115. {$ifdef netbsd}
  116. {$ifdef cpupowerpc}
  117. {$define netbsdmacppc}
  118. {$endif}
  119. {$endif}
  120. {$ifdef netbsdmacppc}
  121. {$i sysofft.inc} // odd ball calling convention.
  122. {$else}
  123. // generic versions.
  124. function Fplseek(fd : cint; offset : off_t; whence : cint): off_t; [public, alias : 'FPC_SYSC_LSEEK'];
  125. {
  126. this one is special for the return value being 64-bit..
  127. hi/lo offset not yet tested.
  128. NetBSD: ok, but implicit return value in edx:eax
  129. FreeBSD: same implementation as NetBSD.
  130. }
  131. var
  132. args: SysCallArgs;
  133. begin
  134. args.param[1] := fd;
  135. args.param[2] := cint(offset and $FFFFFFFF);
  136. args.param[3] := cint((offset shr 32) and $FFFFFFFF);
  137. args.param[4] := whence;
  138. { we currently only support seeks upto 32-bit in length }
  139. fplseek := off_t(SysCall(syscall_nr_lseek,args));
  140. (*begin
  141. Fplseek:=do_syscall(syscall_nr___syscall,syscall_nr_lseek,0,TSysParam(fd),0,lo(Offset),{0} hi(offset),Whence);
  142. *)
  143. end;
  144. type
  145. { _kwstat_ kernel call structure }
  146. pwstat = ^twstat;
  147. twstat = packed record
  148. {00} filler : array[1..3] of longint;
  149. {12} newmode : mode_t; { chmod mode_t parameter }
  150. {16} unknown1 : longint;
  151. {20} newuser : uid_t; { chown uid_t parameter }
  152. {24} newgroup : gid_t; { chown gid_t parameter }
  153. {28} trunc_offset : off_t; { ftrucnate parameter }
  154. {36} unknown2 : array[1..2] of longint;
  155. {44} utime_param: int64;
  156. {52} unknown3 : array[1..2] of longint;
  157. end;
  158. function Fpftruncate(fd : cint; flength : off_t): cint; [public, alias : 'FPC_SYSC_FTRUNCATE'];
  159. var
  160. args: SysCallArgs;
  161. wstat : pwstat;
  162. begin
  163. New(wstat);
  164. FillChar(wstat^,sizeof(wstat),0);
  165. wstat^.trunc_offset := flength;
  166. args.param[1] := fd;
  167. args.param[2] := $00000000;
  168. args.param[3] := cint(wstat);
  169. args.param[4] := $00000008;
  170. args.param[5] := $00000001;
  171. fpftruncate:=SysCall(syscall_nr_ftruncate, args);
  172. Dispose(wstat);
  173. {begin
  174. Fpftruncate:=Do_syscall(syscall_nr___syscall,syscall_nr_ftruncate,0,fd,0,lo(flength),hi(flength));
  175. }
  176. end;
  177. const
  178. B_OS_NAME_LENGTH = 32;
  179. B_PAGE_SIZE = 4096;
  180. const
  181. B_NO_LOCK = 0;
  182. B_LAZY_LOCK = 1;
  183. B_FULL_LOCK = 2;
  184. B_CONTIGUOUS = 3;
  185. B_LOMEM = 4;
  186. B_ANY_ADDRESS = 0;
  187. B_EXACT_ADDRESS = 1;
  188. B_BASE_ADDRESS = 2;
  189. B_CLONE_ADDRESS = 3;
  190. B_ANY_KERNEL_ADDRESS = 4;
  191. B_READ_AREA = 1;
  192. B_WRITE_AREA = 2;
  193. type
  194. area_id = Longint;
  195. function create_area(name : pchar; var addr : longint;
  196. addr_typ : longint; size : longint; lock_type: longint; protection : longint): area_id;
  197. var
  198. args : SysCallArgs;
  199. begin
  200. args.param[1] := cint(name);
  201. args.param[2] := cint(@addr);
  202. args.param[3] := cint(addr_typ);
  203. args.param[4] := cint(size);
  204. args.param[5] := cint(lock_type);
  205. args.param[6] := cint(protection);
  206. create_area := SysCall(syscall_nr_create_area, args);
  207. end;
  208. Function Fpmmap(start:pointer;len:size_t;prot:cint;flags:cint;fd:cint;offst:off_t):pointer; [public, alias: 'FPC_SYSC_MMAP'];
  209. var
  210. heap_handle : area_id;
  211. const
  212. zero=0;
  213. myheapsize=$20000;
  214. myheaprealsize=$20000;
  215. var
  216. myheapstart:pointer;
  217. s : string;
  218. begin
  219. WriteLn('fpmmap');
  220. Str(len, s);
  221. WriteLn(s);
  222. myheapstart:=start;
  223. {$IFDEF FPC_USE_LIBC}
  224. heap_handle := create_area('fpcheap',myheapstart,0,len,0,3);//!!
  225. {$ELSE}
  226. heap_handle := create_area('fpcheap',longint(myheapstart),0,len,0,3);//!!
  227. {$ENDIF}
  228. case heap_handle of
  229. B_BAD_VALUE : WriteLn('B_BAD_VALUE');
  230. B_PAGE_SIZE : WriteLn('B_PAGE_SIZE');
  231. B_NO_MEMORY : WriteLn('B_NO_MEMORY');
  232. B_ERROR : WriteLn('B_ERROR');
  233. end;
  234. fpmmap := myheapstart;
  235. // not available under BeOS
  236. // Fpmmap:=pointer(longint(do_syscall(syscall_nr_mmap,TSysParam(Start),Len,Prot,Flags,fd,{$ifdef cpupowerpc}0,{$endif}offst{$ifdef cpui386},0{$endif})));
  237. end;
  238. {$endif}
  239. function Fpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_READ'];
  240. var
  241. args : SysCallArgs;
  242. funcresult: ssize_t;
  243. errorcode : cint;
  244. begin
  245. args.param[1] := fd;
  246. args.param[2] := cint(buf);
  247. args.param[3] := cint(nbytes);
  248. args.param[4] := cint(@errorcode);
  249. funcresult := ssize_t(Do_SysCall(syscall_nr_read,args));
  250. if funcresult >= 0 then
  251. begin
  252. fpread := funcresult;
  253. errno := 0;
  254. end
  255. else
  256. begin
  257. fpread := -1;
  258. errno := errorcode;
  259. end;
  260. {begin
  261. Fpread:=do_syscall(syscall_nr_read,Fd,TSysParam(buf),nbytes);
  262. }
  263. end;
  264. //function Fpmywrite(fd: cint;const buf:pchar; nbytes : size_t): ssize_t; cdecl; external name 'write';
  265. function Fpwrite(fd: cint;buf:pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_WRITE'];
  266. var
  267. args : SysCallArgs;
  268. funcresult : ssize_t;
  269. errorcode : cint;
  270. begin
  271. errorcode := 0;
  272. // There is a bug in syscall in 1.9 under BeOS !!!
  273. // Fixed ! 26/05/2004 ! See in syscall.inc
  274. args.param[1] := fd;
  275. args.param[2] := cint(buf);
  276. args.param[3] := cint(nbytes);
  277. args.param[4] := cint(@errorcode);
  278. funcresult := Do_SysCall(syscall_nr_write,args);
  279. // funcresult := Fpmywrite(fd, buf, nbytes);
  280. if funcresult >= 0 then
  281. begin
  282. fpwrite := funcresult;
  283. errno := 0;
  284. end
  285. else
  286. begin
  287. fpwrite := -1;
  288. errno := errorcode;
  289. end;
  290. {begin
  291. Fpwrite:=do_syscall(syscall_nr_write,Fd,TSysParam(buf),nbytes);
  292. }
  293. end;
  294. function Fpunlink(const path: pchar): cint; [public, alias : 'FPC_SYSC_UNLINK'];
  295. var
  296. args :SysCallArgs;
  297. begin
  298. args.param[1] := $FFFFFFFF;
  299. args.param[2] := cint(path);
  300. fpunlink := SysCall(syscall_nr_unlink,args);
  301. {begin
  302. Fpunlink:=do_syscall(syscall_nr_unlink,TSysParam(path));
  303. }
  304. end;
  305. function Fprename(old : pchar; newpath: pchar): cint; [public, alias : 'FPC_SYSC_RENAME'];
  306. var
  307. args: SysCallArgs;
  308. begin
  309. args.param[1] := $FFFFFFFF;
  310. args.param[2] := cint(old);
  311. args.param[3] := $FFFFFFFF;
  312. args.param[4] := cint(newpath);
  313. fprename := SysCall(syscall_nr_rename,args);
  314. {begin
  315. Fprename:=do_syscall(syscall_nr_rename,TSysParam(old),TSysParam(newpath));
  316. }
  317. end;
  318. function Fpstat(const path: pchar; var buf : stat):cint; [public, alias : 'FPC_SYSC_STAT'];
  319. var
  320. args : SysCallArgs;
  321. begin
  322. args.param[1] := $FFFFFFFF;
  323. args.param[2] := cint(path);
  324. args.param[3] := cint(@buf);
  325. args.param[4] := $01000000;
  326. fpstat := SysCall(syscall_nr_stat, args);
  327. {begin
  328. Fpstat:=do_syscall(syscall_nr_stat,TSysParam(path),TSysParam(@buf));
  329. }
  330. end;
  331. {*****************************************************************************
  332. --- Directory:Directory related calls ---
  333. *****************************************************************************}
  334. function Fpchdir(path : pchar): cint; [public, alias : 'FPC_SYSC_CHDIR'];
  335. var
  336. args: SysCallArgs;
  337. begin
  338. args.param[1] := $FFFFFFFF;
  339. args.param[2] := cint(path);
  340. fpchdir := SysCall(syscall_nr_chdir, args);
  341. {begin
  342. Fpchdir:=do_syscall(syscall_nr_chdir,TSysParam(path));
  343. }
  344. end;
  345. function Fpmkdir(path : pchar; mode: mode_t):cint; [public, alias : 'FPC_SYSC_MKDIR'];
  346. var
  347. args :SysCallArgs;
  348. begin
  349. args.param[1] := $FFFFFFFF;
  350. args.param[2] := cint(path);
  351. args.param[3] := cint(mode);
  352. fpmkdir := SysCall(syscall_nr_mkdir,args);
  353. (*begin {Mode is 16-bit on F-BSD 4!}
  354. Fpmkdir:=do_syscall(syscall_nr_mkdir,TSysParam(path),mode);
  355. *)
  356. end;
  357. function Fprmdir(path : pchar): cint; [public, alias : 'FPC_SYSC_RMDIR'];
  358. var
  359. args: SysCallArgs;
  360. begin
  361. args.param[1] := $FFFFFFFF;
  362. args.param[2] := cint(path);
  363. fprmdir := SysCall(syscall_nr_rmdir,args);
  364. {begin
  365. Fprmdir:=do_syscall(syscall_nr_rmdir,TSysParam(path));
  366. }
  367. end;
  368. {$ifndef NewReaddir}
  369. const DIRBLKSIZ=1024;
  370. function Fpopendir(dirname : pchar): pdir; [public, alias : 'FPC_SYSC_OPENDIR'];
  371. var
  372. args : SysCallArgs;
  373. dirp: pdir;
  374. fd : cint;
  375. begin
  376. New(dirp);
  377. { just in case }
  378. FillChar(dirp^,sizeof(dir),#0);
  379. if assigned(dirp) then
  380. begin
  381. args.param[1] := $FFFFFFFF;
  382. args.param[2] := cint(dirname);
  383. args.param[3] := 0;
  384. fd:=SysCall(syscall_nr_opendir,args);
  385. if fd = -1 then
  386. begin
  387. Dispose(dirp);
  388. fpopendir := nil;
  389. exit;
  390. end;
  391. dirp^.fd := fd;
  392. fpopendir := dirp;
  393. exit;
  394. end;
  395. Errno := ESysEMFILE;
  396. fpopendir := nil;
  397. (*var
  398. fd:longint;
  399. st:stat;
  400. ptr:pdir;
  401. begin
  402. Fpopendir:=nil;
  403. if Fpstat(dirname,st)<0 then
  404. exit;
  405. { Is it a dir ? }
  406. if not((st.st_mode and $f000)=$4000)then
  407. begin
  408. errno:=ESysENOTDIR;
  409. exit
  410. end;
  411. { Open it}
  412. fd:=Fpopen(dirname,O_RDONLY,438);
  413. if fd<0 then
  414. Begin
  415. Errno:=-1;
  416. exit;
  417. End;
  418. new(ptr);
  419. if ptr=nil then
  420. Begin
  421. Errno:=1;
  422. exit;
  423. End;
  424. Getmem(ptr^.dd_buf,2*DIRBLKSIZ);
  425. if ptr^.dd_buf=nil then
  426. exit;
  427. ptr^.dd_fd:=fd;
  428. ptr^.dd_loc:=-1;
  429. ptr^.dd_rewind:=longint(ptr^.dd_buf);
  430. ptr^.dd_size:=0;
  431. // ptr^.dd_max:=sizeof(ptr^.dd_buf^);
  432. Fpopendir:=ptr;
  433. *)
  434. end;
  435. function Fpclosedir(dirp : pdir): cint; [public, alias : 'FPC_SYSC_CLOSEDIR'];
  436. var
  437. args : SysCallArgs;
  438. begin
  439. if assigned(dirp) then
  440. begin
  441. args.param[1] := dirp^.fd;
  442. fpclosedir := SysCall(syscall_nr_closedir,args);
  443. Dispose(dirp);
  444. dirp := nil;
  445. exit;
  446. end;
  447. Errno := ESysEBADF;
  448. fpclosedir := -1;
  449. {begin
  450. Fpclosedir:=Fpclose(dirp^.dd_fd);
  451. Freemem(dirp^.dd_buf);
  452. dispose(dirp);
  453. }
  454. end;
  455. function Fpreaddir(dirp : pdir) : pdirent; [public, alias : 'FPC_SYSC_READDIR'];
  456. {Different from Linux, Readdir on BSD is based on Getdents, due to the
  457. missing of the readdir syscall.
  458. Getdents requires the buffer to be larger than the blocksize.
  459. This usually the sectorsize =512 bytes, but maybe tapedrives and harddisks
  460. with blockmode have this higher?}
  461. (*function readbuffer:longint;
  462. var retval :longint;
  463. begin
  464. Retval:=do_syscall(syscall_nr_getdents,TSysParam(dirp^.dd_fd),TSysParam(@dirp^.dd_buf^),DIRBLKSIZ {sizeof(getdentsbuffer)});
  465. dirp^.dd_rewind:=TSysParam(dirp^.dd_buf);
  466. if retval=0 then
  467. begin
  468. dirp^.dd_rewind:=0;
  469. dirp^.dd_loc:=0;
  470. end
  471. else
  472. dirP^.dd_loc:=retval;
  473. readbuffer:=retval;
  474. end;*)
  475. var
  476. args : SysCallArgs;
  477. funcresult : cint;
  478. begin
  479. args.param[1] := dirp^.fd;
  480. args.param[2] := cint(@(dirp^.ent));
  481. args.param[3] := $0000011C;
  482. args.param[4] := $00000001;
  483. { the error will be processed here }
  484. funcresult := Do_SysCall(syscall_nr_readdir, args);
  485. if funcresult <> 1 then
  486. begin
  487. if funcresult <> 0 then
  488. errno := funcresult;
  489. fpreaddir := nil;
  490. exit;
  491. end;
  492. errno := 0;
  493. fpreaddir := @dirp^.ent
  494. (*
  495. var
  496. FinalEntry : pdirent;
  497. novalid : boolean;
  498. Reclen : Longint;
  499. CurEntry : PDirent;
  500. begin
  501. if (dirp^.dd_buf=nil) or (dirp^.dd_loc=0) THEN
  502. exit(nil);
  503. if (dirp^.dd_loc=-1) OR {First readdir on this pdir. Initial fill of buffer}
  504. (dirp^.dd_rewind>=(longint(dirp^.dd_buf)+dirblksiz)) then {no more entries left?}
  505. Begin
  506. if readbuffer=0 then {succesful read?}
  507. Exit(NIL); {No more data}
  508. End;
  509. FinalEntry:=NIL;
  510. CurEntry:=nil;
  511. repeat
  512. novalid:=false;
  513. CurEntry:=pdirent(dirp^.dd_rewind);
  514. RecLen:=CurEntry^.d_reclen;
  515. if RecLen<>0 Then
  516. begin {valid direntry?}
  517. if CurEntry^.d_fileno<>0 then
  518. FinalEntry:=CurEntry;
  519. inc(dirp^.dd_rewind,Reclen);
  520. end
  521. else
  522. begin {block entirely searched or reclen=0}
  523. Novalid:=True;
  524. if dirp^.dd_loc<>0 THEN {blocks left?}
  525. if readbuffer()<>0 then {succesful read?}
  526. novalid:=false;
  527. end;
  528. until (FinalEntry<>nil) or novalid;
  529. If novalid then
  530. FinalEntry:=nil;
  531. FpReadDir:=FinalEntry;*)
  532. end;
  533. {$endif}
  534. {*****************************************************************************
  535. --- Process:Process & program handling - related calls ---
  536. *****************************************************************************}
  537. procedure Fpexit(status : cint); [public, alias : 'FPC_SYSC_EXIT'];
  538. var
  539. args : SysCallArgs;
  540. begin
  541. // sys_exit(status);
  542. args.param[1] := status;
  543. do_syscall(syscall_nr_exit, args);
  544. end;
  545. {
  546. Change action of process upon receipt of a signal.
  547. Signum specifies the signal (all except SigKill and SigStop).
  548. If Act is non-nil, it is used to specify the new action.
  549. If OldAct is non-nil the previous action is saved there.
  550. }
  551. function Fpsigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint; [public, alias : 'FPC_SYSC_SIGACTION'];
  552. {
  553. Change action of process upon receipt of a signal.
  554. Signum specifies the signal (all except SigKill and SigStop).
  555. If Act is non-nil, it is used to specify the new action.
  556. If OldAct is non-nil the previous action is saved there.
  557. }
  558. var
  559. args : SysCallArgs;
  560. begin
  561. args.param[1] := sig;
  562. args.param[2] := cint(@act);
  563. args.param[3] := cint(@oact);
  564. fpsigaction := SysCall(syscall_nr_sigaction, args);
  565. //begin
  566. // do_syscall(syscall_nr_sigaction,TSysParam(sig),TSysParam(@act),TSysParam(@oact));
  567. end;
  568. (*=================== MOVED from sysunix.inc ========================*)
  569. function Fpfstat(fd : cint; var sb : stat): cint; [public, alias : 'FPC_SYSC_FSTAT'];
  570. var
  571. args : SysCallArgs;
  572. begin
  573. args.param[1] := fd;
  574. args.param[2] := $00;
  575. args.param[3] := cint(@sb);
  576. args.param[4] := $00000001;
  577. fpfstat := SysCall(syscall_nr_fstat, args);
  578. {begin
  579. fpFStat:=do_SysCall(syscall_nr_fstat,fd,TSysParam(@sb));
  580. }
  581. end;
  582. {$ifdef NewReaddir}
  583. {$I readdir.inc}
  584. {$endif}
  585. function fork : pid_t; external 'root' name 'fork';
  586. { These routines are currently not required for BeOS }
  587. function Fpfork : pid_t; [public, alias : 'FPC_SYSC_FORK'];
  588. {
  589. This function issues the 'fork' System call. the program is duplicated in memory
  590. and Execution continues in parent and child process.
  591. In the parent process, fork returns the PID of the child. In the child process,
  592. zero is returned.
  593. A negative value indicates that an error has occurred, the error is returned in
  594. LinuxError.
  595. }
  596. Begin
  597. WriteLn('fpfork');
  598. fpfork := fork;
  599. // Not required for BeOS
  600. // Fpfork:=Do_syscall(SysCall_nr_fork);
  601. End;
  602. {
  603. function Fpexecve(const path : pathstr; const argv : ppchar; const envp: ppchar): cint;
  604. }
  605. {
  606. Replaces the current program by the program specified in path,
  607. arguments in args are passed to Execve.
  608. environment specified in ep is passed on.
  609. }
  610. {
  611. Begin
  612. path:=path+#0;
  613. do_syscall(syscall_nr_Execve,TSysParam(@path[1]),TSysParam(Argv),TSysParam(envp));
  614. End;
  615. }
  616. {
  617. function Fpexecve(const path : pchar; const argv : ppchar; const envp: ppchar): cint; [public, alias : 'FPC_SYSC_EXECVE'];
  618. }
  619. {
  620. Replaces the current program by the program specified in path,
  621. arguments in args are passed to Execve.
  622. environment specified in ep is passed on.
  623. }
  624. {
  625. Begin
  626. do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(Argv),TSysParam(envp));
  627. End;
  628. }
  629. function waitpid(pid : pid_t; stat_loc : pcint; options: cint): pid_t; external 'root' name 'waitpid';
  630. function Fpwaitpid(pid : pid_t; stat_loc : pcint; options: cint): pid_t; [public, alias : 'FPC_SYSC_WAITPID'];
  631. {
  632. Waits until a child with PID Pid exits, or returns if it is exited already.
  633. Any resources used by the child are freed.
  634. The exit status is reported in the adress referred to by Status. It should
  635. be a longint.
  636. }
  637. begin // actually a wait4() call with 4th arg 0.
  638. FpWaitPID := waitpid(pid, stat_loc, options);
  639. // FpWaitPID:=do_syscall(syscall_nr_WaitPID,PID,TSysParam(Stat_loc),options,0);
  640. end;
  641. function Fpaccess(const pathname : pchar; amode : cint): cint; [public, alias : 'FPC_SYSC_ACCESS'];
  642. {
  643. Test users access rights on the specified file.
  644. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  645. R,W,X stand for read,write and Execute access, simultaneously.
  646. F_OK checks whether the test would be allowed on the file.
  647. i.e. It checks the search permissions in all directory components
  648. of the path.
  649. The test is done with the real user-ID, instead of the effective.
  650. If access is denied, or an error occurred, false is returned.
  651. If access is granted, true is returned.
  652. Errors other than no access,are reported in unixerror.
  653. }
  654. var
  655. args : SysCallArgs;
  656. begin
  657. args.param[1] := $FFFFFFFF;
  658. args.param[2] := cint(pathname);
  659. args.param[3] := amode;
  660. fpaccess := SysCall(syscall_nr_access,args);
  661. {begin
  662. FpAccess:=do_syscall(syscall_nr_access,TSysParam(pathname),amode);
  663. }
  664. end;
  665. (*
  666. function Fpaccess(const pathname : pathstr; amode : cint): cint;
  667. {
  668. Test users access rights on the specified file.
  669. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  670. R,W,X stand for read,write and Execute access, simultaneously.
  671. F_OK checks whether the test would be allowed on the file.
  672. i.e. It checks the search permissions in all directory components
  673. of the path.
  674. The test is done with the real user-ID, instead of the effective.
  675. If access is denied, or an error occurred, false is returned.
  676. If access is granted, true is returned.
  677. Errors other than no access,are reported in unixerror.
  678. }
  679. begin
  680. pathname:=pathname+#0;
  681. Access:=do_syscall(syscall_nr_access, TSysParam(@pathname[1]),mode)=0;
  682. end;
  683. *)
  684. Function FpDup(fildes:cint):cint; [public, alias : 'FPC_SYSC_DUP'];
  685. begin
  686. {$warning TODO BeOS FpDup implementation}
  687. // Fpdup:=Do_syscall(syscall_nr_dup,TSysParam(fildes));
  688. end;
  689. Function FpDup2(fildes,fildes2:cint):cint; [public, alias : 'FPC_SYSC_DUP2'];
  690. begin
  691. {$warning TODO BeOS FpDup2 implementation}
  692. // Fpdup2:=do_syscall(syscall_nr_dup2,TSysParam(fildes),TSysParam(fildes2));
  693. end;
  694. Function Fpmunmap(start:pointer;len:size_t):cint; [public, alias :'FPC_SYSC_MUNMAP'];
  695. begin
  696. {$warning TODO BeOS Fpmunmap implementation}
  697. // Fpmunmap:=do_syscall(syscall_nr_munmap,TSysParam(start),Len);
  698. end;
  699. {
  700. Interface to Unix ioctl call.
  701. Performs various operations on the filedescriptor Handle.
  702. Ndx describes the operation to perform.
  703. Data points to data needed for the Ndx function. The structure of this
  704. data is function-dependent.
  705. }
  706. Function FpIOCtl(Handle:cint;Ndx: culong;Data: Pointer):cint; [public, alias : 'FPC_SYSC_IOCTL'];
  707. // This was missing here, instead hardcoded in Do_IsDevice
  708. begin
  709. {$warning TODO BeOS FpIOCtl implementation}
  710. // FpIOCtl:=do_SysCall(syscall_nr_ioctl,handle,Ndx,TSysParam(data));
  711. end;
  712. Function FpGetPid:LongInt; [public, alias : 'FPC_SYSC_GETPID'];
  713. {
  714. Get Process ID.
  715. }
  716. begin
  717. {$warning TODO BeOS FpGetPid implementation}
  718. // FpGetPID:=do_syscall(syscall_nr_getpid);
  719. end;
  720. function fpgettimeofday(tp: ptimeval;tzp:ptimezone):cint; [public, alias: 'FPC_SYSC_GETTIMEOFDAY'];
  721. begin
  722. {$warning TODO BeOS fpgettimeofday implementation}
  723. // fpgettimeofday:=do_syscall(syscall_nr_gettimeofday,TSysParam(tp),TSysParam(tzp));
  724. end;
  725. function FPSigProcMask(how:cint;nset : psigset;oset : psigset):cint; [public, alias : 'FPC_SYSC_SIGPROCMASK'];
  726. {
  727. Change the list of currently blocked signals.
  728. How determines which signals will be blocked :
  729. SigBlock : Add SSet to the current list of blocked signals
  730. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  731. SigSetMask : Set the list of blocked signals to SSet
  732. if OldSSet is non-null, the old set will be saved there.
  733. }
  734. begin
  735. {$warning TODO BeOS FPSigProcMask implementation}
  736. // FPsigprocmask:=do_syscall(syscall_nr_sigprocmask,longint(how),longint(nset),longint(oset));
  737. end;
  738. {$user BLA!}
  739. Function FpNanoSleep(req : ptimespec;rem : ptimespec) : cint; [public, alias : 'FPC_SYSC_NANOSLEEP'];
  740. begin
  741. {$warning TODO BeOS FpNanoSleep implementation}
  742. {$ifndef darwin}
  743. // FpNanoSleep:=Do_SysCall(syscall_nr_nanosleep,TSysParam(req),TSysParam(rem));
  744. {$else not darwin}
  745. {$warning: TODO: nanosleep!!!}
  746. {$endif not darwin}
  747. end;
  748. function Fpgetcwd(pt:pchar; _size:size_t):pchar;[public, alias :'FPC_SYSC_GETCWD'];
  749. {$ifndef darwin}
  750. const intpathmax = 1024-4; // didn't use POSIX data in libc
  751. // implementation.
  752. var ept,bpt : pchar;
  753. c : char;
  754. ret : cint;
  755. begin
  756. {$warning TODO BeOS Fpgetcwd implementation}
  757. (* if pt=NIL Then
  758. begin
  759. // POSIX: undefined. (exit(nil) ?)
  760. // BSD : allocate mem for path.
  761. getmem(pt,intpathmax);
  762. if pt=nil Then
  763. exit(nil);
  764. ept:=pt+intpathmax;
  765. end
  766. else
  767. Begin
  768. if (_size=0) Then
  769. Begin
  770. seterrno(ESysEINVAL);
  771. exit(nil);
  772. End;
  773. if (_size=1) Then
  774. Begin
  775. seterrno(ESysERANGE);
  776. exit(nil);
  777. End;
  778. ept:=pt+_size;
  779. end;
  780. ret := do_syscall(syscall_nr___getcwd,TSysParam(pt),TSysParam( ept - pt));
  781. If (ret = 0) Then
  782. If (pt[0] <> '/') Then
  783. Begin
  784. bpt := pt;
  785. ept := pt + strlen(pt) - 1;
  786. While (bpt < ept) Do
  787. Begin
  788. c := bpt^;
  789. bpt^:=ept^;
  790. inc(bpt);
  791. ept^:=c;
  792. dec(ept);
  793. End;
  794. End;
  795. Fpgetcwd:=pt;*)
  796. end;
  797. {$else not darwin}
  798. {$i getcwd.inc}
  799. {$endif darwin}
  800. {$endif}
  801. Function Do_IsDevice(Handle:Longint):boolean;
  802. {
  803. Interface to Unix ioctl call.
  804. Performs various operations on the filedescriptor Handle.
  805. Ndx describes the operation to perform.
  806. Data points to data needed for the Ndx function. The structure of this
  807. data is function-dependent.
  808. }
  809. begin
  810. do_isdevice:= (handle=StdInputHandle) or
  811. (handle=StdOutputHandle) or
  812. (handle=StdErrorHandle);
  813. end;
  814. {
  815. extern _IMPEXP_ROOT status_t get_image_symbol(image_id imid,
  816. const char *name, int32 sclass, void **ptr);
  817. extern _IMPEXP_ROOT status_t get_nth_image_symbol(image_id imid, int32 index,
  818. char *buf, int32 *bufsize, int32 *sclass,
  819. void **ptr);
  820. }
  821. //
  822. {$ifdef FPC_USE_LIBC}
  823. // private; use the macros, below
  824. function _get_image_info(image : image_id; var info : image_info; size : size_t)
  825. : status_t; cdecl; external 'root' name '_get_image_info';
  826. function _get_next_image_info(team : team_id; var cookie : Longint; var info : image_info; size : size_t)
  827. : status_t; cdecl; external 'root' name '_get_next_image_info';
  828. function get_image_info(image : image_id; var info : image_info) : status_t;
  829. begin
  830. Result := _get_image_info(image, info, SizeOf(info));
  831. end;
  832. function get_next_image_info(team : team_id; var cookie : Longint; var info : image_info) : status_t;
  833. begin
  834. Result := _get_next_image_info(team, cookie, info, SizeOf(info));
  835. end;
  836. {$else}
  837. function wait_for_thread(thread: thread_id; var status : status_t): status_t;
  838. var
  839. args: SysCallArgs;
  840. i: longint;
  841. begin
  842. args.param[1] := cint(thread);
  843. args.param[2] := cint(@status);
  844. wait_for_thread := SysCall(syscall_nr_wait_thread, args);
  845. end;
  846. function get_team_info(team: team_id; var info : team_info): status_t;
  847. var
  848. args: SysCallArgs;
  849. begin
  850. args.param[1] := cint(team);
  851. args.param[2] := cint(@info);
  852. get_team_info := SysCall(syscall_nr_get_team_info, args);
  853. end;
  854. function kill_team(team: team_id): status_t;
  855. var
  856. args: SysCallArgs;
  857. begin
  858. args.param[1] := cint(team);
  859. kill_team := SysCall(syscall_nr_kill_team, args);
  860. end;
  861. function get_next_image_info(team : team_id; var cookie: longint;var info : image_info): status_t;
  862. var
  863. args: SysCallArgs;
  864. begin
  865. args.param[1] := cint(team);
  866. args.param[2] := cint(@cookie);
  867. args.param[3] := cint(@info);
  868. args.param[4] := cint(sizeof(image_info));
  869. get_next_image_info := SysCall(syscall_nr_get_next_image_info, args);
  870. end;
  871. function load_image(argc : longint; argv : ppchar; envp : ppchar): thread_id;
  872. var
  873. args: SysCallArgs;
  874. i: longint;
  875. begin
  876. args.param[1] := cint(argc);
  877. args.param[2] := cint(argv);
  878. args.param[3] := cint(envp);
  879. load_image := SysCall(syscall_nr_load_image, args);
  880. end;
  881. function get_system_info(var info: system_info): status_t;
  882. var
  883. args: SysCallArgs;
  884. i: longint;
  885. begin
  886. args.param[1] := cint(@info);
  887. i := SysCall(syscall_nr_get_system_info, args);
  888. get_system_info := i;
  889. end;
  890. function dev_for_path(const pathname : pchar): dev_t;
  891. var
  892. args: SysCallArgs;
  893. buffer: array[1..15] of longint;
  894. i: cint;
  895. begin
  896. args.param[1] := $FFFFFFFF;
  897. args.param[2] := cint(pathname);
  898. args.param[3] := cint(@buffer);
  899. args.param[4] := $01000000;
  900. if SysCall(syscall_nr_rstat, args)=0 then
  901. i:=buffer[1]
  902. else
  903. i:=-1;
  904. dev_for_path := i;
  905. end;
  906. function fs_stat_dev(device: dev_t; var info: fs_info): dev_t;
  907. var
  908. args: SysCallArgs;
  909. begin
  910. args.param[1] := cint(device);
  911. args.param[2] := 0;
  912. args.param[3] := $FFFFFFFF;
  913. args.param[4] := 0;
  914. args.param[5] := cint(@info);
  915. fs_stat_dev := SysCall(syscall_nr_statfs, args);
  916. end;
  917. {$endif}
  918. (* Implemented in sytem under BeOS
  919. CONST
  920. { Constansts for MMAP }
  921. MAP_PRIVATE =2;
  922. MAP_ANONYMOUS =$1000;
  923. Function sbrk(size : cint) : pointer;
  924. begin
  925. sbrk:=Fpmmap(nil,cardinal(Size),3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0);
  926. if sbrk=pointer(-1) then
  927. sbrk:=nil
  928. else
  929. seterrno(0);
  930. end;
  931. *)