linuxold.inc 72 KB

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