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