bunxovl.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. var sa,osa : sigactionrec;
  94. begin
  95. sa.sa_handler:=handler;
  96. FillChar(sa.sa_mask,sizeof(sigset),#0);
  97. sa.sa_flags := 0;
  98. { if (sigintr and signum) =0 then
  99. {restart behaviour needs libc}
  100. sa.sa_flags :=sa.sa_flags or SA_RESTART;
  101. }
  102. FPSigaction(signum,@sa,@osa);
  103. if getErrNo<>0 then
  104. fpsignal:=NIL
  105. else
  106. fpsignal:=osa.sa_handler;
  107. end;
  108. function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; external name 'FPC_SYSC_READ';
  109. Function FpRead (fd : cInt;var buf; nbytes : TSize): TSsize;
  110. begin
  111. FPRead:=xFpRead(fd,pchar(@buf),nbytes);
  112. end;
  113. Function FpWrite (fd : cInt;const buf; nbytes : TSize): TSsize;
  114. begin
  115. FpWrite:=FpWrite(fd,pchar(@buf),nbytes);
  116. end;
  117. Function FpOpen (path : pChar; flags : cInt):cInt;
  118. begin
  119. FpOpen:=FpOpen(path,flags,438);
  120. end;
  121. Function FpOpen (path : AnsiString; flags : cInt):cInt;
  122. begin
  123. FpOpen:=FpOpen(pchar(path),flags,438);
  124. end;
  125. Function FpOpen (path : String; flags : cInt):cInt;
  126. begin
  127. path:=path+#0;
  128. FpOpen:=FpOpen(@path[1],flags,438);
  129. end;
  130. Function FpOpen (path : String; flags : cInt; Mode: TMode):cInt;
  131. begin
  132. path:=path+#0;
  133. FpOpen:=FpOpen(@path[1],flags,Mode);
  134. end;
  135. Function FpOpendir (dirname : AnsiString): pDir;
  136. Begin
  137. FpOpenDir:=FpOpenDir(dirname);
  138. End;
  139. Function FpStat (path: String; var buf : stat): cInt;
  140. begin
  141. path:=path+#0;
  142. FpStat:=FpStat(pchar(@path[1]),buf);
  143. end;
  144. Function fpDup(var oldfile,newfile:text):cint;
  145. {
  146. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  147. oldfile.
  148. After which the two textfiles are, in effect, the same, except
  149. that they don't share the same buffer, and don't share the same
  150. close_on_exit flag.
  151. }
  152. begin
  153. flush(oldfile);{ We cannot share buffers, so we flush them. }
  154. textrec(newfile):=textrec(oldfile);
  155. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  156. textrec(newfile).handle:=fpDup(textrec(oldfile).handle);
  157. fpdup:=textrec(newfile).handle;
  158. end;
  159. Function fpDup(var oldfile,newfile:file):cint;
  160. {
  161. Copies the filedescriptor oldfile to newfile
  162. }
  163. begin
  164. filerec(newfile):=filerec(oldfile);
  165. filerec(newfile).handle:=fpDup(filerec(oldfile).handle);
  166. fpdup:= filerec(newfile).handle;
  167. end;
  168. Function FpDup2(var oldfile,newfile:text):cint;
  169. {
  170. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  171. oldfile. It closes newfile if it was still open.
  172. After which the two textfiles are, in effect, the same, except
  173. that they don't share the same buffer, and don't share the same
  174. close_on_exit flag.
  175. }
  176. var
  177. tmphandle : word;
  178. begin
  179. case TextRec(oldfile).mode of
  180. fmOutput, fmInOut, fmAppend :
  181. flush(oldfile);{ We cannot share buffers, so we flush them. }
  182. end;
  183. case TextRec(newfile).mode of
  184. fmOutput, fmInOut, fmAppend :
  185. flush(newfile);
  186. end;
  187. tmphandle:=textrec(newfile).handle;
  188. textrec(newfile):=textrec(oldfile);
  189. textrec(newfile).handle:=tmphandle;
  190. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  191. fpDup2:=fpDup2(textrec(oldfile).handle,textrec(newfile).handle);
  192. end;
  193. Function FpDup2(var oldfile,newfile:file):cint;
  194. {
  195. Copies the filedescriptor oldfile to newfile
  196. }
  197. begin
  198. filerec(newfile):=filerec(oldfile);
  199. fpDup2:=fpDup2(filerec(oldfile).handle,filerec(newfile).handle);
  200. end;
  201. function fptime :time_t;
  202. var t:time_t;
  203. begin
  204. fptime:=fptime(t);
  205. end;
  206. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdset;TimeOut:cint):cint;
  207. {
  208. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  209. have changed.
  210. This function allows specification of a timeout as a longint.
  211. }
  212. var
  213. p : PTimeVal;
  214. tv : TimeVal;
  215. begin
  216. if TimeOut=-1 then
  217. p:=nil
  218. else
  219. begin
  220. tv.tv_Sec:=Timeout div 1000;
  221. tv.tv_Usec:=(Timeout mod 1000)*1000;
  222. p:=@tv;
  223. end;
  224. fpSelect:=fpSelect(N,Readfds,WriteFds,ExceptFds,p);
  225. end;
  226. Function fpSelect(var T:Text;TimeOut :PTimeval):cint;
  227. Var
  228. F:TfdSet;
  229. begin
  230. if textrec(t).mode=fmclosed then
  231. begin
  232. SetErrNo(ESysEBADF);
  233. exit(-1);
  234. end;
  235. Fpfdemptyset(f);
  236. fpfdaddset(f,textrec(T).handle);
  237. if textrec(T).mode=fminput then
  238. fpselect:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  239. else
  240. fpSelect:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  241. end;
  242. Function fpSelect(var T:Text;TimeOut :time_t):cint;
  243. var
  244. p : PTimeVal;
  245. tv : TimeVal;
  246. begin
  247. if TimeOut=-1 then
  248. p:=nil
  249. else
  250. begin
  251. tv.tv_Sec:=Timeout div 1000;
  252. tv.tv_Usec:=(Timeout mod 1000)*1000;
  253. p:=@tv;
  254. end;
  255. fpSelect:=fpSelect(T,p);
  256. end;
  257. {
  258. $Log$
  259. Revision 1.3 2003-09-14 20:15:01 marco
  260. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  261. Revision 1.2 2003/06/01 16:28:41 marco
  262. * Enhancements to make the compiler baseunix using.
  263. Revision 1.1 2002/12/18 16:49:02 marco
  264. * New RTL. Linux system unit and baseunix operational.
  265. }