linux.pp 70 KB

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