bunxovl.inc 8.5 KB

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