bunxovl.inc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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(sa.sa_mask),#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. {$ifdef linux}
  144. function FppRead (fd : cInt;var buf; nbytes : TSize; offset:Toff): TSsize; {$ifdef VER2_0}inline;{$endif}
  145. begin
  146. FppRead:=FppRead(fd,pchar(@buf),nbytes,offset);
  147. end;
  148. function FppWrite (fd : cInt;const buf; nbytes : TSize; offset:Toff): TSsize; {$ifdef VER2_0}inline;{$endif}
  149. begin
  150. FppWrite:=FppWrite(fd,pchar(@buf),nbytes,offset);
  151. end;
  152. {$endif}
  153. Function FpOpen (path : pChar; flags : cInt):cInt; {$ifdef VER2_0}inline;{$endif}
  154. begin
  155. FpOpen:=FpOpen(path,flags,438);
  156. end;
  157. Function FpOpen (path : AnsiString; flags : cInt):cInt; {$ifdef VER2_0}inline;{$endif}
  158. begin
  159. FpOpen:=FpOpen(pchar(path),flags,438);
  160. end;
  161. Function FpOpen (path : String; flags : cInt):cInt;
  162. begin
  163. path:=path+#0;
  164. FpOpen:=FpOpen(@path[1],flags,438);
  165. end;
  166. Function FpOpen (path : String; flags : cInt; Mode: TMode):cInt;
  167. begin
  168. path:=path+#0;
  169. FpOpen:=FpOpen(@path[1],flags,Mode);
  170. end;
  171. Function FpOpendir (dirname : AnsiString): pDir; {$ifdef VER2_0}inline;{$endif}
  172. Begin
  173. FpOpenDir:=FpOpenDir(pchar(dirname));
  174. End;
  175. Function FpOpendir (dirname : shortString): pDir; {$ifdef VER2_0}inline;{$endif}
  176. Begin
  177. dirname:=dirname+#0;
  178. FpOpenDir:=FpOpenDir(pchar(@dirname[1]));
  179. End;
  180. Function FpStat (path: String; var buf : stat): cInt;
  181. begin
  182. path:=path+#0;
  183. FpStat:=FpStat(pchar(@path[1]),buf);
  184. end;
  185. Function fpDup(var oldfile,newfile:text):cint;
  186. {
  187. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  188. oldfile.
  189. After which the two textfiles are, in effect, the same, except
  190. that they don't share the same buffer, and don't share the same
  191. close_on_exit flag.
  192. }
  193. begin
  194. flush(oldfile);{ We cannot share buffers, so we flush them. }
  195. textrec(newfile):=textrec(oldfile);
  196. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  197. textrec(newfile).handle:=fpDup(textrec(oldfile).handle);
  198. fpdup:=textrec(newfile).handle;
  199. end;
  200. Function fpDup(var oldfile,newfile:file):cint;
  201. {
  202. Copies the filedescriptor oldfile to newfile
  203. }
  204. begin
  205. filerec(newfile):=filerec(oldfile);
  206. filerec(newfile).handle:=fpDup(filerec(oldfile).handle);
  207. fpdup:= filerec(newfile).handle;
  208. end;
  209. Function FpDup2(var oldfile,newfile:text):cint;
  210. {
  211. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  212. oldfile. It closes newfile if it was still open.
  213. After which the two textfiles are, in effect, the same, except
  214. that they don't share the same buffer, and don't share the same
  215. close_on_exit flag.
  216. }
  217. var
  218. tmphandle : word;
  219. begin
  220. case TextRec(oldfile).mode of
  221. fmOutput, fmInOut, fmAppend :
  222. flush(oldfile);{ We cannot share buffers, so we flush them. }
  223. end;
  224. case TextRec(newfile).mode of
  225. fmOutput, fmInOut, fmAppend :
  226. flush(newfile);
  227. end;
  228. tmphandle:=textrec(newfile).handle;
  229. textrec(newfile):=textrec(oldfile);
  230. textrec(newfile).handle:=tmphandle;
  231. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  232. fpDup2:=fpDup2(textrec(oldfile).handle,textrec(newfile).handle);
  233. end;
  234. Function FpDup2(var oldfile,newfile:file):cint;
  235. {
  236. Copies the filedescriptor oldfile to newfile
  237. }
  238. var
  239. tmphandle : word;
  240. begin
  241. tmphandle := filerec(newfile).handle;
  242. filerec(newfile):=filerec(oldfile);
  243. filerec(newfile).handle := tmphandle;
  244. fpDup2:=fpDup2(filerec(oldfile).handle,filerec(newfile).handle);
  245. end;
  246. function fptime :time_t; {$ifdef VER2_0}inline;{$endif}
  247. var t:time_t;
  248. begin
  249. fptime:=fptime(t);
  250. end;
  251. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdset;TimeOut:cint):cint;
  252. {
  253. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  254. have changed.
  255. This function allows specification of a timeout as a longint.
  256. }
  257. var
  258. p : PTimeVal;
  259. tv : TimeVal;
  260. begin
  261. if TimeOut=-1 then
  262. p:=nil
  263. else
  264. begin
  265. tv.tv_Sec:=Timeout div 1000;
  266. tv.tv_Usec:=(Timeout mod 1000)*1000;
  267. p:=@tv;
  268. end;
  269. fpSelect:=fpSelect(N,Readfds,WriteFds,ExceptFds,p);
  270. end;
  271. Function fpSelect(var T:Text;TimeOut :PTimeval):cint;
  272. Var
  273. F:TfdSet;
  274. begin
  275. if textrec(t).mode=fmclosed then
  276. begin
  277. fpSetErrNo(ESysEBADF);
  278. exit(-1);
  279. end;
  280. FpFD_ZERO(f);
  281. fpFD_SET(textrec(T).handle,f);
  282. if textrec(T).mode=fminput then
  283. fpselect:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  284. else
  285. fpSelect:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  286. end;
  287. Function fpSelect(var T:Text;TimeOut :time_t):cint;
  288. var
  289. p : PTimeVal;
  290. tv : TimeVal;
  291. begin
  292. if TimeOut=-1 then
  293. p:=nil
  294. else
  295. begin
  296. tv.tv_Sec:=Timeout div 1000;
  297. tv.tv_Usec:=(Timeout mod 1000)*1000;
  298. p:=@tv;
  299. end;
  300. fpSelect:=fpSelect(T,p);
  301. end;
  302. function FpWaitPid (pid : TPid; Var Status : cInt; Options : cint) : TPid;
  303. begin
  304. fpWaitPID:=fpWaitPID(Pid,@Status,Options);
  305. end;
  306. Function fpReadLink(Name:ansistring):ansistring;
  307. {
  308. Read a link (where it points to)
  309. }
  310. var
  311. LinkName : ansistring;
  312. i : cint;
  313. begin
  314. SetLength(linkname,PATH_MAX);
  315. i:=fpReadLink(pchar(name),pchar(linkname),PATH_MAX);
  316. if i>0 then
  317. begin
  318. SetLength(linkname,i);
  319. fpReadLink:=LinkName;
  320. end
  321. else
  322. fpReadLink:='';
  323. end;