bunxovl.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. sa.sa_handler:=SigActionHandler(handler);
  112. FillChar(sa.sa_mask,sizeof(sigset),#0);
  113. sa.sa_flags := 0;
  114. { if (sigintr and signum) =0 then
  115. {restart behaviour needs libc}
  116. sa.sa_flags :=sa.sa_flags or SA_RESTART;
  117. }
  118. {$ifdef RTSIGACTION}
  119. sa.sa_flags:=SA_SIGINFO
  120. {$ifdef cpux86_64}
  121. or $4000000
  122. {$endif cpux86_64}
  123. ;
  124. {$endif RTSIGACTION}
  125. FPSigaction(signum,@sa,@osa);
  126. if fpgetErrNo<>0 then
  127. fpsignal:=NIL
  128. else
  129. fpsignal:=signalhandler(osa.sa_handler);
  130. end;
  131. {$ifdef FPC_USE_LIBC} // can't remember why this is the case. Might be legacy.
  132. function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; cdecl; external clib name 'read';
  133. {$else}
  134. function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; external name 'FPC_SYSC_READ';
  135. {$endif}
  136. Function FpRead (fd : cInt;var buf; nbytes : TSize): TSsize;
  137. begin
  138. FPRead:=xFpRead(fd,pchar(@buf),nbytes);
  139. end;
  140. Function FpWrite (fd : cInt;const buf; nbytes : TSize): TSsize;
  141. begin
  142. FpWrite:=FpWrite(fd,pchar(@buf),nbytes);
  143. end;
  144. Function FpOpen (path : pChar; flags : cInt):cInt;
  145. begin
  146. FpOpen:=FpOpen(path,flags,438);
  147. end;
  148. Function FpOpen (path : AnsiString; flags : cInt):cInt;
  149. begin
  150. FpOpen:=FpOpen(pchar(path),flags,438);
  151. end;
  152. Function FpOpen (path : String; flags : cInt):cInt;
  153. begin
  154. path:=path+#0;
  155. FpOpen:=FpOpen(@path[1],flags,438);
  156. end;
  157. Function FpOpen (path : String; flags : cInt; Mode: TMode):cInt;
  158. begin
  159. path:=path+#0;
  160. FpOpen:=FpOpen(@path[1],flags,Mode);
  161. end;
  162. Function FpOpendir (dirname : AnsiString): pDir;
  163. Begin
  164. FpOpenDir:=FpOpenDir(pchar(dirname));
  165. End;
  166. Function FpOpendir (dirname : shortString): pDir;
  167. Begin
  168. dirname:=dirname+#0;
  169. FpOpenDir:=FpOpenDir(pchar(@dirname[1]));
  170. End;
  171. Function FpStat (path: String; var buf : stat): cInt;
  172. begin
  173. path:=path+#0;
  174. FpStat:=FpStat(pchar(@path[1]),buf);
  175. end;
  176. Function fpDup(var oldfile,newfile:text):cint;
  177. {
  178. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  179. oldfile.
  180. After which the two textfiles are, in effect, the same, except
  181. that they don't share the same buffer, and don't share the same
  182. close_on_exit flag.
  183. }
  184. begin
  185. flush(oldfile);{ We cannot share buffers, so we flush them. }
  186. textrec(newfile):=textrec(oldfile);
  187. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  188. textrec(newfile).handle:=fpDup(textrec(oldfile).handle);
  189. fpdup:=textrec(newfile).handle;
  190. end;
  191. Function fpDup(var oldfile,newfile:file):cint;
  192. {
  193. Copies the filedescriptor oldfile to newfile
  194. }
  195. begin
  196. filerec(newfile):=filerec(oldfile);
  197. filerec(newfile).handle:=fpDup(filerec(oldfile).handle);
  198. fpdup:= filerec(newfile).handle;
  199. end;
  200. Function FpDup2(var oldfile,newfile:text):cint;
  201. {
  202. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  203. oldfile. It closes newfile if it was still open.
  204. After which the two textfiles are, in effect, the same, except
  205. that they don't share the same buffer, and don't share the same
  206. close_on_exit flag.
  207. }
  208. var
  209. tmphandle : word;
  210. begin
  211. case TextRec(oldfile).mode of
  212. fmOutput, fmInOut, fmAppend :
  213. flush(oldfile);{ We cannot share buffers, so we flush them. }
  214. end;
  215. case TextRec(newfile).mode of
  216. fmOutput, fmInOut, fmAppend :
  217. flush(newfile);
  218. end;
  219. tmphandle:=textrec(newfile).handle;
  220. textrec(newfile):=textrec(oldfile);
  221. textrec(newfile).handle:=tmphandle;
  222. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  223. fpDup2:=fpDup2(textrec(oldfile).handle,textrec(newfile).handle);
  224. end;
  225. Function FpDup2(var oldfile,newfile:file):cint;
  226. {
  227. Copies the filedescriptor oldfile to newfile
  228. }
  229. begin
  230. filerec(newfile):=filerec(oldfile);
  231. fpDup2:=fpDup2(filerec(oldfile).handle,filerec(newfile).handle);
  232. end;
  233. function fptime :time_t;
  234. var t:time_t;
  235. begin
  236. fptime:=fptime(t);
  237. end;
  238. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdset;TimeOut:cint):cint;
  239. {
  240. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  241. have changed.
  242. This function allows specification of a timeout as a longint.
  243. }
  244. var
  245. p : PTimeVal;
  246. tv : TimeVal;
  247. begin
  248. if TimeOut=-1 then
  249. p:=nil
  250. else
  251. begin
  252. tv.tv_Sec:=Timeout div 1000;
  253. tv.tv_Usec:=(Timeout mod 1000)*1000;
  254. p:=@tv;
  255. end;
  256. fpSelect:=fpSelect(N,Readfds,WriteFds,ExceptFds,p);
  257. end;
  258. Function fpSelect(var T:Text;TimeOut :PTimeval):cint;
  259. Var
  260. F:TfdSet;
  261. begin
  262. if textrec(t).mode=fmclosed then
  263. begin
  264. fpSetErrNo(ESysEBADF);
  265. exit(-1);
  266. end;
  267. FpFD_ZERO(f);
  268. fpFD_SET(textrec(T).handle,f);
  269. if textrec(T).mode=fminput then
  270. fpselect:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  271. else
  272. fpSelect:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  273. end;
  274. Function fpSelect(var T:Text;TimeOut :time_t):cint;
  275. var
  276. p : PTimeVal;
  277. tv : TimeVal;
  278. begin
  279. if TimeOut=-1 then
  280. p:=nil
  281. else
  282. begin
  283. tv.tv_Sec:=Timeout div 1000;
  284. tv.tv_Usec:=(Timeout mod 1000)*1000;
  285. p:=@tv;
  286. end;
  287. fpSelect:=fpSelect(T,p);
  288. end;
  289. function FpWaitPid (pid : TPid; Var Status : cInt; Options : cint) : TPid;
  290. begin
  291. fpWaitPID:=fpWaitPID(Pid,@Status,Options);
  292. end;
  293. Function fpReadLink(Name:ansistring):ansistring;
  294. {
  295. Read a link (where it points to)
  296. }
  297. var
  298. LinkName : ansistring;
  299. i : cint;
  300. begin
  301. SetLength(linkname,PATH_MAX);
  302. i:=fpReadLink(pchar(name),pchar(linkname),PATH_MAX);
  303. if i>0 then
  304. begin
  305. SetLength(linkname,i);
  306. fpReadLink:=LinkName;
  307. end
  308. else
  309. fpReadLink:='';
  310. end;
  311. {
  312. $Log$
  313. Revision 1.18 2005-02-14 17:13:31 peter
  314. * truncate log
  315. Revision 1.17 2005/01/30 18:01:15 peter
  316. * signal cleanup for linux
  317. * sigactionhandler instead of tsigaction for bsds
  318. * sigcontext moved to cpu dir
  319. }