linux.pp 72 KB

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