bunxovl.inc 12 KB

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