bunxovl.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2002 by Marco van de Voort
  5. Some generic overloads for stringfunctions in the baseunix unit.
  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. {$if defined(CPUARM) or defined(CPUX86_64) or defined(CPUSPARC)}
  13. {$define RTSIGACTION}
  14. {$endif}
  15. {$I textrec.inc}
  16. {$I filerec.inc}
  17. Function FpLink (existing : AnsiString; newone : AnsiString): cInt;
  18. Begin
  19. FpLink:=FpLink(pchar(existing),pchar(newone));
  20. End;
  21. Function FpMkfifo (path : AnsiString; Mode : TMode): cInt;
  22. Begin
  23. FpMkfifo:=FpMkfifo(pchar(path),mode);
  24. End;
  25. Function FpChmod (path : AnsiString; Mode : TMode): cInt;
  26. Begin
  27. FpChmod:=FpChmod(pchar(path),mode);
  28. End;
  29. Function FpChown (path : AnsiString; owner : TUid; group : TGid): cInt;
  30. Begin
  31. FpChown:=FpChown(pchar(path),owner,group);
  32. End;
  33. Function FpUtime (path : AnsiString; times : putimbuf): cInt;
  34. Begin
  35. FpUtime:=FpUtime(pchar(path),times);
  36. End;
  37. {
  38. Function FpGetcwd (path:AnsiString; siz:TSize):AnsiString;
  39. Begin
  40. FpGetcwd:=ansistring(pchar(FpGetcwd(pchar(path),siz)));
  41. End;
  42. }
  43. Function FpGetcwd :AnsiString;
  44. Var
  45. Buf : Array[0..PATH_MAX+1] of char;
  46. Begin
  47. Buf[PATH_MAX+1]:=#0;
  48. If FpGetcwd(@Buf[0],PATH_MAX)=Nil then
  49. FpGetcwd:=''
  50. else
  51. FpGetcwd:=Buf;
  52. End;
  53. Function FpExecve (path : AnsiString; argv : ppchar; envp: ppchar): cInt;
  54. Begin
  55. FpExecve:=FpExecve (pchar(path),argv,envp);
  56. End;
  57. Function FpExecv (path : AnsiString; argv : ppchar): cInt;
  58. Begin
  59. FpExecv:=FpExecve (pchar(path),argv,envp);
  60. End;
  61. Function FpChdir (path : AnsiString): cInt;
  62. Begin
  63. FpChDir:=FpChdir(pchar(Path));
  64. End;
  65. Function FpOpen (path : AnsiString; flags : cInt; Mode: TMode):cInt;
  66. Begin
  67. FpOpen:=FpOpen(pchar(Path),flags,mode);
  68. End;
  69. Function FpMkdir (path : AnsiString; Mode: TMode):cInt;
  70. Begin
  71. FpMkdir:=FpMkdir(pchar(Path),mode);
  72. End;
  73. Function FpUnlink (path : AnsiString): cInt;
  74. Begin
  75. FpUnlink:=FpUnlink(pchar(path));
  76. End;
  77. Function FpRmdir (path : AnsiString): cInt;
  78. Begin
  79. FpRmdir:=FpRmdir(pchar(path));
  80. End;
  81. Function FpRename (old : AnsiString;newpath: AnsiString): cInt;
  82. Begin
  83. FpRename:=FpRename(pchar(old),pchar(newpath));
  84. End;
  85. Function FpStat (path: AnsiString; var buf : stat): cInt;
  86. begin
  87. FpStat:=FpStat(pchar(path),buf);
  88. End;
  89. Function FpAccess (pathname : AnsiString; aMode : cInt): cInt;
  90. Begin
  91. FpAccess:=FpAccess(pchar(pathname),amode);
  92. End;
  93. Function FPFStat(var F:Text;Var Info:stat):Boolean;
  94. {
  95. Get all information on a text file, and return it in info.
  96. }
  97. begin
  98. FPFStat:=FPFstat(TextRec(F).Handle,INfo)=0;
  99. end;
  100. Function FPFStat(var F:File;Var Info:stat):Boolean;
  101. {
  102. Get all information on a untyped file, and return it in info.
  103. }
  104. begin
  105. FPFStat:=FPFstat(FileRec(F).Handle,Info)=0;
  106. end;
  107. Function FpSignal(signum:longint;Handler:signalhandler):signalhandler;
  108. // should be moved out of generic files. Too specific.
  109. var sa,osa : sigactionrec;
  110. begin
  111. {$Ifdef BSD}
  112. sa.sa_handler:=tsigaction(handler);
  113. {$else}
  114. sa.sa_handler:=handler;
  115. {$endif}
  116. FillChar(sa.sa_mask,sizeof(sigset),#0);
  117. sa.sa_flags := 0;
  118. { if (sigintr and signum) =0 then
  119. {restart behaviour needs libc}
  120. sa.sa_flags :=sa.sa_flags or SA_RESTART;
  121. }
  122. {$ifdef RTSIGACTION}
  123. sa.sa_flags:=SA_SIGINFO
  124. {$ifdef cpux86_64}
  125. or $4000000
  126. {$endif cpux86_64}
  127. ;
  128. {$endif RTSIGACTION}
  129. FPSigaction(signum,@sa,@osa);
  130. if fpgetErrNo<>0 then
  131. fpsignal:=NIL
  132. else
  133. {$ifdef BSD}
  134. fpsignal:=signalhandler(osa.sa_handler);
  135. {$else}
  136. fpsignal:=osa.sa_handler;
  137. {$endif}
  138. end;
  139. {$ifdef FPC_USE_LIBC} // can't remember why this is the case. Might be legacy.
  140. function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; cdecl; external clib name 'read';
  141. {$else}
  142. function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; external name 'FPC_SYSC_READ';
  143. {$endif}
  144. Function FpRead (fd : cInt;var buf; nbytes : TSize): TSsize;
  145. begin
  146. FPRead:=xFpRead(fd,pchar(@buf),nbytes);
  147. end;
  148. Function FpWrite (fd : cInt;const buf; nbytes : TSize): TSsize;
  149. begin
  150. FpWrite:=FpWrite(fd,pchar(@buf),nbytes);
  151. end;
  152. Function FpOpen (path : pChar; flags : cInt):cInt;
  153. begin
  154. FpOpen:=FpOpen(path,flags,438);
  155. end;
  156. Function FpOpen (path : AnsiString; flags : cInt):cInt;
  157. begin
  158. FpOpen:=FpOpen(pchar(path),flags,438);
  159. end;
  160. Function FpOpen (path : String; flags : cInt):cInt;
  161. begin
  162. path:=path+#0;
  163. FpOpen:=FpOpen(@path[1],flags,438);
  164. end;
  165. Function FpOpen (path : String; flags : cInt; Mode: TMode):cInt;
  166. begin
  167. path:=path+#0;
  168. FpOpen:=FpOpen(@path[1],flags,Mode);
  169. end;
  170. Function FpOpendir (dirname : AnsiString): pDir;
  171. Begin
  172. FpOpenDir:=FpOpenDir(pchar(dirname));
  173. End;
  174. Function FpOpendir (dirname : shortString): pDir;
  175. Begin
  176. dirname:=dirname+#0;
  177. FpOpenDir:=FpOpenDir(pchar(@dirname[1]));
  178. End;
  179. Function FpStat (path: String; var buf : stat): cInt;
  180. begin
  181. path:=path+#0;
  182. FpStat:=FpStat(pchar(@path[1]),buf);
  183. end;
  184. Function fpDup(var oldfile,newfile:text):cint;
  185. {
  186. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  187. oldfile.
  188. After which the two textfiles are, in effect, the same, except
  189. that they don't share the same buffer, and don't share the same
  190. close_on_exit flag.
  191. }
  192. begin
  193. flush(oldfile);{ We cannot share buffers, so we flush them. }
  194. textrec(newfile):=textrec(oldfile);
  195. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  196. textrec(newfile).handle:=fpDup(textrec(oldfile).handle);
  197. fpdup:=textrec(newfile).handle;
  198. end;
  199. Function fpDup(var oldfile,newfile:file):cint;
  200. {
  201. Copies the filedescriptor oldfile to newfile
  202. }
  203. begin
  204. filerec(newfile):=filerec(oldfile);
  205. filerec(newfile).handle:=fpDup(filerec(oldfile).handle);
  206. fpdup:= filerec(newfile).handle;
  207. end;
  208. Function FpDup2(var oldfile,newfile:text):cint;
  209. {
  210. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  211. oldfile. It closes newfile if it was still open.
  212. After which the two textfiles are, in effect, the same, except
  213. that they don't share the same buffer, and don't share the same
  214. close_on_exit flag.
  215. }
  216. var
  217. tmphandle : word;
  218. begin
  219. case TextRec(oldfile).mode of
  220. fmOutput, fmInOut, fmAppend :
  221. flush(oldfile);{ We cannot share buffers, so we flush them. }
  222. end;
  223. case TextRec(newfile).mode of
  224. fmOutput, fmInOut, fmAppend :
  225. flush(newfile);
  226. end;
  227. tmphandle:=textrec(newfile).handle;
  228. textrec(newfile):=textrec(oldfile);
  229. textrec(newfile).handle:=tmphandle;
  230. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  231. fpDup2:=fpDup2(textrec(oldfile).handle,textrec(newfile).handle);
  232. end;
  233. Function FpDup2(var oldfile,newfile:file):cint;
  234. {
  235. Copies the filedescriptor oldfile to newfile
  236. }
  237. begin
  238. filerec(newfile):=filerec(oldfile);
  239. fpDup2:=fpDup2(filerec(oldfile).handle,filerec(newfile).handle);
  240. end;
  241. function fptime :time_t;
  242. var t:time_t;
  243. begin
  244. fptime:=fptime(t);
  245. end;
  246. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdset;TimeOut:cint):cint;
  247. {
  248. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  249. have changed.
  250. This function allows specification of a timeout as a longint.
  251. }
  252. var
  253. p : PTimeVal;
  254. tv : TimeVal;
  255. begin
  256. if TimeOut=-1 then
  257. p:=nil
  258. else
  259. begin
  260. tv.tv_Sec:=Timeout div 1000;
  261. tv.tv_Usec:=(Timeout mod 1000)*1000;
  262. p:=@tv;
  263. end;
  264. fpSelect:=fpSelect(N,Readfds,WriteFds,ExceptFds,p);
  265. end;
  266. Function fpSelect(var T:Text;TimeOut :PTimeval):cint;
  267. Var
  268. F:TfdSet;
  269. begin
  270. if textrec(t).mode=fmclosed then
  271. begin
  272. fpSetErrNo(ESysEBADF);
  273. exit(-1);
  274. end;
  275. FpFD_ZERO(f);
  276. fpFD_SET(textrec(T).handle,f);
  277. if textrec(T).mode=fminput then
  278. fpselect:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  279. else
  280. fpSelect:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  281. end;
  282. Function fpSelect(var T:Text;TimeOut :time_t):cint;
  283. var
  284. p : PTimeVal;
  285. tv : TimeVal;
  286. begin
  287. if TimeOut=-1 then
  288. p:=nil
  289. else
  290. begin
  291. tv.tv_Sec:=Timeout div 1000;
  292. tv.tv_Usec:=(Timeout mod 1000)*1000;
  293. p:=@tv;
  294. end;
  295. fpSelect:=fpSelect(T,p);
  296. end;
  297. function FpWaitPid (pid : TPid; Var Status : cInt; Options : cint) : TPid;
  298. begin
  299. fpWaitPID:=fpWaitPID(Pid,@Status,Options);
  300. end;
  301. Function fpReadLink(Name:ansistring):ansistring;
  302. {
  303. Read a link (where it points to)
  304. }
  305. var
  306. LinkName : ansistring;
  307. i : cint;
  308. begin
  309. SetLength(linkname,PATH_MAX);
  310. i:=fpReadLink(pchar(name),pchar(linkname),PATH_MAX);
  311. if i>0 then
  312. begin
  313. SetLength(linkname,i);
  314. fpReadLink:=LinkName;
  315. end
  316. else
  317. fpReadLink:='';
  318. end;
  319. {
  320. $Log$
  321. Revision 1.16 2004-11-25 12:18:35 jonas
  322. * fixed invalid type conversion
  323. Revision 1.15 2004/11/23 08:40:34 michael
  324. + Added overloaded functions
  325. Revision 1.14 2004/11/19 13:15:14 marco
  326. * external rework. Mostly done.
  327. Revision 1.13 2004/11/14 12:21:08 marco
  328. * moved some calls from unix to baseunix. Darwin untested.
  329. Revision 1.12 2004/11/02 14:49:48 florian
  330. * fixed baseunix.signal for CPU using rt_sigaction
  331. * fixed it for x86_64 too
  332. Revision 1.11 2004/06/01 10:30:03 jonas
  333. * fixed missing cdecl procedure directive
  334. Revision 1.10 2004/03/28 19:36:19 marco
  335. * fix for recursive fpc.exe ?
  336. Revision 1.9 2004/01/04 21:04:08 jonas
  337. * declare C-library routines as external in libc for Darwin (so we
  338. generate proper import entries)
  339. Revision 1.8 2003/12/30 12:24:01 marco
  340. * FPC_USE_LIBC
  341. Revision 1.7 2003/10/27 17:12:45 marco
  342. * fixes for signal handling.
  343. Revision 1.6 2003/10/13 11:37:57 marco
  344. * more small fixes
  345. Revision 1.5 2003/10/12 14:37:10 marco
  346. * small bug fixed in opendir that core dumped the IDE. Now the IDE SIGFPE's in FV.
  347. Revision 1.4 2003/09/16 16:13:56 marco
  348. * fdset functions renamed to fp<posix name>
  349. Revision 1.3 2003/09/14 20:15:01 marco
  350. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  351. Revision 1.2 2003/06/01 16:28:41 marco
  352. * Enhancements to make the compiler baseunix using.
  353. Revision 1.1 2002/12/18 16:49:02 marco
  354. * New RTL. Linux system unit and baseunix operational.
  355. }