bunxovl.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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(PAnsiChar(SystemExistingFileName),PAnsiChar(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(PAnsiChar(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(PAnsiChar(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(PAnsiChar(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(PAnsiChar(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(PAnsiChar(FpGetcwd(PAnsiChar(SystemPath),siz)));
  54. SetCodePage(FpGetcwd,DefaultFileSystemCodePage,false);
  55. End;
  56. }
  57. Function FpGetcwd: RawByteString;
  58. Var
  59. Buf : Array[0..PATH_MAX+1] of AnsiChar;
  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 : PPAnsiChar; envp: PPAnsiChar): cInt; {$ifdef VER2_0}inline;{$endif}
  71. var
  72. SystemPath: RawByteString;
  73. Begin
  74. SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  75. FpExecve:=FpExecve (PAnsiChar(SystemPath),argv,envp);
  76. End;
  77. Function FpExecv (const path : RawByteString; argv : PPAnsiChar): cInt; {$ifdef VER2_0}inline;{$endif}
  78. var
  79. SystemPath: RawByteString;
  80. Begin
  81. SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  82. FpExecv:=FpExecve (PAnsiChar(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(PAnsiChar(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(PAnsiChar(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(PAnsiChar(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(PAnsiChar(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(PAnsiChar(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(PAnsiChar(OldSystemPath),PAnsiChar(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(PAnsiChar(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(PAnsiChar(SystemPath), info);
  140. end;
  141. Function fpLstat (path:PAnsiChar;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(PAnsiChar(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: PAnsiChar; nbytes : size_t): ssize_t; cdecl; external clib name 'read';
  189. {$else}
  190. function xFpread(fd: cint; buf: PAnsiChar; 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,PAnsiChar(@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,PAnsiChar(@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,PAnsiChar(@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,PAnsiChar(@buf),nbytes,offset);
  208. end;
  209. {$endif}
  210. const
  211. { read/write permission for everyone }
  212. MODE_FPOPEN = S_IWUSR OR S_IRUSR OR
  213. S_IWGRP OR S_IRGRP OR
  214. S_IWOTH OR S_IROTH;
  215. Function FpOpen (path : PAnsiChar; flags : cInt):cInt; {$ifdef VER2_0}inline;{$endif}
  216. begin
  217. FpOpen:=FpOpen(path,flags,MODE_FPOPEN);
  218. end;
  219. Function FpOpen (const path : RawByteString; flags : cInt):cInt; {$ifdef VER2_0}inline;{$endif}
  220. var
  221. SystemPath: RawByteString;
  222. Begin
  223. SystemPath:=ToSingleByteFileSystemEncodedFileName(path);
  224. FpOpen:=FpOpen(PAnsiChar(SystemPath),flags,MODE_FPOPEN);
  225. end;
  226. Function FpOpen (path : ShortString; flags : cInt):cInt;
  227. begin
  228. path:=path+#0;
  229. FpOpen:=FpOpen(@path[1],flags,MODE_FPOPEN);
  230. end;
  231. Function FpOpen (path : ShortString; flags : cInt; Mode: TMode):cInt;
  232. begin
  233. path:=path+#0;
  234. FpOpen:=FpOpen(@path[1],flags,Mode);
  235. end;
  236. Function FpOpendir (const dirname : RawByteString): pDir; {$ifdef VER2_0}inline;{$endif}
  237. var
  238. SystemDirName: RawByteString;
  239. Begin
  240. SystemDirName:=ToSingleByteFileSystemEncodedFileName(dirname);
  241. FpOpenDir:=FpOpenDir(PAnsiChar(SystemDirName));
  242. End;
  243. Function FpOpendir (dirname : shortString): pDir; {$ifdef VER2_0}inline;{$endif}
  244. Begin
  245. dirname:=dirname+#0;
  246. FpOpenDir:=FpOpenDir(PAnsiChar(@dirname[1]));
  247. End;
  248. Function FpStat (path: ShortString; var buf : stat): cInt;
  249. begin
  250. path:=path+#0;
  251. FpStat:=FpStat(PAnsiChar(@path[1]),buf);
  252. end;
  253. Function fpDup(var oldfile,newfile:text):cint;
  254. {
  255. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  256. oldfile.
  257. After which the two textfiles are, in effect, the same, except
  258. that they don't share the same buffer, and don't share the same
  259. close_on_exit flag.
  260. }
  261. begin
  262. flush(oldfile);{ We cannot share buffers, so we flush them. }
  263. textrec(newfile):=textrec(oldfile);
  264. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  265. textrec(newfile).handle:=fpDup(textrec(oldfile).handle);
  266. fpdup:=textrec(newfile).handle;
  267. end;
  268. Function fpDup(var oldfile,newfile:file):cint;
  269. {
  270. Copies the filedescriptor oldfile to newfile
  271. }
  272. begin
  273. filerec(newfile):=filerec(oldfile);
  274. filerec(newfile).handle:=fpDup(filerec(oldfile).handle);
  275. fpdup:= filerec(newfile).handle;
  276. end;
  277. Function FpDup2(var oldfile,newfile:text):cint;
  278. {
  279. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  280. oldfile. It closes newfile if it was still open.
  281. After which the two textfiles are, in effect, the same, except
  282. that they don't share the same buffer, and don't share the same
  283. close_on_exit flag.
  284. }
  285. var
  286. tmphandle : word;
  287. begin
  288. case TextRec(oldfile).mode of
  289. fmOutput, fmInOut, fmAppend :
  290. flush(oldfile);{ We cannot share buffers, so we flush them. }
  291. end;
  292. case TextRec(newfile).mode of
  293. fmOutput, fmInOut, fmAppend :
  294. flush(newfile);
  295. end;
  296. tmphandle:=textrec(newfile).handle;
  297. textrec(newfile):=textrec(oldfile);
  298. textrec(newfile).handle:=tmphandle;
  299. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  300. fpDup2:=fpDup2(textrec(oldfile).handle,textrec(newfile).handle);
  301. end;
  302. Function FpDup2(var oldfile,newfile:file):cint;
  303. {
  304. Copies the filedescriptor oldfile to newfile
  305. }
  306. var
  307. tmphandle : word;
  308. begin
  309. tmphandle := filerec(newfile).handle;
  310. filerec(newfile):=filerec(oldfile);
  311. filerec(newfile).handle := tmphandle;
  312. fpDup2:=fpDup2(filerec(oldfile).handle,filerec(newfile).handle);
  313. end;
  314. function fptime :time_t; {$ifdef VER2_0}inline;{$endif}
  315. var t:time_t;
  316. begin
  317. fptime:=fptime(t);
  318. end;
  319. Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdset;TimeOut:cint):cint;
  320. {
  321. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  322. have changed.
  323. This function allows specification of a timeout as a longint.
  324. }
  325. var
  326. p : PTimeVal;
  327. tv : TimeVal;
  328. begin
  329. if TimeOut=-1 then
  330. p:=nil
  331. else
  332. begin
  333. tv.tv_Sec:=Timeout div 1000;
  334. tv.tv_Usec:=(Timeout mod 1000)*1000;
  335. p:=@tv;
  336. end;
  337. fpSelect:=fpSelect(N,Readfds,WriteFds,ExceptFds,p);
  338. end;
  339. Function fpSelect(var T:Text;TimeOut :PTimeval):cint;
  340. Var
  341. F:TfdSet;
  342. begin
  343. if textrec(t).mode=fmclosed then
  344. begin
  345. fpSetErrNo(ESysEBADF);
  346. exit(-1);
  347. end;
  348. FpFD_ZERO(f);
  349. fpFD_SET(textrec(T).handle,f);
  350. if textrec(T).mode=fminput then
  351. fpselect:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  352. else
  353. fpSelect:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  354. end;
  355. Function fpSelect(var T:Text;TimeOut :time_t):cint;
  356. var
  357. p : PTimeVal;
  358. tv : TimeVal;
  359. begin
  360. if TimeOut=-1 then
  361. p:=nil
  362. else
  363. begin
  364. tv.tv_Sec:=Timeout div 1000;
  365. tv.tv_Usec:=(Timeout mod 1000)*1000;
  366. p:=@tv;
  367. end;
  368. fpSelect:=fpSelect(T,p);
  369. end;
  370. function FpWaitPid (pid : TPid; Var Status : cInt; Options : cint) : TPid;
  371. begin
  372. fpWaitPID:=fpWaitPID(Pid,@Status,Options);
  373. end;
  374. Function fpReadLink(const Name: RawByteString): RawByteString;
  375. {
  376. Read a link (where it points to)
  377. }
  378. var
  379. SystemFileName : RawByteString;
  380. i : cint;
  381. begin
  382. SetLength(fpReadLink,PATH_MAX);
  383. SystemFileName:=ToSingleByteFileSystemEncodedFileName(Name);
  384. i:=fpReadLink(PAnsiChar(SystemFileName),PAnsiChar(fpReadLink),PATH_MAX);
  385. if i>0 then
  386. begin
  387. SetLength(fpReadLink,i);
  388. SetCodePage(fpReadLink,DefaultFileSystemCodePage,false);
  389. end
  390. else
  391. fpReadLink:='';
  392. end;