bunxovl.inc 9.1 KB

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