linux.pp 70 KB

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