linuxold.inc 72 KB

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