bunxovl.inc 8.1 KB

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