bunxovl.inc 9.9 KB

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