bunxovl.inc 12 KB

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