bunxovl.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 fpgetErrNo<>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. {$ifdef FPC_USE_LIBC} // can't remember why this is the case. Might be legacy.
  118. function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; external clib name 'read';
  119. {$else}
  120. function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; external name 'FPC_SYSC_READ';
  121. {$endif}
  122. Function FpRead (fd : cInt;var buf; nbytes : TSize): TSsize;
  123. begin
  124. FPRead:=xFpRead(fd,pchar(@buf),nbytes);
  125. end;
  126. Function FpWrite (fd : cInt;const buf; nbytes : TSize): TSsize;
  127. begin
  128. FpWrite:=FpWrite(fd,pchar(@buf),nbytes);
  129. end;
  130. Function FpOpen (path : pChar; flags : cInt):cInt;
  131. begin
  132. FpOpen:=FpOpen(path,flags,438);
  133. end;
  134. Function FpOpen (path : AnsiString; flags : cInt):cInt;
  135. begin
  136. FpOpen:=FpOpen(pchar(path),flags,438);
  137. end;
  138. Function FpOpen (path : String; flags : cInt):cInt;
  139. begin
  140. path:=path+#0;
  141. FpOpen:=FpOpen(@path[1],flags,438);
  142. end;
  143. Function FpOpen (path : String; flags : cInt; Mode: TMode):cInt;
  144. begin
  145. path:=path+#0;
  146. FpOpen:=FpOpen(@path[1],flags,Mode);
  147. end;
  148. Function FpOpendir (dirname : AnsiString): pDir;
  149. Begin
  150. FpOpenDir:=FpOpenDir(pchar(dirname));
  151. End;
  152. Function FpOpendir (dirname : shortString): pDir;
  153. Begin
  154. dirname:=dirname+#0;
  155. FpOpenDir:=FpOpenDir(pchar(@dirname[1]));
  156. End;
  157. Function FpStat (path: String; var buf : stat): cInt;
  158. begin
  159. path:=path+#0;
  160. FpStat:=FpStat(pchar(@path[1]),buf);
  161. end;
  162. Function fpDup(var oldfile,newfile:text):cint;
  163. {
  164. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  165. oldfile.
  166. After which the two textfiles are, in effect, the same, except
  167. that they don't share the same buffer, and don't share the same
  168. close_on_exit flag.
  169. }
  170. begin
  171. flush(oldfile);{ We cannot share buffers, so we flush them. }
  172. textrec(newfile):=textrec(oldfile);
  173. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  174. textrec(newfile).handle:=fpDup(textrec(oldfile).handle);
  175. fpdup:=textrec(newfile).handle;
  176. end;
  177. Function fpDup(var oldfile,newfile:file):cint;
  178. {
  179. Copies the filedescriptor oldfile to newfile
  180. }
  181. begin
  182. filerec(newfile):=filerec(oldfile);
  183. filerec(newfile).handle:=fpDup(filerec(oldfile).handle);
  184. fpdup:= filerec(newfile).handle;
  185. end;
  186. Function FpDup2(var oldfile,newfile:text):cint;
  187. {
  188. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  189. oldfile. It closes newfile if it was still open.
  190. After which the two textfiles are, in effect, the same, except
  191. that they don't share the same buffer, and don't share the same
  192. close_on_exit flag.
  193. }
  194. var
  195. tmphandle : word;
  196. begin
  197. case TextRec(oldfile).mode of
  198. fmOutput, fmInOut, fmAppend :
  199. flush(oldfile);{ We cannot share buffers, so we flush them. }
  200. end;
  201. case TextRec(newfile).mode of
  202. fmOutput, fmInOut, fmAppend :
  203. flush(newfile);
  204. end;
  205. tmphandle:=textrec(newfile).handle;
  206. textrec(newfile):=textrec(oldfile);
  207. textrec(newfile).handle:=tmphandle;
  208. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  209. fpDup2:=fpDup2(textrec(oldfile).handle,textrec(newfile).handle);
  210. end;
  211. Function FpDup2(var oldfile,newfile:file):cint;
  212. {
  213. Copies the filedescriptor oldfile to newfile
  214. }
  215. begin
  216. filerec(newfile):=filerec(oldfile);
  217. fpDup2:=fpDup2(filerec(oldfile).handle,filerec(newfile).handle);
  218. end;
  219. function fptime :time_t;
  220. var t:time_t;
  221. begin
  222. fptime:=fptime(t);
  223. end;
  224. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdset;TimeOut:cint):cint;
  225. {
  226. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  227. have changed.
  228. This function allows specification of a timeout as a longint.
  229. }
  230. var
  231. p : PTimeVal;
  232. tv : TimeVal;
  233. begin
  234. if TimeOut=-1 then
  235. p:=nil
  236. else
  237. begin
  238. tv.tv_Sec:=Timeout div 1000;
  239. tv.tv_Usec:=(Timeout mod 1000)*1000;
  240. p:=@tv;
  241. end;
  242. fpSelect:=fpSelect(N,Readfds,WriteFds,ExceptFds,p);
  243. end;
  244. Function fpSelect(var T:Text;TimeOut :PTimeval):cint;
  245. Var
  246. F:TfdSet;
  247. begin
  248. if textrec(t).mode=fmclosed then
  249. begin
  250. fpSetErrNo(ESysEBADF);
  251. exit(-1);
  252. end;
  253. FpFD_ZERO(f);
  254. fpFD_SET(textrec(T).handle,f);
  255. if textrec(T).mode=fminput then
  256. fpselect:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  257. else
  258. fpSelect:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  259. end;
  260. Function fpSelect(var T:Text;TimeOut :time_t):cint;
  261. var
  262. p : PTimeVal;
  263. tv : TimeVal;
  264. begin
  265. if TimeOut=-1 then
  266. p:=nil
  267. else
  268. begin
  269. tv.tv_Sec:=Timeout div 1000;
  270. tv.tv_Usec:=(Timeout mod 1000)*1000;
  271. p:=@tv;
  272. end;
  273. fpSelect:=fpSelect(T,p);
  274. end;
  275. {
  276. $Log$
  277. Revision 1.9 2004-01-04 21:04:08 jonas
  278. * declare C-library routines as external in libc for Darwin (so we
  279. generate proper import entries)
  280. Revision 1.8 2003/12/30 12:24:01 marco
  281. * FPC_USE_LIBC
  282. Revision 1.7 2003/10/27 17:12:45 marco
  283. * fixes for signal handling.
  284. Revision 1.6 2003/10/13 11:37:57 marco
  285. * more small fixes
  286. Revision 1.5 2003/10/12 14:37:10 marco
  287. * small bug fixed in opendir that core dumped the IDE. Now the IDE SIGFPE's in FV.
  288. Revision 1.4 2003/09/16 16:13:56 marco
  289. * fdset functions renamed to fp<posix name>
  290. Revision 1.3 2003/09/14 20:15:01 marco
  291. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  292. Revision 1.2 2003/06/01 16:28:41 marco
  293. * Enhancements to make the compiler baseunix using.
  294. Revision 1.1 2002/12/18 16:49:02 marco
  295. * New RTL. Linux system unit and baseunix operational.
  296. }