linux.pp 68 KB

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