2
0

bunxovl.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002 by Marco van de Voort
  4. Some generic overloads for stringfunctions in the baseunix unit.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$if defined(CPUARM) or defined(CPUX86_64) or defined(CPUSPARC)}
  12. {$define RTSIGACTION}
  13. {$endif}
  14. {$I textrec.inc}
  15. {$I filerec.inc}
  16. Function FpLink (existing : AnsiString; newone : AnsiString): cInt; {$ifdef VER2_0}inline;{$endif}
  17. Begin
  18. FpLink:=FpLink(pchar(existing),pchar(newone));
  19. End;
  20. Function FpMkfifo (path : AnsiString; Mode : TMode): cInt; {$ifdef VER2_0}inline;{$endif}
  21. Begin
  22. FpMkfifo:=FpMkfifo(pchar(path),mode);
  23. End;
  24. Function FpChmod (path : AnsiString; Mode : TMode): cInt; {$ifdef VER2_0}inline;{$endif}
  25. Begin
  26. FpChmod:=FpChmod(pchar(path),mode);
  27. End;
  28. Function FpChown (path : AnsiString; owner : TUid; group : TGid): cInt;{$ifdef VER2_0}inline;{$endif}
  29. Begin
  30. FpChown:=FpChown(pchar(path),owner,group);
  31. End;
  32. Function FpUtime (path : AnsiString; times : putimbuf): cInt; {$ifdef VER2_0}inline;{$endif}
  33. Begin
  34. FpUtime:=FpUtime(pchar(path),times);
  35. End;
  36. {
  37. Function FpGetcwd (path:AnsiString; siz:TSize):AnsiString; {$ifdef VER2_0}inline;{$endif}
  38. Begin
  39. FpGetcwd:=ansistring(pchar(FpGetcwd(pchar(path),siz)));
  40. End;
  41. }
  42. Function FpGetcwd :AnsiString;
  43. Var
  44. Buf : Array[0..PATH_MAX+1] of char;
  45. Begin
  46. Buf[PATH_MAX+1]:=#0;
  47. If FpGetcwd(@Buf[0],PATH_MAX)=Nil then
  48. FpGetcwd:=''
  49. else
  50. FpGetcwd:=Buf;
  51. End;
  52. Function FpExecve (path : AnsiString; argv : ppchar; envp: ppchar): cInt; {$ifdef VER2_0}inline;{$endif}
  53. Begin
  54. FpExecve:=FpExecve (pchar(path),argv,envp);
  55. End;
  56. Function FpExecv (path : AnsiString; argv : ppchar): cInt; {$ifdef VER2_0}inline;{$endif}
  57. Begin
  58. FpExecv:=FpExecve (pchar(path),argv,envp);
  59. End;
  60. Function FpChdir (path : AnsiString): cInt; {$ifdef VER2_0}inline;{$endif}
  61. Begin
  62. FpChDir:=FpChdir(pchar(Path));
  63. End;
  64. Function FpOpen (path : AnsiString; flags : cInt; Mode: TMode):cInt; {$ifdef VER2_0}inline;{$endif}
  65. Begin
  66. FpOpen:=FpOpen(pchar(Path),flags,mode);
  67. End;
  68. Function FpMkdir (path : AnsiString; Mode: TMode):cInt; {$ifdef VER2_0}inline;{$endif}
  69. Begin
  70. FpMkdir:=FpMkdir(pchar(Path),mode);
  71. End;
  72. Function FpUnlink (path : AnsiString): cInt; {$ifdef VER2_0}inline;{$endif}
  73. Begin
  74. FpUnlink:=FpUnlink(pchar(path));
  75. End;
  76. Function FpRmdir (path : AnsiString): cInt; {$ifdef VER2_0}inline;{$endif}
  77. Begin
  78. FpRmdir:=FpRmdir(pchar(path));
  79. End;
  80. Function FpRename (old : AnsiString;newpath: AnsiString): cInt; {$ifdef VER2_0}inline;{$endif}
  81. Begin
  82. FpRename:=FpRename(pchar(old),pchar(newpath));
  83. End;
  84. Function FpStat (path: AnsiString; var buf : stat): cInt; {$ifdef VER2_0}inline;{$endif}
  85. begin
  86. FpStat:=FpStat(pchar(path),buf);
  87. End;
  88. Function FpAccess (pathname : AnsiString; aMode : cInt): cInt; {$ifdef VER2_0}inline;{$endif}
  89. Begin
  90. FpAccess:=FpAccess(pchar(pathname),amode);
  91. End;
  92. Function FPFStat(var F:Text;Var Info:stat):Boolean; {$ifdef VER2_0}inline;{$endif}
  93. {
  94. Get all information on a text file, and return it in info.
  95. }
  96. begin
  97. FPFStat:=FPFstat(TextRec(F).Handle,INfo)=0;
  98. end;
  99. Function FPFStat(var F:File;Var Info:stat):Boolean; {$ifdef VER2_0}inline;{$endif}
  100. {
  101. Get all information on a untyped file, and return it in info.
  102. }
  103. begin
  104. FPFStat:=FPFstat(FileRec(F).Handle,Info)=0;
  105. end;
  106. Function FpSignal(signum:longint;Handler:signalhandler):signalhandler;
  107. // should be moved out of generic files. Too specific.
  108. var sa,osa : sigactionrec;
  109. begin
  110. sa.sa_handler:=SigActionHandler(handler);
  111. FillChar(sa.sa_mask,sizeof(sigset),#0);
  112. sa.sa_flags := 0;
  113. { if (sigintr and signum) =0 then
  114. {restart behaviour needs libc}
  115. sa.sa_flags :=sa.sa_flags or SA_RESTART;
  116. }
  117. {$ifdef RTSIGACTION}
  118. sa.sa_flags:=SA_SIGINFO
  119. {$ifdef cpux86_64}
  120. or $4000000
  121. {$endif cpux86_64}
  122. ;
  123. {$endif RTSIGACTION}
  124. FPSigaction(signum,@sa,@osa);
  125. if fpgetErrNo<>0 then
  126. fpsignal:=NIL
  127. else
  128. fpsignal:=signalhandler(osa.sa_handler);
  129. end;
  130. {$ifdef FPC_USE_LIBC} // can't remember why this is the case. Might be legacy.
  131. function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; cdecl; external clib name 'read';
  132. {$else}
  133. function xFpread(fd: cint; buf: pchar; nbytes : size_t): ssize_t; external name 'FPC_SYSC_READ';
  134. {$endif}
  135. Function FpRead (fd : cInt;var buf; nbytes : TSize): TSsize; {$ifdef VER2_0}inline;{$endif}
  136. begin
  137. FPRead:=xFpRead(fd,pchar(@buf),nbytes);
  138. end;
  139. Function FpWrite (fd : cInt;const buf; nbytes : TSize): TSsize; {$ifdef VER2_0}inline;{$endif}
  140. begin
  141. FpWrite:=FpWrite(fd,pchar(@buf),nbytes);
  142. end;
  143. Function FpOpen (path : pChar; flags : cInt):cInt; {$ifdef VER2_0}inline;{$endif}
  144. begin
  145. FpOpen:=FpOpen(path,flags,438);
  146. end;
  147. Function FpOpen (path : AnsiString; flags : cInt):cInt; {$ifdef VER2_0}inline;{$endif}
  148. begin
  149. FpOpen:=FpOpen(pchar(path),flags,438);
  150. end;
  151. Function FpOpen (path : String; flags : cInt):cInt;
  152. begin
  153. path:=path+#0;
  154. FpOpen:=FpOpen(@path[1],flags,438);
  155. end;
  156. Function FpOpen (path : String; flags : cInt; Mode: TMode):cInt;
  157. begin
  158. path:=path+#0;
  159. FpOpen:=FpOpen(@path[1],flags,Mode);
  160. end;
  161. Function FpOpendir (dirname : AnsiString): pDir; {$ifdef VER2_0}inline;{$endif}
  162. Begin
  163. FpOpenDir:=FpOpenDir(pchar(dirname));
  164. End;
  165. Function FpOpendir (dirname : shortString): pDir; {$ifdef VER2_0}inline;{$endif}
  166. Begin
  167. dirname:=dirname+#0;
  168. FpOpenDir:=FpOpenDir(pchar(@dirname[1]));
  169. End;
  170. Function FpStat (path: String; var buf : stat): cInt;
  171. begin
  172. path:=path+#0;
  173. FpStat:=FpStat(pchar(@path[1]),buf);
  174. end;
  175. Function fpDup(var oldfile,newfile:text):cint;
  176. {
  177. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  178. oldfile.
  179. After which the two textfiles are, in effect, the same, except
  180. that they don't share the same buffer, and don't share the same
  181. close_on_exit flag.
  182. }
  183. begin
  184. flush(oldfile);{ We cannot share buffers, so we flush them. }
  185. textrec(newfile):=textrec(oldfile);
  186. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  187. textrec(newfile).handle:=fpDup(textrec(oldfile).handle);
  188. fpdup:=textrec(newfile).handle;
  189. end;
  190. Function fpDup(var oldfile,newfile:file):cint;
  191. {
  192. Copies the filedescriptor oldfile to newfile
  193. }
  194. begin
  195. filerec(newfile):=filerec(oldfile);
  196. filerec(newfile).handle:=fpDup(filerec(oldfile).handle);
  197. fpdup:= filerec(newfile).handle;
  198. end;
  199. Function FpDup2(var oldfile,newfile:text):cint;
  200. {
  201. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  202. oldfile. It closes newfile if it was still open.
  203. After which the two textfiles are, in effect, the same, except
  204. that they don't share the same buffer, and don't share the same
  205. close_on_exit flag.
  206. }
  207. var
  208. tmphandle : word;
  209. begin
  210. case TextRec(oldfile).mode of
  211. fmOutput, fmInOut, fmAppend :
  212. flush(oldfile);{ We cannot share buffers, so we flush them. }
  213. end;
  214. case TextRec(newfile).mode of
  215. fmOutput, fmInOut, fmAppend :
  216. flush(newfile);
  217. end;
  218. tmphandle:=textrec(newfile).handle;
  219. textrec(newfile):=textrec(oldfile);
  220. textrec(newfile).handle:=tmphandle;
  221. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  222. fpDup2:=fpDup2(textrec(oldfile).handle,textrec(newfile).handle);
  223. end;
  224. Function FpDup2(var oldfile,newfile:file):cint;
  225. {
  226. Copies the filedescriptor oldfile to newfile
  227. }
  228. var
  229. tmphandle : word;
  230. begin
  231. tmphandle := filerec(newfile).handle;
  232. filerec(newfile):=filerec(oldfile);
  233. filerec(newfile).handle := tmphandle;
  234. fpDup2:=fpDup2(filerec(oldfile).handle,filerec(newfile).handle);
  235. end;
  236. function fptime :time_t; {$ifdef VER2_0}inline;{$endif}
  237. var t:time_t;
  238. begin
  239. fptime:=fptime(t);
  240. end;
  241. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdset;TimeOut:cint):cint;
  242. {
  243. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  244. have changed.
  245. This function allows specification of a timeout as a longint.
  246. }
  247. var
  248. p : PTimeVal;
  249. tv : TimeVal;
  250. begin
  251. if TimeOut=-1 then
  252. p:=nil
  253. else
  254. begin
  255. tv.tv_Sec:=Timeout div 1000;
  256. tv.tv_Usec:=(Timeout mod 1000)*1000;
  257. p:=@tv;
  258. end;
  259. fpSelect:=fpSelect(N,Readfds,WriteFds,ExceptFds,p);
  260. end;
  261. Function fpSelect(var T:Text;TimeOut :PTimeval):cint;
  262. Var
  263. F:TfdSet;
  264. begin
  265. if textrec(t).mode=fmclosed then
  266. begin
  267. fpSetErrNo(ESysEBADF);
  268. exit(-1);
  269. end;
  270. FpFD_ZERO(f);
  271. fpFD_SET(textrec(T).handle,f);
  272. if textrec(T).mode=fminput then
  273. fpselect:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  274. else
  275. fpSelect:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  276. end;
  277. Function fpSelect(var T:Text;TimeOut :time_t):cint;
  278. var
  279. p : PTimeVal;
  280. tv : TimeVal;
  281. begin
  282. if TimeOut=-1 then
  283. p:=nil
  284. else
  285. begin
  286. tv.tv_Sec:=Timeout div 1000;
  287. tv.tv_Usec:=(Timeout mod 1000)*1000;
  288. p:=@tv;
  289. end;
  290. fpSelect:=fpSelect(T,p);
  291. end;
  292. function FpWaitPid (pid : TPid; Var Status : cInt; Options : cint) : TPid;
  293. begin
  294. fpWaitPID:=fpWaitPID(Pid,@Status,Options);
  295. end;
  296. Function fpReadLink(Name:ansistring):ansistring;
  297. {
  298. Read a link (where it points to)
  299. }
  300. var
  301. LinkName : ansistring;
  302. i : cint;
  303. begin
  304. SetLength(linkname,PATH_MAX);
  305. i:=fpReadLink(pchar(name),pchar(linkname),PATH_MAX);
  306. if i>0 then
  307. begin
  308. SetLength(linkname,i);
  309. fpReadLink:=LinkName;
  310. end
  311. else
  312. fpReadLink:='';
  313. end;