bunxovl.inc 10.0 KB

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