linux.pp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  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 Linux;
  14. Interface
  15. { Get Types and Constants }
  16. {$i sysconst.inc}
  17. {$i systypes.inc}
  18. { Get System call numbers and error-numbers}
  19. {$i sysnr.inc}
  20. {$i errno.inc}
  21. {$I signal.inc}
  22. var
  23. ErrNo,
  24. LinuxError : Longint;
  25. {********************
  26. Process
  27. ********************}
  28. const {Checked for BSD using Linuxthreads port}
  29. { cloning flags }
  30. CSIGNAL = $000000ff; // signal mask to be sent at exit
  31. CLONE_VM = $00000100; // set if VM shared between processes
  32. CLONE_FS = $00000200; // set if fs info shared between processes
  33. CLONE_FILES = $00000400; // set if open files shared between processes
  34. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  35. CLONE_PID = $00001000; // set if pid shared
  36. type
  37. TCloneFunc=function(args:pointer):longint;cdecl;
  38. const
  39. { For getting/setting priority }
  40. Prio_Process = 0;
  41. Prio_PGrp = 1;
  42. Prio_User = 2;
  43. WNOHANG = $1;
  44. WUNTRACED = $2;
  45. __WCLONE = $80000000;
  46. {********************
  47. File
  48. ********************}
  49. Const
  50. P_IN = 1;
  51. P_OUT = 2;
  52. Const
  53. LOCK_SH = 1;
  54. LOCK_EX = 2;
  55. LOCK_UN = 8;
  56. LOCK_NB = 4;
  57. Type
  58. Tpipe = array[1..2] of longint;
  59. pglob = ^tglob;
  60. tglob = record
  61. name : pchar;
  62. next : pglob;
  63. end;
  64. ComStr = String[255];
  65. PathStr = String[255];
  66. DirStr = String[255];
  67. NameStr = String[255];
  68. ExtStr = String[255];
  69. const
  70. { For testing access rights }
  71. R_OK = 4;
  72. W_OK = 2;
  73. X_OK = 1;
  74. F_OK = 0;
  75. { For File control mechanism }
  76. F_GetFd = 1;
  77. F_SetFd = 2;
  78. F_GetFl = 3;
  79. F_SetFl = 4;
  80. F_GetLk = 5;
  81. F_SetLk = 6;
  82. F_SetLkW = 7;
  83. F_GetOwn = 8;
  84. F_SetOwn = 9;
  85. {$I termios.inc}
  86. {********************
  87. Info
  88. ********************}
  89. Type
  90. UTimBuf = packed record{in BSD array[0..1] of timeval, but this is
  91. backwards compatible with linux version}
  92. actime,
  93. {$ifdef BSD}
  94. uactime, {BSD Micro seconds}
  95. {$endif}
  96. modtime
  97. {$ifdef BSD}
  98. ,
  99. umodtime {BSD Micro seconds}
  100. {$endif}
  101. : longint;
  102. end;
  103. UTimeBuf=UTimBuf;
  104. TUTimeBuf=UTimeBuf;
  105. PUTimeBuf=^UTimeBuf;
  106. TSysinfo = packed record
  107. uptime : longint;
  108. loads : array[1..3] of longint;
  109. totalram,
  110. freeram,
  111. sharedram,
  112. bufferram,
  113. totalswap,
  114. freeswap : longint;
  115. procs : integer;
  116. s : string[18];
  117. end;
  118. PSysInfo = ^TSysInfo;
  119. {******************************************************************************
  120. Procedure/Functions
  121. ******************************************************************************}
  122. {$ifdef bsd}
  123. function Do_SysCall(sysnr:longint):longint;
  124. function Do_Syscall(sysnr,param1:integer):longint;
  125. function Do_SysCall(sysnr,param1:LONGINT):longint;
  126. function Do_SysCall(sysnr,param1,param2:LONGINT):longint;
  127. function Do_SysCall(sysnr,param1,param2,param3:LONGINT):longint;
  128. function Do_SysCall(sysnr,param1,param2,param3,param4:LONGINT):longint;
  129. function Do_SysCall(sysnr,param1,param2,param3,param4,param5:LONGINT):longint;
  130. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6:LONGINT):longint;
  131. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6,param7:LONGINT):longint;
  132. {$else}
  133. Function SysCall(callnr:longint;var regs:SysCallregs):longint;
  134. {$endif}
  135. {**************************
  136. Time/Date Handling
  137. ***************************}
  138. var
  139. tzdaylight : boolean;
  140. tzseconds : longint;
  141. tzname : array[boolean] of pchar;
  142. { timezone support }
  143. procedure GetLocalTimezone(timer:longint;var leap_correct,leap_hit:longint);
  144. procedure GetLocalTimezone(timer:longint);
  145. procedure ReadTimezoneFile(fn:string);
  146. function GetTimezoneFile:string;
  147. Procedure GetTimeOfDay(var tv:timeval);
  148. Function GetTimeOfDay:longint;
  149. {$ifndef bsd}
  150. Function GetEpochTime: longint;
  151. {$endif}
  152. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  153. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  154. procedure GetTime(var hour,min,sec,msec,usec:word);
  155. procedure GetTime(var hour,min,sec,sec100:word);
  156. procedure GetTime(var hour,min,sec:word);
  157. Procedure GetDate(Var Year,Month,Day:Word);
  158. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  159. {**************************
  160. Process Handling
  161. ***************************}
  162. function CreateShellArgV(const prog:string):ppchar;
  163. function CreateShellArgV(const prog:Ansistring):ppchar;
  164. Procedure Execve(Path:pathstr;args:ppchar;ep:ppchar);
  165. Procedure Execve(path:pchar;args:ppchar;ep:ppchar);
  166. Procedure Execv(const path:pathstr;args:ppchar);
  167. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  168. Procedure Execl(const Todo:string);
  169. Procedure Execle(Todo:string;Ep:ppchar);
  170. Procedure Execlp(Todo:string;Ep:ppchar);
  171. Function Shell(const Command:String):Longint;
  172. Function Shell(const Command:AnsiString):Longint;
  173. Function Fork:longint;
  174. {Clone for FreeBSD is copied from the LinuxThread port, and rfork based}
  175. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  176. Procedure ExitProcess(val:longint);
  177. Function WaitPid(Pid:longint;Status:pointer;Options:longint):Longint;
  178. Procedure Nice(N:integer);
  179. {$ifdef bsd}
  180. Function GetPriority(Which,Who:longint):longint;
  181. procedure SetPriority(Which,Who,What:longint);
  182. {$else}
  183. Function GetPriority(Which,Who:Integer):integer;
  184. Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
  185. {$endif}
  186. Function GetPid:LongInt;
  187. Function GetPPid:LongInt;
  188. Function GetUid:Longint;
  189. Function GetEUid:Longint;
  190. Function GetGid:Longint;
  191. Function GetEGid:Longint;
  192. {**************************
  193. File Handling
  194. ***************************}
  195. Function fdOpen(pathname:string;flags:longint):longint;
  196. Function fdOpen(pathname:string;flags,mode:longint):longint;
  197. Function fdOpen(pathname:pchar;flags:longint):longint;
  198. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  199. Function fdClose(fd:longint):boolean;
  200. Function fdRead(fd:longint;var buf;size:longint):longint;
  201. Function fdWrite(fd:longint;var buf;size:longint):longint;
  202. Function fdTruncate(fd,size:longint):boolean;
  203. Function fdSeek (fd,pos,seektype :longint): longint;
  204. Function fdFlush (fd : Longint) : Boolean;
  205. Function Link(OldPath,NewPath:pathstr):boolean;
  206. Function SymLink(OldPath,NewPath:pathstr):boolean;
  207. {$ifndef bsd}
  208. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  209. Function ReadLink(name:pathstr):pathstr;
  210. {$endif}
  211. Function UnLink(Path:pathstr):boolean;
  212. Function UnLink(Path:pchar):Boolean;
  213. Function FReName (OldName,NewName : Pchar) : Boolean;
  214. Function FReName (OldName,NewName : String) : Boolean;
  215. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  216. Function Chmod(path:pathstr;Newmode:longint):boolean;
  217. Function Utime(path:pathstr;utim:utimebuf):boolean;
  218. {$ifdef BSD}
  219. Function Access(Path:Pathstr ;mode:longint):boolean;
  220. {$else}
  221. Function Access(Path:Pathstr ;mode:integer):boolean;
  222. {$endif}
  223. Function Umask(Mask:Integer):integer;
  224. Function Flock (fd,mode : longint) : boolean;
  225. Function Flock (var T : text;mode : longint) : boolean;
  226. Function Flock (var F : File;mode : longint) : boolean;
  227. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  228. Function FStat(Fd:longint;Var Info:stat):Boolean;
  229. Function FStat(var F:Text;Var Info:stat):Boolean;
  230. Function FStat(var F:File;Var Info:stat):Boolean;
  231. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  232. Function FSStat(Path:Pathstr;Var Info:statfs):Boolean;
  233. Function FSStat(Fd: Longint;Var Info:statfs):Boolean;
  234. {$ifdef bsd}
  235. Function Fcntl(Fd:longint;Cmd:longint):longint;
  236. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  237. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  238. Procedure Fcntl(var Fd:Text;Cmd:longint;Arg:Longint);
  239. {$else}
  240. Function Fcntl(Fd:longint;Cmd:Integer):integer;
  241. Procedure Fcntl(Fd:longint;Cmd:Integer;Arg:Longint);
  242. Function Fcntl(var Fd:Text;Cmd:Integer):integer;
  243. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  244. {$endif}
  245. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  246. Function Dup(var oldfile,newfile:text):Boolean;
  247. Function Dup(var oldfile,newfile:file):Boolean;
  248. Function Dup2(oldfile,newfile:longint):Boolean;
  249. Function Dup2(var oldfile,newfile:text):Boolean;
  250. Function Dup2(var oldfile,newfile:file):Boolean;
  251. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
  252. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  253. Function SelectText(var T:Text;TimeOut :PTimeVal):Longint;
  254. {**************************
  255. Directory Handling
  256. ***************************}
  257. Function OpenDir(f:pchar):pdir;
  258. Function OpenDir(f: String):pdir;
  259. function CloseDir(p:pdir):integer;
  260. Function ReadDir(p:pdir):pdirent;
  261. procedure SeekDir(p:pdir;off:longint);
  262. function TellDir(p:pdir):longint;
  263. {**************************
  264. Pipe/Fifo/Stream
  265. ***************************}
  266. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  267. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  268. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  269. Function PClose(Var F:text) : longint;
  270. Function PClose(Var F:file) : longint;
  271. Procedure POpen(var F:text;const Prog:String;rw:char);
  272. Procedure POpen(var F:file;const Prog:String;rw:char);
  273. Function mkFifo(pathname:string;mode:longint):boolean;
  274. function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  275. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  276. {**************************
  277. General information
  278. ***************************}
  279. Function GetEnv(P:string):Pchar;
  280. {$ifndef BSD}
  281. Function GetDomainName:String;
  282. Function GetHostName:String;
  283. Function Sysinfo(var Info:TSysinfo):Boolean;
  284. Function Uname(var unamerec:utsname):Boolean;
  285. {$endif}
  286. {**************************
  287. Signal
  288. ***************************}
  289. Procedure SigAction(Signum:longint;Act,OldAct:PSigActionRec );
  290. Procedure SigProcMask (How:longint;SSet,OldSSet:PSigSet);
  291. Function SigPending:SigSet;
  292. Procedure SigSuspend(Mask:Sigset);
  293. Function Signal(Signum:longint;Handler:SignalHandler):SignalHandler;
  294. Function Kill(Pid:longint;Sig:longint):integer;
  295. Procedure SigRaise(Sig:integer);
  296. {$ifndef BSD}
  297. Function Alarm(Sec : Longint) : longint;
  298. Procedure Pause;
  299. {$endif}
  300. {**************************
  301. IOCtl/Termios Functions
  302. ***************************}
  303. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  304. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  305. Function TCSetAttr(fd:longint;OptAct:longint;var tios:TermIOS):boolean;
  306. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  307. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  308. Procedure CFMakeRaw(var tios:TermIOS);
  309. Function TCSendBreak(fd,duration:longint):boolean;
  310. Function TCSetPGrp(fd,id:longint):boolean;
  311. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  312. Function TCFlush(fd,qsel:longint):boolean;
  313. Function TCDrain(fd:longint):boolean;
  314. Function TCFlow(fd,act:longint):boolean;
  315. Function IsATTY(Handle:Longint):Boolean;
  316. Function IsATTY(f:text):Boolean;
  317. function TTYname(Handle:Longint):string;
  318. function TTYname(var F:Text):string;
  319. {**************************
  320. Memory functions
  321. ***************************}
  322. const
  323. PROT_READ = $1; { page can be read }
  324. PROT_WRITE = $2; { page can be written }
  325. PROT_EXEC = $4; { page can be executed }
  326. PROT_NONE = $0; { page can not be accessed }
  327. MAP_SHARED = $1; { Share changes }
  328. MAP_PRIVATE = $2; { Changes are private }
  329. MAP_TYPE = $f; { Mask for type of mapping }
  330. MAP_FIXED = $10; { Interpret addr exactly }
  331. MAP_ANONYMOUS = $20; { don't use a file }
  332. MAP_GROWSDOWN = $100; { stack-like segment }
  333. MAP_DENYWRITE = $800; { ETXTBSY }
  334. MAP_EXECUTABLE = $1000; { mark it as an executable }
  335. MAP_LOCKED = $2000; { pages are locked }
  336. MAP_NORESERVE = $4000; { don't check for reservations }
  337. type
  338. tmmapargs=record
  339. address : longint;
  340. size : longint;
  341. prot : longint;
  342. flags : longint;
  343. fd : longint;
  344. offset : longint;
  345. end;
  346. function MMap(const m:tmmapargs):longint;
  347. function MUnMap (P : Pointer; Size : Longint) : Boolean;
  348. {**************************
  349. Port IO functions
  350. ***************************}
  351. {$ifndef BSD}
  352. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  353. {$IFDEF I386}
  354. Procedure WritePort (Port : Longint; Value : Byte);
  355. Procedure WritePort (Port : Longint; Value : Word);
  356. Procedure WritePort (Port : Longint; Value : Longint);
  357. Procedure WritePortB (Port : Longint; Value : Byte);
  358. Procedure WritePortW (Port : Longint; Value : Word);
  359. Procedure WritePortL (Port : Longint; Value : Longint);
  360. Procedure WritePortL (Port : Longint; Var Buf; Count: longint);
  361. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  362. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  363. Procedure ReadPort (Port : Longint; Var Value : Byte);
  364. Procedure ReadPort (Port : Longint; Var Value : Word);
  365. Procedure ReadPort (Port : Longint; Var Value : Longint);
  366. function ReadPortB (Port : Longint): Byte;
  367. function ReadPortW (Port : Longint): Word;
  368. function ReadPortL (Port : Longint): LongInt;
  369. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  370. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  371. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  372. {$endif}
  373. {$endif}
  374. {**************************
  375. Utility functions
  376. ***************************}
  377. Function Octal(l:longint):longint;
  378. Function FExpand(Const Path: PathStr):PathStr;
  379. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  380. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  381. Function Dirname(Const path:pathstr):pathstr;
  382. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  383. Function FNMatch(const Pattern,Name:string):Boolean;
  384. Function Glob(Const path:pathstr):pglob;
  385. Procedure Globfree(var p:pglob);
  386. Function StringToPPChar(Var S:STring):ppchar;
  387. Function GetFS(var T:Text):longint;
  388. Function GetFS(Var F:File):longint;
  389. {Filedescriptorsets}
  390. Procedure FD_Zero(var fds:fdSet);
  391. Procedure FD_Clr(fd:longint;var fds:fdSet);
  392. Procedure FD_Set(fd:longint;var fds:fdSet);
  393. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  394. {Stat.Mode Types}
  395. Function S_ISLNK(m:word):boolean;
  396. Function S_ISREG(m:word):boolean;
  397. Function S_ISDIR(m:word):boolean;
  398. Function S_ISCHR(m:word):boolean;
  399. Function S_ISBLK(m:word):boolean;
  400. Function S_ISFIFO(m:word):boolean;
  401. Function S_ISSOCK(m:word):boolean;
  402. {******************************************************************************
  403. Implementation
  404. ******************************************************************************}
  405. Implementation
  406. Uses Strings;
  407. { Get the definitions of textrec and filerec }
  408. {$i textrec.inc}
  409. {$i filerec.inc}
  410. { Raw System calls are in Syscalls.inc}
  411. {$i syscalls.inc}
  412. {$ifdef BSD}
  413. {$i bsdsysca.inc}
  414. {$else}
  415. {$i linsysca.inc}
  416. {$endif}
  417. {******************************************************************************
  418. Process related calls
  419. ******************************************************************************}
  420. function CreateShellArgV(const prog:string):ppchar;
  421. {
  422. Create an argv which executes a command in a shell using /bin/sh -c
  423. }
  424. var
  425. pp,p : ppchar;
  426. temp : string;
  427. begin
  428. getmem(pp,4*4);
  429. temp:='/bin/sh'#0'-c'#0+prog+#0;
  430. p:=pp;
  431. p^:=@temp[1];
  432. inc(p);
  433. p^:=@temp[9];
  434. inc(p);
  435. p^:=@temp[12];
  436. inc(p);
  437. p^:=Nil;
  438. CreateShellArgV:=pp;
  439. end;
  440. function CreateShellArgV(const prog:Ansistring):ppchar;
  441. {
  442. Create an argv which executes a command in a shell using /bin/sh -c
  443. using a AnsiString;
  444. }
  445. var
  446. pp,p : ppchar;
  447. temp : AnsiString;
  448. begin
  449. getmem(pp,4*4);
  450. temp:='/bin/sh'#0'-c'#0+prog+#0;
  451. p:=pp;
  452. GetMem(p^,Length(Temp));
  453. Move(Temp[1],p^^,Length(Temp));
  454. inc(p);
  455. p^:=@pp[0][8];
  456. inc(p);
  457. p^:=@pp[0][11];
  458. inc(p);
  459. p^:=Nil;
  460. CreateShellArgV:=pp;
  461. end;
  462. Procedure Execv(const path:pathstr;args:ppchar);
  463. {
  464. Replaces the current program by the program specified in path,
  465. arguments in args are passed to Execve.
  466. the current environment is passed on.
  467. }
  468. begin
  469. Execve(path,args,envp); {On error linuxerror will get set there}
  470. end;
  471. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  472. {
  473. This does the same as Execve, only it searches the PATH environment
  474. for the place of the Executable, except when Path starts with a slash.
  475. if the PATH environment variable is unavailable, the path is set to '.'
  476. }
  477. var
  478. thepath : string;
  479. begin
  480. if path[1]<>'/' then
  481. begin
  482. Thepath:=strpas(getenv('PATH'));
  483. if thepath='' then
  484. thepath:='.';
  485. Path:=FSearch(path,thepath)
  486. end
  487. else
  488. Path:='';
  489. if Path='' then
  490. linuxerror:=Sys_enoent
  491. else
  492. Execve(Path,args,ep);{On error linuxerror will get set there}
  493. end;
  494. Procedure Execle(Todo:string;Ep:ppchar);
  495. {
  496. This procedure takes the string 'Todo', parses it for command and
  497. command options, and Executes the command with the given options.
  498. The string 'Todo' shoud be of the form 'command options', options
  499. separated by commas.
  500. the PATH environment is not searched for 'command'.
  501. The specified environment(in 'ep') is passed on to command
  502. }
  503. var
  504. p : ppchar;
  505. begin
  506. p:=StringToPPChar(ToDo);
  507. if (p=nil) or (p^=nil) then
  508. exit;
  509. ExecVE(p^,p,EP);
  510. end;
  511. Procedure Execl(const Todo:string);
  512. {
  513. This procedure takes the string 'Todo', parses it for command and
  514. command options, and Executes the command with the given options.
  515. The string 'Todo' shoud be of the form 'command options', options
  516. separated by commas.
  517. the PATH environment is not searched for 'command'.
  518. The current environment is passed on to command
  519. }
  520. begin
  521. ExecLE(ToDo,EnvP);
  522. end;
  523. Procedure Execlp(Todo:string;Ep:ppchar);
  524. {
  525. This procedure takes the string 'Todo', parses it for command and
  526. command options, and Executes the command with the given options.
  527. The string 'Todo' shoud be of the form 'command options', options
  528. separated by commas.
  529. the PATH environment is searched for 'command'.
  530. The specified environment (in 'ep') is passed on to command
  531. }
  532. var
  533. p : ppchar;
  534. begin
  535. p:=StringToPPchar(todo);
  536. if (p=nil) or (p^=nil) then
  537. exit;
  538. ExecVP(StrPas(p^),p,EP);
  539. end;
  540. Function Shell(const Command:String):Longint;
  541. {
  542. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  543. The current environment is passed to the shell.
  544. It waits for the shell to exit, and returns its exit status.
  545. If the Exec call failed exit status 127 is reported.
  546. }
  547. var
  548. p : ppchar;
  549. temp,pid : longint;
  550. begin
  551. pid:=fork;
  552. if pid=-1 then
  553. exit; {Linuxerror already set in Fork}
  554. if pid=0 then
  555. begin
  556. {This is the child.}
  557. p:=CreateShellArgv(command);
  558. Execve(p^,p,envp);
  559. exit(127);
  560. end;
  561. temp:=0;
  562. WaitPid(pid,@temp,0);{Linuxerror is set there}
  563. Shell:=temp;{ Return exit status }
  564. end;
  565. Function Shell(const Command:AnsiString):Longint;
  566. {
  567. AnsiString version of Shell
  568. }
  569. var
  570. p : ppchar;
  571. temp,pid : longint;
  572. begin
  573. pid:=fork;
  574. if pid=-1 then
  575. exit; {Linuxerror already set in Fork}
  576. if pid=0 then
  577. begin
  578. {This is the child.}
  579. p:=CreateShellArgv(command);
  580. Execve(p^,p,envp);
  581. exit(127);
  582. end;
  583. temp:=0;
  584. WaitPid(pid,@temp,0);{Linuxerror is set there}
  585. Shell:=temp;{ Return exit status }
  586. end;
  587. {******************************************************************************
  588. Date and Time related calls
  589. ******************************************************************************}
  590. Const
  591. {Date Translation}
  592. C1970=2440588;
  593. D0 = 1461;
  594. D1 = 146097;
  595. D2 =1721119;
  596. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  597. Var
  598. Century,XYear: LongInt;
  599. Begin
  600. If Month<=2 Then
  601. Begin
  602. Dec(Year);
  603. Inc(Month,12);
  604. End;
  605. Dec(Month,3);
  606. Century:=(longint(Year Div 100)*D1) shr 2;
  607. XYear:=(longint(Year Mod 100)*D0) shr 2;
  608. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  609. End;
  610. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  611. Var
  612. YYear,XYear,Temp,TempMonth : LongInt;
  613. Begin
  614. Temp:=((JulianDN-D2) shl 2)-1;
  615. JulianDN:=Temp Div D1;
  616. XYear:=(Temp Mod D1) or 3;
  617. YYear:=(XYear Div D0);
  618. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  619. Day:=((Temp Mod 153)+5) Div 5;
  620. TempMonth:=Temp Div 153;
  621. If TempMonth>=10 Then
  622. Begin
  623. inc(YYear);
  624. dec(TempMonth,12);
  625. End;
  626. inc(TempMonth,3);
  627. Month := TempMonth;
  628. Year:=YYear+(JulianDN*100);
  629. end;
  630. Function GetEpochTime: longint;
  631. {
  632. Get the number of seconds since 00:00, January 1 1970, GMT
  633. the time NOT corrected any way
  634. }
  635. begin
  636. GetEpochTime:=GetTimeOfDay;
  637. end;
  638. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  639. {
  640. Transforms Epoch time into local time (hour, minute,seconds)
  641. }
  642. Var
  643. DateNum: LongInt;
  644. Begin
  645. inc(Epoch,TZSeconds);
  646. Datenum:=(Epoch Div 86400) + c1970;
  647. JulianToGregorian(DateNum,Year,Month,day);
  648. Epoch:=Epoch Mod 86400;
  649. Hour:=Epoch Div 3600;
  650. Epoch:=Epoch Mod 3600;
  651. Minute:=Epoch Div 60;
  652. Second:=Epoch Mod 60;
  653. End;
  654. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  655. {
  656. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  657. (seconds since 00:00, january 1 1970, corrected for local time zone)
  658. }
  659. Begin
  660. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  661. (LongInt(Hour)*3600)+(Minute*60)+Second-TZSeconds;
  662. End;
  663. procedure GetTime(var hour,min,sec,msec,usec:word);
  664. {
  665. Gets the current time, adjusted to local time
  666. }
  667. var
  668. year,day,month:Word;
  669. t : timeval;
  670. begin
  671. gettimeofday(t);
  672. EpochToLocal(t.sec,year,month,day,hour,min,sec);
  673. msec:=t.usec div 1000;
  674. usec:=t.usec mod 1000;
  675. end;
  676. procedure GetTime(var hour,min,sec,sec100:word);
  677. {
  678. Gets the current time, adjusted to local time
  679. }
  680. var
  681. usec : word;
  682. begin
  683. gettime(hour,min,sec,sec100,usec);
  684. sec100:=sec100 div 10;
  685. end;
  686. Procedure GetTime(Var Hour,Min,Sec:Word);
  687. {
  688. Gets the current time, adjusted to local time
  689. }
  690. var
  691. msec,usec : Word;
  692. Begin
  693. gettime(hour,min,sec,msec,usec);
  694. End;
  695. Procedure GetDate(Var Year,Month,Day:Word);
  696. {
  697. Gets the current date, adjusted to local time
  698. }
  699. var
  700. hour,minute,second : word;
  701. Begin
  702. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  703. End;
  704. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  705. {
  706. Gets the current date, adjusted to local time
  707. }
  708. Begin
  709. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  710. End;
  711. { Include timezone handling routines which use /usr/share/timezone info }
  712. {$i timezone.inc}
  713. {******************************************************************************
  714. FileSystem calls
  715. ******************************************************************************}
  716. Function fdOpen(pathname:string;flags:longint):longint;
  717. begin
  718. pathname:=pathname+#0;
  719. fdOpen:=Sys_Open(@pathname[1],flags,438);
  720. LinuxError:=Errno;
  721. end;
  722. Function fdOpen(pathname:string;flags,mode:longint):longint;
  723. begin
  724. pathname:=pathname+#0;
  725. fdOpen:=Sys_Open(@pathname[1],flags,mode);
  726. LinuxError:=Errno;
  727. end;
  728. Function fdOpen(pathname:pchar;flags:longint):longint;
  729. begin
  730. fdOpen:=Sys_Open(pathname,flags,0);
  731. LinuxError:=Errno;
  732. end;
  733. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  734. begin
  735. fdOpen:=Sys_Open(pathname,flags,mode);
  736. LinuxError:=Errno;
  737. end;
  738. Function fdClose(fd:longint):boolean;
  739. begin
  740. fdClose:=(Sys_Close(fd)=0);
  741. LinuxError:=Errno;
  742. end;
  743. Function fdRead(fd:longint;var buf;size:longint):longint;
  744. begin
  745. fdRead:=Sys_Read(fd,pchar(@buf),size);
  746. LinuxError:=Errno;
  747. end;
  748. Function fdWrite(fd:longint;var buf;size:longint):longint;
  749. begin
  750. fdWrite:=Sys_Write(fd,pchar(@buf),size);
  751. LinuxError:=Errno;
  752. end;
  753. Function fdSeek (fd,pos,seektype :longint): longint;
  754. {
  755. Do a Seek on a file descriptor fd to position pos, starting from seektype
  756. }
  757. begin
  758. fdseek:=Sys_LSeek (fd,pos,seektype);
  759. LinuxError:=Errno;
  760. end;
  761. {$ifdef BSD}
  762. Function Fcntl(Fd:longint;Cmd:longint):longint;
  763. {
  764. Read or manipulate a file.(See also fcntl (2) )
  765. Possible values for Cmd are :
  766. F_GetFd,F_GetFl,F_GetOwn
  767. Errors are reported in Linuxerror;
  768. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  769. }
  770. begin
  771. if (cmd in [F_GetFd,F_GetFl,F_GetOwn]) then
  772. begin
  773. Linuxerror:=sys_fcntl(fd,cmd,0);
  774. if linuxerror=-1 then
  775. begin
  776. linuxerror:=errno;
  777. fcntl:=0;
  778. end
  779. else
  780. begin
  781. fcntl:=linuxerror;
  782. linuxerror:=0;
  783. end;
  784. end
  785. else
  786. begin
  787. linuxerror:=Sys_einval;
  788. Fcntl:=0;
  789. end;
  790. end;
  791. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  792. {
  793. Read or manipulate a file. (See also fcntl (2) )
  794. Possible values for Cmd are :
  795. F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
  796. Errors are reported in Linuxerror;
  797. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  798. F_DupFD is not allowed, due to the structure of Files in Pascal.
  799. }
  800. begin
  801. if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn]) then
  802. begin
  803. sys_fcntl(fd,cmd,arg);
  804. LinuxError:=ErrNo;
  805. end
  806. else
  807. linuxerror:=Sys_einval;
  808. end;
  809. {$endif}
  810. {$ifdef BSD}
  811. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  812. {$else}
  813. Function Fcntl(var Fd:Text;Cmd:integer):integer;
  814. {$endif}
  815. begin
  816. Fcntl := Fcntl(textrec(Fd).handle, Cmd);
  817. end;
  818. {$ifdef BSD}
  819. Procedure Fcntl(var Fd:Text;Cmd,Arg:Longint);
  820. {$else}
  821. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  822. {$endif}
  823. begin
  824. Fcntl(textrec(Fd).handle, Cmd, Arg);
  825. end;
  826. Function Flock (var T : text;mode : longint) : boolean;
  827. begin
  828. Flock:=Flock(TextRec(T).Handle,mode);
  829. end;
  830. Function Flock (var F : File;mode : longint) : boolean;
  831. begin
  832. Flock:=Flock(FileRec(F).Handle,mode);
  833. end;
  834. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  835. {
  836. Get all information on a file, and return it in Info.
  837. }
  838. begin
  839. path:=path+#0;
  840. FStat:=(Sys_stat(@(path[1]),Info)=0);
  841. LinuxError:=errno;
  842. end;
  843. Function FStat(var F:Text;Var Info:stat):Boolean;
  844. {
  845. Get all information on a text file, and return it in info.
  846. }
  847. begin
  848. FStat:=Fstat(TextRec(F).Handle,INfo);
  849. end;
  850. Function FStat(var F:File;Var Info:stat):Boolean;
  851. {
  852. Get all information on a untyped file, and return it in info.
  853. }
  854. begin
  855. FStat:=Fstat(FileRec(F).Handle,Info);
  856. end;
  857. Function SymLink(OldPath,newPath:pathstr):boolean;
  858. {
  859. Proceduces a soft link from new to old.
  860. }
  861. begin
  862. oldpath:=oldpath+#0;
  863. newpath:=newpath+#0;
  864. Symlink:=Sys_symlink(pchar(@(oldpath[1])),pchar(@(newpath[1])))=0;
  865. linuxerror:=errno;
  866. end;
  867. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  868. {
  869. Read a link (where it points to)
  870. }
  871. begin
  872. Readlink:=Sys_readlink(Name,LinkName,maxlen);
  873. linuxerror:=errno;
  874. end;
  875. Function ReadLink(Name:pathstr):pathstr;
  876. {
  877. Read a link (where it points to)
  878. }
  879. var
  880. LinkName : pathstr;
  881. i : longint;
  882. begin
  883. Name:=Name+#0;
  884. i:=ReadLink(@Name[1],@LinkName[1],high(linkname));
  885. if i>0 then
  886. begin
  887. linkname[0]:=chr(i);
  888. ReadLink:=LinkName;
  889. end
  890. else
  891. ReadLink:='';
  892. end;
  893. Function UnLink(Path:pathstr):boolean;
  894. {
  895. Removes the file in 'Path' (that is, it decreases the link count with one.
  896. if the link count is zero, the file is removed from the disk.
  897. }
  898. begin
  899. path:=path+#0;
  900. Unlink:=Sys_unlink(pchar(@(path[1])))=0;
  901. linuxerror:=errno;
  902. end;
  903. Function UnLink(Path:pchar):Boolean;
  904. {
  905. Removes the file in 'Path' (that is, it decreases the link count with one.
  906. if the link count is zero, the file is removed from the disk.
  907. }
  908. begin
  909. Unlink:=(Sys_unlink(path)=0);
  910. linuxerror:=errno;
  911. end;
  912. Function FRename (OldName,NewName : Pchar) : Boolean;
  913. begin
  914. FRename:=Sys_rename(OldName,NewName)=0;
  915. LinuxError:=Errno;
  916. end;
  917. Function FRename (OldName,NewName : String) : Boolean;
  918. begin
  919. OldName:=OldName+#0;
  920. NewName:=NewName+#0;
  921. FRename:=FRename (@OldName[1],@NewName[1]);
  922. end;
  923. Function Dup(var oldfile,newfile:text):Boolean;
  924. {
  925. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  926. oldfile.
  927. After which the two textfiles are, in effect, the same, except
  928. that they don't share the same buffer, and don't share the same
  929. close_on_exit flag.
  930. }
  931. begin
  932. flush(oldfile);{ We cannot share buffers, so we flush them. }
  933. textrec(newfile):=textrec(oldfile);
  934. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  935. Dup:=Dup(textrec(oldfile).handle,textrec(newfile).handle);
  936. end;
  937. Function Dup(var oldfile,newfile:file):Boolean;
  938. {
  939. Copies the filedescriptor oldfile to newfile
  940. }
  941. begin
  942. filerec(newfile):=filerec(oldfile);
  943. Dup:=Dup(filerec(oldfile).handle,filerec(newfile).handle);
  944. end;
  945. Function Dup2(var oldfile,newfile:text):Boolean;
  946. {
  947. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  948. oldfile. It closes newfile if it was still open.
  949. After which the two textfiles are, in effect, the same, except
  950. that they don't share the same buffer, and don't share the same
  951. close_on_exit flag.
  952. }
  953. var
  954. tmphandle : word;
  955. begin
  956. case TextRec(oldfile).mode of
  957. fmOutput, fmInOut, fmAppend :
  958. flush(oldfile);{ We cannot share buffers, so we flush them. }
  959. end;
  960. case TextRec(newfile).mode of
  961. fmOutput, fmInOut, fmAppend :
  962. flush(newfile);
  963. end;
  964. tmphandle:=textrec(newfile).handle;
  965. textrec(newfile):=textrec(oldfile);
  966. textrec(newfile).handle:=tmphandle;
  967. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  968. Dup2:=Dup2(textrec(oldfile).handle,textrec(newfile).handle);
  969. end;
  970. Function Dup2(var oldfile,newfile:file):Boolean;
  971. {
  972. Copies the filedescriptor oldfile to newfile
  973. }
  974. begin
  975. filerec(newfile):=filerec(oldfile);
  976. Dup2:=Dup2(filerec(oldfile).handle,filerec(newfile).handle);
  977. end;
  978. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  979. {
  980. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  981. have changed.
  982. This function allows specification of a timeout as a longint.
  983. }
  984. var
  985. p : PTimeVal;
  986. tv : TimeVal;
  987. begin
  988. if TimeOut=-1 then
  989. p:=nil
  990. else
  991. begin
  992. tv.Sec:=Timeout div 1000;
  993. tv.Usec:=(Timeout mod 1000)*1000;
  994. p:=@tv;
  995. end;
  996. Select:=Select(N,Readfds,WriteFds,ExceptFds,p);
  997. end;
  998. Function SelectText(var T:Text;TimeOut :PTimeval):Longint;
  999. Var
  1000. F:FDSet;
  1001. begin
  1002. if textrec(t).mode=fmclosed then
  1003. begin
  1004. LinuxError:=Sys_EBADF;
  1005. exit(-1);
  1006. end;
  1007. FD_Zero(f);
  1008. FD_Set(textrec(T).handle,f);
  1009. if textrec(T).mode=fminput then
  1010. SelectText:=select(textrec(T).handle+1,@f,nil,nil,TimeOut)
  1011. else
  1012. SelectText:=select(textrec(T).handle+1,nil,@f,nil,TimeOut);
  1013. end;
  1014. {******************************************************************************
  1015. Directory
  1016. ******************************************************************************}
  1017. Function OpenDir(F:String):PDir;
  1018. begin
  1019. F:=F+#0;
  1020. OpenDir:=OpenDir(@F[1]);
  1021. end;
  1022. procedure SeekDir(p:pdir;off:longint);
  1023. begin
  1024. if p=nil then
  1025. begin
  1026. errno:=Sys_EBADF;
  1027. exit;
  1028. end;
  1029. {$ifndef bsd}
  1030. p^.nextoff:=Sys_lseek(p^.fd,off,seek_set);
  1031. {$endif}
  1032. p^.size:=0;
  1033. p^.loc:=0;
  1034. end;
  1035. function TellDir(p:pdir):longint;
  1036. begin
  1037. if p=nil then
  1038. begin
  1039. errno:=Sys_EBADF;
  1040. telldir:=-1;
  1041. exit;
  1042. end;
  1043. telldir:=Sys_lseek(p^.fd,0,seek_cur)
  1044. { We could try to use the nextoff field here, but on my 1.2.13
  1045. kernel, this gives nothing... This may have to do with
  1046. the readdir implementation of libc... I also didn't find any trace of
  1047. the field in the kernel code itself, So I suspect it is an artifact of libc.
  1048. Michael. }
  1049. end;
  1050. Function ReadDir(P:pdir):pdirent;
  1051. begin
  1052. ReadDir:=Sys_ReadDir(p);
  1053. LinuxError:=Errno;
  1054. end;
  1055. {******************************************************************************
  1056. Pipes/Fifo
  1057. ******************************************************************************}
  1058. Procedure OpenPipe(var F:Text);
  1059. begin
  1060. case textrec(f).mode of
  1061. fmoutput :
  1062. if textrec(f).userdata[1]<>P_OUT then
  1063. textrec(f).mode:=fmclosed;
  1064. fminput :
  1065. if textrec(f).userdata[1]<>P_IN then
  1066. textrec(f).mode:=fmclosed;
  1067. else
  1068. textrec(f).mode:=fmclosed;
  1069. end;
  1070. end;
  1071. Procedure IOPipe(var F:text);
  1072. begin
  1073. case textrec(f).mode of
  1074. fmoutput :
  1075. begin
  1076. { first check if we need something to write, else we may
  1077. get a SigPipe when Close() is called (PFV) }
  1078. if textrec(f).bufpos>0 then
  1079. Sys_write(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  1080. end;
  1081. fminput :
  1082. textrec(f).bufend:=Sys_read(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  1083. end;
  1084. textrec(f).bufpos:=0;
  1085. end;
  1086. Procedure FlushPipe(var F:Text);
  1087. begin
  1088. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  1089. IOPipe(f);
  1090. textrec(f).bufpos:=0;
  1091. end;
  1092. Procedure ClosePipe(var F:text);
  1093. begin
  1094. textrec(f).mode:=fmclosed;
  1095. Sys_close(textrec(f).handle);
  1096. end;
  1097. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  1098. {
  1099. Sets up a pair of file variables, which act as a pipe. The first one can
  1100. be read from, the second one can be written to.
  1101. If the operation was unsuccesful, linuxerror is set.
  1102. }
  1103. var
  1104. f_in,f_out : longint;
  1105. begin
  1106. if not AssignPipe(f_in,f_out) then
  1107. begin
  1108. AssignPipe:=false;
  1109. exit;
  1110. end;
  1111. { Set up input }
  1112. Assign(Pipe_in,'');
  1113. Textrec(Pipe_in).Handle:=f_in;
  1114. Textrec(Pipe_in).Mode:=fmInput;
  1115. Textrec(Pipe_in).userdata[1]:=P_IN;
  1116. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  1117. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  1118. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  1119. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  1120. { Set up output }
  1121. Assign(Pipe_out,'');
  1122. Textrec(Pipe_out).Handle:=f_out;
  1123. Textrec(Pipe_out).Mode:=fmOutput;
  1124. Textrec(Pipe_out).userdata[1]:=P_OUT;
  1125. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  1126. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  1127. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  1128. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  1129. AssignPipe:=true;
  1130. end;
  1131. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  1132. {
  1133. Sets up a pair of file variables, which act as a pipe. The first one can
  1134. be read from, the second one can be written to.
  1135. If the operation was unsuccesful, linuxerror is set.
  1136. }
  1137. var
  1138. f_in,f_out : longint;
  1139. begin
  1140. if not AssignPipe(f_in,f_out) then
  1141. begin
  1142. AssignPipe:=false;
  1143. exit;
  1144. end;
  1145. { Set up input }
  1146. Assign(Pipe_in,'');
  1147. Filerec(Pipe_in).Handle:=f_in;
  1148. Filerec(Pipe_in).Mode:=fmInput;
  1149. Filerec(Pipe_in).recsize:=1;
  1150. Filerec(Pipe_in).userdata[1]:=P_IN;
  1151. { Set up output }
  1152. Assign(Pipe_out,'');
  1153. Filerec(Pipe_out).Handle:=f_out;
  1154. Filerec(Pipe_out).Mode:=fmoutput;
  1155. Filerec(Pipe_out).recsize:=1;
  1156. Filerec(Pipe_out).userdata[1]:=P_OUT;
  1157. AssignPipe:=true;
  1158. end;
  1159. Procedure PCloseText(Var F:text);
  1160. {
  1161. May not use @PClose due overloading
  1162. }
  1163. begin
  1164. PClose(f);
  1165. end;
  1166. Procedure POpen(var F:text;const Prog:String;rw:char);
  1167. {
  1168. Starts the program in 'Prog' and makes it's input or out put the
  1169. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1170. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1171. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1172. read from 'f'.
  1173. }
  1174. var
  1175. pipi,
  1176. pipo : text;
  1177. pid : longint;
  1178. pl : ^longint;
  1179. pp : ppchar;
  1180. begin
  1181. LinuxError:=0;
  1182. rw:=upcase(rw);
  1183. if not (rw in ['R','W']) then
  1184. begin
  1185. LinuxError:=Sys_enoent;
  1186. exit;
  1187. end;
  1188. AssignPipe(pipi,pipo);
  1189. if Linuxerror<>0 then
  1190. exit;
  1191. pid:=fork;
  1192. if linuxerror<>0 then
  1193. begin
  1194. close(pipi);
  1195. close(pipo);
  1196. exit;
  1197. end;
  1198. if pid=0 then
  1199. begin
  1200. { We're in the child }
  1201. if rw='W' then
  1202. begin
  1203. close(pipo);
  1204. dup2(pipi,input);
  1205. close(pipi);
  1206. if linuxerror<>0 then
  1207. halt(127);
  1208. end
  1209. else
  1210. begin
  1211. close(pipi);
  1212. dup2(pipo,output);
  1213. close(pipo);
  1214. if linuxerror<>0 then
  1215. halt(127);
  1216. end;
  1217. pp:=createshellargv(prog);
  1218. Execve(pp^,pp,envp);
  1219. halt(127);
  1220. end
  1221. else
  1222. begin
  1223. { We're in the parent }
  1224. if rw='W' then
  1225. begin
  1226. close(pipi);
  1227. f:=pipo;
  1228. textrec(f).bufptr:=@textrec(f).buffer;
  1229. end
  1230. else
  1231. begin
  1232. close(pipo);
  1233. f:=pipi;
  1234. textrec(f).bufptr:=@textrec(f).buffer;
  1235. end;
  1236. {Save the process ID - needed when closing }
  1237. pl:=@(textrec(f).userdata[2]);
  1238. pl^:=pid;
  1239. textrec(f).closefunc:=@PCloseText;
  1240. end;
  1241. end;
  1242. Procedure POpen(var F:file;const Prog:String;rw:char);
  1243. {
  1244. Starts the program in 'Prog' and makes it's input or out put the
  1245. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1246. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1247. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1248. read from 'f'.
  1249. }
  1250. var
  1251. pipi,
  1252. pipo : file;
  1253. pid : longint;
  1254. pl : ^longint;
  1255. p,pp : ppchar;
  1256. temp : string[255];
  1257. begin
  1258. LinuxError:=0;
  1259. rw:=upcase(rw);
  1260. if not (rw in ['R','W']) then
  1261. begin
  1262. LinuxError:=Sys_enoent;
  1263. exit;
  1264. end;
  1265. AssignPipe(pipi,pipo);
  1266. if Linuxerror<>0 then
  1267. exit;
  1268. pid:=fork;
  1269. if linuxerror<>0 then
  1270. begin
  1271. close(pipi);
  1272. close(pipo);
  1273. exit;
  1274. end;
  1275. if pid=0 then
  1276. begin
  1277. { We're in the child }
  1278. if rw='W' then
  1279. begin
  1280. close(pipo);
  1281. dup2(filerec(pipi).handle,stdinputhandle);
  1282. close(pipi);
  1283. if linuxerror<>0 then
  1284. halt(127);
  1285. end
  1286. else
  1287. begin
  1288. close(pipi);
  1289. dup2(filerec(pipo).handle,stdoutputhandle);
  1290. close(pipo);
  1291. if linuxerror<>0 then
  1292. halt(127);
  1293. end;
  1294. getmem(pp,sizeof(pchar)*4);
  1295. temp:='/bin/sh'#0'-c'#0+prog+#0;
  1296. p:=pp;
  1297. p^:=@temp[1];
  1298. inc(p);
  1299. p^:=@temp[9];
  1300. inc(p);
  1301. p^:=@temp[12];
  1302. inc(p);
  1303. p^:=Nil;
  1304. Execve('/bin/sh',pp,envp);
  1305. halt(127);
  1306. end
  1307. else
  1308. begin
  1309. { We're in the parent }
  1310. if rw='W' then
  1311. begin
  1312. close(pipi);
  1313. f:=pipo;
  1314. end
  1315. else
  1316. begin
  1317. close(pipo);
  1318. f:=pipi;
  1319. end;
  1320. {Save the process ID - needed when closing }
  1321. pl:=@(filerec(f).userdata[2]);
  1322. pl^:=pid;
  1323. end;
  1324. end;
  1325. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  1326. {
  1327. Starts the program in 'Prog' and makes its input and output the
  1328. other end of two pipes, which are the stdin and stdout of a program
  1329. specified in 'Prog'.
  1330. streamout can be used to write to the program, streamin can be used to read
  1331. the output of the program. See the following diagram :
  1332. Parent Child
  1333. STreamout --> Input
  1334. Streamin <-- Output
  1335. Return value is the process ID of the process being spawned, or -1 in case of failure.
  1336. }
  1337. var
  1338. pipi,
  1339. pipo : text;
  1340. pid : longint;
  1341. pl : ^Longint;
  1342. begin
  1343. LinuxError:=0;
  1344. AssignStream:=-1;
  1345. AssignPipe(streamin,pipo);
  1346. if Linuxerror<>0 then
  1347. exit;
  1348. AssignPipe(pipi,streamout);
  1349. if Linuxerror<>0 then
  1350. exit;
  1351. pid:=fork;
  1352. if linuxerror<>0 then
  1353. begin
  1354. close(pipi);
  1355. close(pipo);
  1356. close (streamin);
  1357. close (streamout);
  1358. exit;
  1359. end;
  1360. if pid=0 then
  1361. begin
  1362. { We're in the child }
  1363. { Close what we don't need }
  1364. close(streamout);
  1365. close(streamin);
  1366. dup2(pipi,input);
  1367. if linuxerror<>0 then
  1368. halt(127);
  1369. close(pipi);
  1370. dup2(pipo,output);
  1371. if linuxerror<>0 then
  1372. halt (127);
  1373. close(pipo);
  1374. Execl(Prog);
  1375. halt(127);
  1376. end
  1377. else
  1378. begin
  1379. { we're in the parent}
  1380. close(pipo);
  1381. close(pipi);
  1382. {Save the process ID - needed when closing }
  1383. pl:=@(textrec(StreamIn).userdata[2]);
  1384. pl^:=pid;
  1385. textrec(StreamIn).closefunc:=@PCloseText;
  1386. {Save the process ID - needed when closing }
  1387. pl:=@(textrec(StreamOut).userdata[2]);
  1388. pl^:=pid;
  1389. textrec(StreamOut).closefunc:=@PCloseText;
  1390. AssignStream:=Pid;
  1391. end;
  1392. end;
  1393. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  1394. {
  1395. Starts the program in 'prog' and makes its input, output and error output the
  1396. other end of three pipes, which are the stdin, stdout and stderr of a program
  1397. specified in 'prog'.
  1398. StreamOut can be used to write to the program, StreamIn can be used to read
  1399. the output of the program, StreamErr reads the error output of the program.
  1400. See the following diagram :
  1401. Parent Child
  1402. StreamOut --> StdIn (input)
  1403. StreamIn <-- StdOut (output)
  1404. StreamErr <-- StdErr (error output)
  1405. }
  1406. var
  1407. PipeIn, PipeOut, PipeErr: text;
  1408. pid: LongInt;
  1409. pl: ^LongInt;
  1410. begin
  1411. LinuxError := 0;
  1412. AssignStream := -1;
  1413. // Assign pipes
  1414. AssignPipe(StreamIn, PipeOut);
  1415. if LinuxError <> 0 then exit;
  1416. AssignPipe(StreamErr, PipeErr);
  1417. if LinuxError <> 0 then begin
  1418. Close(StreamIn);
  1419. Close(PipeOut);
  1420. exit;
  1421. end;
  1422. AssignPipe(PipeIn, StreamOut);
  1423. if LinuxError <> 0 then begin
  1424. Close(StreamIn);
  1425. Close(PipeOut);
  1426. Close(StreamErr);
  1427. Close(PipeErr);
  1428. exit;
  1429. end;
  1430. // Fork
  1431. pid := Fork;
  1432. if LinuxError <> 0 then begin
  1433. Close(StreamIn);
  1434. Close(PipeOut);
  1435. Close(StreamErr);
  1436. Close(PipeErr);
  1437. Close(PipeIn);
  1438. Close(StreamOut);
  1439. exit;
  1440. end;
  1441. if pid = 0 then begin
  1442. // *** We are in the child ***
  1443. // Close what we don not need
  1444. Close(StreamOut);
  1445. Close(StreamIn);
  1446. Close(StreamErr);
  1447. // Connect pipes
  1448. dup2(PipeIn, Input);
  1449. if LinuxError <> 0 then Halt(127);
  1450. Close(PipeIn);
  1451. dup2(PipeOut, Output);
  1452. if LinuxError <> 0 then Halt(127);
  1453. Close(PipeOut);
  1454. dup2(PipeErr, StdErr);
  1455. if LinuxError <> 0 then Halt(127);
  1456. Close(PipeErr);
  1457. // Execute program
  1458. Execl(Prog);
  1459. Halt(127);
  1460. end else begin
  1461. // *** We are in the parent ***
  1462. Close(PipeErr);
  1463. Close(PipeOut);
  1464. Close(PipeIn);
  1465. // Save the process ID - needed when closing
  1466. pl := @(TextRec(StreamIn).userdata[2]);
  1467. pl^ := pid;
  1468. TextRec(StreamIn).closefunc := @PCloseText;
  1469. // Save the process ID - needed when closing
  1470. pl := @(TextRec(StreamOut).userdata[2]);
  1471. pl^ := pid;
  1472. TextRec(StreamOut).closefunc := @PCloseText;
  1473. // Save the process ID - needed when closing
  1474. pl := @(TextRec(StreamErr).userdata[2]);
  1475. pl^ := pid;
  1476. TextRec(StreamErr).closefunc := @PCloseText;
  1477. AssignStream := pid;
  1478. end;
  1479. end;
  1480. {******************************************************************************
  1481. General information calls
  1482. ******************************************************************************}
  1483. Function GetEnv(P:string):Pchar;
  1484. {
  1485. Searches the environment for a string with name p and
  1486. returns a pchar to it's value.
  1487. A pchar is used to accomodate for strings of length > 255
  1488. }
  1489. var
  1490. ep : ppchar;
  1491. found : boolean;
  1492. Begin
  1493. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1494. ep:=envp;
  1495. found:=false;
  1496. if ep<>nil then
  1497. begin
  1498. while (not found) and (ep^<>nil) do
  1499. begin
  1500. if strlcomp(@p[1],(ep^),length(p))=0 then
  1501. found:=true
  1502. else
  1503. inc(ep);
  1504. end;
  1505. end;
  1506. if found then
  1507. getenv:=ep^+length(p)
  1508. else
  1509. getenv:=nil;
  1510. end;
  1511. {$ifndef bsd}
  1512. Function GetDomainName:String;
  1513. {
  1514. Get machines domain name. Returns empty string if not set.
  1515. }
  1516. Var
  1517. Sysn : utsname;
  1518. begin
  1519. Uname(Sysn);
  1520. linuxerror:=errno;
  1521. If linuxerror<>0 then
  1522. getdomainname:=''
  1523. else
  1524. getdomainname:=strpas(@Sysn.domainname[0]);
  1525. end;
  1526. Function GetHostName:String;
  1527. {
  1528. Get machines name. Returns empty string if not set.
  1529. }
  1530. Var
  1531. Sysn : utsname;
  1532. begin
  1533. uname(Sysn);
  1534. linuxerror:=errno;
  1535. If linuxerror<>0 then
  1536. gethostname:=''
  1537. else
  1538. gethostname:=strpas(@Sysn.nodename[0]);
  1539. end;
  1540. {$endif}
  1541. {******************************************************************************
  1542. Signal handling calls
  1543. ******************************************************************************}
  1544. procedure SigRaise(sig:integer);
  1545. begin
  1546. Kill(GetPid,Sig);
  1547. end;
  1548. {******************************************************************************
  1549. IOCtl and Termios calls
  1550. ******************************************************************************}
  1551. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  1552. begin
  1553. {$ifndef BSD}
  1554. TCGetAttr:=IOCtl(fd,TCGETS,@tios);
  1555. {$else}
  1556. TCGETAttr:=IoCtl(Fd,TIOCGETA,@tios);
  1557. {$endif}
  1558. end;
  1559. Function TCSetAttr(fd:longint;OptAct:longint;var tios:TermIOS):boolean;
  1560. var
  1561. nr:longint;
  1562. begin
  1563. {$ifndef BSD}
  1564. case OptAct of
  1565. TCSANOW : nr:=TCSETS;
  1566. TCSADRAIN : nr:=TCSETSW;
  1567. TCSAFLUSH : nr:=TCSETSF;
  1568. {$else}
  1569. case OptAct of
  1570. TCSANOW : nr:=TIOCSETA;
  1571. TCSADRAIN : nr:=TIOCSETAW;
  1572. TCSAFLUSH : nr:=TIOCSETAF;
  1573. {$endif}
  1574. else
  1575. begin
  1576. ErrNo:=Sys_EINVAL;
  1577. TCSetAttr:=false;
  1578. exit;
  1579. end;
  1580. end;
  1581. TCSetAttr:=IOCtl(fd,nr,@Tios);
  1582. end;
  1583. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  1584. begin
  1585. {$ifndef BSD}
  1586. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  1587. {$else}
  1588. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  1589. {$endif}
  1590. end;
  1591. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  1592. begin
  1593. {$ifndef BSD}
  1594. CFSetISpeed(tios,speed);
  1595. {$else}
  1596. tios.c_ospeed:=speed;
  1597. {$endif}
  1598. end;
  1599. Procedure CFMakeRaw(var tios:TermIOS);
  1600. begin
  1601. {$ifndef BSD}
  1602. with tios do
  1603. begin
  1604. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  1605. INLCR or IGNCR or ICRNL or IXON));
  1606. c_oflag:=c_oflag and (not OPOST);
  1607. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  1608. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  1609. end;
  1610. {$else}
  1611. with tios do
  1612. begin
  1613. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  1614. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  1615. IGNPAR));
  1616. c_iflag:=c_iflag OR IGNBRK;
  1617. c_oflag:=c_oflag and (not OPOST);
  1618. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  1619. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  1620. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  1621. c_cc[VMIN]:=1;
  1622. c_cc[VTIME]:=0;
  1623. end;
  1624. {$endif}
  1625. end;
  1626. Function TCSendBreak(fd,duration:longint):boolean;
  1627. begin
  1628. {$ifndef BSD}
  1629. TCSendBreak:=IOCtl(fd,TCSBRK,pointer(duration));
  1630. {$else}
  1631. TCSendBreak:=IOCtl(fd,TIOCSBRK,0);
  1632. {$endif}
  1633. end;
  1634. Function TCSetPGrp(fd,id:longint):boolean;
  1635. begin
  1636. TCSetPGrp:=IOCtl(fd,TIOCSPGRP,pointer(id));
  1637. end;
  1638. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  1639. begin
  1640. TCGetPGrp:=IOCtl(fd,TIOCGPGRP,@id);
  1641. end;
  1642. Function TCDrain(fd:longint):boolean;
  1643. begin
  1644. {$ifndef BSD}
  1645. TCDrain:=IOCtl(fd,TCSBRK,pointer(1));
  1646. {$else}
  1647. TCDrain:=IOCtl(fd,TIOCDRAIN,0); {Should set timeout to 1 first?}
  1648. {$endif}
  1649. end;
  1650. Function TCFlow(fd,act:longint):boolean;
  1651. begin
  1652. {$ifndef BSD}
  1653. TCFlow:=IOCtl(fd,TCXONC,pointer(act));
  1654. {$else}
  1655. case act OF
  1656. TCOOFF : TCFlow:=Ioctl(fd,TIOCSTOP,0);
  1657. TCOOn : TCFlow:=IOctl(Fd,TIOCStart,0);
  1658. TCIOFF : {N/I}
  1659. end;
  1660. {$endif}
  1661. end;
  1662. Function TCFlush(fd,qsel:longint):boolean;
  1663. var com:longint;
  1664. begin
  1665. {$ifndef BSD}
  1666. TCFlush:=IOCtl(fd,TCFLSH,pointer(qsel));
  1667. {$else}
  1668. {
  1669. CASE Qsel of
  1670. TCIFLUSH : com:=fread;
  1671. TCOFLUSH : com:=FWRITE;
  1672. TCIOFLUSH: com:=FREAD OR FWRITE;
  1673. else
  1674. exit(false);
  1675. end;
  1676. }
  1677. TCFlush:=IOCtl(fd,TIOCFLUSH,pointer(qsel));
  1678. {$endif}
  1679. end;
  1680. Function IsATTY(Handle:Longint):Boolean;
  1681. {
  1682. Check if the filehandle described by 'handle' is a TTY (Terminal)
  1683. }
  1684. var
  1685. t : Termios;
  1686. begin
  1687. IsAtty:=TCGetAttr(Handle,t);
  1688. end;
  1689. Function IsATTY(f: text):Boolean;
  1690. {
  1691. Idem as previous, only now for text variables.
  1692. }
  1693. begin
  1694. IsATTY:=IsaTTY(textrec(f).handle);
  1695. end;
  1696. function TTYName(Handle:Longint):string;
  1697. {
  1698. Return the name of the current tty described by handle f.
  1699. returns empty string in case of an error.
  1700. }
  1701. Const
  1702. dev='/dev';
  1703. var
  1704. name : string;
  1705. st : stat;
  1706. mydev,
  1707. myino : longint;
  1708. dirstream : pdir;
  1709. d : pdirent;
  1710. begin
  1711. TTYName:='';
  1712. fstat(handle,st);
  1713. if (errno<>0) and isatty (handle) then
  1714. exit;
  1715. mydev:=st.dev;
  1716. myino:=st.ino;
  1717. dirstream:=opendir(dev);
  1718. if (linuxerror<>0) then
  1719. exit;
  1720. d:=Readdir(dirstream);
  1721. while (d<>nil) do
  1722. begin
  1723. if (d^.ino=myino) then
  1724. begin
  1725. name:=dev+'/'+strpas(@(d^.name));
  1726. fstat(name,st);
  1727. if (linuxerror=0) and (st.dev=mydev) then
  1728. begin
  1729. closedir(dirstream);
  1730. ttyname:=name;
  1731. exit;
  1732. end;
  1733. end;
  1734. d:=Readdir(dirstream);
  1735. end;
  1736. closedir(dirstream);
  1737. end;
  1738. function TTYName(var F:Text):string;
  1739. {
  1740. Idem as previous, only now for text variables;
  1741. }
  1742. begin
  1743. TTYName:=TTYName(textrec(f).handle);
  1744. end;
  1745. {******************************************************************************
  1746. Utility calls
  1747. ******************************************************************************}
  1748. Function Octal(l:longint):longint;
  1749. {
  1750. Convert an octal specified number to decimal;
  1751. }
  1752. var
  1753. octnr,
  1754. oct : longint;
  1755. begin
  1756. octnr:=0;
  1757. oct:=0;
  1758. while (l>0) do
  1759. begin
  1760. oct:=oct or ((l mod 10) shl octnr);
  1761. l:=l div 10;
  1762. inc(octnr,3);
  1763. end;
  1764. Octal:=oct;
  1765. end;
  1766. Function StringToPPChar(Var S:STring):ppchar;
  1767. {
  1768. Create a PPChar to structure of pchars which are the arguments specified
  1769. in the string S. Especially usefull for creating an ArgV for Exec-calls
  1770. }
  1771. var
  1772. nr : longint;
  1773. Buf : ^char;
  1774. p : ppchar;
  1775. begin
  1776. s:=s+#0;
  1777. buf:=@s[1];
  1778. nr:=0;
  1779. while(buf^<>#0) do
  1780. begin
  1781. while (buf^ in [' ',#8,#10]) do
  1782. inc(buf);
  1783. inc(nr);
  1784. while not (buf^ in [' ',#0,#8,#10]) do
  1785. inc(buf);
  1786. end;
  1787. getmem(p,nr*4);
  1788. StringToPPChar:=p;
  1789. if p=nil then
  1790. begin
  1791. LinuxError:=sys_enomem;
  1792. exit;
  1793. end;
  1794. buf:=@s[1];
  1795. while (buf^<>#0) do
  1796. begin
  1797. while (buf^ in [' ',#8,#10]) do
  1798. begin
  1799. buf^:=#0;
  1800. inc(buf);
  1801. end;
  1802. p^:=buf;
  1803. inc(p);
  1804. p^:=nil;
  1805. while not (buf^ in [' ',#0,#8,#10]) do
  1806. inc(buf);
  1807. end;
  1808. end;
  1809. Function FExpand(Const Path:PathStr):PathStr;
  1810. var
  1811. temp : pathstr;
  1812. i,j : longint;
  1813. p : pchar;
  1814. Begin
  1815. {Remove eventual drive - doesn't exist in Linux}
  1816. if path[2]=':' then
  1817. i:=3
  1818. else
  1819. i:=1;
  1820. temp:='';
  1821. {Replace ~/ with $HOME}
  1822. if (path[i]='~') and ((i+1>length(path)) or (path[i+1]='/')) then
  1823. begin
  1824. p:=getenv('HOME');
  1825. if not (p=nil) then
  1826. Insert(StrPas(p),temp,i);
  1827. i:=1;
  1828. temp:=temp+Copy(Path,2,255);
  1829. end;
  1830. {Do we have an absolute path ? No - prefix the current dir}
  1831. if temp='' then
  1832. begin
  1833. if path[i]<>'/' then
  1834. begin
  1835. {$I-}
  1836. getdir(0,temp);
  1837. {$I+}
  1838. if ioresult<>0 then;
  1839. end
  1840. else
  1841. inc(i);
  1842. temp:=temp+'/'+copy(path,i,length(path)-i+1)+'/';
  1843. end;
  1844. {First remove all references to '/./'}
  1845. while pos('/./',temp)<>0 do
  1846. delete(temp,pos('/./',temp),2);
  1847. {Now remove also all references to '/../' + of course previous dirs..}
  1848. repeat
  1849. i:=pos('/../',temp);
  1850. {Find the pos of the previous dir}
  1851. if i>1 then
  1852. begin
  1853. j:=i-1;
  1854. while (j>1) and (temp[j]<>'/') do
  1855. dec (j);{temp[1] is always '/'}
  1856. delete(temp,j,i-j+3);
  1857. end
  1858. else
  1859. if i=1 then {i=1, so we have temp='/../something', just delete '/../'}
  1860. delete(temp,1,3);
  1861. until i=0;
  1862. { Remove ending /.. }
  1863. i:=pos('/..',temp);
  1864. if (i<>0) and (i =length(temp)-2) then
  1865. begin
  1866. j:=i-1;
  1867. while (j>1) and (temp[j]<>'/') do
  1868. dec (j);
  1869. delete (temp,j,i-j+3);
  1870. end;
  1871. { if last character is / then remove it - dir is also a file :-) }
  1872. if (length(temp)>0) and (temp[length(temp)]='/') then
  1873. dec(byte(temp[0]));
  1874. fexpand:=temp;
  1875. End;
  1876. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  1877. {
  1878. Searches for a file 'path' in the list of direcories in 'dirlist'.
  1879. returns an empty string if not found. Wildcards are NOT allowed.
  1880. If dirlist is empty, it is set to '.'
  1881. }
  1882. Var
  1883. NewDir : PathStr;
  1884. p1 : Longint;
  1885. Info : Stat;
  1886. Begin
  1887. {Replace ':' with ';'}
  1888. for p1:=1to length(dirlist) do
  1889. if dirlist[p1]=':' then
  1890. dirlist[p1]:=';';
  1891. {Check for WildCards}
  1892. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  1893. FSearch:='' {No wildcards allowed in these things.}
  1894. Else
  1895. Begin
  1896. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  1897. Repeat
  1898. p1:=Pos(';',DirList);
  1899. If p1=0 Then
  1900. p1:=255;
  1901. NewDir:=Copy(DirList,1,P1 - 1);
  1902. if NewDir[Length(NewDir)]<>'/' then
  1903. NewDir:=NewDir+'/';
  1904. NewDir:=NewDir+Path;
  1905. Delete(DirList,1,p1);
  1906. if FStat(NewDir,Info) then
  1907. Begin
  1908. If Pos('./',NewDir)=1 Then
  1909. Delete(NewDir,1,2);
  1910. {DOS strips off an initial .\}
  1911. End
  1912. Else
  1913. NewDir:='';
  1914. Until (DirList='') or (Length(NewDir) > 0);
  1915. FSearch:=NewDir;
  1916. End;
  1917. End;
  1918. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  1919. Var
  1920. DotPos,SlashPos,i : longint;
  1921. Begin
  1922. SlashPos:=0;
  1923. DotPos:=256;
  1924. i:=Length(Path);
  1925. While (i>0) and (SlashPos=0) Do
  1926. Begin
  1927. If (DotPos=256) and (Path[i]='.') Then
  1928. begin
  1929. DotPos:=i;
  1930. end;
  1931. If (Path[i]='/') Then
  1932. SlashPos:=i;
  1933. Dec(i);
  1934. End;
  1935. Ext:=Copy(Path,DotPos,255);
  1936. Dir:=Copy(Path,1,SlashPos);
  1937. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  1938. End;
  1939. Function Dirname(Const path:pathstr):pathstr;
  1940. {
  1941. This function returns the directory part of a complete path.
  1942. Unless the directory is root '/', The last character is not
  1943. a slash.
  1944. }
  1945. var
  1946. Dir : PathStr;
  1947. Name : NameStr;
  1948. Ext : ExtStr;
  1949. begin
  1950. FSplit(Path,Dir,Name,Ext);
  1951. if length(Dir)>1 then
  1952. Delete(Dir,length(Dir),1);
  1953. DirName:=Dir;
  1954. end;
  1955. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  1956. {
  1957. This function returns the filename part of a complete path. If suf is
  1958. supplied, it is cut off the filename.
  1959. }
  1960. var
  1961. Dir : PathStr;
  1962. Name : NameStr;
  1963. Ext : ExtStr;
  1964. begin
  1965. FSplit(Path,Dir,Name,Ext);
  1966. if Suf<>Ext then
  1967. Name:=Name+Ext;
  1968. BaseName:=Name;
  1969. end;
  1970. Function FNMatch(const Pattern,Name:string):Boolean;
  1971. Var
  1972. LenPat,LenName : longint;
  1973. Function DoFNMatch(i,j:longint):Boolean;
  1974. Var
  1975. Found : boolean;
  1976. Begin
  1977. Found:=true;
  1978. While Found and (i<=LenPat) Do
  1979. Begin
  1980. Case Pattern[i] of
  1981. '?' : Found:=(j<=LenName);
  1982. '*' : Begin
  1983. {find the next character in pattern, different of ? and *}
  1984. while Found and (i<LenPat) do
  1985. begin
  1986. inc(i);
  1987. case Pattern[i] of
  1988. '*' : ;
  1989. '?' : begin
  1990. inc(j);
  1991. Found:=(j<=LenName);
  1992. end;
  1993. else
  1994. Found:=false;
  1995. end;
  1996. end;
  1997. {Now, find in name the character which i points to, if the * or ?
  1998. wasn't the last character in the pattern, else, use up all the
  1999. chars in name}
  2000. Found:=true;
  2001. if (i<=LenPat) then
  2002. begin
  2003. repeat
  2004. {find a letter (not only first !) which maches pattern[i]}
  2005. while (j<=LenName) and (name[j]<>pattern[i]) do
  2006. inc (j);
  2007. if (j<LenName) then
  2008. begin
  2009. if DoFnMatch(i+1,j+1) then
  2010. begin
  2011. i:=LenPat;
  2012. j:=LenName;{we can stop}
  2013. Found:=true;
  2014. end
  2015. else
  2016. inc(j);{We didn't find one, need to look further}
  2017. end;
  2018. until (j>=LenName);
  2019. end
  2020. else
  2021. j:=LenName;{we can stop}
  2022. end;
  2023. else {not a wildcard character in pattern}
  2024. Found:=(j<=LenName) and (pattern[i]=name[j]);
  2025. end;
  2026. inc(i);
  2027. inc(j);
  2028. end;
  2029. DoFnMatch:=Found and (j>LenName);
  2030. end;
  2031. Begin {start FNMatch}
  2032. LenPat:=Length(Pattern);
  2033. LenName:=Length(Name);
  2034. FNMatch:=DoFNMatch(1,1);
  2035. End;
  2036. Procedure Globfree(var p : pglob);
  2037. {
  2038. Release memory occupied by pglob structure, and names in it.
  2039. sets p to nil.
  2040. }
  2041. var
  2042. temp : pglob;
  2043. begin
  2044. while assigned(p) do
  2045. begin
  2046. temp:=p^.next;
  2047. if assigned(p^.name) then
  2048. freemem(p^.name);
  2049. dispose(p);
  2050. p:=temp;
  2051. end;
  2052. end;
  2053. Function Glob(Const path:pathstr):pglob;
  2054. {
  2055. Fills a tglob structure with entries matching path,
  2056. and returns a pointer to it. Returns nil on error,
  2057. linuxerror is set accordingly.
  2058. }
  2059. var
  2060. temp,
  2061. temp2 : string[255];
  2062. thedir : pdir;
  2063. buffer : pdirent;
  2064. root,
  2065. current : pglob;
  2066. begin
  2067. { Get directory }
  2068. temp:=dirname(path);
  2069. if temp='' then
  2070. temp:='.';
  2071. temp:=temp+#0;
  2072. thedir:=opendir(@temp[1]);
  2073. if thedir=nil then
  2074. begin
  2075. glob:=nil;
  2076. linuxerror:=errno;
  2077. exit;
  2078. end;
  2079. temp:=basename(path,''); { get the pattern }
  2080. if thedir^.fd<0 then
  2081. begin
  2082. linuxerror:=errno;
  2083. glob:=nil;
  2084. exit;
  2085. end;
  2086. {get the entries}
  2087. root:=nil;
  2088. current:=nil;
  2089. repeat
  2090. buffer:=Sys_readdir(thedir);
  2091. if buffer=nil then
  2092. break;
  2093. temp2:=strpas(@(buffer^.name[0]));
  2094. if fnmatch(temp,temp2) then
  2095. begin
  2096. if root=nil then
  2097. begin
  2098. new(root);
  2099. current:=root;
  2100. end
  2101. else
  2102. begin
  2103. new(current^.next);
  2104. current:=current^.next;
  2105. end;
  2106. if current=nil then
  2107. begin
  2108. linuxerror:=Sys_ENOMEM;
  2109. globfree(root);
  2110. break;
  2111. end;
  2112. current^.next:=nil;
  2113. getmem(current^.name,length(temp2)+1);
  2114. if current^.name=nil then
  2115. begin
  2116. linuxerror:=Sys_ENOMEM;
  2117. globfree(root);
  2118. break;
  2119. end;
  2120. move(buffer^.name[0],current^.name^,length(temp2)+1);
  2121. end;
  2122. until false;
  2123. closedir(thedir);
  2124. glob:=root;
  2125. end;
  2126. {--------------------------------
  2127. FiledescriptorSets
  2128. --------------------------------}
  2129. Procedure FD_Zero(var fds:fdSet);
  2130. {
  2131. Clear the set of filedescriptors
  2132. }
  2133. begin
  2134. FillChar(fds,sizeof(fdSet),0);
  2135. end;
  2136. Procedure FD_Clr(fd:longint;var fds:fdSet);
  2137. {
  2138. Remove fd from the set of filedescriptors
  2139. }
  2140. begin
  2141. fds[fd shr 5]:=fds[fd shr 5] and (not (1 shl (fd and 31)));
  2142. end;
  2143. Procedure FD_Set(fd:longint;var fds:fdSet);
  2144. {
  2145. Add fd to the set of filedescriptors
  2146. }
  2147. begin
  2148. fds[fd shr 5]:=fds[fd shr 5] or (1 shl (fd and 31));
  2149. end;
  2150. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  2151. {
  2152. Test if fd is part of the set of filedescriptors
  2153. }
  2154. begin
  2155. FD_IsSet:=((fds[fd shr 5] and (1 shl (fd and 31)))<>0);
  2156. end;
  2157. Function GetFS (var T:Text):longint;
  2158. {
  2159. Get File Descriptor of a text file.
  2160. }
  2161. begin
  2162. if textrec(t).mode=fmclosed then
  2163. exit(-1)
  2164. else
  2165. GETFS:=textrec(t).Handle
  2166. end;
  2167. Function GetFS(Var F:File):longint;
  2168. {
  2169. Get File Descriptor of an unTyped file.
  2170. }
  2171. begin
  2172. { Handle and mode are on the same place in textrec and filerec. }
  2173. if filerec(f).mode=fmclosed then
  2174. exit(-1)
  2175. else
  2176. GETFS:=filerec(f).Handle
  2177. end;
  2178. {--------------------------------
  2179. Stat.Mode Macro's
  2180. --------------------------------}
  2181. Function S_ISLNK(m:word):boolean;
  2182. {
  2183. Check mode field of inode for link.
  2184. }
  2185. begin
  2186. S_ISLNK:=(m and STAT_IFMT)=STAT_IFLNK;
  2187. end;
  2188. Function S_ISREG(m:word):boolean;
  2189. {
  2190. Check mode field of inode for regular file.
  2191. }
  2192. begin
  2193. S_ISREG:=(m and STAT_IFMT)=STAT_IFREG;
  2194. end;
  2195. Function S_ISDIR(m:word):boolean;
  2196. {
  2197. Check mode field of inode for directory.
  2198. }
  2199. begin
  2200. S_ISDIR:=(m and STAT_IFMT)=STAT_IFDIR;
  2201. end;
  2202. Function S_ISCHR(m:word):boolean;
  2203. {
  2204. Check mode field of inode for character device.
  2205. }
  2206. begin
  2207. S_ISCHR:=(m and STAT_IFMT)=STAT_IFCHR;
  2208. end;
  2209. Function S_ISBLK(m:word):boolean;
  2210. {
  2211. Check mode field of inode for block device.
  2212. }
  2213. begin
  2214. S_ISBLK:=(m and STAT_IFMT)=STAT_IFBLK;
  2215. end;
  2216. Function S_ISFIFO(m:word):boolean;
  2217. {
  2218. Check mode field of inode for named pipe (FIFO).
  2219. }
  2220. begin
  2221. S_ISFIFO:=(m and STAT_IFMT)=STAT_IFIFO;
  2222. end;
  2223. Function S_ISSOCK(m:word):boolean;
  2224. {
  2225. Check mode field of inode for socket.
  2226. }
  2227. begin
  2228. S_ISSOCK:=(m and STAT_IFMT)=STAT_IFSOCK;
  2229. end;
  2230. {--------------------------------
  2231. Memory functions
  2232. --------------------------------}
  2233. Initialization
  2234. InitLocalTime;
  2235. finalization
  2236. DoneLocalTime;
  2237. End.
  2238. {
  2239. $Log$
  2240. Revision 1.4 2000-10-11 13:59:16 marco
  2241. * FreeBSD TermIOS support and minor changes to some related files.
  2242. Revision 1.3 2000/10/10 12:02:35 marco
  2243. * Terminal stuff of Linux moved to separate file. I think it is too
  2244. much to do with ifdefs.
  2245. Revision 1.2 2000/09/18 13:14:50 marco
  2246. * Global Linux +bsd to (rtl/freebsd rtl/unix rtl/linux structure)
  2247. Revision 1.7 2000/09/12 08:51:43 marco
  2248. * fixed some small problems left from merging. (waitpid has now last param longint)
  2249. Revision 1.6 2000/09/11 14:05:31 marco
  2250. * FreeBSD support and removed old signalhandling
  2251. Revision 1.5 2000/09/06 20:47:34 peter
  2252. * removed previous fsplit() patch as it's not the correct behaviour for
  2253. LFNs. The code showing the bug could easily be adapted (merged)
  2254. Revision 1.4 2000/09/04 20:17:53 peter
  2255. * fixed previous commit (merged)
  2256. Revision 1.3 2000/09/04 19:38:13 peter
  2257. * fsplit with .. fix from Thomas (merged)
  2258. Revision 1.2 2000/07/13 11:33:48 michael
  2259. + removed logs
  2260. }