ossysc.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. function Fptime( tloc:ptime): time_t; [public, alias : 'FPC_SYSC_TIME'];
  14. VAR tv : timeval;
  15. tz : timezone;
  16. retval : longint;
  17. begin
  18. Retval:=do_syscall(syscall_nr_gettimeofday,TSysParam(@tv),TSysParam(@tz));
  19. If retval=-1 then
  20. Fptime:=-1
  21. else
  22. Begin
  23. If Assigned(tloc) Then
  24. TLoc^:=tv.tv_sec;
  25. Fptime:=tv.tv_sec;
  26. End;
  27. End;
  28. {*****************************************************************************
  29. --- File:File handling related calls ---
  30. *****************************************************************************}
  31. function Fpopen(path: pchar; flags : cint; mode: mode_t):cint; [public, alias : 'FPC_SYSC_OPEN'];
  32. Begin
  33. Fpopen:=do_syscall(syscall_nr_open,TSysParam(path),TSysParam(flags),TSysParam(mode));
  34. End;
  35. function Fpclose(fd : cint): cint; [public, alias : 'FPC_SYSC_CLOSE'];
  36. begin
  37. Fpclose:=do_syscall(syscall_nr_close,fd);
  38. end;
  39. {$ifdef netbsd}
  40. {$ifdef cpupowerpc}
  41. {$define netbsdmacppc}
  42. {$endif}
  43. {$endif}
  44. {$ifdef netbsdmacppc}
  45. {$i sysofft.inc} // odd ball calling convention.
  46. {$else}
  47. // generic versions.
  48. function Fplseek(fd : cint; offset : off_t; whence : cint): off_t; [public, alias : 'FPC_SYSC_LSEEK'];
  49. {$ifdef cpum68k}
  50. begin
  51. Fplseek:=do_syscall64(syscall_nr_lseek,TSysParam(fd),0,hi(offset),lo(offset),whence);
  52. end;
  53. {$else}
  54. {
  55. this one is special for the return value being 64-bit..
  56. hi/lo offset not yet tested.
  57. NetBSD: ok, but implicit return value in edx:eax
  58. FreeBSD: same implementation as NetBSD.
  59. }
  60. begin
  61. {$ifdef CPU64}
  62. Fplseek:=do_syscall(syscall_nr___syscall,syscall_nr_lseek,TSysParam(fd),Offset,whence);
  63. {$else}
  64. Fplseek:=do_syscall(syscall_nr___syscall,syscall_nr_lseek,0,TSysParam(fd),
  65. {$ifdef ENDIAN_BIG}hi(offset),lo(offset){$endif}
  66. {$ifdef ENDIAN_LITTLE}lo(Offset),hi(offset){$endif},
  67. Whence);
  68. {$endif}
  69. end;
  70. {$endif}
  71. function Fpftruncate(fd : cint; flength : off_t): cint; [public, alias : 'FPC_SYSC_FTRUNCATE'];
  72. begin
  73. {$ifdef CPU64}
  74. Fpftruncate:=Do_syscall(syscall_nr___syscall,syscall_nr_ftruncate, fd ,flength);
  75. {$else}
  76. Fpftruncate:=Do_syscall(syscall_nr___syscall,syscall_nr_ftruncate,0,fd,lo(flength),hi(flength));
  77. {$endif}
  78. end;
  79. Function Fpmmap(start:pointer;len:size_t;prot:cint;flags:cint;fd:cint;offst:off_t):pointer; [public, alias: 'FPC_SYSC_MMAP'];
  80. begin
  81. {$ifdef CPU64}
  82. Fpmmap:=pointer(ptruint(do_syscall(TSysParam(syscall_nr_mmap),TSysParam(Start),TSysParam(Len),TSysParam(Prot),TSysParam(Flags),TSysParam(fd),TSysParam(offst))));
  83. {$else}
  84. Fpmmap:=pointer(ptruint(do_syscall(syscall_nr_mmap,TSysParam(Start),Len,Prot,Flags,fd,
  85. {$ifdef FPC_BIG_ENDIAN} hi(offst),lo(offst){$endif}
  86. {$ifdef FPC_LITTLE_ENDIAN} lo(offst),hi(offst){$endif}
  87. )));
  88. {$endif}
  89. end;
  90. {$endif}
  91. function Fpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_READ'];
  92. begin
  93. Fpread:=do_syscall(syscall_nr_read,Fd,TSysParam(buf),nbytes);
  94. end;
  95. function Fpwrite(fd: cint;buf:pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_WRITE'];
  96. begin
  97. Fpwrite:=do_syscall(syscall_nr_write,Fd,TSysParam(buf),nbytes);
  98. end;
  99. function Fpunlink(const path: pchar): cint; [public, alias : 'FPC_SYSC_UNLINK'];
  100. begin
  101. Fpunlink:=do_syscall(syscall_nr_unlink,TSysParam(path));
  102. end;
  103. function Fprename(old : pchar; newpath: pchar): cint; [public, alias : 'FPC_SYSC_RENAME'];
  104. begin
  105. Fprename:=do_syscall(syscall_nr_rename,TSysParam(old),TSysParam(newpath));
  106. end;
  107. {$ifdef freebsd}
  108. Function fpFstatat(fd: cint; path: pchar; var sb: stat; flag: cint):cint;
  109. begin
  110. fpFStatat:=do_syscall(syscall_nr_fstatat,fd,TSysParam(path),TSysParam(@sb),flag);
  111. end;
  112. {$endif}
  113. function Fpstat(const path: pchar; var buf : stat):cint; [public, alias : 'FPC_SYSC_STAT'];
  114. const
  115. AT_FDCWD=-100;
  116. begin
  117. {$ifdef freebsd}
  118. Fpstat:=FpFstatat(AT_FDCWD, path, buf, 0);
  119. {$else}
  120. Fpstat:=do_syscall(syscall_nr_stat,TSysParam(path),TSysParam(@buf));
  121. {$endif}
  122. end;
  123. {*****************************************************************************
  124. --- Directory:Directory related calls ---
  125. *****************************************************************************}
  126. function Fpchdir(path : pchar): cint; [public, alias : 'FPC_SYSC_CHDIR'];
  127. begin
  128. Fpchdir:=do_syscall(syscall_nr_chdir,TSysParam(path));
  129. end;
  130. function Fpmkdir(path : pchar; mode: mode_t):cint; [public, alias : 'FPC_SYSC_MKDIR'];
  131. begin {Mode is 16-bit on F-BSD 4!}
  132. Fpmkdir:=do_syscall(syscall_nr_mkdir,TSysParam(path),mode);
  133. end;
  134. function Fprmdir(path : pchar): cint; [public, alias : 'FPC_SYSC_RMDIR'];
  135. begin
  136. Fprmdir:=do_syscall(syscall_nr_rmdir,TSysParam(path));
  137. end;
  138. {$ifndef NewReaddir}
  139. const DIRBLKSIZ=1024;
  140. {$ifdef freebsd}
  141. function FpGetdirentries(fd : cint; buf : pchar; nbytes : clong) : cint;
  142. begin
  143. FpGetdirentries:=do_syscall(syscall_nr_getdirentries,fd,TSysParam(buf),nbytes,TSysParam(0));
  144. end;
  145. {$endif}
  146. function Fpfstat(fd : cint; var sb : stat): cint; forward;
  147. function Fpopendir(dirname : pchar): pdir; [public, alias : 'FPC_SYSC_OPENDIR'];
  148. var
  149. fd:longint;
  150. st:stat;
  151. ptr:pdir;
  152. save_errno:cint;
  153. begin
  154. Fpopendir:=nil;
  155. { Open it}
  156. fd:=Fpopen(dirname,O_RDONLY,438);
  157. if fd<0 then
  158. Begin
  159. Errno:=-1;
  160. exit;
  161. End;
  162. if FpFStat(fd,st)<0 then
  163. begin
  164. save_errno:=errno;
  165. FpClose(fd);
  166. errno:=save_errno;
  167. exit;
  168. end;
  169. { Is it a dir ? }
  170. if not((st.st_mode and $f000)=$4000)then
  171. begin
  172. FpClose(fd);
  173. errno:=ESysENOTDIR;
  174. exit
  175. end;
  176. new(ptr);
  177. if ptr=nil then
  178. Begin
  179. FpClose(fd);
  180. Errno:=1;
  181. exit;
  182. End;
  183. Getmem(ptr^.dd_buf,2*DIRBLKSIZ);
  184. if ptr^.dd_buf=nil then
  185. begin
  186. dispose(ptr);
  187. FpClose(fd);
  188. Errno:=1;
  189. exit;
  190. end;
  191. ptr^.dd_fd:=fd;
  192. ptr^.dd_loc:=-1;
  193. ptr^.dd_rewind:=ptrint(ptr^.dd_buf);
  194. ptr^.dd_size:=0;
  195. // ptr^.dd_max:=sizeof(ptr^.dd_buf^);
  196. Fpopendir:=ptr;
  197. end;
  198. function Fpclosedir(dirp : pdir): cint; [public, alias : 'FPC_SYSC_CLOSEDIR'];
  199. begin
  200. Fpclosedir:=Fpclose(dirp^.dd_fd);
  201. Freemem(dirp^.dd_buf);
  202. dispose(dirp);
  203. end;
  204. function Fpreaddir(dirp : pdir) : pdirent; [public, alias : 'FPC_SYSC_READDIR'];
  205. {Different from Linux, Readdir on BSD is based on Getdents, due to the
  206. missing of the readdir syscall.
  207. Getdents requires the buffer to be larger than the blocksize.
  208. This usually the sectorsize =512 bytes, but maybe tapedrives and harddisks
  209. with blockmode have this higher?}
  210. function readbuffer:longint;
  211. var retval :longint;
  212. begin
  213. {$ifdef freebsd}
  214. Retval:=FpGetdirentries(dirp^.dd_fd, @dirp^.dd_buf^, DIRBLKSIZ {sizeof(getdentsbuffer)});
  215. {$else}
  216. Retval:=do_syscall(syscall_nr_getdents,TSysParam(dirp^.dd_fd),TSysParam(@dirp^.dd_buf^),DIRBLKSIZ {sizeof(getdentsbuffer)});
  217. {$endif}
  218. dirp^.dd_rewind:=TSysParam(dirp^.dd_buf);
  219. if retval=0 then
  220. begin
  221. dirp^.dd_rewind:=0;
  222. dirp^.dd_loc:=0;
  223. end
  224. else
  225. dirP^.dd_loc:=retval;
  226. dirP^.dd_size:=retval;
  227. readbuffer:=retval;
  228. end;
  229. var
  230. FinalEntry : pdirent;
  231. novalid : boolean;
  232. Reclen : Longint;
  233. CurEntry : PDirent;
  234. begin
  235. if (dirp^.dd_buf=nil) or (dirp^.dd_loc=0) THEN
  236. exit(nil);
  237. if (dirp^.dd_loc=-1) OR {First readdir on this pdir. Initial fill of buffer}
  238. (dirp^.dd_rewind>=(ptrint(dirp^.dd_buf)+dirp^.dd_size)) then {no more entries left?}
  239. Begin
  240. if readbuffer=0 then {succesful read?}
  241. Exit(NIL); {No more data}
  242. End;
  243. FinalEntry:=NIL;
  244. CurEntry:=nil;
  245. repeat
  246. novalid:=false;
  247. CurEntry:=pdirent(dirp^.dd_rewind);
  248. {$ifdef dragonfly}
  249. RecLen:=(CurEntry^.d_namlen + 24) and $FFFFFFF8;
  250. {$else}
  251. RecLen:=CurEntry^.d_reclen;
  252. {$endif}
  253. if RecLen<>0 Then
  254. begin {valid direntry?}
  255. if CurEntry^.d_fileno<>0 then
  256. FinalEntry:=CurEntry;
  257. inc(dirp^.dd_rewind,Reclen);
  258. end
  259. else
  260. begin {block entirely searched or reclen=0}
  261. Novalid:=True;
  262. if dirp^.dd_loc<>0 THEN {blocks left?}
  263. if readbuffer()<>0 then {succesful read?}
  264. novalid:=false;
  265. end;
  266. until (FinalEntry<>nil) or novalid;
  267. If novalid then
  268. FinalEntry:=nil;
  269. FpReadDir:=FinalEntry;
  270. end;
  271. {$endif}
  272. {*****************************************************************************
  273. --- Process:Process & program handling - related calls ---
  274. *****************************************************************************}
  275. procedure Fpexit(status : cint); [public, alias : 'FPC_SYSC_EXIT'];
  276. begin
  277. do_syscall(syscall_nr_exit,status);
  278. end;
  279. {
  280. Change action of process upon receipt of a signal.
  281. Signum specifies the signal (all except SigKill and SigStop).
  282. If Act is non-nil, it is used to specify the new action.
  283. If OldAct is non-nil the previous action is saved there.
  284. }
  285. {$ifdef USE_SIGACTION_SIGTRAMP}
  286. procedure signal_trampoline; cdecl; forward;
  287. {$endif def USE_SIGACTION_SIGTRAMP}
  288. function Fpsigaction(sig: cint; act, oact: psigactionrec): cint; [public, alias : 'FPC_SYSC_SIGACTION'];
  289. {
  290. Change action of process upon receipt of a signal.
  291. Signum specifies the signal (all except SigKill and SigStop).
  292. If Act is non-nil, it is used to specify the new action.
  293. If OldAct is non-nil the previous action is saved there.
  294. }
  295. begin
  296. {$ifdef USE_SIGACTION_SIGTRAMP}
  297. fpsigaction:=do_syscall(syscall_nr___sigaction_sigtramp,TSysParam(sig),TSysParam(act),TSysParam(oact),
  298. TSysParam(@signal_trampoline),2);
  299. {$else not USE_SIGACTION_SIGTRAMP}
  300. fpsigaction:=do_syscall(syscall_nr_sigaction,TSysParam(sig),TSysParam(act),TSysParam(oact));
  301. {$endif not USE_SIGACTION_SIGTRAMP}
  302. end;
  303. (*=================== MOVED from sysunix.inc ========================*)
  304. function Fpfstat(fd : cint; var sb : stat): cint; [public, alias : 'FPC_SYSC_FSTAT'];
  305. begin
  306. fpFStat:=do_SysCall(syscall_nr_fstat,fd,TSysParam(@sb));
  307. end;
  308. {$ifdef NewReaddir}
  309. {$I readdir.inc}
  310. {$endif}
  311. function Fpfork : pid_t; [public, alias : 'FPC_SYSC_FORK'];
  312. {
  313. This function issues the 'fork' System call. the program is duplicated in memory
  314. and Execution continues in parent and child process.
  315. In the parent process, fork returns the PID of the child. In the child process,
  316. zero is returned.
  317. A negative value indicates that an error has occurred, the error is returned in
  318. LinuxError.
  319. }
  320. Begin
  321. Fpfork:=Do_syscall(SysCall_nr_fork);
  322. End;
  323. {
  324. function Fpexecve(const path : pathstr; const argv : ppchar; const envp: ppchar): cint;
  325. }
  326. {
  327. Replaces the current program by the program specified in path,
  328. arguments in args are passed to Execve.
  329. environment specified in ep is passed on.
  330. }
  331. {
  332. Begin
  333. path:=path+#0;
  334. do_syscall(syscall_nr_Execve,TSysParam(@path[1]),TSysParam(Argv),TSysParam(envp));
  335. End;
  336. }
  337. {
  338. function Fpexecve(const path : pchar; const argv : ppchar; const envp: ppchar): cint; [public, alias : 'FPC_SYSC_EXECVE'];
  339. }
  340. {
  341. Replaces the current program by the program specified in path,
  342. arguments in args are passed to Execve.
  343. environment specified in ep is passed on.
  344. }
  345. {
  346. Begin
  347. do_syscall(syscall_nr_Execve,TSysParam(path),TSysParam(Argv),TSysParam(envp));
  348. End;
  349. }
  350. function Fpwaitpid(pid : pid_t; stat_loc : pcint; options: cint): pid_t; [public, alias : 'FPC_SYSC_WAITPID'];
  351. {
  352. Waits until a child with PID Pid exits, or returns if it is exited already.
  353. Any resources used by the child are freed.
  354. The exit status is reported in the adress referred to by Status. It should
  355. be a longint.
  356. }
  357. begin // actually a wait4() call with 4th arg 0.
  358. FpWaitPID:=do_syscall(syscall_nr_WaitPID,PID,TSysParam(Stat_loc),options,0);
  359. end;
  360. function Fpaccess(const pathname : pchar; amode : cint): cint; [public, alias : 'FPC_SYSC_ACCESS'];
  361. {
  362. Test users access rights on the specified file.
  363. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  364. R,W,X stand for read,write and Execute access, simultaneously.
  365. F_OK checks whether the test would be allowed on the file.
  366. i.e. It checks the search permissions in all directory components
  367. of the path.
  368. The test is done with the real user-ID, instead of the effective.
  369. If access is denied, or an error occurred, false is returned.
  370. If access is granted, true is returned.
  371. Errors other than no access,are reported in unixerror.
  372. }
  373. begin
  374. FpAccess:=do_syscall(syscall_nr_access,TSysParam(pathname),amode);
  375. end;
  376. (*
  377. function Fpaccess(const pathname : pathstr; amode : cint): cint;
  378. {
  379. Test users access rights on the specified file.
  380. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  381. R,W,X stand for read,write and Execute access, simultaneously.
  382. F_OK checks whether the test would be allowed on the file.
  383. i.e. It checks the search permissions in all directory components
  384. of the path.
  385. The test is done with the real user-ID, instead of the effective.
  386. If access is denied, or an error occurred, false is returned.
  387. If access is granted, true is returned.
  388. Errors other than no access,are reported in unixerror.
  389. }
  390. begin
  391. pathname:=pathname+#0;
  392. Access:=do_syscall(syscall_nr_access, TSysParam(@pathname[1]),mode)=0;
  393. end;
  394. *)
  395. Function FpDup(fildes:cint):cint; [public, alias : 'FPC_SYSC_DUP'];
  396. begin
  397. Fpdup:=Do_syscall(syscall_nr_dup,TSysParam(fildes));
  398. end;
  399. Function FpDup2(fildes,fildes2:cint):cint; [public, alias : 'FPC_SYSC_DUP2'];
  400. begin
  401. Fpdup2:=do_syscall(syscall_nr_dup2,TSysParam(fildes),TSysParam(fildes2));
  402. end;
  403. Function Fpmunmap(start:pointer;len:size_t):cint; [public, alias :'FPC_SYSC_MUNMAP'];
  404. begin
  405. Fpmunmap:=do_syscall(syscall_nr_munmap,TSysParam(start),Len);
  406. end;
  407. Function Fpmprotect(start:pointer;len:size_t;prot:cint):cint; [public, alias : 'FPC_SYSC_MPROTECT'];
  408. begin
  409. Fpmprotect:=do_syscall(syscall_nr_mprotect,TSysParam(start),TSysParam(len),TSysParam(prot));
  410. end;
  411. {
  412. Interface to Unix ioctl call.
  413. Performs various operations on the filedescriptor Handle.
  414. Ndx describes the operation to perform.
  415. Data points to data needed for the Ndx function. The structure of this
  416. data is function-dependent.
  417. }
  418. Function FpIOCtl(Handle:cint;Ndx: TIOCtlRequest;Data: Pointer):cint; [public, alias : 'FPC_SYSC_IOCTL'];
  419. // This was missing here, instead hardcoded in Do_IsDevice
  420. begin
  421. FpIOCtl:=do_SysCall(syscall_nr_ioctl,handle,Ndx,TSysParam(data));
  422. end;
  423. Function FpGetPid:LongInt; [public, alias : 'FPC_SYSC_GETPID'];
  424. {
  425. Get Process ID.
  426. }
  427. begin
  428. FpGetPID:=do_syscall(syscall_nr_getpid);
  429. end;
  430. function fpgettimeofday(tp: ptimeval;tzp:ptimezone):cint; [public, alias: 'FPC_SYSC_GETTIMEOFDAY'];
  431. begin
  432. fpgettimeofday:=do_syscall(syscall_nr_gettimeofday,TSysParam(tp),TSysParam(tzp));
  433. end;
  434. function FPSigProcMask(how:cint;nset : psigset;oset : psigset):cint; [public, alias : 'FPC_SYSC_SIGPROCMASK'];
  435. {
  436. Change the list of currently blocked signals.
  437. How determines which signals will be blocked :
  438. SigBlock : Add SSet to the current list of blocked signals
  439. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  440. SigSetMask : Set the list of blocked signals to SSet
  441. if OldSSet is non-null, the old set will be saved there.
  442. }
  443. {$ifdef OpenBSD}
  444. { OpenBSD sigprocmask signal uses
  445. sigset_t that are cint type
  446. the value of nset^[0] must be passed as second parameter
  447. the old mask value is in return value }
  448. { How do we know if the call failed... PM }
  449. {$define OS_SIGPROCMASK_RETURNS_OVAL}
  450. {$endif}
  451. {$ifdef OS_SIGPROCMASK_RETURNS_OVAL}
  452. var
  453. res : cint;
  454. {$endif OS_SIGPROCMASK_RETURNS_OVAL}
  455. begin
  456. {$ifdef OS_SIGPROCMASK_RETURNS_OVAL}
  457. res:=do_syscall(syscall_nr_sigprocmask,tsysparam(how),TSysParam(nset^[0]));
  458. if assigned(oset) then
  459. oset^[0]:=res;
  460. FPsigprocmask:=0;
  461. {$else OS_SIGPROCMASK_RETURNS_OVAL}
  462. FPsigprocmask:=do_syscall(syscall_nr_sigprocmask,tsysparam(how),TSysParam(nset),TSysParam(oset));
  463. {$endif OS_SIGPROCMASK_RETURNS_OVAL}
  464. end;
  465. {$warning user BLA!}
  466. Function FpNanoSleep(req : ptimespec;rem : ptimespec) : cint; [public, alias : 'FPC_SYSC_NANOSLEEP'];
  467. begin
  468. FpNanoSleep:=Do_SysCall(syscall_nr_nanosleep,TSysParam(req),TSysParam(rem));
  469. end;
  470. function Fpgetcwd(pt:pchar; _size:size_t):pchar;[public, alias :'FPC_SYSC_GETCWD'];
  471. const intpathmax = 1024-4; // didn't use POSIX data in libc
  472. // implementation.
  473. var ept,bpt : pchar;
  474. c : char;
  475. ret : cint;
  476. begin
  477. if pt=NIL Then
  478. begin
  479. // POSIX: undefined. (exit(nil) ?)
  480. // BSD : allocate mem for path.
  481. getmem(pt,intpathmax);
  482. if pt=nil Then
  483. exit(nil);
  484. ept:=pt+intpathmax;
  485. end
  486. else
  487. Begin
  488. if (_size=0) Then
  489. Begin
  490. seterrno(ESysEINVAL);
  491. exit(nil);
  492. End;
  493. if (_size=1) Then
  494. Begin
  495. seterrno(ESysERANGE);
  496. exit(nil);
  497. End;
  498. ept:=pt+_size;
  499. end;
  500. ret := do_syscall(syscall_nr___getcwd,TSysParam(pt),TSysParam( ept - pt));
  501. If (ret = 0) Then
  502. begin
  503. If (pt[0] <> '/') Then
  504. Begin
  505. bpt := pt;
  506. ept := pt + strlen(pt) - 1;
  507. While (bpt < ept) Do
  508. Begin
  509. c := bpt^;
  510. bpt^:=ept^;
  511. inc(bpt);
  512. ept^:=c;
  513. dec(ept);
  514. End;
  515. End
  516. end
  517. {$if defined(openbsd) or defined (netbsd)}
  518. { At least for openbsd, a positive return value is
  519. the length of the returned pchar }
  520. else if (ret<0) then
  521. {$else not opensd}
  522. else
  523. {$endif openbsd}
  524. begin
  525. seterrno(-ret);
  526. pt:=nil;
  527. end;
  528. Fpgetcwd:=pt;
  529. end;
  530. Function fpReadLink(name,linkname:pchar;maxlen:size_t):cint; [public, alias : 'FPC_SYSC_READLINK'];
  531. begin
  532. fpreadlink:=do_syscall(syscall_nr_readlink, TSysParam(name),TSysParam(linkname),maxlen);
  533. end;
  534. function FpGetRLimit(resource:cint;rlim:PRLimit):cint; [public, alias : 'FPC_SYSC_GETRLIMIT'];
  535. begin
  536. fpgetrlimit:=do_syscall(syscall_nr_getrlimit,TSysParam(Resource),TSysParam(rlim));
  537. end;
  538. function FpSetRLimit(Resource:cint;rlim:PRLimit):cint; [public, alias : 'FPC_SYSC_SETRLIMIT'];
  539. begin
  540. fpsetrlimit:=do_syscall(syscall_nr_setrlimit,TSysParam(Resource),TSysParam(rlim));
  541. end;