bunxovl.inc 10 KB

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