unix.pp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. BSD parts (c) 2000 by Marco van de Voort
  6. members of the Free Pascal development team.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY;without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. Unit Unix;
  14. Interface
  15. Uses UnixUtil,BaseUnix;
  16. {// i ostypes.inc}
  17. { Get Types and Constants }
  18. {$i sysconst.inc}
  19. {$i systypes.inc}
  20. { Get System call numbers and error-numbers}
  21. {$i sysnr.inc}
  22. {$i errno.inc}
  23. {$I signal.inc}
  24. {$i ostypes.inc}
  25. var
  26. LinuxError : Longint;
  27. {********************
  28. Process
  29. ********************}
  30. const
  31. { For getting/setting priority }
  32. Prio_Process = 0;
  33. Prio_PGrp = 1;
  34. Prio_User = 2;
  35. {$ifdef Solaris}
  36. WNOHANG = $100;
  37. WUNTRACED = $4;
  38. {$ELSE}
  39. WNOHANG = $1;
  40. WUNTRACED = $2;
  41. __WCLONE = $80000000;
  42. {$ENDIF}
  43. {********************
  44. File
  45. ********************}
  46. Const
  47. P_IN = 1;
  48. P_OUT = 2;
  49. Const
  50. LOCK_SH = 1;
  51. LOCK_EX = 2;
  52. LOCK_UN = 8;
  53. LOCK_NB = 4;
  54. Type
  55. Tpipe = array[1..2] of longint;
  56. pglob = ^tglob;
  57. tglob = record
  58. name : pchar;
  59. next : pglob;
  60. end;
  61. const
  62. { For File control mechanism }
  63. F_GetFd = 1;
  64. F_SetFd = 2;
  65. F_GetFl = 3;
  66. F_SetFl = 4;
  67. {$ifdef Solaris}
  68. F_DupFd = 0;
  69. F_Dup2Fd = 9;
  70. F_GetOwn = 23;
  71. F_SetOwn = 24;
  72. F_GetLk = 14;
  73. F_SetLk = 6;
  74. F_SetLkW = 7;
  75. F_FreeSp = 11;
  76. {$else}
  77. F_GetLk = 5;
  78. F_SetLk = 6;
  79. F_SetLkW = 7;
  80. F_SetOwn = 8;
  81. F_GetOwn = 9;
  82. {$endif}
  83. {********************
  84. IOCtl(TermIOS)
  85. ********************}
  86. {Is too freebsd/Linux specific}
  87. {$I termios.inc}
  88. {******************************************************************************
  89. Procedure/Functions
  90. ******************************************************************************}
  91. {**************************
  92. Time/Date Handling
  93. ***************************}
  94. var
  95. tzdaylight : boolean;
  96. tzname : array[boolean] of pchar;
  97. { timezone support }
  98. procedure GetLocalTimezone(timer:longint;var leap_correct,leap_hit:longint);
  99. procedure GetLocalTimezone(timer:longint);
  100. procedure ReadTimezoneFile(fn:string);
  101. function GetTimezoneFile:string;
  102. Function GetEpochTime: longint;
  103. procedure GetTime(var hour,min,sec,msec,usec:word);
  104. procedure GetTime(var hour,min,sec,sec100:word);
  105. procedure GetTime(var hour,min,sec:word);
  106. Procedure GetDate(Var Year,Month,Day:Word);
  107. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  108. function SetTime(Hour,Min,Sec:word) : Boolean;
  109. function SetDate(Year,Month,Day:Word) : Boolean;
  110. function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  111. {**************************
  112. Process Handling
  113. ***************************}
  114. function CreateShellArgV(const prog:string):ppchar;
  115. function CreateShellArgV(const prog:Ansistring):ppchar;
  116. //Procedure Execve(Path: pathstr;args:ppchar;ep:ppchar);
  117. //Procedure Execve(Path: AnsiString;args:ppchar;ep:ppchar);
  118. //Procedure Execve(path: pchar;args:ppchar;ep:ppchar);
  119. Function Execv(const path:pathstr;args:ppchar):cint;
  120. Function Execv(const path: AnsiString;args:ppchar):cint;
  121. Function Execvp(Path: Pathstr;Args:ppchar;Ep:ppchar):cint;
  122. Function Execvp(Path: AnsiString; Args:ppchar;Ep:ppchar):cint;
  123. Function Execl(const Todo: String):cint;
  124. Function Execl(const Todo: Ansistring):cint;
  125. Function Execle(Todo: String;Ep:ppchar):cint;
  126. Function Execle(Todo: AnsiString;Ep:ppchar):cint;
  127. Function Execlp(Todo: string;Ep:ppchar):cint;
  128. Function Execlp(Todo: Ansistring;Ep:ppchar):cint;
  129. Function Shell(const Command:String):Longint;
  130. Function Shell(const Command:AnsiString):Longint;
  131. {Clone for FreeBSD is copied from the LinuxThread port, and rfork based}
  132. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  133. Function WaitProcess(Pid:longint):Longint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  134. Function WIFSTOPPED(Status: Integer): Boolean;
  135. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  136. Function W_STOPCODE(Signal: Integer): Integer;
  137. {**************************
  138. File Handling
  139. ***************************}
  140. Function fdFlush (fd : Longint) : Boolean;
  141. Function Flock (fd,mode : longint) : boolean;
  142. Function Flock (var T : text;mode : longint) : boolean;
  143. Function Flock (var F : File;mode : longint) : boolean;
  144. Function StatFS(Path:Pathstr;Var Info:tstatfs):Boolean;
  145. Function StatFS(Fd: Longint;Var Info:tstatfs):Boolean;
  146. Function SelectText(var T:Text;TimeOut :PTimeVal):Longint;
  147. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  148. {**************************
  149. Directory Handling
  150. ***************************}
  151. procedure SeekDir(p:pdir;off:longint);
  152. function TellDir(p:pdir):longint;
  153. {**************************
  154. Pipe/Fifo/Stream
  155. ***************************}
  156. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  157. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  158. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  159. Function PClose(Var F:text) : longint;
  160. Function PClose(Var F:file) : longint;
  161. Procedure POpen(var F:text;const Prog:String;rw:char);
  162. Procedure POpen(var F:file;const Prog:String;rw:char);
  163. function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  164. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  165. {$ifndef BSD}
  166. Function GetDomainName:String;
  167. Function GetHostName:String;
  168. {$endif}
  169. {**************************
  170. IOCtl/Termios Functions
  171. ***************************}
  172. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  173. Function TCSetAttr(fd:longint;OptAct:longint;const tios:TermIOS):boolean;
  174. Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal);
  175. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal);
  176. Procedure CFMakeRaw(var tios:TermIOS);
  177. Function TCSendBreak(fd,duration:longint):boolean;
  178. Function TCSetPGrp(fd,id:longint):boolean;
  179. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  180. Function TCFlush(fd,qsel:longint):boolean;
  181. Function TCDrain(fd:longint):boolean;
  182. Function TCFlow(fd,act:longint):boolean;
  183. Function IsATTY(Handle:Longint):Boolean;
  184. Function IsATTY(var f:text):Boolean;
  185. function TTYname(Handle:Longint):string;
  186. function TTYname(var F:Text):string;
  187. {**************************
  188. Memory functions
  189. ***************************}
  190. const
  191. PROT_READ = $1; { page can be read }
  192. PROT_WRITE = $2; { page can be written }
  193. PROT_EXEC = $4; { page can be executed }
  194. PROT_NONE = $0; { page can not be accessed }
  195. MAP_SHARED = $1; { Share changes }
  196. // MAP_PRIVATE = $2; { Changes are private }
  197. MAP_TYPE = $f; { Mask for type of mapping }
  198. MAP_FIXED = $10; { Interpret addr exactly }
  199. // MAP_ANONYMOUS = $20; { don't use a file }
  200. MAP_GROWSDOWN = $100; { stack-like segment }
  201. MAP_DENYWRITE = $800; { ETXTBSY }
  202. MAP_EXECUTABLE = $1000; { mark it as an executable }
  203. MAP_LOCKED = $2000; { pages are locked }
  204. MAP_NORESERVE = $4000; { don't check for reservations }
  205. {**************************
  206. Utility functions
  207. ***************************}
  208. Function Octal(l:longint):longint;
  209. Function FExpand(Const Path: PathStr):PathStr;
  210. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  211. Function Glob(Const path:pathstr):pglob;
  212. Procedure Globfree(var p:pglob);
  213. {Filedescriptorsets}
  214. {Stat.Mode Types}
  215. procedure SigRaise(sig:integer);
  216. {******************************************************************************
  217. Implementation
  218. ******************************************************************************}
  219. {$i unxsysch.inc}
  220. Implementation
  221. Uses Strings;
  222. {$i syscallh.inc}
  223. {$i ossysch.inc}
  224. {$i unxsysc.inc}
  225. { Get the definitions of textrec and filerec }
  226. {$i textrec.inc}
  227. {$i filerec.inc}
  228. { Raw System calls are in Syscalls.inc}
  229. {$ifdef Linux} // Linux has more "oldlinux" compability.
  230. {$i sysc11.inc}
  231. {$else}
  232. {$i syscalls.inc}
  233. {$endif}
  234. {$i unixsysc.inc} {Syscalls only used in unit Unix/Linux}
  235. Function getenv(name:string):Pchar; external name 'FPC_SYSC_FPGETENV';
  236. {******************************************************************************
  237. Process related calls
  238. ******************************************************************************}
  239. { Most calls of WaitPID do not handle the result correctly, this funktion treats errors more correctly }
  240. Function WaitProcess(Pid:longint):Longint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  241. var r,s : LongInt;
  242. begin
  243. repeat
  244. s:=$7F00;
  245. r:=fpWaitPid(Pid,@s,0);
  246. until (r<>-1) or (LinuxError<>ESysEINTR);
  247. if (r=-1) or (r=0) then // 0 is not a valid return and should never occur (it means status invalid when using WNOHANG)
  248. WaitProcess:=-1 // return -1 to indicate an error
  249. else
  250. begin
  251. {$ifndef Solaris}
  252. { at least correct for Linux and Darwin (JM) }
  253. if (s and $7F)=0 then // Only this is a valid returncode
  254. {$else}
  255. if (s and $FF)=0 then // Only this is a valid returncode
  256. {$endif}
  257. WaitProcess:=s shr 8
  258. else if (s>0) then // Until now there is not use of the highest bit , but check this for the future
  259. WaitProcess:=-s // normal case
  260. else
  261. WaitProcess:=s; // s<0 should not occur, but wie return also a negativ value
  262. end;
  263. end;
  264. function InternalCreateShellArgV(cmd:pChar; len:longint):ppchar;
  265. {
  266. Create an argv which executes a command in a shell using /bin/sh -c
  267. }
  268. const Shell = '/bin/sh'#0'-c'#0;
  269. var
  270. pp,p : ppchar;
  271. // temp : string; !! Never pass a local var back!!
  272. begin
  273. getmem(pp,4*4);
  274. p:=pp;
  275. p^:=@Shell[1];
  276. inc(p);
  277. p^:=@Shell[9];
  278. inc(p);
  279. getmem(p^,len+1);
  280. move(cmd^,p^^,len);
  281. pchar(p^)[len]:=#0;
  282. inc(p);
  283. p^:=Nil;
  284. InternalCreateShellArgV:=pp;
  285. end;
  286. function CreateShellArgV(const prog:string):ppchar;
  287. begin
  288. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog));
  289. end;
  290. function CreateShellArgV(const prog:Ansistring):ppchar;
  291. {
  292. Create an argv which executes a command in a shell using /bin/sh -c
  293. using a AnsiString;
  294. }
  295. begin
  296. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog)); // if ppc works like delphi this also work when @prog[1] is invalid (len=0)
  297. end;
  298. procedure FreeShellArgV(p:ppchar);
  299. begin
  300. if (p<>nil) then begin
  301. freemem(p[2]);
  302. freemem(p);
  303. end;
  304. end;
  305. Function Execv(const path: AnsiString;args:ppchar):cint;
  306. {
  307. Overloaded ansistring version.
  308. }
  309. begin
  310. Execv:=fpExecVe(Path,Args,envp);
  311. end;
  312. Function Execvp(Path: AnsiString; Args:ppchar;Ep:ppchar):cint;
  313. {
  314. Overloaded ansistring version
  315. }
  316. var
  317. thepath : Ansistring;
  318. begin
  319. if path[1]<>'/' then
  320. begin
  321. Thepath:=strpas(fpgetenv('PATH'));
  322. if thepath='' then
  323. thepath:='.';
  324. Path:=FSearch(path,thepath)
  325. end
  326. else
  327. Path:='';
  328. if Path='' then
  329. Begin
  330. fpsetErrno(ESysEnoEnt);
  331. Exit(-1);
  332. end
  333. else
  334. Execvp:=fpExecve(Path,args,ep);
  335. end;
  336. Function Execv(const path:pathstr;args:ppchar):cint;
  337. {
  338. Replaces the current program by the program specified in path,
  339. arguments in args are passed to Execve.
  340. the current environment is passed on.
  341. }
  342. begin
  343. Execv:=fpExecve(path,args,envp);
  344. end;
  345. Function Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar):cint;
  346. {
  347. This does the same as Execve, only it searches the PATH environment
  348. for the place of the Executable, except when Path starts with a slash.
  349. if the PATH environment variable is unavailable, the path is set to '.'
  350. }
  351. var
  352. thepath : string;
  353. begin
  354. if path[1]<>'/' then
  355. begin
  356. Thepath:=strpas(fpgetenv('PATH'));
  357. if thepath='' then
  358. thepath:='.';
  359. Path:=FSearch(path,thepath)
  360. end
  361. else
  362. Path:='';
  363. if Path='' then
  364. Begin
  365. fpsetErrno(ESysEnoEnt);
  366. Exit(-1);
  367. end
  368. else
  369. execvp:=fpExecve(Path,args,ep);
  370. end;
  371. Function Execle(Todo:string;Ep:ppchar):cint;
  372. {
  373. This procedure takes the string 'Todo', parses it for command and
  374. command options, and Executes the command with the given options.
  375. The string 'Todo' shoud be of the form 'command options', options
  376. separated by commas.
  377. the PATH environment is not searched for 'command'.
  378. The specified environment(in 'ep') is passed on to command
  379. }
  380. var
  381. p : ppchar;
  382. begin
  383. p:=StringToPPChar(ToDo);
  384. if (p=nil) or (p^=nil) then
  385. Begin
  386. fpsetErrno(ESysEnoEnt);
  387. Exit(-1);
  388. end
  389. else
  390. execle:=fpExecVE(p^,p,EP);
  391. end;
  392. function Execle(Todo:AnsiString;Ep:ppchar):cint;
  393. {
  394. This procedure takes the string 'Todo', parses it for command and
  395. command options, and Executes the command with the given options.
  396. The string 'Todo' shoud be of the form 'command options', options
  397. separated by commas.
  398. the PATH environment is not searched for 'command'.
  399. The specified environment(in 'ep') is passed on to command
  400. }
  401. var
  402. p : ppchar;
  403. begin
  404. p:=StringToPPChar(ToDo);
  405. if (p=nil) or (p^=nil) then
  406. Begin
  407. fpsetErrno(ESysEnoEnt);
  408. Exit(-1);
  409. end;
  410. ExecLe:=fpExecVE(p^,p,EP);
  411. end;
  412. Function Execl(const Todo:string):cint;
  413. {
  414. This procedure takes the string 'Todo', parses it for command and
  415. command options, and Executes the command with the given options.
  416. The string 'Todo' shoud be of the form 'command options', options
  417. separated by commas.
  418. the PATH environment is not searched for 'command'.
  419. The current environment is passed on to command
  420. }
  421. begin
  422. Execl:=ExecLE(ToDo,EnvP);
  423. end;
  424. Function Execlp(Todo:string;Ep:ppchar):cint;
  425. {
  426. This procedure takes the string 'Todo', parses it for command and
  427. command options, and Executes the command with the given options.
  428. The string 'Todo' shoud be of the form 'command options', options
  429. separated by commas.
  430. the PATH environment is searched for 'command'.
  431. The specified environment (in 'ep') is passed on to command
  432. }
  433. var
  434. p : ppchar;
  435. begin
  436. p:=StringToPPchar(todo);
  437. if (p=nil) or (p^=nil) then
  438. Begin
  439. fpsetErrno(ESysEnoEnt);
  440. Exit(-1);
  441. end;
  442. Execlp:=ExecVP(StrPas(p^),p,EP);
  443. end;
  444. Function Execlp(Todo: Ansistring;Ep:ppchar):cint;
  445. {
  446. Overloaded ansistring version.
  447. }
  448. var
  449. p : ppchar;
  450. begin
  451. p:=StringToPPchar(todo);
  452. if (p=nil) or (p^=nil) then
  453. Begin
  454. fpsetErrno(ESysEnoEnt);
  455. Exit(-1);
  456. end;
  457. execlp:=ExecVP(StrPas(p^),p,EP);
  458. end;
  459. Function Shell(const Command:String):cint;
  460. {
  461. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  462. The current environment is passed to the shell.
  463. It waits for the shell to exit, and returns its exit status.
  464. If the Exec call failed exit status 127 is reported.
  465. }
  466. { Changed the structure:
  467. - the previous version returns an undefinied value if fork fails
  468. - it returns the status of Waitpid instead of the Process returnvalue (see the doc to Shell)
  469. - it uses exit(127) not ExitProc (The Result in pp386: going on Compiling in 2 processes!)
  470. - ShellArgs are now released
  471. - The Old CreateShellArg gives back pointers to a local var
  472. }
  473. var
  474. p : ppchar;
  475. pid : longint;
  476. begin
  477. p:=CreateShellArgv(command);
  478. pid:=fpfork;
  479. if pid=0 then // We are in the Child
  480. begin
  481. {This is the child.}
  482. fpExecve(p^,p,envp);
  483. fpExit(127); // was Exit(127)
  484. end
  485. else if (pid<>-1) then // Successfull started
  486. Shell:=WaitProcess(pid)
  487. else // no success
  488. Shell:=-1; // indicate an error
  489. FreeShellArgV(p);
  490. end;
  491. Function Shell(const Command:AnsiString):cint;
  492. {
  493. AnsiString version of Shell
  494. }
  495. var
  496. p : ppchar;
  497. pid : longint;
  498. begin { Changes as above }
  499. p:=CreateShellArgv(command);
  500. pid:=fpfork;
  501. if pid=0 then // We are in the Child
  502. begin
  503. fpExecve(p^,p,envp);
  504. fpExit(127); // was exit(127)!! We must exit the Process, not the function
  505. end
  506. else if (pid<>-1) then // Successfull started
  507. Shell:=WaitProcess(pid)
  508. else // no success
  509. Shell:=-1;
  510. FreeShellArgV(p);
  511. end;
  512. Function WIFSTOPPED(Status: Integer): Boolean;
  513. begin
  514. WIFSTOPPED:=((Status and $FF)=$7F);
  515. end;
  516. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  517. begin
  518. W_EXITCODE:=(ReturnCode shl 8) or Signal;
  519. end;
  520. Function W_STOPCODE(Signal: Integer): Integer;
  521. begin
  522. W_STOPCODE:=(Signal shl 8) or $7F;
  523. end;
  524. {******************************************************************************
  525. Date and Time related calls
  526. ******************************************************************************}
  527. Function GetEpochTime: longint;
  528. {
  529. Get the number of seconds since 00:00, January 1 1970, GMT
  530. the time NOT corrected any way
  531. }
  532. begin
  533. GetEpochTime:=fptime;
  534. end;
  535. procedure GetTime(var hour,min,sec,msec,usec:word);
  536. {
  537. Gets the current time, adjusted to local time
  538. }
  539. var
  540. year,day,month:Word;
  541. tz:timeval;
  542. begin
  543. fpgettimeofday(@tz,nil);
  544. EpochToLocal(tz.tv_sec,year,month,day,hour,min,sec);
  545. msec:=tz.tv_usec div 1000;
  546. usec:=tz.tv_usec mod 1000;
  547. end;
  548. procedure GetTime(var hour,min,sec,sec100:word);
  549. {
  550. Gets the current time, adjusted to local time
  551. }
  552. var
  553. usec : word;
  554. begin
  555. gettime(hour,min,sec,sec100,usec);
  556. sec100:=sec100 div 10;
  557. end;
  558. Procedure GetTime(Var Hour,Min,Sec:Word);
  559. {
  560. Gets the current time, adjusted to local time
  561. }
  562. var
  563. msec,usec : Word;
  564. Begin
  565. gettime(hour,min,sec,msec,usec);
  566. End;
  567. Procedure GetDate(Var Year,Month,Day:Word);
  568. {
  569. Gets the current date, adjusted to local time
  570. }
  571. var
  572. hour,minute,second : word;
  573. Begin
  574. EpochToLocal(fptime,year,month,day,hour,minute,second);
  575. End;
  576. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  577. {
  578. Gets the current date, adjusted to local time
  579. }
  580. Begin
  581. EpochToLocal(fptime,year,month,day,hour,minute,second);
  582. End;
  583. {$ifndef BSD}
  584. {$ifdef linux}
  585. Function stime (t : longint) : Boolean;
  586. begin
  587. stime:=do_SysCall(Syscall_nr_stime,longint(@t))=0;
  588. end;
  589. {$endif}
  590. {$endif}
  591. {$ifdef BSD}
  592. Function stime (t : longint) : Boolean;
  593. begin
  594. end;
  595. {$endif}
  596. Function SetTime(Hour,Min,Sec:word) : boolean;
  597. var
  598. Year, Month, Day : Word;
  599. begin
  600. GetDate (Year, Month, Day);
  601. SetTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Min, Sec ) );
  602. end;
  603. Function SetDate(Year,Month,Day:Word) : boolean;
  604. var
  605. Hour, Minute, Second, Sec100 : Word;
  606. begin
  607. GetTime ( Hour, Minute, Second, Sec100 );
  608. SetDate:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  609. end;
  610. Function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  611. begin
  612. SetDateTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  613. end;
  614. { Include timezone handling routines which use /usr/share/timezone info }
  615. {$i timezone.inc}
  616. {******************************************************************************
  617. FileSystem calls
  618. ******************************************************************************}
  619. Function Execl(const Todo:Ansistring):cint;
  620. {
  621. Overloaded AnsiString Version of ExecL.
  622. }
  623. begin
  624. Execl:=ExecLE(ToDo,EnvP);
  625. end;
  626. Function Flock (var T : text;mode : longint) : boolean;
  627. begin
  628. Flock:=Flock(TextRec(T).Handle,mode);
  629. end;
  630. Function Flock (var F : File;mode : longint) : boolean;
  631. begin
  632. Flock:=Flock(FileRec(F).Handle,mode);
  633. end;
  634. Function SelectText(var T:Text;TimeOut :PTimeval):Longint;
  635. Var
  636. F:TfdSet;
  637. begin
  638. if textrec(t).mode=fmclosed then
  639. begin
  640. fpseterrno(ESysEBADF);
  641. exit(-1);
  642. end;
  643. FpFD_ZERO(f);
  644. fpFD_SET(textrec(T).handle,f);
  645. if textrec(T).mode=fminput then
  646. SelectText:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  647. else
  648. SelectText:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  649. end;
  650. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  651. var
  652. p : PTimeVal;
  653. tv : TimeVal;
  654. begin
  655. if TimeOut=-1 then
  656. p:=nil
  657. else
  658. begin
  659. tv.tv_Sec:=Timeout div 1000;
  660. tv.tv_Usec:=(Timeout mod 1000)*1000;
  661. p:=@tv;
  662. end;
  663. SelectText:=SelectText(T,p);
  664. end;
  665. {******************************************************************************
  666. Directory
  667. ******************************************************************************}
  668. procedure SeekDir(p:pdir;off:longint);
  669. begin
  670. if p=nil then
  671. begin
  672. fpseterrno(ESysEBADF);
  673. exit;
  674. end;
  675. {$ifndef bsd}
  676. p^.dd_nextoff:=fplseek(p^.dd_fd,off,seek_set);
  677. {$endif}
  678. p^.dd_size:=0;
  679. p^.dd_loc:=0;
  680. end;
  681. function TellDir(p:pdir):longint;
  682. begin
  683. if p=nil then
  684. begin
  685. fpseterrno(ESysEBADF);
  686. telldir:=-1;
  687. exit;
  688. end;
  689. telldir:=fplseek(p^.dd_fd,0,seek_cur)
  690. { We could try to use the nextoff field here, but on my 1.2.13
  691. kernel, this gives nothing... This may have to do with
  692. the readdir implementation of libc... I also didn't find any trace of
  693. the field in the kernel code itself, So I suspect it is an artifact of libc.
  694. Michael. }
  695. end;
  696. {******************************************************************************
  697. Pipes/Fifo
  698. ******************************************************************************}
  699. Procedure OpenPipe(var F:Text);
  700. begin
  701. case textrec(f).mode of
  702. fmoutput :
  703. if textrec(f).userdata[1]<>P_OUT then
  704. textrec(f).mode:=fmclosed;
  705. fminput :
  706. if textrec(f).userdata[1]<>P_IN then
  707. textrec(f).mode:=fmclosed;
  708. else
  709. textrec(f).mode:=fmclosed;
  710. end;
  711. end;
  712. Function IOPipe(var F:text):cint;
  713. begin
  714. IOPipe:=0;
  715. case textrec(f).mode of
  716. fmoutput :
  717. begin
  718. { first check if we need something to write, else we may
  719. get a SigPipe when Close() is called (PFV) }
  720. if textrec(f).bufpos>0 then
  721. IOPipe:=fpwrite(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  722. end;
  723. fminput : Begin
  724. textrec(f).bufend:=fpread(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  725. IOPipe:=textrec(f).bufend;
  726. End;
  727. end;
  728. textrec(f).bufpos:=0;
  729. end;
  730. Function FlushPipe(var F:Text):cint;
  731. begin
  732. FlushPipe:=0;
  733. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  734. FlushPipe:=IOPipe(f);
  735. textrec(f).bufpos:=0;
  736. end;
  737. Function ClosePipe(var F:text):cint;
  738. begin
  739. textrec(f).mode:=fmclosed;
  740. ClosePipe:=fpclose(textrec(f).handle);
  741. end;
  742. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  743. {
  744. Sets up a pair of file variables, which act as a pipe. The first one can
  745. be read from, the second one can be written to.
  746. If the operation was unsuccesful, linuxerror is set.
  747. }
  748. var
  749. f_in,f_out : longint;
  750. begin
  751. if not AssignPipe(f_in,f_out) then
  752. begin
  753. AssignPipe:=false;
  754. exit;
  755. end;
  756. { Set up input }
  757. Assign(Pipe_in,'');
  758. Textrec(Pipe_in).Handle:=f_in;
  759. Textrec(Pipe_in).Mode:=fmInput;
  760. Textrec(Pipe_in).userdata[1]:=P_IN;
  761. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  762. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  763. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  764. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  765. { Set up output }
  766. Assign(Pipe_out,'');
  767. Textrec(Pipe_out).Handle:=f_out;
  768. Textrec(Pipe_out).Mode:=fmOutput;
  769. Textrec(Pipe_out).userdata[1]:=P_OUT;
  770. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  771. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  772. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  773. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  774. AssignPipe:=true;
  775. end;
  776. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  777. {
  778. Sets up a pair of file variables, which act as a pipe. The first one can
  779. be read from, the second one can be written to.
  780. If the operation was unsuccesful,
  781. }
  782. var
  783. f_in,f_out : longint;
  784. begin
  785. if not AssignPipe(f_in,f_out) then
  786. begin
  787. AssignPipe:=false;
  788. exit;
  789. end;
  790. { Set up input }
  791. Assign(Pipe_in,'');
  792. Filerec(Pipe_in).Handle:=f_in;
  793. Filerec(Pipe_in).Mode:=fmInput;
  794. Filerec(Pipe_in).recsize:=1;
  795. Filerec(Pipe_in).userdata[1]:=P_IN;
  796. { Set up output }
  797. Assign(Pipe_out,'');
  798. Filerec(Pipe_out).Handle:=f_out;
  799. Filerec(Pipe_out).Mode:=fmoutput;
  800. Filerec(Pipe_out).recsize:=1;
  801. Filerec(Pipe_out).userdata[1]:=P_OUT;
  802. AssignPipe:=true;
  803. end;
  804. Procedure PCloseText(Var F:text);
  805. {
  806. May not use @PClose due overloading
  807. }
  808. begin
  809. PClose(f);
  810. end;
  811. Procedure POpen(var F:text;const Prog:String;rw:char);
  812. {
  813. Starts the program in 'Prog' and makes it's input or out put the
  814. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  815. F, will be read from stdin by the program in 'Prog'. The inverse is true
  816. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  817. read from 'f'.
  818. }
  819. var
  820. pipi,
  821. pipo : text;
  822. pid : longint;
  823. pl : ^longint;
  824. pp : ppchar;
  825. begin
  826. LinuxError:=0;
  827. rw:=upcase(rw);
  828. if not (rw in ['R','W']) then
  829. begin
  830. LinuxError:=ESysEnoent;
  831. exit;
  832. end;
  833. AssignPipe(pipi,pipo);
  834. if Linuxerror<>0 then
  835. exit;
  836. pid:=fpfork;
  837. if linuxerror<>0 then
  838. begin
  839. close(pipi);
  840. close(pipo);
  841. exit;
  842. end;
  843. if pid=0 then
  844. begin
  845. { We're in the child }
  846. if rw='W' then
  847. begin
  848. close(pipo);
  849. fpdup2(pipi,input);
  850. close(pipi);
  851. if linuxerror<>0 then
  852. halt(127);
  853. end
  854. else
  855. begin
  856. close(pipi);
  857. fpdup2(pipo,output);
  858. close(pipo);
  859. if linuxerror<>0 then
  860. halt(127);
  861. end;
  862. pp:=createshellargv(prog);
  863. fpExecve(pp^,pp,envp);
  864. halt(127);
  865. end
  866. else
  867. begin
  868. { We're in the parent }
  869. if rw='W' then
  870. begin
  871. close(pipi);
  872. f:=pipo;
  873. textrec(f).bufptr:=@textrec(f).buffer;
  874. end
  875. else
  876. begin
  877. close(pipo);
  878. f:=pipi;
  879. textrec(f).bufptr:=@textrec(f).buffer;
  880. end;
  881. {Save the process ID - needed when closing }
  882. pl:=@(textrec(f).userdata[2]);
  883. pl^:=pid;
  884. textrec(f).closefunc:=@PCloseText;
  885. end;
  886. end;
  887. Procedure POpen(var F:file;const Prog:String;rw:char);
  888. {
  889. Starts the program in 'Prog' and makes it's input or out put the
  890. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  891. F, will be read from stdin by the program in 'Prog'. The inverse is true
  892. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  893. read from 'f'.
  894. }
  895. var
  896. pipi,
  897. pipo : file;
  898. pid : longint;
  899. pl : ^longint;
  900. p,pp : ppchar;
  901. temp : string[255];
  902. begin
  903. LinuxError:=0;
  904. rw:=upcase(rw);
  905. if not (rw in ['R','W']) then
  906. begin
  907. LinuxError:=ESysEnoent;
  908. exit;
  909. end;
  910. AssignPipe(pipi,pipo);
  911. if Linuxerror<>0 then
  912. exit;
  913. pid:=fpfork;
  914. if linuxerror<>0 then
  915. begin
  916. close(pipi);
  917. close(pipo);
  918. exit;
  919. end;
  920. if pid=0 then
  921. begin
  922. { We're in the child }
  923. if rw='W' then
  924. begin
  925. close(pipo);
  926. fpdup2(filerec(pipi).handle,stdinputhandle);
  927. close(pipi);
  928. if linuxerror<>0 then
  929. halt(127);
  930. end
  931. else
  932. begin
  933. close(pipi);
  934. fpdup2(filerec(pipo).handle,stdoutputhandle);
  935. close(pipo);
  936. if linuxerror<>0 then
  937. halt(127);
  938. end;
  939. getmem(pp,sizeof(pchar)*4);
  940. temp:='/bin/sh'#0'-c'#0+prog+#0;
  941. p:=pp;
  942. p^:=@temp[1];
  943. inc(p);
  944. p^:=@temp[9];
  945. inc(p);
  946. p^:=@temp[12];
  947. inc(p);
  948. p^:=Nil;
  949. fpExecve(ansistring('/bin/sh'),pp,envp);
  950. halt(127);
  951. end
  952. else
  953. begin
  954. { We're in the parent }
  955. if rw='W' then
  956. begin
  957. close(pipi);
  958. f:=pipo;
  959. end
  960. else
  961. begin
  962. close(pipo);
  963. f:=pipi;
  964. end;
  965. {Save the process ID - needed when closing }
  966. pl:=@(filerec(f).userdata[2]);
  967. pl^:=pid;
  968. end;
  969. end;
  970. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  971. {
  972. Starts the program in 'Prog' and makes its input and output the
  973. other end of two pipes, which are the stdin and stdout of a program
  974. specified in 'Prog'.
  975. streamout can be used to write to the program, streamin can be used to read
  976. the output of the program. See the following diagram :
  977. Parent Child
  978. STreamout --> Input
  979. Streamin <-- Output
  980. Return value is the process ID of the process being spawned, or -1 in case of failure.
  981. }
  982. var
  983. pipi,
  984. pipo : text;
  985. pid : longint;
  986. pl : ^Longint;
  987. begin
  988. LinuxError:=0;
  989. AssignStream:=-1;
  990. AssignPipe(streamin,pipo);
  991. if Linuxerror<>0 then
  992. exit;
  993. AssignPipe(pipi,streamout);
  994. if Linuxerror<>0 then
  995. exit;
  996. pid:=fpfork;
  997. if linuxerror<>0 then
  998. begin
  999. close(pipi);
  1000. close(pipo);
  1001. close (streamin);
  1002. close (streamout);
  1003. exit;
  1004. end;
  1005. if pid=0 then
  1006. begin
  1007. { We're in the child }
  1008. { Close what we don't need }
  1009. close(streamout);
  1010. close(streamin);
  1011. fpdup2(pipi,input);
  1012. if linuxerror<>0 then
  1013. halt(127);
  1014. close(pipi);
  1015. fpdup2(pipo,output);
  1016. if linuxerror<>0 then
  1017. halt (127);
  1018. close(pipo);
  1019. Execl(Prog);
  1020. halt(127);
  1021. end
  1022. else
  1023. begin
  1024. { we're in the parent}
  1025. close(pipo);
  1026. close(pipi);
  1027. {Save the process ID - needed when closing }
  1028. pl:=@(textrec(StreamIn).userdata[2]);
  1029. pl^:=pid;
  1030. textrec(StreamIn).closefunc:=@PCloseText;
  1031. {Save the process ID - needed when closing }
  1032. pl:=@(textrec(StreamOut).userdata[2]);
  1033. pl^:=pid;
  1034. textrec(StreamOut).closefunc:=@PCloseText;
  1035. AssignStream:=Pid;
  1036. end;
  1037. end;
  1038. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  1039. {
  1040. Starts the program in 'prog' and makes its input, output and error output the
  1041. other end of three pipes, which are the stdin, stdout and stderr of a program
  1042. specified in 'prog'.
  1043. StreamOut can be used to write to the program, StreamIn can be used to read
  1044. the output of the program, StreamErr reads the error output of the program.
  1045. See the following diagram :
  1046. Parent Child
  1047. StreamOut --> StdIn (input)
  1048. StreamIn <-- StdOut (output)
  1049. StreamErr <-- StdErr (error output)
  1050. }
  1051. var
  1052. PipeIn, PipeOut, PipeErr: text;
  1053. pid: LongInt;
  1054. pl: ^LongInt;
  1055. begin
  1056. LinuxError := 0;
  1057. AssignStream := -1;
  1058. // Assign pipes
  1059. AssignPipe(StreamIn, PipeOut);
  1060. if LinuxError <> 0 then exit;
  1061. AssignPipe(StreamErr, PipeErr);
  1062. if LinuxError <> 0 then begin
  1063. Close(StreamIn);
  1064. Close(PipeOut);
  1065. exit;
  1066. end;
  1067. AssignPipe(PipeIn, StreamOut);
  1068. if LinuxError <> 0 then begin
  1069. Close(StreamIn);
  1070. Close(PipeOut);
  1071. Close(StreamErr);
  1072. Close(PipeErr);
  1073. exit;
  1074. end;
  1075. // Fork
  1076. pid := fpFork;
  1077. if LinuxError <> 0 then begin
  1078. Close(StreamIn);
  1079. Close(PipeOut);
  1080. Close(StreamErr);
  1081. Close(PipeErr);
  1082. Close(PipeIn);
  1083. Close(StreamOut);
  1084. exit;
  1085. end;
  1086. if pid = 0 then begin
  1087. // *** We are in the child ***
  1088. // Close what we don not need
  1089. Close(StreamOut);
  1090. Close(StreamIn);
  1091. Close(StreamErr);
  1092. // Connect pipes
  1093. fpdup2(PipeIn, Input);
  1094. if LinuxError <> 0 then Halt(127);
  1095. Close(PipeIn);
  1096. fpdup2(PipeOut, Output);
  1097. if LinuxError <> 0 then Halt(127);
  1098. Close(PipeOut);
  1099. fpdup2(PipeErr, StdErr);
  1100. if LinuxError <> 0 then Halt(127);
  1101. Close(PipeErr);
  1102. // Execute program
  1103. Execl(Prog);
  1104. Halt(127);
  1105. end else begin
  1106. // *** We are in the parent ***
  1107. Close(PipeErr);
  1108. Close(PipeOut);
  1109. Close(PipeIn);
  1110. // Save the process ID - needed when closing
  1111. pl := @(TextRec(StreamIn).userdata[2]);
  1112. pl^ := pid;
  1113. TextRec(StreamIn).closefunc := @PCloseText;
  1114. // Save the process ID - needed when closing
  1115. pl := @(TextRec(StreamOut).userdata[2]);
  1116. pl^ := pid;
  1117. TextRec(StreamOut).closefunc := @PCloseText;
  1118. // Save the process ID - needed when closing
  1119. pl := @(TextRec(StreamErr).userdata[2]);
  1120. pl^ := pid;
  1121. TextRec(StreamErr).closefunc := @PCloseText;
  1122. AssignStream := pid;
  1123. end;
  1124. end;
  1125. {******************************************************************************
  1126. General information calls
  1127. ******************************************************************************}
  1128. {$ifndef BSD}
  1129. Function GetDomainName:String; { linux only!}
  1130. // domainname is a glibc extension.
  1131. {
  1132. Get machines domain name. Returns empty string if not set.
  1133. }
  1134. Var
  1135. Sysn : utsname;
  1136. begin
  1137. fpUname(Sysn);
  1138. linuxerror:=fpgeterrno;;
  1139. If linuxerror<>0 then
  1140. getdomainname:=''
  1141. else
  1142. getdomainname:=strpas(@Sysn.domain[0]);
  1143. end;
  1144. {$endif}
  1145. Function GetHostName:String;
  1146. {
  1147. Get machines name. Returns empty string if not set.
  1148. }
  1149. Var
  1150. Sysn : utsname;
  1151. begin
  1152. fpuname(Sysn);
  1153. linuxerror:=fpgeterrno;;
  1154. If linuxerror<>0 then
  1155. gethostname:=''
  1156. else
  1157. gethostname:=strpas(@Sysn.nodename[0]);
  1158. end;
  1159. {******************************************************************************
  1160. Signal handling calls
  1161. ******************************************************************************}
  1162. procedure SigRaise(sig:integer);
  1163. begin
  1164. fpKill(fpGetPid,Sig);
  1165. end;
  1166. {******************************************************************************
  1167. IOCtl and Termios calls
  1168. ******************************************************************************}
  1169. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  1170. begin
  1171. {$ifndef BSD}
  1172. TCGetAttr:=fpIOCtl(fd,TCGETS,@tios)=0;
  1173. {$else}
  1174. TCGETAttr:=fpIoCtl(Fd,TIOCGETA,@tios)=0;
  1175. {$endif}
  1176. end;
  1177. Function TCSetAttr(fd:longint;OptAct:longint;const tios:TermIOS):boolean;
  1178. var
  1179. nr:longint;
  1180. begin
  1181. {$ifndef BSD}
  1182. case OptAct of
  1183. TCSANOW : nr:=TCSETS;
  1184. TCSADRAIN : nr:=TCSETSW;
  1185. TCSAFLUSH : nr:=TCSETSF;
  1186. {$else}
  1187. case OptAct of
  1188. TCSANOW : nr:=TIOCSETA;
  1189. TCSADRAIN : nr:=TIOCSETAW;
  1190. TCSAFLUSH : nr:=TIOCSETAF;
  1191. {$endif}
  1192. else
  1193. begin
  1194. fpsetErrNo(ESysEINVAL);
  1195. TCSetAttr:=false;
  1196. exit;
  1197. end;
  1198. end;
  1199. TCSetAttr:=fpIOCtl(fd,nr,@Tios)=0;
  1200. end;
  1201. Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal);
  1202. begin
  1203. {$ifndef BSD}
  1204. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  1205. {$else}
  1206. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  1207. {$endif}
  1208. end;
  1209. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal);
  1210. begin
  1211. {$ifndef BSD}
  1212. CFSetISpeed(tios,speed);
  1213. {$else}
  1214. tios.c_ospeed:=speed;
  1215. {$endif}
  1216. end;
  1217. Procedure CFMakeRaw(var tios:TermIOS);
  1218. begin
  1219. {$ifndef BSD}
  1220. with tios do
  1221. begin
  1222. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  1223. INLCR or IGNCR or ICRNL or IXON));
  1224. c_oflag:=c_oflag and (not OPOST);
  1225. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  1226. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  1227. end;
  1228. {$else}
  1229. with tios do
  1230. begin
  1231. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  1232. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  1233. IGNPAR));
  1234. c_iflag:=c_iflag OR IGNBRK;
  1235. c_oflag:=c_oflag and (not OPOST);
  1236. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  1237. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  1238. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  1239. c_cc[VMIN]:=1;
  1240. c_cc[VTIME]:=0;
  1241. end;
  1242. {$endif}
  1243. end;
  1244. Function TCSendBreak(fd,duration:longint):boolean;
  1245. begin
  1246. {$ifndef BSD}
  1247. TCSendBreak:=fpIOCtl(fd,TCSBRK,pointer(duration))=0;
  1248. {$else}
  1249. TCSendBreak:=fpIOCtl(fd,TIOCSBRK,0)=0;
  1250. {$endif}
  1251. end;
  1252. Function TCSetPGrp(fd,id:longint):boolean;
  1253. begin
  1254. TCSetPGrp:=fpIOCtl(fd,TIOCSPGRP,pointer(id))=0;
  1255. end;
  1256. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  1257. begin
  1258. TCGetPGrp:=fpIOCtl(fd,TIOCGPGRP,@id)=0;
  1259. end;
  1260. Function TCDrain(fd:longint):boolean;
  1261. begin
  1262. {$ifndef BSD}
  1263. TCDrain:=fpIOCtl(fd,TCSBRK,pointer(1))=0;
  1264. {$else}
  1265. TCDrain:=fpIOCtl(fd,TIOCDRAIN,0)=0; {Should set timeout to 1 first?}
  1266. {$endif}
  1267. end;
  1268. Function TCFlow(fd,act:longint):boolean;
  1269. begin
  1270. {$ifndef BSD}
  1271. TCFlow:=fpIOCtl(fd,TCXONC,pointer(act))=0;
  1272. {$else}
  1273. case act OF
  1274. TCOOFF : TCFlow:=fpIoctl(fd,TIOCSTOP,0)=0;
  1275. TCOOn : TCFlow:=fpIOctl(Fd,TIOCStart,0)=0;
  1276. TCIOFF : {N/I}
  1277. end;
  1278. {$endif}
  1279. end;
  1280. Function TCFlush(fd,qsel:longint):boolean;
  1281. begin
  1282. {$ifndef BSD}
  1283. TCFlush:=fpIOCtl(fd,TCFLSH,pointer(qsel))=0;
  1284. {$else}
  1285. TCFlush:=fpIOCtl(fd,TIOCFLUSH,pointer(qsel))=0;
  1286. {$endif}
  1287. end;
  1288. Function IsATTY (Handle:Longint):Boolean;
  1289. {
  1290. Check if the filehandle described by 'handle' is a TTY (Terminal)
  1291. }
  1292. var
  1293. t : Termios;
  1294. begin
  1295. IsAtty:=TCGetAttr(Handle,t);
  1296. end;
  1297. Function IsATTY(var f: text):Boolean;
  1298. {
  1299. Idem as previous, only now for text variables.
  1300. }
  1301. begin
  1302. IsATTY:=IsaTTY(textrec(f).handle);
  1303. end;
  1304. function TTYName(Handle:Longint):string;
  1305. {
  1306. Return the name of the current tty described by handle f.
  1307. returns empty string in case of an error.
  1308. }
  1309. var
  1310. mydev : dev_t;
  1311. myino : ino_t;
  1312. st : stat;
  1313. function mysearch(n:string): boolean;
  1314. {searches recursively for the device in the directory given by n,
  1315. returns true if found and sets the name of the device in ttyname}
  1316. var dirstream : pdir;
  1317. d : pdirent;
  1318. name : string;
  1319. st : stat;
  1320. begin
  1321. dirstream:=fpopendir(n);
  1322. if (linuxerror<>0) then
  1323. exit;
  1324. d:=fpReaddir(dirstream^);
  1325. while (d<>nil) do
  1326. begin
  1327. name:=n+'/'+strpas(@(d^.d_name));
  1328. fpstat(name,st);
  1329. if linuxerror=0 then
  1330. begin
  1331. if (fpS_ISDIR(st.st_mode)) and { if it is a directory }
  1332. (strpas(@(d^.d_name))<>'.') and { but not ., .. and fd subdirs }
  1333. (strpas(@(d^.d_name))<>'..') and
  1334. (strpas(@(d^.d_name))<>'') and
  1335. (strpas(@(d^.d_name))<>'fd') then
  1336. begin {we found a directory, search inside it}
  1337. if mysearch(name) then
  1338. begin {the device is here}
  1339. fpclosedir(dirstream^); {then don't continue searching}
  1340. mysearch:=true;
  1341. exit;
  1342. end;
  1343. end
  1344. else if (ino_t(d^.d_fileno)=myino) and (st.st_dev=mydev) then
  1345. begin
  1346. fpclosedir(dirstream^);
  1347. ttyname:=name;
  1348. mysearch:=true;
  1349. exit;
  1350. end;
  1351. end;
  1352. d:=fpReaddir(dirstream^);
  1353. end;
  1354. fpclosedir(dirstream^);
  1355. mysearch:=false;
  1356. end;
  1357. begin
  1358. TTYName:='';
  1359. fpfstat(handle,st);
  1360. if (fpgeterrno<>0) and isatty (handle) then
  1361. exit;
  1362. mydev:=st.st_dev;
  1363. myino:=st.st_ino;
  1364. mysearch('/dev');
  1365. end;
  1366. function TTYName(var F:Text):string;
  1367. {
  1368. Idem as previous, only now for text variables;
  1369. }
  1370. begin
  1371. TTYName:=TTYName(textrec(f).handle);
  1372. end;
  1373. {******************************************************************************
  1374. Utility calls
  1375. ******************************************************************************}
  1376. Function Octal(l:longint):longint;
  1377. {
  1378. Convert an octal specified number to decimal;
  1379. }
  1380. var
  1381. octnr,
  1382. oct : longint;
  1383. begin
  1384. octnr:=0;
  1385. oct:=0;
  1386. while (l>0) do
  1387. begin
  1388. oct:=oct or ((l mod 10) shl octnr);
  1389. l:=l div 10;
  1390. inc(octnr,3);
  1391. end;
  1392. Octal:=oct;
  1393. end;
  1394. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  1395. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  1396. {$I fexpand.inc}
  1397. {$UNDEF FPC_FEXPAND_GETENVPCHAR}
  1398. {$UNDEF FPC_FEXPAND_TILDE}
  1399. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  1400. {
  1401. Searches for a file 'path' in the list of direcories in 'dirlist'.
  1402. returns an empty string if not found. Wildcards are NOT allowed.
  1403. If dirlist is empty, it is set to '.'
  1404. }
  1405. Var
  1406. NewDir : PathStr;
  1407. p1 : Longint;
  1408. Info : Stat;
  1409. Begin
  1410. {Replace ':' with ';'}
  1411. for p1:=1to length(dirlist) do
  1412. if dirlist[p1]=':' then
  1413. dirlist[p1]:=';';
  1414. {Check for WildCards}
  1415. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  1416. FSearch:='' {No wildcards allowed in these things.}
  1417. Else
  1418. Begin
  1419. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  1420. Repeat
  1421. p1:=Pos(';',DirList);
  1422. If p1=0 Then
  1423. p1:=255;
  1424. NewDir:=Copy(DirList,1,P1 - 1);
  1425. if NewDir[Length(NewDir)]<>'/' then
  1426. NewDir:=NewDir+'/';
  1427. NewDir:=NewDir+Path;
  1428. Delete(DirList,1,p1);
  1429. if FpStat(NewDir,Info)>=0 then
  1430. Begin
  1431. If Pos('./',NewDir)=1 Then
  1432. Delete(NewDir,1,2);
  1433. {DOS strips off an initial .\}
  1434. End
  1435. Else
  1436. NewDir:='';
  1437. Until (DirList='') or (Length(NewDir) > 0);
  1438. FSearch:=NewDir;
  1439. End;
  1440. End;
  1441. Procedure Globfree(var p : pglob);
  1442. {
  1443. Release memory occupied by pglob structure, and names in it.
  1444. sets p to nil.
  1445. }
  1446. var
  1447. temp : pglob;
  1448. begin
  1449. while assigned(p) do
  1450. begin
  1451. temp:=p^.next;
  1452. if assigned(p^.name) then
  1453. freemem(p^.name);
  1454. dispose(p);
  1455. p:=temp;
  1456. end;
  1457. end;
  1458. Function Glob(Const path:pathstr):pglob;
  1459. {
  1460. Fills a tglob structure with entries matching path,
  1461. and returns a pointer to it. Returns nil on error,
  1462. linuxerror is set accordingly.
  1463. }
  1464. var
  1465. temp,
  1466. temp2 : string[255];
  1467. thedir : pdir;
  1468. buffer : pdirent;
  1469. root,
  1470. current : pglob;
  1471. begin
  1472. { Get directory }
  1473. temp:=dirname(path);
  1474. if temp='' then
  1475. temp:='.';
  1476. temp:=temp+#0;
  1477. thedir:=fpopendir(@temp[1]);
  1478. if thedir=nil then
  1479. begin
  1480. glob:=nil;
  1481. linuxerror:=fpgeterrno;;
  1482. exit;
  1483. end;
  1484. temp:=basename(path,''); { get the pattern }
  1485. if thedir^.dd_fd<0 then
  1486. begin
  1487. linuxerror:=fpgeterrno;;
  1488. glob:=nil;
  1489. exit;
  1490. end;
  1491. {get the entries}
  1492. root:=nil;
  1493. current:=nil;
  1494. repeat
  1495. buffer:=fpreaddir(thedir^);
  1496. if buffer=nil then
  1497. break;
  1498. temp2:=strpas(@(buffer^.d_name[0]));
  1499. if fnmatch(temp,temp2) then
  1500. begin
  1501. if root=nil then
  1502. begin
  1503. new(root);
  1504. current:=root;
  1505. end
  1506. else
  1507. begin
  1508. new(current^.next);
  1509. current:=current^.next;
  1510. end;
  1511. if current=nil then
  1512. begin
  1513. linuxerror:=ESysENOMEM;
  1514. globfree(root);
  1515. break;
  1516. end;
  1517. current^.next:=nil;
  1518. getmem(current^.name,length(temp2)+1);
  1519. if current^.name=nil then
  1520. begin
  1521. linuxerror:=ESysENOMEM;
  1522. globfree(root);
  1523. break;
  1524. end;
  1525. move(buffer^.d_name[0],current^.name^,length(temp2)+1);
  1526. end;
  1527. until false;
  1528. fpclosedir(thedir^);
  1529. glob:=root;
  1530. end;
  1531. Function GetFS (var T:Text):longint;
  1532. {
  1533. Get File Descriptor of a text file.
  1534. }
  1535. begin
  1536. if textrec(t).mode=fmclosed then
  1537. exit(-1)
  1538. else
  1539. GETFS:=textrec(t).Handle
  1540. end;
  1541. Function GetFS(Var F:File):longint;
  1542. {
  1543. Get File Descriptor of an unTyped file.
  1544. }
  1545. begin
  1546. { Handle and mode are on the same place in textrec and filerec. }
  1547. if filerec(f).mode=fmclosed then
  1548. exit(-1)
  1549. else
  1550. GETFS:=filerec(f).Handle
  1551. end;
  1552. {--------------------------------
  1553. Stat.Mode Macro's
  1554. --------------------------------}
  1555. Initialization
  1556. InitLocalTime;
  1557. finalization
  1558. DoneLocalTime;
  1559. End.
  1560. {
  1561. $Log$
  1562. Revision 1.44 2003-11-12 22:19:45 marco
  1563. * more linuxeror fixes
  1564. Revision 1.43 2003/11/03 09:42:28 marco
  1565. * Peter's Cardinal<->Longint fixes patch
  1566. Revision 1.42 2003/10/30 16:42:58 marco
  1567. * fixes for old syscall() convention removing
  1568. Revision 1.41 2003/10/12 19:40:43 marco
  1569. * ioctl fixes. IDE now starts, but
  1570. Revision 1.40 2003/09/29 14:36:06 peter
  1571. * fixed for stricter compiler
  1572. Revision 1.39 2003/09/27 12:51:33 peter
  1573. * fpISxxx macros renamed to C compliant fpS_ISxxx
  1574. Revision 1.38 2003/09/20 12:38:29 marco
  1575. * FCL now compiles for FreeBSD with new 1.1. Now Linux.
  1576. Revision 1.37 2003/09/17 19:07:44 marco
  1577. * more fixes for Unix<->unixutil
  1578. Revision 1.36 2003/09/17 17:30:46 marco
  1579. * Introduction of unixutil
  1580. Revision 1.35 2003/09/16 21:46:27 marco
  1581. * small fixes, checking things on linux
  1582. Revision 1.34 2003/09/16 20:52:24 marco
  1583. * small cleanups. Mostly killing of already commented code in unix etc
  1584. Revision 1.33 2003/09/16 16:13:56 marco
  1585. * fdset functions renamed to fp<posix name>
  1586. Revision 1.32 2003/09/15 20:08:49 marco
  1587. * small fixes. FreeBSD now cycles
  1588. Revision 1.31 2003/09/14 20:15:01 marco
  1589. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  1590. Revision 1.30 2003/07/08 21:23:24 peter
  1591. * sparc fixes
  1592. Revision 1.29 2003/05/30 19:58:40 marco
  1593. * Getting NetBSD/i386 to compile.
  1594. Revision 1.28 2003/05/29 19:16:16 marco
  1595. * fixed a small *BSD gotcha
  1596. Revision 1.27 2003/05/24 20:39:54 jonas
  1597. * fixed ExitCode translation in WaitProcess for Linux and Darwin (and
  1598. probably other BSD's as well)
  1599. Revision 1.26 2003/03/11 08:27:59 michael
  1600. * stringtoppchar should use tabs instead of backspace as delimiter
  1601. Revision 1.25 2002/12/18 16:50:39 marco
  1602. * Unix RTL generic parts. Linux working, *BSD will follow shortly
  1603. Revision 1.24 2002/09/07 16:01:28 peter
  1604. * old logs removed and tabs fixed
  1605. Revision 1.23 2002/08/06 13:30:46 sg
  1606. * replaced some Longints with Cardinals, to mach the C headers
  1607. * updated the termios record
  1608. Revision 1.22 2002/03/05 20:04:25 michael
  1609. + Patch from Sebastian for FCNTL call
  1610. Revision 1.21 2002/01/02 12:22:54 marco
  1611. * Removed ifdef arround getepoch.
  1612. }