unix.pp 70 KB

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