linux.pp 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,97 by Michael Van Canneyt,
  5. member of the Free Pascal development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY;without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. Unit Linux;
  13. Interface
  14. { Get Types and Constants }
  15. {$i sysconst.inc}
  16. {$i systypes.inc}
  17. { Get System call numbers and error-numbers}
  18. {$i sysnr.inc}
  19. {$i errno.inc}
  20. var
  21. ErrNo,
  22. LinuxError : Longint;
  23. {********************
  24. Process
  25. ********************}
  26. const
  27. { cloning flags }
  28. CSIGNAL = $000000ff; // signal mask to be sent at exit
  29. CLONE_VM = $00000100; // set if VM shared between processes
  30. CLONE_FS = $00000200; // set if fs info shared between processes
  31. CLONE_FILES = $00000400; // set if open files shared between processes
  32. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  33. CLONE_PID = $00001000; // set if pid shared
  34. type
  35. TCloneFunc=function(args:pointer):longint;cdecl;
  36. const
  37. { For getting/setting priority }
  38. Prio_Process = 0;
  39. Prio_PGrp = 1;
  40. Prio_User = 2;
  41. WNOHANG = $1;
  42. WUNTRACED = $2;
  43. __WCLONE = $80000000;
  44. {********************
  45. File
  46. ********************}
  47. Const
  48. P_IN = 1;
  49. P_OUT = 2;
  50. Const
  51. LOCK_SH = 1;
  52. LOCK_EX = 2;
  53. LOCK_UN = 8;
  54. LOCK_NB = 4;
  55. Type
  56. Tpipe = array[1..2] of longint;
  57. pglob = ^tglob;
  58. tglob = record
  59. name : pchar;
  60. next : pglob;
  61. end;
  62. ComStr = String[255];
  63. PathStr = String[255];
  64. DirStr = String[255];
  65. NameStr = String[255];
  66. ExtStr = String[255];
  67. const
  68. { For testing access rights }
  69. R_OK = 4;
  70. W_OK = 2;
  71. X_OK = 1;
  72. F_OK = 0;
  73. { For File control mechanism }
  74. F_GetFd = 1;
  75. F_SetFd = 2;
  76. F_GetFl = 3;
  77. F_SetFl = 4;
  78. F_GetLk = 5;
  79. F_SetLk = 6;
  80. F_SetLkW = 7;
  81. F_GetOwn = 8;
  82. F_SetOwn = 9;
  83. {********************
  84. Signal
  85. ********************}
  86. Const
  87. { For sending a signal }
  88. SA_NOCLDSTOP = 1;
  89. SA_SHIRQ = $04000000;
  90. SA_STACK = $08000000;
  91. SA_RESTART = $10000000;
  92. SA_INTERRUPT = $20000000;
  93. SA_NOMASK = $40000000;
  94. SA_ONESHOT = $80000000;
  95. SIG_BLOCK = 0;
  96. SIG_UNBLOCK = 1;
  97. SIG_SETMASK = 2;
  98. SIG_DFL = 0 ;
  99. SIG_IGN = 1 ;
  100. SIG_ERR = -1 ;
  101. SIGHUP = 1;
  102. SIGINT = 2;
  103. SIGQUIT = 3;
  104. SIGILL = 4;
  105. SIGTRAP = 5;
  106. SIGABRT = 6;
  107. SIGIOT = 6;
  108. SIGBUS = 7;
  109. SIGFPE = 8;
  110. SIGKILL = 9;
  111. SIGUSR1 = 10;
  112. SIGSEGV = 11;
  113. SIGUSR2 = 12;
  114. SIGPIPE = 13;
  115. SIGALRM = 14;
  116. SIGTerm = 15;
  117. SIGSTKFLT = 16;
  118. SIGCHLD = 17;
  119. SIGCONT = 18;
  120. SIGSTOP = 19;
  121. SIGTSTP = 20;
  122. SIGTTIN = 21;
  123. SIGTTOU = 22;
  124. SIGURG = 23;
  125. SIGXCPU = 24;
  126. SIGXFSZ = 25;
  127. SIGVTALRM = 26;
  128. SIGPROF = 27;
  129. SIGWINCH = 28;
  130. SIGIO = 29;
  131. SIGPOLL = SIGIO;
  132. SIGPWR = 30;
  133. SIGUNUSED = 31;
  134. Type
  135. SignalHandler = Procedure(Sig : LongInt);cdecl;
  136. PSignalHandler = ^SignalHandler;
  137. SignalRestorer = Procedure;cdecl;
  138. PSignalRestorer = ^SignalRestorer;
  139. SigSet = Integer;
  140. PSigSet = ^SigSet;
  141. SigActionRec = packed record
  142. Sa_Handler : SignalHandler;
  143. Sa_Mask : longint;
  144. Sa_Flags : Integer;
  145. Sa_restorer : SignalRestorer;{ Obsolete - Don't use }
  146. end;
  147. PSigActionRec = ^SigActionRec;
  148. {********************
  149. IOCtl(TermIOS)
  150. ********************}
  151. Const
  152. { Amount of Control Chars }
  153. NCCS = 19;
  154. NCC = 8;
  155. { For Terminal handling }
  156. TCGETS = $5401;
  157. TCSETS = $5402;
  158. TCSETSW = $5403;
  159. TCSETSF = $5404;
  160. TCGETA = $5405;
  161. TCSETA = $5406;
  162. TCSETAW = $5407;
  163. TCSETAF = $5408;
  164. TCSBRK = $5409;
  165. TCXONC = $540A;
  166. TCFLSH = $540B;
  167. TIOCEXCL = $540C;
  168. TIOCNXCL = $540D;
  169. TIOCSCTTY = $540E;
  170. TIOCGPGRP = $540F;
  171. TIOCSPGRP = $5410;
  172. TIOCOUTQ = $5411;
  173. TIOCSTI = $5412;
  174. TIOCGWINSZ = $5413;
  175. TIOCSWINSZ = $5414;
  176. TIOCMGET = $5415;
  177. TIOCMBIS = $5416;
  178. TIOCMBIC = $5417;
  179. TIOCMSET = $5418;
  180. TIOCGSOFTCAR = $5419;
  181. TIOCSSOFTCAR = $541A;
  182. FIONREAD = $541B;
  183. TIOCINQ = FIONREAD;
  184. TIOCLINUX = $541C;
  185. TIOCCONS = $541D;
  186. TIOCGSERIAL = $541E;
  187. TIOCSSERIAL = $541F;
  188. TIOCPKT = $5420;
  189. FIONBIO = $5421;
  190. TIOCNOTTY = $5422;
  191. TIOCSETD = $5423;
  192. TIOCGETD = $5424;
  193. TCSBRKP = $5425;
  194. TIOCTTYGSTRUCT = $5426;
  195. FIONCLEX = $5450;
  196. FIOCLEX = $5451;
  197. FIOASYNC = $5452;
  198. TIOCSERCONFIG = $5453;
  199. TIOCSERGWILD = $5454;
  200. TIOCSERSWILD = $5455;
  201. TIOCGLCKTRMIOS = $5456;
  202. TIOCSLCKTRMIOS = $5457;
  203. TIOCSERGSTRUCT = $5458;
  204. TIOCSERGETLSR = $5459;
  205. TIOCSERGETMULTI = $545A;
  206. TIOCSERSETMULTI = $545B;
  207. TIOCMIWAIT = $545C;
  208. TIOCGICOUNT = $545D;
  209. TIOCPKT_DATA = 0;
  210. TIOCPKT_FLUSHREAD = 1;
  211. TIOCPKT_FLUSHWRITE = 2;
  212. TIOCPKT_STOP = 4;
  213. TIOCPKT_START = 8;
  214. TIOCPKT_NOSTOP = 16;
  215. TIOCPKT_DOSTOP = 32;
  216. Type
  217. winsize = packed record
  218. ws_row,
  219. ws_col,
  220. ws_xpixel,
  221. ws_ypixel : word;
  222. end;
  223. TWinSize=winsize;
  224. Termio = packed record
  225. c_iflag, { input mode flags }
  226. c_oflag, { output mode flags }
  227. c_cflag, { control mode flags }
  228. c_lflag : Word; { local mode flags }
  229. c_line : Word; { line discipline - careful, only High byte in use}
  230. c_cc : array [0..NCC-1] of char;{ control characters }
  231. end;
  232. TTermio=Termio;
  233. Termios = packed record
  234. c_iflag,
  235. c_oflag,
  236. c_cflag,
  237. c_lflag : longint;
  238. c_line : char;
  239. c_cc : array[0..NCCS-1] of byte;
  240. end;
  241. TTermios=Termios;
  242. const
  243. InitCC:array[0..NCCS-1] of byte=(3,34,177,25,4,0,1,0,21,23,32,0,22,17,27,26,0,0,0);
  244. const
  245. {c_cc characters}
  246. VINTR = 0;
  247. VQUIT = 1;
  248. VERASE = 2;
  249. VKILL = 3;
  250. VEOF = 4;
  251. VTIME = 5;
  252. VMIN = 6;
  253. VSWTC = 7;
  254. VSTART = 8;
  255. VSTOP = 9;
  256. VSUSP = 10;
  257. VEOL = 11;
  258. VREPRINT = 12;
  259. VDISCARD = 13;
  260. VWERASE = 14;
  261. VLNEXT = 15;
  262. VEOL2 = 16;
  263. {c_iflag bits}
  264. IGNBRK = $0000001;
  265. BRKINT = $0000002;
  266. IGNPAR = $0000004;
  267. PARMRK = $0000008;
  268. INPCK = $0000010;
  269. ISTRIP = $0000020;
  270. INLCR = $0000040;
  271. IGNCR = $0000080;
  272. ICRNL = $0000100;
  273. IUCLC = $0000200;
  274. IXON = $0000400;
  275. IXANY = $0000800;
  276. IXOFF = $0001000;
  277. IMAXBEL = $0002000;
  278. {c_oflag bits}
  279. OPOST = $0000001;
  280. OLCUC = $0000002;
  281. ONLCR = $0000004;
  282. OCRNL = $0000008;
  283. ONOCR = $0000010;
  284. ONLRET = $0000020;
  285. OFILL = $0000040;
  286. OFDEL = $0000080;
  287. NLDLY = $0000100;
  288. NL0 = $0000000;
  289. NL1 = $0000100;
  290. CRDLY = $0000600;
  291. CR0 = $0000000;
  292. CR1 = $0000200;
  293. CR2 = $0000400;
  294. CR3 = $0000600;
  295. TABDLY = $0001800;
  296. TAB0 = $0000000;
  297. TAB1 = $0000800;
  298. TAB2 = $0001000;
  299. TAB3 = $0001800;
  300. XTABS = $0001800;
  301. BSDLY = $0002000;
  302. BS0 = $0000000;
  303. BS1 = $0002000;
  304. VTDLY = $0004000;
  305. VT0 = $0000000;
  306. VT1 = $0004000;
  307. FFDLY = $0008000;
  308. FF0 = $0000000;
  309. FF1 = $0008000;
  310. {c_cflag bits}
  311. CBAUD = $000100F;
  312. B0 = $0000000;
  313. B50 = $0000001;
  314. B75 = $0000002;
  315. B110 = $0000003;
  316. B134 = $0000004;
  317. B150 = $0000005;
  318. B200 = $0000006;
  319. B300 = $0000007;
  320. B600 = $0000008;
  321. B1200 = $0000009;
  322. B1800 = $000000A;
  323. B2400 = $000000B;
  324. B4800 = $000000C;
  325. B9600 = $000000D;
  326. B19200 = $000000E;
  327. B38400 = $000000F;
  328. EXTA = B19200;
  329. EXTB = B38400;
  330. CSIZE = $0000030;
  331. CS5 = $0000000;
  332. CS6 = $0000010;
  333. CS7 = $0000020;
  334. CS8 = $0000030;
  335. CSTOPB = $0000040;
  336. CREAD = $0000080;
  337. PARENB = $0000100;
  338. PARODD = $0000200;
  339. HUPCL = $0000400;
  340. CLOCAL = $0000800;
  341. CBAUDEX = $0001000;
  342. B57600 = $0001001;
  343. B115200 = $0001002;
  344. B230400 = $0001003;
  345. B460800 = $0001004;
  346. CIBAUD = $100F0000;
  347. CMSPAR = $40000000;
  348. CRTSCTS = $80000000;
  349. {c_lflag bits}
  350. ISIG = $0000001;
  351. ICANON = $0000002;
  352. XCASE = $0000004;
  353. ECHO = $0000008;
  354. ECHOE = $0000010;
  355. ECHOK = $0000020;
  356. ECHONL = $0000040;
  357. NOFLSH = $0000080;
  358. TOSTOP = $0000100;
  359. ECHOCTL = $0000200;
  360. ECHOPRT = $0000400;
  361. ECHOKE = $0000800;
  362. FLUSHO = $0001000;
  363. PENDIN = $0004000;
  364. IEXTEN = $0008000;
  365. {c_line bits}
  366. TIOCM_LE = $001;
  367. TIOCM_DTR = $002;
  368. TIOCM_RTS = $004;
  369. TIOCM_ST = $008;
  370. TIOCM_SR = $010;
  371. TIOCM_CTS = $020;
  372. TIOCM_CAR = $040;
  373. TIOCM_RNG = $080;
  374. TIOCM_DSR = $100;
  375. TIOCM_CD = TIOCM_CAR;
  376. TIOCM_RI = TIOCM_RNG;
  377. TIOCM_OUT1 = $2000;
  378. TIOCM_OUT2 = $4000;
  379. {TCSetAttr}
  380. TCSANOW = 0;
  381. TCSADRAIN = 1;
  382. TCSAFLUSH = 2;
  383. {TCFlow}
  384. TCOOFF = 0;
  385. TCOON = 1;
  386. TCIOFF = 2;
  387. TCION = 3;
  388. {TCFlush}
  389. TCIFLUSH = 0;
  390. TCOFLUSH = 1;
  391. TCIOFLUSH = 2;
  392. {********************
  393. Info
  394. ********************}
  395. Type
  396. UTimBuf = packed record
  397. actime,modtime : Longint;
  398. end;
  399. UTimeBuf=UTimBuf;
  400. TUTimeBuf=UTimeBuf;
  401. PUTimeBuf=^UTimeBuf;
  402. TSysinfo = packed record
  403. uptime : longint;
  404. loads : array[1..3] of longint;
  405. totalram,
  406. freeram,
  407. sharedram,
  408. bufferram,
  409. totalswap,
  410. freeswap : longint;
  411. procs : integer;
  412. s : string[18];
  413. end;
  414. PSysInfo = ^TSysInfo;
  415. {******************************************************************************
  416. Procedure/Functions
  417. ******************************************************************************}
  418. Function SysCall(callnr:longint;var regs:SysCallregs):longint;
  419. {**************************
  420. Time/Date Handling
  421. ***************************}
  422. Function GetEpochTime:longint;
  423. Procedure GetTimeOfDay(var tv:timeval;var tz:timezone);
  424. Procedure SetTimeOfDay(Const tv:timeval;const tz:timezone);
  425. Function GetTimeOfDay: longint;
  426. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  427. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  428. Procedure GetTime(Var Hour,Minute,Second:Word);
  429. Procedure GetDate(Var Year,Month,Day:Word);
  430. { For compatibility with earlier versions }
  431. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Integer);
  432. Function LocalToEpoch(year,month,day,hour,minute,second:Integer):Longint;
  433. Procedure GetTime(Var Hour,Minute,Second:Integer);
  434. Procedure GetDate(Var Year,Month,Day:Integer);
  435. {**************************
  436. Process Handling
  437. ***************************}
  438. function CreateShellArgV(const prog:string):ppchar;
  439. Procedure Execve(Path:pathstr;args:ppchar;ep:ppchar);
  440. Procedure Execve(path:pchar;args:ppchar;ep:ppchar);
  441. Procedure Execv(const path:pathstr;args:ppchar);
  442. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  443. Procedure Execl(const Todo:string);
  444. Procedure Execle(Todo:string;Ep:ppchar);
  445. Procedure Execlp(Todo:string;Ep:ppchar);
  446. Function Shell(const Command:String):Longint;
  447. Function Fork:longint;
  448. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  449. Procedure ExitProcess(val:longint);
  450. Function WaitPid(Pid:longint;Status:pointer;Options:Integer):Longint;
  451. Procedure Nice(N:integer);
  452. Function GetPriority(Which,Who:Integer):integer;
  453. Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
  454. Function GetPid:LongInt;
  455. Function GetPPid:LongInt;
  456. Function GetUid:Longint;
  457. Function GetEUid:Longint;
  458. Function GetGid:Longint;
  459. Function GetEGid:Longint;
  460. {**************************
  461. File Handling
  462. ***************************}
  463. Function fdOpen(pathname:string;flags:longint):longint;
  464. Function fdOpen(pathname:string;flags,mode:longint):longint;
  465. Function fdOpen(pathname:pchar;flags:longint):longint;
  466. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  467. Function fdClose(fd:longint):boolean;
  468. Function fdRead(fd:longint;var buf;size:longint):longint;
  469. Function fdWrite(fd:longint;var buf;size:longint):longint;
  470. Function fdTruncate(fd,size:longint):boolean;
  471. Function fdSeek (fd,pos,seektype :longint): longint;
  472. Function fdFlush (fd : Longint) : Boolean;
  473. Function Link(OldPath,NewPath:pathstr):boolean;
  474. Function SymLink(OldPath,NewPath:pathstr):boolean;
  475. Function UnLink(Path:pathstr):boolean;
  476. Function UnLink(Path:pchar):Boolean;
  477. Function FReName (OldName,NewName : Pchar) : Boolean;
  478. Function FReName (OldName,NewName : String) : Boolean;
  479. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  480. Function Chmod(path:pathstr;Newmode:longint):boolean;
  481. Function Utime(path:pathstr;utim:utimebuf):boolean;
  482. Function Access(Path:Pathstr ;mode:integer):boolean;
  483. Function Umask(Mask:Integer):integer;
  484. Function Flock (fd,mode : longint) : boolean;
  485. Function Flock (var T : text;mode : longint) : boolean;
  486. Function Flock (var F : File;mode : longint) : boolean;
  487. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  488. Function FStat(Fd:longint;Var Info:stat):Boolean;
  489. Function FStat(var F:Text;Var Info:stat):Boolean;
  490. Function FStat(var F:File;Var Info:stat):Boolean;
  491. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  492. Function FSStat(Path:Pathstr;Var Info:statfs):Boolean;
  493. Function FSStat(Fd: Longint;Var Info:statfs):Boolean;
  494. Function Fcntl(var Fd:Text;Cmd:Integer):integer;
  495. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  496. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  497. Function Dup(var oldfile,newfile:text):Boolean;
  498. Function Dup(var oldfile,newfile:file):Boolean;
  499. Function Dup2(oldfile,newfile:longint):Boolean;
  500. Function Dup2(var oldfile,newfile:text):Boolean;
  501. Function Dup2(var oldfile,newfile:file):Boolean;
  502. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
  503. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  504. Function SelectText(var T:Text;TimeOut :PTimeVal):Longint;
  505. {**************************
  506. Directory Handling
  507. ***************************}
  508. Function OpenDir(f:pchar):pdir;
  509. Function OpenDir(f: String):pdir;
  510. function CloseDir(p:pdir):integer;
  511. Function ReadDir(p:pdir):pdirent;
  512. procedure SeekDir(p:pdir;off:longint);
  513. function TellDir(p:pdir):longint;
  514. {**************************
  515. Pipe/Fifo/Stream
  516. ***************************}
  517. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  518. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  519. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  520. Function PClose(Var F:text) : longint;
  521. Function PClose(Var F:file) : longint;
  522. Procedure POpen(var F:text;const Prog:String;rw:char);
  523. Procedure POpen(var F:file;const Prog:String;rw:char);
  524. Function mkFifo(pathname:string;mode:longint):boolean;
  525. Procedure AssignStream(Var StreamIn,Streamout:text;Const Prog:String);
  526. {**************************
  527. General information
  528. ***************************}
  529. Function GetDomainName:String;
  530. Function GetHostName:String;
  531. Function GetEnv(P:string):Pchar;
  532. Function Sysinfo(var Info:TSysinfo):Boolean;
  533. Function Uname(var unamerec:utsname):Boolean;
  534. {**************************
  535. Signal
  536. ***************************}
  537. Procedure SigAction(Signum:Integer;Var Act,OldAct:PSigActionRec );
  538. Procedure SigProcMask (How:Integer;SSet,OldSSet:PSigSet);
  539. Function SigPending:SigSet;
  540. Procedure SigSuspend(Mask:Sigset);
  541. Function Signal(Signum:Integer;Handler:SignalHandler):SignalHandler;
  542. Function Kill(Pid:longint;Sig:integer):integer;
  543. Procedure SigRaise(Sig:integer);
  544. {**************************
  545. IOCtl/Termios Functions
  546. ***************************}
  547. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  548. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  549. Function TCSetAttr(fd:longint;OptAct:longint;var tios:TermIOS):boolean;
  550. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  551. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  552. Procedure CFMakeRaw(var tios:TermIOS);
  553. Function TCSendBreak(fd,duration:longint):boolean;
  554. Function TCSetPGrp(fd,id:longint):boolean;
  555. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  556. Function TCFlush(fd,qsel:longint):boolean;
  557. Function TCDrain(fd:longint):boolean;
  558. Function TCFlow(fd,act:longint):boolean;
  559. Function IsATTY(Handle:Longint):Boolean;
  560. Function IsATTY(f:text):Boolean;
  561. function TTYname(Handle:Longint):string;
  562. function TTYname(var F:Text):string;
  563. {**************************
  564. Port IO functions
  565. ***************************}
  566. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  567. {$IFDEF I386}
  568. Procedure WritePort (Port : Longint; Value : Byte);
  569. Procedure WritePort (Port : Longint; Value : Word);
  570. Procedure WritePort (Port : Longint; Value : Longint);
  571. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  572. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  573. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  574. Procedure ReadPort (Port : Longint; Var Value : Byte);
  575. Procedure ReadPort (Port : Longint; Var Value : Word);
  576. Procedure ReadPort (Port : Longint; Var Value : Longint);
  577. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  578. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  579. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  580. {$ENDIF}
  581. {**************************
  582. Utility functions
  583. ***************************}
  584. Function Octal(l:longint):longint;
  585. Function FExpand(Const Path: PathStr):PathStr;
  586. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  587. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  588. Function Dirname(Const path:pathstr):pathstr;
  589. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  590. Function FNMatch(const Pattern,Name:string):Boolean;
  591. Function Glob(Const path:pathstr):pglob;
  592. Procedure Globfree(var p:pglob);
  593. Function StringToPPChar(Var S:STring):ppchar;
  594. Function GetFS(var T:Text):longint;
  595. Function GetFS(Var F:File):longint;
  596. {Filedescriptorsets}
  597. Procedure FD_Zero(var fds:fdSet);
  598. Procedure FD_Clr(fd:longint;var fds:fdSet);
  599. Procedure FD_Set(fd:longint;var fds:fdSet);
  600. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  601. {Stat.Mode Types}
  602. Function S_ISLNK(m:word):boolean;
  603. Function S_ISREG(m:word):boolean;
  604. Function S_ISDIR(m:word):boolean;
  605. Function S_ISCHR(m:word):boolean;
  606. Function S_ISBLK(m:word):boolean;
  607. Function S_ISFIFO(m:word):boolean;
  608. Function S_ISSOCK(m:word):boolean;
  609. {******************************************************************************
  610. Implementation
  611. ******************************************************************************}
  612. Implementation
  613. Uses Strings;
  614. var
  615. LocalTZ:TimeZone;
  616. { Get the definitions of textrec and filerec }
  617. {$i textrec.inc}
  618. {$i filerec.inc}
  619. { Raw System calls are in Syscalls.inc}
  620. {$i syscalls.inc}
  621. {******************************************************************************
  622. Process related calls
  623. ******************************************************************************}
  624. function CreateShellArgV(const prog:string):ppchar;
  625. {
  626. Create an argv which executes a command in a shell using /bin/sh -c
  627. }
  628. var
  629. pp,p : ppchar;
  630. temp : string;
  631. begin
  632. getmem(pp,4*4);
  633. temp:='/bin/sh'#0'-c'#0+prog+#0;
  634. pp^:=@temp[1];
  635. p:=pp+4;
  636. p^:=@temp[9];
  637. p:=p+4;
  638. p^:=@temp[12];
  639. p:=p+4;
  640. p^:=Nil;
  641. CreateShellArgV:=pp;
  642. end;
  643. Function Fork:longint;
  644. {
  645. This function issues the 'fork' System call. the program is duplicated in memory
  646. and Execution continues in parent and child process.
  647. In the parent process, fork returns the PID of the child. In the child process,
  648. zero is returned.
  649. A negative value indicates that an error has occurred, the error is returned in
  650. LinuxError.
  651. }
  652. var
  653. regs:SysCallregs;
  654. begin
  655. Fork:=SysCall(SysCall_nr_fork,regs);
  656. LinuxError:=Errno;
  657. End;
  658. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  659. begin
  660. if (pointer(func)=nil) or (sp=nil) then
  661. begin
  662. LinuxError:=Sys_EInval;
  663. exit;
  664. end;
  665. asm
  666. { Insert the argument onto the new stack. }
  667. movl sp,%ecx
  668. subl $8,%ecx
  669. movl args,%eax
  670. movl %eax,4(%ecx)
  671. { Save the function pointer as the zeroth argument.
  672. It will be popped off in the child in the ebx frobbing below. }
  673. movl func,%eax
  674. movl %eax,0(%ecx)
  675. { Do the system call }
  676. pushl %ebx
  677. movl flags,%ebx
  678. movl SysCall_nr_clone,%eax
  679. int $0x80
  680. popl %ebx
  681. test %eax,%eax
  682. jnz .Lclone_end
  683. { We're in the new thread }
  684. subl %ebp,%ebp { terminate the stack frame }
  685. call *%ebx
  686. { exit process }
  687. movl %eax,%ebx
  688. movl $1,%eax
  689. int $0x80
  690. .Lclone_end:
  691. movl %eax,__RESULT
  692. end;
  693. end;
  694. Procedure Execve(path:pathstr;args:ppchar;ep:ppchar);
  695. {
  696. Replaces the current program by the program specified in path,
  697. arguments in args are passed to Execve.
  698. environment specified in ep is passed on.
  699. }
  700. var
  701. regs:SysCallregs;
  702. begin
  703. path:=path+#0;
  704. regs.reg2:=longint(@path[1]);
  705. regs.reg3:=longint(args);
  706. regs.reg4:=longint(ep);
  707. SysCall(SysCall_nr_Execve,regs);
  708. { This only gets set when the call fails, otherwise we don't get here ! }
  709. Linuxerror:=errno;
  710. end;
  711. Procedure Execve(path:pchar;args:ppchar;ep:ppchar);
  712. {
  713. Replaces the current program by the program specified in path,
  714. arguments in args are passed to Execve.
  715. environment specified in ep is passed on.
  716. }
  717. var
  718. regs:SysCallregs;
  719. begin
  720. regs.reg2:=longint(path);
  721. regs.reg3:=longint(args);
  722. regs.reg4:=longint(ep);
  723. SysCall(SysCall_nr_Execve,regs);
  724. { This only gets set when the call fails, otherwise we don't get here ! }
  725. Linuxerror:=errno;
  726. end;
  727. Procedure Execv(const path:pathstr;args:ppchar);
  728. {
  729. Replaces the current program by the program specified in path,
  730. arguments in args are passed to Execve.
  731. the current environment is passed on.
  732. }
  733. begin
  734. Execve(path,args,envp); {On error linuxerror will get set there}
  735. end;
  736. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  737. {
  738. This does the same as Execve, only it searches the PATH environment
  739. for the place of the Executable, except when Path starts with a slash.
  740. if the PATH environment variable is unavailable, the path is set to '.'
  741. }
  742. var
  743. thepath : string;
  744. begin
  745. if path[1]<>'/' then
  746. begin
  747. Thepath:=strpas(getenv('PATH'));
  748. if thepath='' then
  749. thepath:='.';
  750. Path:=FSearch(path,thepath)
  751. end
  752. else
  753. Path:='';
  754. if Path='' then
  755. linuxerror:=Sys_enoent
  756. else
  757. Execve(Path,args,ep);{On error linuxerror will get set there}
  758. end;
  759. Procedure Execle(Todo:string;Ep:ppchar);
  760. {
  761. This procedure takes the string 'Todo', parses it for command and
  762. command options, and Executes the command with the given options.
  763. The string 'Todo' shoud be of the form 'command options', options
  764. separated by commas.
  765. the PATH environment is not searched for 'command'.
  766. The specified environment(in 'ep') is passed on to command
  767. }
  768. var
  769. p : ppchar;
  770. begin
  771. p:=StringToPPChar(ToDo);
  772. if (p=nil) or (p^=nil) then
  773. exit;
  774. ExecVE(p^,p,EP);
  775. end;
  776. Procedure Execl(const Todo:string);
  777. {
  778. This procedure takes the string 'Todo', parses it for command and
  779. command options, and Executes the command with the given options.
  780. The string 'Todo' shoud be of the form 'command options', options
  781. separated by commas.
  782. the PATH environment is not searched for 'command'.
  783. The current environment is passed on to command
  784. }
  785. begin
  786. ExecLE(ToDo,EnvP);
  787. end;
  788. Procedure Execlp(Todo:string;Ep:ppchar);
  789. {
  790. This procedure takes the string 'Todo', parses it for command and
  791. command options, and Executes the command with the given options.
  792. The string 'Todo' shoud be of the form 'command options', options
  793. separated by commas.
  794. the PATH environment is searched for 'command'.
  795. The specified environment (in 'ep') is passed on to command
  796. }
  797. var
  798. p : ppchar;
  799. begin
  800. p:=StringToPPchar(todo);
  801. if (p=nil) or (p^=nil) then
  802. exit;
  803. ExecVP(StrPas(p^),p,EP);
  804. end;
  805. Procedure ExitProcess(val:longint);
  806. var
  807. regs : SysCallregs;
  808. begin
  809. regs.reg2:=val;
  810. SysCall(SysCall_nr_exit,regs);
  811. end;
  812. Function WaitPid(Pid:longint;Status:pointer;Options:Integer):Longint;
  813. {
  814. Waits until a child with PID Pid exits, or returns if it is exited already.
  815. Any resources used by the child are freed.
  816. The exit status is reported in the adress referred to by Status. It should
  817. be a longint.
  818. }
  819. var
  820. regs : SysCallregs;
  821. begin
  822. regs.reg2:=pid;
  823. regs.reg3:=longint(status);
  824. regs.reg4:=options;
  825. WaitPid:=SysCall(SysCall_nr_waitpid,regs);
  826. LinuxError:=errno;
  827. end;
  828. Function Shell(const Command:String):Longint;
  829. {
  830. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  831. The current environment is passed to the shell.
  832. It waits for the shell to exit, and returns its exit status.
  833. If the Exec call failed exit status 127 is reported.
  834. }
  835. var
  836. p : ppchar;
  837. temp,pid : longint;
  838. begin
  839. pid:=fork;
  840. if pid=-1 then
  841. exit; {Linuxerror already set in Fork}
  842. if pid=0 then
  843. begin
  844. {This is the child.}
  845. p:=CreateShellArgv(command);
  846. Execve(p^,p,envp);
  847. exit(127);
  848. end;
  849. temp:=0;
  850. WaitPid(pid,@temp,0);{Linuxerror is set there}
  851. Shell:=temp;{ Return exit status }
  852. end;
  853. Function GetPriority(Which,Who:Integer):integer;
  854. {
  855. Get Priority of process, process group, or user.
  856. Which : selects what kind of priority is used.
  857. can be one of the following predefined Constants :
  858. Prio_User.
  859. Prio_PGrp.
  860. Prio_Process.
  861. Who : depending on which, this is , respectively :
  862. Uid
  863. Pid
  864. Process Group id
  865. Errors are reported in linuxerror _only_. (priority can be negative)
  866. }
  867. var
  868. sr : Syscallregs;
  869. begin
  870. errno:=0;
  871. if (which<prio_process) or (which>prio_user) then
  872. begin
  873. { We can save an interrupt here }
  874. getpriority:=0;
  875. linuxerror:=Sys_einval;
  876. end
  877. else
  878. begin
  879. sr.reg2:=which;
  880. sr.reg3:=who;
  881. getpriority:=SysCall(Syscall_nr_getpriority,sr);
  882. linuxerror:=errno;
  883. end;
  884. end;
  885. Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
  886. {
  887. Set Priority of process, process group, or user.
  888. Which : selects what kind of priority is used.
  889. can be one of the following predefined Constants :
  890. Prio_User.
  891. Prio_PGrp.
  892. Prio_Process.
  893. Who : depending on value of which, this is, respectively :
  894. Uid
  895. Pid
  896. Process Group id
  897. what : A number between -20 and 20. -20 is most favorable, 20 least.
  898. 0 is the default.
  899. }
  900. var
  901. sr : Syscallregs;
  902. begin
  903. errno:=0;
  904. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  905. linuxerror:=Sys_einval { We can save an interrupt here }
  906. else
  907. begin
  908. sr.reg2:=which;
  909. sr.reg3:=who;
  910. sr.reg4:=what;
  911. SysCall(Syscall_nr_setpriority,sr);
  912. linuxerror:=errno;
  913. end;
  914. end;
  915. Procedure Nice(N:integer);
  916. {
  917. Set process priority. A positive N means a lower priority.
  918. A negative N decreases priority.
  919. }
  920. var
  921. sr : Syscallregs;
  922. begin
  923. sr.reg2:=n;
  924. SysCall(Syscall_nr_nice,sr);
  925. linuxerror:=errno;
  926. end;
  927. Function GetPid:LongInt;
  928. {
  929. Get Process ID.
  930. }
  931. var
  932. regs : SysCallregs;
  933. begin
  934. GetPid:=SysCall(SysCall_nr_getpid,regs);
  935. linuxerror:=errno;
  936. end;
  937. Function GetPPid:LongInt;
  938. {
  939. Get Process ID of parent process.
  940. }
  941. var
  942. regs : SysCallregs;
  943. begin
  944. GetPpid:=SysCall(SysCall_nr_getppid,regs);
  945. linuxerror:=errno;
  946. end;
  947. Function GetUid:Longint;
  948. {
  949. Get User ID.
  950. }
  951. var
  952. regs : SysCallregs;
  953. begin
  954. GetUid:=SysCall(SysCall_nr_getuid,regs);
  955. Linuxerror:=errno;
  956. end;
  957. Function GetEUid:Longint;
  958. {
  959. Get _effective_ User ID.
  960. }
  961. var
  962. regs : SysCallregs;
  963. begin
  964. GetEuid:=SysCall(SysCall_nr_geteuid,regs);
  965. Linuxerror:=errno;
  966. end;
  967. Function GetGid:Longint;
  968. {
  969. Get Group ID.
  970. }
  971. var
  972. regs : SysCallregs;
  973. begin
  974. Getgid:=SysCall(SysCall_nr_getgid,regs);
  975. Linuxerror:=errno;
  976. end;
  977. Function GetEGid:Longint;
  978. {
  979. Get _effective_ Group ID.
  980. }
  981. var
  982. regs : SysCallregs;
  983. begin
  984. GetEgid:=SysCall(SysCall_nr_getegid,regs);
  985. Linuxerror:=errno;
  986. end;
  987. {******************************************************************************
  988. Date and Time related calls
  989. ******************************************************************************}
  990. Const
  991. {Date Translation}
  992. C1970=2440588;
  993. D0 = 1461;
  994. D1 = 146097;
  995. D2 =1721119;
  996. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  997. Var
  998. Century,XYear: LongInt;
  999. Begin
  1000. If Month<=2 Then
  1001. Begin
  1002. Dec(Year);
  1003. Inc(Month,12);
  1004. End;
  1005. Dec(Month,3);
  1006. Century:=(longint(Year Div 100)*D1) shr 2;
  1007. XYear:=(longint(Year Mod 100)*D0) shr 2;
  1008. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  1009. End;
  1010. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  1011. Var
  1012. YYear,XYear,Temp,TempMonth : LongInt;
  1013. Begin
  1014. Temp:=((JulianDN-D2) shl 2)-1;
  1015. JulianDN:=Temp Div D1;
  1016. XYear:=(Temp Mod D1) or 3;
  1017. YYear:=(XYear Div D0);
  1018. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  1019. Day:=((Temp Mod 153)+5) Div 5;
  1020. TempMonth:=Temp Div 153;
  1021. If TempMonth>=10 Then
  1022. Begin
  1023. inc(YYear);
  1024. dec(TempMonth,12);
  1025. End;
  1026. inc(TempMonth,3);
  1027. Month := TempMonth;
  1028. Year:=YYear+(JulianDN*100);
  1029. end;
  1030. Procedure GetTimeOfDay(var tv:timeval;var tz:timezone);
  1031. {
  1032. Get the time of day and timezone.
  1033. }
  1034. var
  1035. regs : SysCallregs;
  1036. begin
  1037. regs.reg2:=longint(@tv);
  1038. regs.reg3:=longint(@tz);
  1039. SysCall(SysCall_nr_gettimeofday,regs);
  1040. LinuxError:=Errno;
  1041. end;
  1042. Procedure SetTimeOfDay(Const tv:timeval;Const tz:timezone);
  1043. {
  1044. Get the time of day and timezone.
  1045. }
  1046. var
  1047. regs : SysCallregs;
  1048. begin
  1049. regs.reg2:=longint(@tv);
  1050. regs.reg3:=longint(@tz);
  1051. SysCall(SysCall_nr_settimeofday,regs);
  1052. LinuxError:=Errno;
  1053. end;
  1054. Function GetTimeOfDay: longint;
  1055. {
  1056. Get the number of seconds since 00:00, January 1 1970, GMT
  1057. the time NOT corrected any way
  1058. }
  1059. var
  1060. t : timeval ;
  1061. tz : timezone ;
  1062. begin
  1063. gettimeofday(t,tz);{Sets LinuxError also}
  1064. GetTimeOfDay:=t.sec;
  1065. end;
  1066. Function GetEpochTime:longint;
  1067. {
  1068. Get the number of seconds since 00:00, January 1 1970, GMT
  1069. the time is corrected according to the time zone, but NOT
  1070. DST corrected.
  1071. }
  1072. var
  1073. t : timeval ;
  1074. tz : timezone ;
  1075. begin
  1076. gettimeofday(t,tz);{Sets LinuxError also}
  1077. Getepochtime:=t.sec-tz.minuteswest*60;
  1078. end;
  1079. Procedure InitEpochToLocal;
  1080. var
  1081. tv:TimeVal;
  1082. begin
  1083. GetTimeOfDay(tv,LocalTZ);
  1084. end;
  1085. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  1086. {
  1087. Transforms Epoch time(seconds since 00:00, january 1 1970, corrected for
  1088. local time zone) into local time (hour, minute,seconds)
  1089. }
  1090. Var
  1091. DateNum: LongInt;
  1092. Begin { Beginning of Localtime }
  1093. dec(Epoch,LocalTZ.minuteswest*60);
  1094. Datenum:=(Epoch Div 86400) + c1970;
  1095. JulianToGregorian(DateNum,Year,Month,day);
  1096. Epoch:=Epoch Mod 86400;
  1097. Hour:=Epoch Div 3600;
  1098. Epoch:=Epoch Mod 3600;
  1099. Minute:=Epoch Div 60;
  1100. Second:=Epoch Mod 60;
  1101. End;
  1102. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  1103. {
  1104. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  1105. (seconds since 00:00, january 1 1970, corrected for local time zone)
  1106. }
  1107. Begin
  1108. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  1109. (LongInt(Hour)*3600)+(Minute*60)+Second+(LocalTZ.minuteswest*60);
  1110. End;
  1111. Procedure GetTime(Var Hour,Minute,Second:Word);
  1112. {
  1113. Gets the current time, adjusted to local time, but not DST,
  1114. in hours, minutes and seconds.
  1115. }
  1116. var
  1117. year,day,month:Word;
  1118. Begin
  1119. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  1120. End;
  1121. Procedure GetDate(Var Year,Month,Day:Word);
  1122. {
  1123. Gets the current date, adjusted to local time, but not DST,
  1124. in year,month,day.
  1125. }
  1126. var
  1127. hour,minute,second : Word;
  1128. Begin
  1129. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  1130. End;
  1131. { The now following are for compatibility with earlier versions
  1132. of the linux unit... }
  1133. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Integer);
  1134. begin
  1135. EpochToLocal(epoch,word(year),word(month),word(day),word(hour),word(minute),word(second));
  1136. end;
  1137. Function LocalToEpoch(year,month,day,hour,minute,second:Integer):Longint;
  1138. begin
  1139. LocalToEpoch:=LocalToEpoch(word(year),word(month),word(day),word(hour),word(minute),word(second));
  1140. end;
  1141. Procedure GetTime(Var Hour,Minute,Second:Integer);
  1142. begin
  1143. GetTime(Word(Hour),Word(Minute),Word(Second));
  1144. end;
  1145. Procedure GetDate(Var Year,Month,Day:Integer);
  1146. begin
  1147. GetDate(Word(Year),Word(Month),Word(Day));
  1148. end;
  1149. {******************************************************************************
  1150. FileSystem calls
  1151. ******************************************************************************}
  1152. Function fdOpen(pathname:string;flags:longint):longint;
  1153. begin
  1154. pathname:=pathname+#0;
  1155. fdOpen:=Sys_Open(@pathname[1],flags,438);
  1156. LinuxError:=Errno;
  1157. end;
  1158. Function fdOpen(pathname:string;flags,mode:longint):longint;
  1159. begin
  1160. pathname:=pathname+#0;
  1161. fdOpen:=Sys_Open(@pathname[1],flags,mode);
  1162. LinuxError:=Errno;
  1163. end;
  1164. Function fdOpen(pathname:pchar;flags:longint):longint;
  1165. begin
  1166. fdOpen:=Sys_Open(pathname,flags,0);
  1167. LinuxError:=Errno;
  1168. end;
  1169. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  1170. begin
  1171. fdOpen:=Sys_Open(pathname,flags,mode);
  1172. LinuxError:=Errno;
  1173. end;
  1174. Function fdClose(fd:longint):boolean;
  1175. begin
  1176. fdClose:=(Sys_Close(fd)=0);
  1177. LinuxError:=Errno;
  1178. end;
  1179. Function fdRead(fd:longint;var buf;size:longint):longint;
  1180. begin
  1181. fdRead:=Sys_Read(fd,pchar(@buf),size);
  1182. LinuxError:=Errno;
  1183. end;
  1184. Function fdWrite(fd:longint;var buf;size:longint):longint;
  1185. begin
  1186. fdWrite:=Sys_Write(fd,pchar(@buf),size);
  1187. LinuxError:=Errno;
  1188. end;
  1189. Function fdTruncate(fd,size:longint):boolean;
  1190. var
  1191. Regs : SysCallRegs;
  1192. begin
  1193. Regs.reg2:=fd;
  1194. Regs.reg3:=size;
  1195. fdTruncate:=(SysCall(Syscall_nr_ftruncate,regs)=0);
  1196. LinuxError:=Errno;
  1197. end;
  1198. Function fdSeek (fd,pos,seektype :longint): longint;
  1199. {
  1200. Do a Seek on a file descriptor fd to position pos, starting from seektype
  1201. }
  1202. begin
  1203. fdseek:=Sys_LSeek (fd,pos,seektype);
  1204. LinuxError:=Errno;
  1205. end;
  1206. Function fdFlush (fd : Longint) : Boolean;
  1207. var
  1208. SR: SysCallRegs;
  1209. begin
  1210. SR.reg2 := fd;
  1211. fdFlush := (SysCall(syscall_nr_fsync, SR)=0);
  1212. LinuxError:=Errno;
  1213. end;
  1214. Function Fcntl(var Fd:Text;Cmd:integer):integer;
  1215. {
  1216. Read or manipulate a file.(See also fcntl (2) )
  1217. Possible values for Cmd are :
  1218. F_GetFd,F_GetFl,F_GetOwn
  1219. Errors are reported in Linuxerror;
  1220. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  1221. }
  1222. var
  1223. sr : Syscallregs;
  1224. begin
  1225. if (cmd in [F_GetFd,F_GetFl,F_GetOwn]) then
  1226. begin
  1227. sr.reg2:=textrec(fd).handle;
  1228. sr.reg3:=cmd;
  1229. Linuxerror:=SysCall(Syscall_nr_fcntl,sr);
  1230. if linuxerror=-1 then
  1231. begin
  1232. linuxerror:=errno;
  1233. fcntl:=0;
  1234. end
  1235. else
  1236. begin
  1237. fcntl:=linuxerror;
  1238. linuxerror:=0;
  1239. end;
  1240. end
  1241. else
  1242. begin
  1243. linuxerror:=Sys_einval;
  1244. Fcntl:=0;
  1245. end;
  1246. end;
  1247. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  1248. {
  1249. Read or manipulate a file. (See also fcntl (2) )
  1250. Possible values for Cmd are :
  1251. F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
  1252. Errors are reported in Linuxerror;
  1253. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  1254. F_DupFD is not allowed, due to the structure of Files in Pascal.
  1255. }
  1256. var
  1257. sr : Syscallregs;
  1258. begin
  1259. if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn]) then
  1260. begin
  1261. sr.reg2:=textrec(fd).handle;
  1262. sr.reg3:=cmd;
  1263. sr.reg4:=arg;
  1264. SysCall(Syscall_nr_fcntl,sr);
  1265. linuxerror:=errno;
  1266. end
  1267. else
  1268. linuxerror:=Sys_einval;
  1269. end;
  1270. Function Chmod(path:pathstr;Newmode:longint):Boolean;
  1271. {
  1272. Changes the permissions of a file.
  1273. }
  1274. var
  1275. sr : Syscallregs;
  1276. begin
  1277. path:=path+#0;
  1278. sr.reg2:=longint(@(path[1]));
  1279. sr.reg3:=newmode;
  1280. Chmod:=(SysCall(Syscall_nr_chmod,sr)=0);
  1281. linuxerror:=errno;
  1282. end;
  1283. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  1284. {
  1285. Change the owner and group of a file.
  1286. A user can only change the group to a group of which he is a member.
  1287. The super-user can change uid and gid of any file.
  1288. }
  1289. var
  1290. sr : Syscallregs;
  1291. begin
  1292. path:=path+#0;
  1293. sr.reg2:=longint(@(path[1]));
  1294. sr.reg3:=newuid;
  1295. sr.reg4:=newgid;
  1296. ChOwn:=(Syscall(Syscall_nr_chown,sr)=0);
  1297. linuxerror:=errno;
  1298. end;
  1299. Function Utime(path:pathstr;utim:utimebuf):boolean;
  1300. var
  1301. sr : Syscallregs;
  1302. begin
  1303. path:=path+#0;
  1304. sr.reg2:=longint(@(path[1]));
  1305. sr.reg3:=longint(@utim);
  1306. Utime:=SysCall(Syscall_nr_utime,sr)=0;
  1307. linuxerror:=errno;
  1308. end;
  1309. Function Flock (fd,mode : longint) : boolean;
  1310. var
  1311. sr : Syscallregs;
  1312. begin
  1313. sr.reg2:=fd;
  1314. sr.reg3:=mode;
  1315. flock:=Syscall(Syscall_nr_flock,sr)=0;
  1316. LinuxError:=errno;
  1317. end;
  1318. Function Flock (var T : text;mode : longint) : boolean;
  1319. begin
  1320. Flock:=Flock(TextRec(T).Handle,mode);
  1321. end;
  1322. Function Flock (var F : File;mode : longint) : boolean;
  1323. begin
  1324. Flock:=Flock(FileRec(F).Handle,mode);
  1325. end;
  1326. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  1327. {
  1328. Get all information on a file, and return it in Info.
  1329. }
  1330. begin
  1331. path:=path+#0;
  1332. FStat:=(Sys_stat(@(path[1]),Info)=0);
  1333. LinuxError:=errno;
  1334. end;
  1335. Function Fstat(Fd:Longint;var Info:stat):Boolean;
  1336. {
  1337. Get all information on a file descriptor, and return it in info.
  1338. }
  1339. var
  1340. regs : SysCallregs;
  1341. begin
  1342. regs.reg2:=Fd;
  1343. regs.reg3:=longint(@Info);
  1344. FStat:=(SysCall(SysCall_nr_fstat,regs)=0);
  1345. LinuxError:=Errno;
  1346. end;
  1347. Function FStat(var F:Text;Var Info:stat):Boolean;
  1348. {
  1349. Get all information on a text file, and return it in info.
  1350. }
  1351. begin
  1352. FStat:=Fstat(TextRec(F).Handle,INfo);
  1353. end;
  1354. Function FStat(var F:File;Var Info:stat):Boolean;
  1355. {
  1356. Get all information on a untyped file, and return it in info.
  1357. }
  1358. begin
  1359. FStat:=Fstat(FileRec(F).Handle,Info);
  1360. end;
  1361. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  1362. {
  1363. Get all information on a link (the link itself), and return it in info.
  1364. }
  1365. var
  1366. regs : SysCallregs;
  1367. begin
  1368. FileName:=FileName+#0;
  1369. regs.reg2:=longint(@filename[1]);
  1370. regs.reg3:=longint(@Info);
  1371. LStat:=(SysCall(SysCall_nr_lstat,regs)=0);
  1372. LinuxError:=Errno;
  1373. end;
  1374. Function FSStat(Path:Pathstr;Var Info:statfs):Boolean;
  1375. {
  1376. Get all information on a fileSystem, and return it in Info.
  1377. Path is the name of a file/directory on the fileSystem you wish to
  1378. investigate.
  1379. }
  1380. var
  1381. regs : SysCallregs;
  1382. begin
  1383. path:=path+#0;
  1384. regs.reg2:=longint(@path[1]);
  1385. regs.reg3:=longint(@Info);
  1386. FSStat:=(SysCall(SysCall_nr_statfs,regs)=0);
  1387. LinuxError:=errno;
  1388. end;
  1389. Function FSStat(Fd:Longint;Var Info:statfs):Boolean;
  1390. {
  1391. Get all information on a fileSystem, and return it in Info.
  1392. Fd is the file descriptor of a file/directory on the fileSystem
  1393. you wish to investigate.
  1394. }
  1395. var
  1396. regs : SysCallregs;
  1397. begin
  1398. regs.reg2:=Fd;
  1399. regs.reg3:=longint(@Info);
  1400. FSStat:=(SysCall(SysCall_nr_fstatfs,regs)=0);
  1401. LinuxError:=errno;
  1402. end;
  1403. Function Link(OldPath,NewPath:pathstr):boolean;
  1404. {
  1405. Proceduces a hard link from new to old.
  1406. In effect, new will be the same file as old.
  1407. }
  1408. var
  1409. regs : SysCallregs;
  1410. begin
  1411. oldpath:=oldpath+#0;
  1412. newpath:=newpath+#0;
  1413. regs.reg2:=longint(@oldpath[1]);
  1414. regs.reg3:=longint(@newpath[1]);
  1415. Link:=SysCall(SysCall_nr_link,regs)=0;
  1416. linuxerror:=errno;
  1417. end;
  1418. Function SymLink(OldPath,newPath:pathstr):boolean;
  1419. {
  1420. Proceduces a soft link from new to old.
  1421. }
  1422. begin
  1423. oldpath:=oldpath+#0;
  1424. newpath:=newpath+#0;
  1425. Symlink:=Sys_symlink(pchar(@(oldpath[1])),pchar(@(newpath[1])))=0;
  1426. linuxerror:=errno;
  1427. end;
  1428. Function UnLink(Path:pathstr):boolean;
  1429. {
  1430. Removes the file in 'Path' (that is, it decreases the link count with one.
  1431. if the link count is zero, the file is removed from the disk.
  1432. }
  1433. begin
  1434. path:=path+#0;
  1435. Unlink:=Sys_unlink(pchar(@(path[1])))=0;
  1436. linuxerror:=errno;
  1437. end;
  1438. Function UnLink(Path:pchar):Boolean;
  1439. {
  1440. Removes the file in 'Path' (that is, it decreases the link count with one.
  1441. if the link count is zero, the file is removed from the disk.
  1442. }
  1443. begin
  1444. Unlink:=(Sys_unlink(path)=0);
  1445. linuxerror:=errno;
  1446. end;
  1447. Function FRename (OldName,NewName : Pchar) : Boolean;
  1448. begin
  1449. FRename:=Sys_rename(OldName,NewName)=0;
  1450. LinuxError:=Errno;
  1451. end;
  1452. Function FRename (OldName,NewName : String) : Boolean;
  1453. begin
  1454. OldName:=OldName+#0;
  1455. NewName:=NewName+#0;
  1456. FRename:=FRename (@OldName[1],@NewName[1]);
  1457. end;
  1458. Function Umask(Mask:Integer):integer;
  1459. {
  1460. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  1461. previous value.
  1462. }
  1463. var
  1464. sr : Syscallregs;
  1465. begin
  1466. sr.reg2:=mask;
  1467. Umask:=SysCall(Syscall_nr_umask,sr);
  1468. linuxerror:=0;
  1469. end;
  1470. Function Access(Path:Pathstr ;mode:integer):boolean;
  1471. {
  1472. Test users access rights on the specified file.
  1473. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  1474. R,W,X stand for read,write and Execute access, simultaneously.
  1475. F_OK checks whether the test would be allowed on the file.
  1476. i.e. It checks the search permissions in all directory components
  1477. of the path.
  1478. The test is done with the real user-ID, instead of the effective.
  1479. If access is denied, or an error occurred, false is returned.
  1480. If access is granted, true is returned.
  1481. Errors other than no access,are reported in linuxerror.
  1482. }
  1483. var
  1484. sr : Syscallregs;
  1485. begin
  1486. path:=path+#0;
  1487. sr.reg2:=longint(@(path[1]));
  1488. sr.reg3:=mode;
  1489. access:=(SysCall(Syscall_nr_access,sr)=0);
  1490. linuxerror:=errno;
  1491. end;
  1492. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  1493. {
  1494. Copies the filedescriptor oldfile to newfile
  1495. }
  1496. var
  1497. sr : Syscallregs;
  1498. begin
  1499. sr.reg2:=oldfile;
  1500. newfile:=Syscall(Syscall_nr_dup,sr);
  1501. linuxerror:=errno;
  1502. Dup:=(LinuxError=0);
  1503. end;
  1504. Function Dup(var oldfile,newfile:text):Boolean;
  1505. {
  1506. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  1507. oldfile.
  1508. After which the two textfiles are, in effect, the same, except
  1509. that they don't share the same buffer, and don't share the same
  1510. close_on_exit flag.
  1511. }
  1512. begin
  1513. flush(oldfile);{ We cannot share buffers, so we flush them. }
  1514. textrec(newfile):=textrec(oldfile);
  1515. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  1516. Dup:=Dup(textrec(oldfile).handle,textrec(newfile).handle);
  1517. end;
  1518. Function Dup(var oldfile,newfile:file):Boolean;
  1519. {
  1520. Copies the filedescriptor oldfile to newfile
  1521. }
  1522. begin
  1523. filerec(newfile):=filerec(oldfile);
  1524. Dup:=Dup(filerec(oldfile).handle,filerec(newfile).handle);
  1525. end;
  1526. Function Dup2(oldfile,newfile:longint):Boolean;
  1527. {
  1528. Copies the filedescriptor oldfile to newfile
  1529. }
  1530. var
  1531. sr : Syscallregs;
  1532. begin
  1533. sr.reg2:=oldfile;
  1534. sr.reg3:=newfile;
  1535. SysCall(Syscall_nr_dup2,sr);
  1536. linuxerror:=errno;
  1537. Dup2:=(LinuxError=0);
  1538. end;
  1539. Function Dup2(var oldfile,newfile:text):Boolean;
  1540. {
  1541. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  1542. oldfile. It closes newfile if it was still open.
  1543. After which the two textfiles are, in effect, the same, except
  1544. that they don't share the same buffer, and don't share the same
  1545. close_on_exit flag.
  1546. }
  1547. var
  1548. tmphandle : word;
  1549. begin
  1550. flush(oldfile);{ We cannot share buffers, so we flush them. }
  1551. flush(newfile);
  1552. tmphandle:=textrec(newfile).handle;
  1553. textrec(newfile):=textrec(oldfile);
  1554. textrec(newfile).handle:=tmphandle;
  1555. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  1556. Dup2:=Dup2(textrec(oldfile).handle,textrec(newfile).handle);
  1557. end;
  1558. Function Dup2(var oldfile,newfile:file):Boolean;
  1559. {
  1560. Copies the filedescriptor oldfile to newfile
  1561. }
  1562. begin
  1563. filerec(newfile):=filerec(oldfile);
  1564. Dup2:=Dup2(filerec(oldfile).handle,filerec(newfile).handle);
  1565. end;
  1566. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
  1567. {
  1568. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  1569. have changed.
  1570. }
  1571. Var
  1572. SelectArray : Array[1..5] of longint;
  1573. Sr : Syscallregs;
  1574. begin
  1575. SelectArray[1]:=n;
  1576. SelectArray[2]:=longint(Readfds);
  1577. Selectarray[3]:=longint(Writefds);
  1578. selectarray[4]:=longint(exceptfds);
  1579. Selectarray[5]:=longint(TimeOut);
  1580. sr.reg2:=longint(@selectarray);
  1581. Select:=SysCall(Syscall_nr_select,sr);
  1582. LinuxError:=Errno;
  1583. end;
  1584. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  1585. {
  1586. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  1587. have changed.
  1588. This function allows specification of a timeout as a longint.
  1589. }
  1590. var
  1591. p : PTimeVal;
  1592. tv : TimeVal;
  1593. begin
  1594. if TimeOut=-1 then
  1595. p:=nil
  1596. else
  1597. begin
  1598. tv.Sec:=Timeout div 1000;
  1599. tv.Usec:=(Timeout mod 1000)*1000;
  1600. p:=@tv;
  1601. end;
  1602. Select:=Select(N,Readfds,WriteFds,ExceptFds,p);
  1603. end;
  1604. Function SelectText(var T:Text;TimeOut :PTimeval):Longint;
  1605. Var
  1606. F:FDSet;
  1607. begin
  1608. if textrec(t).mode=fmclosed then
  1609. begin
  1610. LinuxError:=Sys_EBADF;
  1611. exit(-1);
  1612. end;
  1613. FD_Zero(f);
  1614. FD_Set(textrec(T).handle,f);
  1615. if textrec(T).mode=fminput then
  1616. SelectText:=select(textrec(T).handle+1,@f,nil,nil,TimeOut)
  1617. else
  1618. SelectText:=select(textrec(T).handle+1,nil,@f,nil,TimeOut);
  1619. end;
  1620. {******************************************************************************
  1621. Directory
  1622. ******************************************************************************}
  1623. Function OpenDir(F:String):PDir;
  1624. begin
  1625. F:=F+#0;
  1626. OpenDir:=OpenDir(@F[1]);
  1627. end;
  1628. procedure SeekDir(p:pdir;off:longint);
  1629. begin
  1630. if p=nil then
  1631. begin
  1632. errno:=Sys_EBADF;
  1633. exit;
  1634. end;
  1635. p^.nextoff:=Sys_lseek(p^.fd,off,seek_set);
  1636. p^.size:=0;
  1637. p^.loc:=0;
  1638. end;
  1639. function TellDir(p:pdir):longint;
  1640. begin
  1641. if p=nil then
  1642. begin
  1643. errno:=Sys_EBADF;
  1644. telldir:=-1;
  1645. exit;
  1646. end;
  1647. telldir:=Sys_lseek(p^.fd,0,seek_cur)
  1648. { We could try to use the nextoff field here, but on my 1.2.13
  1649. kernel, this gives nothing... This may have to do with
  1650. the readdir implementation of libc... I also didn't find any trace of
  1651. the field in the kernel code itself, So I suspect it is an artifact of libc.
  1652. Michael. }
  1653. end;
  1654. Function ReadDir(P:pdir):pdirent;
  1655. begin
  1656. ReadDir:=Sys_ReadDir(p);
  1657. LinuxError:=Errno;
  1658. end;
  1659. {******************************************************************************
  1660. Pipes/Fifo
  1661. ******************************************************************************}
  1662. Procedure OpenPipe(var F:Text);
  1663. begin
  1664. case textrec(f).mode of
  1665. fmoutput : if textrec(f).userdata[1]<>P_OUT then
  1666. textrec(f).mode:=fmclosed;
  1667. fminput : if textrec(f).userdata[1]<>P_IN then
  1668. textrec(f).mode:=fmclosed;
  1669. else
  1670. textrec(f).mode:=fmclosed;
  1671. end;
  1672. end;
  1673. Procedure IOPipe(var F:text);
  1674. begin
  1675. case textrec(f).mode of
  1676. fmoutput : begin
  1677. { first check if we need something to write, else we may
  1678. get a SigPipe when Close() is called (PFV) }
  1679. if textrec(f).bufpos>0 then
  1680. Sys_write(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  1681. end;
  1682. fminput : textrec(f).bufend:=Sys_read(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  1683. end;
  1684. textrec(f).bufpos:=0;
  1685. end;
  1686. Procedure FlushPipe(var F:Text);
  1687. begin
  1688. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  1689. IOPipe(f);
  1690. textrec(f).bufpos:=0;
  1691. end;
  1692. Procedure ClosePipe(var F:text);
  1693. begin
  1694. textrec(f).mode:=fmclosed;
  1695. Sys_close(textrec(f).handle);
  1696. end;
  1697. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  1698. {
  1699. Sets up a pair of file variables, which act as a pipe. The first one can
  1700. be read from, the second one can be written to.
  1701. If the operation was unsuccesful, linuxerror is set.
  1702. }
  1703. var
  1704. pip : tpipe;
  1705. regs : SysCallregs;
  1706. begin
  1707. regs.reg2:=longint(@pip);
  1708. SysCall(SysCall_nr_pipe,regs);
  1709. pipe_in:=pip[1];
  1710. pipe_out:=pip[2];
  1711. linuxerror:=errno;
  1712. AssignPipe:=(LinuxError=0);
  1713. end;
  1714. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  1715. {
  1716. Sets up a pair of file variables, which act as a pipe. The first one can
  1717. be read from, the second one can be written to.
  1718. If the operation was unsuccesful, linuxerror is set.
  1719. }
  1720. var
  1721. f_in,f_out : longint;
  1722. begin
  1723. if not AssignPipe(f_in,f_out) then
  1724. begin
  1725. AssignPipe:=false;
  1726. exit;
  1727. end;
  1728. { Set up input }
  1729. Assign(Pipe_in,'');
  1730. Textrec(Pipe_in).Handle:=f_in;
  1731. Textrec(Pipe_in).Mode:=fmInput;
  1732. Textrec(Pipe_in).userdata[1]:=P_IN;
  1733. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  1734. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  1735. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  1736. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  1737. { Set up output }
  1738. Assign(Pipe_out,'');
  1739. Textrec(Pipe_out).Handle:=f_out;
  1740. Textrec(Pipe_out).Mode:=fmOutput;
  1741. Textrec(Pipe_out).userdata[1]:=P_OUT;
  1742. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  1743. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  1744. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  1745. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  1746. AssignPipe:=true;
  1747. end;
  1748. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  1749. {
  1750. Sets up a pair of file variables, which act as a pipe. The first one can
  1751. be read from, the second one can be written to.
  1752. If the operation was unsuccesful, linuxerror is set.
  1753. }
  1754. var
  1755. f_in,f_out : longint;
  1756. begin
  1757. if not AssignPipe(f_in,f_out) then
  1758. begin
  1759. AssignPipe:=false;
  1760. exit;
  1761. end;
  1762. { Set up input }
  1763. Assign(Pipe_in,'');
  1764. Filerec(Pipe_in).Handle:=f_in;
  1765. Filerec(Pipe_in).Mode:=fmInput;
  1766. Filerec(Pipe_in).recsize:=1;
  1767. Filerec(Pipe_in).userdata[1]:=P_IN;
  1768. { Set up output }
  1769. Assign(Pipe_out,'');
  1770. Filerec(Pipe_out).Handle:=f_out;
  1771. Filerec(Pipe_out).Mode:=fmoutput;
  1772. Filerec(Pipe_out).recsize:=1;
  1773. Filerec(Pipe_out).userdata[1]:=P_OUT;
  1774. AssignPipe:=true;
  1775. end;
  1776. Function PClose(Var F:text) :longint;
  1777. var
  1778. sr : syscallregs;
  1779. pl : ^longint;
  1780. res : longint;
  1781. begin
  1782. sr.reg2:=Textrec(F).Handle;
  1783. SysCall (syscall_nr_close,sr);
  1784. { closed our side, Now wait for the other - this appears to be needed ?? }
  1785. pl:=@(textrec(f).userdata[2]);
  1786. waitpid(pl^,@res,0);
  1787. pclose:=res shr 8;
  1788. end;
  1789. Function PClose(Var F:file) : longint;
  1790. var
  1791. sr : syscallregs;
  1792. pl : ^longint;
  1793. res : longint;
  1794. begin
  1795. sr.reg2:=FileRec(F).Handle;
  1796. SysCall (Syscall_nr_close,sr);
  1797. { closed our side, Now wait for the other - this appears to be needed ?? }
  1798. pl:=@(filerec(f).userdata[2]);
  1799. waitpid(pl^,@res,0);
  1800. pclose:=res shr 8;
  1801. end;
  1802. Procedure PCloseText(Var F:text);
  1803. {
  1804. May not use @PClose due overloading
  1805. }
  1806. begin
  1807. PClose(f);
  1808. end;
  1809. Procedure POpen(var F:text;const Prog:String;rw:char);
  1810. {
  1811. Starts the program in 'Prog' and makes it's input or out put the
  1812. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1813. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1814. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1815. read from 'f'.
  1816. }
  1817. var
  1818. pipi,
  1819. pipo : text;
  1820. pid : longint;
  1821. pl : ^longint;
  1822. pp : ppchar;
  1823. begin
  1824. LinuxError:=0;
  1825. rw:=upcase(rw);
  1826. if not (rw in ['R','W']) then
  1827. begin
  1828. LinuxError:=Sys_enoent;
  1829. exit;
  1830. end;
  1831. AssignPipe(pipi,pipo);
  1832. if Linuxerror<>0 then
  1833. exit;
  1834. pid:=fork;
  1835. if linuxerror<>0 then
  1836. begin
  1837. close(pipi);
  1838. close(pipo);
  1839. exit;
  1840. end;
  1841. if pid=0 then
  1842. begin
  1843. { We're in the child }
  1844. if rw='W' then
  1845. begin
  1846. close(pipo);
  1847. dup2(pipi,input);
  1848. close(pipi);
  1849. if linuxerror<>0 then
  1850. halt(127);
  1851. end
  1852. else
  1853. begin
  1854. close(pipi);
  1855. dup2(pipo,output);
  1856. close(pipo);
  1857. if linuxerror<>0 then
  1858. halt(127);
  1859. end;
  1860. pp:=createshellargv(prog);
  1861. Execve(pp^,pp,envp);
  1862. halt(127);
  1863. end
  1864. else
  1865. begin
  1866. { We're in the parent }
  1867. if rw='W' then
  1868. begin
  1869. close(pipi);
  1870. f:=pipo;
  1871. textrec(f).bufptr:=@textrec(f).buffer;
  1872. end
  1873. else
  1874. begin
  1875. close(pipo);
  1876. f:=pipi;
  1877. textrec(f).bufptr:=@textrec(f).buffer;
  1878. end;
  1879. {Save the process ID - needed when closing }
  1880. pl:=@(textrec(f).userdata[2]);
  1881. pl^:=pid;
  1882. textrec(f).closefunc:=@PCloseText;
  1883. end;
  1884. end;
  1885. Procedure POpen(var F:file;const Prog:String;rw:char);
  1886. {
  1887. Starts the program in 'Prog' and makes it's input or out put the
  1888. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1889. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1890. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1891. read from 'f'.
  1892. }
  1893. var
  1894. pipi,
  1895. pipo : file;
  1896. pid : longint;
  1897. pl : ^longint;
  1898. p,pp : ppchar;
  1899. temp : string[255];
  1900. begin
  1901. LinuxError:=0;
  1902. rw:=upcase(rw);
  1903. if not (rw in ['R','W']) then
  1904. begin
  1905. LinuxError:=Sys_enoent;
  1906. exit;
  1907. end;
  1908. AssignPipe(pipi,pipo);
  1909. if Linuxerror<>0 then
  1910. exit;
  1911. pid:=fork;
  1912. if linuxerror<>0 then
  1913. begin
  1914. close(pipi);
  1915. close(pipo);
  1916. exit;
  1917. end;
  1918. if pid=0 then
  1919. begin
  1920. { We're in the child }
  1921. if rw='W' then
  1922. begin
  1923. close(pipo);
  1924. dup2(filerec(pipi).handle,stdinputhandle);
  1925. close(pipi);
  1926. if linuxerror<>0 then
  1927. halt(127);
  1928. end
  1929. else
  1930. begin
  1931. close(pipi);
  1932. dup2(filerec(pipo).handle,stdoutputhandle);
  1933. close(pipo);
  1934. if linuxerror<>0 then
  1935. halt(127);
  1936. end;
  1937. getmem(pp,sizeof(pchar)*4);
  1938. temp:='/bin/sh'#0'-c'#0+prog+#0;
  1939. pp^:=@temp[1];
  1940. p:=pp+sizeof(pchar);
  1941. p^:=@temp[9];
  1942. p:=p+sizeof(pchar);
  1943. p^:=@temp[12];
  1944. p:=p+sizeof(pchar);
  1945. p^:=Nil;
  1946. Execve('/bin/sh',pp,envp);
  1947. halt(127);
  1948. end
  1949. else
  1950. begin
  1951. { We're in the parent }
  1952. if rw='W' then
  1953. begin
  1954. close(pipi);
  1955. f:=pipo;
  1956. end
  1957. else
  1958. begin
  1959. close(pipo);
  1960. f:=pipi;
  1961. end;
  1962. {Save the process ID - needed when closing }
  1963. pl:=@(filerec(f).userdata[2]);
  1964. pl^:=pid;
  1965. end;
  1966. end;
  1967. Function mkFifo(pathname:string;mode:longint):boolean;
  1968. var
  1969. regs : SysCallRegs;
  1970. begin
  1971. pathname:=pathname+#0;
  1972. regs.reg2:=longint(@pathname[1]);
  1973. regs.reg3:=mode or STAT_IFIFO;
  1974. regs.reg4:=0;
  1975. mkFifo:=(SysCall(syscall_nr_mknod,regs)=0);
  1976. end;
  1977. Procedure AssignStream(Var StreamIn,Streamout:text;Const Prog:String);
  1978. {
  1979. Starts the program in 'Prog' and makes its input and output the
  1980. other end of two pipes, which are the stdin and stdout of a program
  1981. specified in 'Prog'.
  1982. streamout can be used to write to the program, streamin can be used to read
  1983. the output of the program. See the following diagram :
  1984. Parent Child
  1985. STreamout --> Input
  1986. Streamin <-- Output
  1987. }
  1988. var
  1989. pipi,
  1990. pipo : text;
  1991. pid : longint;
  1992. pl : ^Longint;
  1993. begin
  1994. LinuxError:=0;
  1995. AssignPipe(streamin,pipo);
  1996. if Linuxerror<>0 then
  1997. exit;
  1998. AssignPipe(pipi,streamout);
  1999. if Linuxerror<>0 then
  2000. exit;
  2001. pid:=fork;
  2002. if linuxerror<>0 then
  2003. begin
  2004. close(pipi);
  2005. close(pipo);
  2006. close (streamin);
  2007. close (streamout);
  2008. exit;
  2009. end;
  2010. if pid=0 then
  2011. begin
  2012. { We're in the child }
  2013. { Close what we don't need }
  2014. close(streamout);
  2015. close(streamin);
  2016. dup2(pipi,input);
  2017. if linuxerror<>0 then
  2018. halt(127);
  2019. close(pipi);
  2020. dup2(pipo,output);
  2021. if linuxerror<>0 then
  2022. halt (127);
  2023. close(pipo);
  2024. Execl(Prog);
  2025. halt(127);
  2026. end
  2027. else
  2028. begin
  2029. { we're in the parent}
  2030. close(pipo);
  2031. close(pipi);
  2032. {Save the process ID - needed when closing }
  2033. pl:=@(textrec(StreamIn).userdata[2]);
  2034. pl^:=pid;
  2035. textrec(StreamIn).closefunc:=@PCloseText;
  2036. {Save the process ID - needed when closing }
  2037. pl:=@(textrec(StreamOut).userdata[2]);
  2038. pl^:=pid;
  2039. textrec(StreamOut).closefunc:=@PCloseText;
  2040. end;
  2041. end;
  2042. {******************************************************************************
  2043. General information calls
  2044. ******************************************************************************}
  2045. Function Sysinfo(var Info:TSysinfo):Boolean;
  2046. {
  2047. Get system info
  2048. }
  2049. var
  2050. regs : SysCallregs;
  2051. Begin
  2052. regs.reg2:=longint(@info);
  2053. Sysinfo:=SysCall(SysCall_nr_Sysinfo,regs)=0;
  2054. End;
  2055. Function Uname(var unamerec:utsname):Boolean;
  2056. {
  2057. Get machine's names
  2058. }
  2059. var
  2060. regs : SysCallregs;
  2061. Begin
  2062. regs.reg2:=longint(@unamerec);
  2063. Uname:=SysCall(SysCall_nr_uname,regs)=0;
  2064. LinuxError:=Errno;
  2065. End;
  2066. Function GetEnv(P:string):Pchar;
  2067. {
  2068. Searches the environment for a string with name p and
  2069. returns a pchar to it's value.
  2070. A pchar is used to accomodate for strings of length > 255
  2071. }
  2072. var
  2073. ep : ppchar;
  2074. found : boolean;
  2075. Begin
  2076. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  2077. ep:=envp;
  2078. found:=false;
  2079. if ep<>nil then
  2080. begin
  2081. while (not found) and (ep^<>nil) do
  2082. begin
  2083. if strlcomp(@p[1],(ep^),length(p))=0 then
  2084. found:=true
  2085. else
  2086. ep:=ep+4;
  2087. end;
  2088. end;
  2089. if found then
  2090. getenv:=ep^+length(p)
  2091. else
  2092. getenv:=nil;
  2093. end;
  2094. Function GetDomainName:String;
  2095. {
  2096. Get machines domain name. Returns empty string if not set.
  2097. }
  2098. Var
  2099. Sysn : utsname;
  2100. begin
  2101. Uname(Sysn);
  2102. linuxerror:=errno;
  2103. If linuxerror<>0 then
  2104. getdomainname:=''
  2105. else
  2106. getdomainname:=strpas(@Sysn.domainname[0]);
  2107. end;
  2108. Function GetHostName:String;
  2109. {
  2110. Get machines name. Returns empty string if not set.
  2111. }
  2112. Var
  2113. Sysn : utsname;
  2114. begin
  2115. uname(Sysn);
  2116. linuxerror:=errno;
  2117. If linuxerror<>0 then
  2118. gethostname:=''
  2119. else
  2120. gethostname:=strpas(@Sysn.nodename[0]);
  2121. end;
  2122. {******************************************************************************
  2123. Signal handling calls
  2124. ******************************************************************************}
  2125. Function Kill(Pid:longint;Sig:integer):integer;
  2126. {
  2127. Send signal 'sig' to a process, or a group of processes.
  2128. If Pid > 0 then the signal is sent to pid
  2129. pid=-1 to all processes except process 1
  2130. pid < -1 to process group -pid
  2131. Return value is zero, except for case three, where the return value
  2132. is the number of processes to which the signal was sent.
  2133. }
  2134. var
  2135. regs : Syscallregs;
  2136. begin
  2137. regs.reg2:=Pid;
  2138. regs.reg3:=Sig;
  2139. kill:=SysCall(Syscall_nr_kill,regs);
  2140. if kill<0 then
  2141. Kill:=0;
  2142. linuxerror:=errno;
  2143. end;
  2144. Procedure SigAction(Signum:Integer;Var Act,OldAct:PSigActionRec );
  2145. {
  2146. Change action of process upon receipt of a signal.
  2147. Signum specifies the signal (all except SigKill and SigStop).
  2148. If Act is non-nil, it is used to specify the new action.
  2149. If OldAct is non-nil the previous action is saved there.
  2150. }
  2151. Var
  2152. sr : Syscallregs;
  2153. begin
  2154. sr.reg2:=Signum;
  2155. sr.reg3:=Longint(act);
  2156. sr.reg4:=Longint(oldact);
  2157. SysCall(Syscall_nr_sigaction,sr);
  2158. linuxerror:=errno;
  2159. end;
  2160. Procedure SigProcMask(How:Integer;SSet,OldSSet:PSigSet);
  2161. {
  2162. Change the list of currently blocked signals.
  2163. How determines which signals will be blocked :
  2164. SigBlock : Add SSet to the current list of blocked signals
  2165. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  2166. SigSetMask : Set the list of blocked signals to SSet
  2167. if OldSSet is non-null, the old set will be saved there.
  2168. }
  2169. Var
  2170. sr : SyscallRegs;
  2171. begin
  2172. sr.reg2:=how;
  2173. sr.reg3:=longint(SSet);
  2174. sr.reg4:=longint(OldSSet);
  2175. SysCall(Syscall_nr_sigprocmask,sr);
  2176. linuxerror:=errno;
  2177. end;
  2178. Function SigPending:SigSet;
  2179. {
  2180. Allows examination of pending signals. The signal mask of pending
  2181. signals is set in SSet
  2182. }
  2183. Var
  2184. sr : SyscallRegs;
  2185. dummy : Sigset;
  2186. begin
  2187. sr.reg2:=longint(@dummy);
  2188. SysCall(Syscall_nr_sigpending,sr);
  2189. linuxerror:=errno;
  2190. Sigpending:=dummy;
  2191. end;
  2192. Procedure SigSuspend(Mask:Sigset);
  2193. {
  2194. Set the signal mask with Mask, and suspend the program until a signal
  2195. is received.
  2196. }
  2197. Var
  2198. sr : SyscallRegs;
  2199. begin
  2200. sr.reg2:=mask;
  2201. SysCall(Syscall_nr_sigsuspend,sr);
  2202. linuxerror:=errno;
  2203. end;
  2204. Function Signal(Signum:Integer;Handler:SignalHandler):SignalHandler;
  2205. {
  2206. Install a new handler for signal Signum.
  2207. The old signal handler is returned.
  2208. This call does, in fact, the same as SigAction.
  2209. }
  2210. var
  2211. sr : Syscallregs;
  2212. begin
  2213. sr.reg2:=signum;
  2214. sr.reg3:=longint(handler);
  2215. Linuxerror:=SysCall(Syscall_nr_signal,sr);
  2216. If linuxerror=Sig_Err then
  2217. begin
  2218. Signal:=nil;
  2219. Linuxerror:=errno;
  2220. end
  2221. else
  2222. begin
  2223. Signal:=signalhandler(Linuxerror);
  2224. linuxerror:=0;
  2225. end;
  2226. end;
  2227. procedure SigRaise(sig:integer);
  2228. begin
  2229. Kill(GetPid,Sig);
  2230. end;
  2231. {******************************************************************************
  2232. IOCtl and Termios calls
  2233. ******************************************************************************}
  2234. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  2235. {
  2236. Interface to Unix ioctl call.
  2237. Performs various operations on the filedescriptor Handle.
  2238. Ndx describes the operation to perform.
  2239. Data points to data needed for the Ndx function. The structure of this
  2240. data is function-dependent.
  2241. }
  2242. var
  2243. sr: SysCallRegs;
  2244. begin
  2245. sr.reg2:=Handle;
  2246. sr.reg3:=Ndx;
  2247. sr.reg4:=Longint(Data);
  2248. IOCtl:=(SysCall(Syscall_nr_ioctl,sr)=0);
  2249. LinuxError:=Errno;
  2250. end;
  2251. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  2252. begin
  2253. TCGetAttr:=IOCtl(fd,TCGETS,@tios);
  2254. end;
  2255. Function TCSetAttr(fd:longint;OptAct:longint;var tios:TermIOS):boolean;
  2256. var
  2257. nr:longint;
  2258. begin
  2259. case OptAct of
  2260. TCSANOW : nr:=TCSETS;
  2261. TCSADRAIN : nr:=TCSETSW;
  2262. TCSAFLUSH : nr:=TCSETSF;
  2263. else
  2264. begin
  2265. ErrNo:=Sys_EINVAL;
  2266. TCSetAttr:=false;
  2267. exit;
  2268. end;
  2269. end;
  2270. TCSetAttr:=IOCtl(fd,nr,@Tios);
  2271. end;
  2272. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  2273. begin
  2274. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  2275. end;
  2276. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  2277. begin
  2278. CFSetISpeed(tios,speed);
  2279. end;
  2280. Procedure CFMakeRaw(var tios:TermIOS);
  2281. begin
  2282. with tios do
  2283. begin
  2284. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  2285. INLCR or IGNCR or ICRNL or IXON));
  2286. c_oflag:=c_oflag and (not OPOST);
  2287. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  2288. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  2289. end;
  2290. end;
  2291. Function TCSendBreak(fd,duration:longint):boolean;
  2292. begin
  2293. TCSendBreak:=IOCtl(fd,TCSBRK,pointer(duration));
  2294. end;
  2295. Function TCSetPGrp(fd,id:longint):boolean;
  2296. begin
  2297. TCSetPGrp:=IOCtl(fd,TIOCSPGRP,pointer(id));
  2298. end;
  2299. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  2300. begin
  2301. TCGetPGrp:=IOCtl(fd,TIOCGPGRP,@id);
  2302. end;
  2303. Function TCDrain(fd:longint):boolean;
  2304. begin
  2305. TCDrain:=IOCtl(fd,TCSBRK,pointer(1));
  2306. end;
  2307. Function TCFlow(fd,act:longint):boolean;
  2308. begin
  2309. TCFlow:=IOCtl(fd,TCXONC,pointer(act));
  2310. end;
  2311. Function TCFlush(fd,qsel:longint):boolean;
  2312. begin
  2313. TCFlush:=IOCtl(fd,TCFLSH,pointer(qsel));
  2314. end;
  2315. Function IsATTY(Handle:Longint):Boolean;
  2316. {
  2317. Check if the filehandle described by 'handle' is a TTY (Terminal)
  2318. }
  2319. var
  2320. t : Termios;
  2321. begin
  2322. IsAtty:=TCGetAttr(Handle,t);
  2323. end;
  2324. Function IsATTY(f: text):Boolean;
  2325. {
  2326. Idem as previous, only now for text variables.
  2327. }
  2328. begin
  2329. IsATTY:=IsaTTY(textrec(f).handle);
  2330. end;
  2331. function TTYName(Handle:Longint):string;
  2332. {
  2333. Return the name of the current tty described by handle f.
  2334. returns empty string in case of an error.
  2335. }
  2336. Const
  2337. dev='/dev';
  2338. var
  2339. name : string;
  2340. st : stat;
  2341. mydev,
  2342. myino : longint;
  2343. dirstream : pdir;
  2344. d : pdirent;
  2345. begin
  2346. TTYName:='';
  2347. fstat(handle,st);
  2348. if (errno<>0) and isatty (handle) then
  2349. exit;
  2350. mydev:=st.dev;
  2351. myino:=st.ino;
  2352. dirstream:=opendir(dev);
  2353. if (linuxerror<>0) then
  2354. exit;
  2355. d:=Readdir(dirstream);
  2356. while (d<>nil) do
  2357. begin
  2358. if (d^.ino=myino) then
  2359. begin
  2360. name:=dev+'/'+strpas(@(d^.name));
  2361. fstat(name,st);
  2362. if (linuxerror=0) and (st.dev=mydev) then
  2363. begin
  2364. closedir(dirstream);
  2365. ttyname:=name;
  2366. exit;
  2367. end;
  2368. end;
  2369. d:=Readdir(dirstream);
  2370. end;
  2371. closedir(dirstream);
  2372. end;
  2373. function TTYName(var F:Text):string;
  2374. {
  2375. Idem as previous, only now for text variables;
  2376. }
  2377. begin
  2378. TTYName:=TTYName(textrec(f).handle);
  2379. end;
  2380. {******************************************************************************
  2381. Utility calls
  2382. ******************************************************************************}
  2383. Function Octal(l:longint):longint;
  2384. {
  2385. Convert an octal specified number to decimal;
  2386. }
  2387. var
  2388. octnr,
  2389. oct : longint;
  2390. begin
  2391. octnr:=0;
  2392. oct:=0;
  2393. while (l>0) do
  2394. begin
  2395. oct:=oct or ((l mod 10) shl octnr);
  2396. l:=l div 10;
  2397. inc(octnr,3);
  2398. end;
  2399. Octal:=oct;
  2400. end;
  2401. Function StringToPPChar(Var S:STring):ppchar;
  2402. {
  2403. Create a PPChar to structure of pchars which are the arguments specified
  2404. in the string S. Especially usefull for creating an ArgV for Exec-calls
  2405. }
  2406. var
  2407. nr : longint;
  2408. Buf : ^char;
  2409. p : ppchar;
  2410. begin
  2411. s:=s+#0;
  2412. buf:=@s[1];
  2413. nr:=0;
  2414. while(buf^<>#0) do
  2415. begin
  2416. while (buf^ in [' ',#8,#10]) do
  2417. buf:=buf+1;
  2418. inc(nr);
  2419. while not (buf^ in [' ',#0,#8,#10]) do
  2420. buf:=buf+1;
  2421. end;
  2422. getmem(p,nr*4);
  2423. StringToPPChar:=p;
  2424. if p=nil then
  2425. begin
  2426. LinuxError:=sys_enomem;
  2427. exit;
  2428. end;
  2429. buf:=@s[1];
  2430. while (buf^<>#0) do
  2431. begin
  2432. while (buf^ in [' ',#8,#10]) do
  2433. begin
  2434. buf^:=#0;
  2435. buf:=buf+1;
  2436. end;
  2437. p^:=buf;
  2438. p:=p+4;
  2439. p^:=nil;
  2440. while not (buf^ in [' ',#0,#8,#10]) do
  2441. buf:=buf+1;
  2442. end;
  2443. end;
  2444. Function FExpand(Const Path:PathStr):PathStr;
  2445. var
  2446. temp : pathstr;
  2447. i,j,k : longint;
  2448. p : pchar;
  2449. Begin
  2450. {Remove eventual drive - doesn't exist in Linux}
  2451. if path[2]=':' then
  2452. i:=3
  2453. else
  2454. i:=1;
  2455. temp:='';
  2456. {Replace ~/ with $HOME}
  2457. if (path[i]='~') and ((i+1>length(path)) or (path[i+1]='/')) then
  2458. begin
  2459. p:=getenv('HOME');
  2460. if not (p=nil) then
  2461. Insert(StrPas(p),temp,i);
  2462. i:=1;
  2463. temp:=temp+Copy(Path,2,255);
  2464. end;
  2465. {Do we have an absolute path ? No - prefix the current dir}
  2466. if temp='' then
  2467. begin
  2468. if path[i]<>'/' then
  2469. begin
  2470. getdir(0,temp);
  2471. k:=ioresult;
  2472. end
  2473. else
  2474. inc(i);
  2475. temp:=temp+'/'+copy(path,i,length(path)-i+1)+'/';
  2476. end;
  2477. {First remove all references to '/./'}
  2478. while pos('/./',temp)<>0 do
  2479. delete(temp,pos('/./',temp),2);
  2480. {Now remove also all references to '/../' + of course previous dirs..}
  2481. repeat
  2482. i:=pos('/../',temp);
  2483. {Find the pos of the previous dir}
  2484. if i>1 then
  2485. begin
  2486. j:=i-1;
  2487. while (j>1) and (temp[j]<>'/') do
  2488. dec (j);{temp[1] is always '/'}
  2489. delete(temp,j,i-j+3);
  2490. end
  2491. else
  2492. if i=1 then {i=1, so we have temp='/../something', just delete '/../'}
  2493. delete(temp,1,3);
  2494. until i=0;
  2495. { Remove ending /.. }
  2496. i:=pos('/..',temp);
  2497. if (i<>0) and (i =length(temp)-2) then
  2498. begin
  2499. j:=i-1;
  2500. while (j>1) and (temp[j]<>'/') do
  2501. dec (j);
  2502. delete (temp,j,i-j+3);
  2503. end;
  2504. { if last character is / then remove it - dir is also a file :-) }
  2505. if (length(temp)>0) and (temp[length(temp)]='/') then
  2506. dec(byte(temp[0]));
  2507. fexpand:=temp;
  2508. End;
  2509. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  2510. {
  2511. Searches for a file 'path' in the list of direcories in 'dirlist'.
  2512. returns an empty string if not found. Wildcards are NOT allowed.
  2513. If dirlist is empty, it is set to '.'
  2514. }
  2515. Var
  2516. NewDir : PathStr;
  2517. p1 : Longint;
  2518. Info : Stat;
  2519. Begin
  2520. {Replace ':' with ';'}
  2521. for p1:=1to length(dirlist) do
  2522. if dirlist[p1]=':' then
  2523. dirlist[p1]:=';';
  2524. {Check for WildCards}
  2525. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  2526. FSearch:='' {No wildcards allowed in these things.}
  2527. Else
  2528. Begin
  2529. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  2530. Repeat
  2531. p1:=Pos(';',DirList);
  2532. If p1=0 Then
  2533. p1:=255;
  2534. NewDir:=Copy(DirList,1,P1 - 1);
  2535. if NewDir[Length(NewDir)]<>'/' then
  2536. NewDir:=NewDir+'/';
  2537. NewDir:=NewDir+Path;
  2538. Delete(DirList,1,p1);
  2539. if FStat(NewDir,Info) then
  2540. Begin
  2541. If Pos('./',NewDir)=1 Then
  2542. Delete(NewDir,1,2);
  2543. {DOS strips off an initial .\}
  2544. End
  2545. Else
  2546. NewDir:='';
  2547. Until (DirList='') or (Length(NewDir) > 0);
  2548. FSearch:=NewDir;
  2549. End;
  2550. End;
  2551. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  2552. Var
  2553. DotPos,SlashPos,i : longint;
  2554. Begin
  2555. SlashPos:=0;
  2556. DotPos:=256;
  2557. i:=Length(Path);
  2558. While (i>0) and (SlashPos=0) Do
  2559. Begin
  2560. If (DotPos=256) and (Path[i]='.') Then
  2561. DotPos:=i;
  2562. If (Path[i]='/') Then
  2563. SlashPos:=i;
  2564. Dec(i);
  2565. End;
  2566. Ext:=Copy(Path,DotPos,255);
  2567. Dir:=Copy(Path,1,SlashPos);
  2568. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  2569. End;
  2570. Function Dirname(Const path:pathstr):pathstr;
  2571. {
  2572. This function returns the directory part of a complete path.
  2573. Unless the directory is root '/', The last character is not
  2574. a slash.
  2575. }
  2576. var
  2577. Dir : PathStr;
  2578. Name : NameStr;
  2579. Ext : ExtStr;
  2580. begin
  2581. FSplit(Path,Dir,Name,Ext);
  2582. if length(Dir)>1 then
  2583. Delete(Dir,length(Dir),1);
  2584. DirName:=Dir;
  2585. end;
  2586. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  2587. {
  2588. This function returns the filename part of a complete path. If suf is
  2589. supplied, it is cut off the filename.
  2590. }
  2591. var
  2592. Dir : PathStr;
  2593. Name : NameStr;
  2594. Ext : ExtStr;
  2595. begin
  2596. FSplit(Path,Dir,Name,Ext);
  2597. if Suf<>Ext then
  2598. Name:=Name+Ext;
  2599. BaseName:=Name;
  2600. end;
  2601. Function FNMatch(const Pattern,Name:string):Boolean;
  2602. Var
  2603. LenPat,LenName : longint;
  2604. Function DoFNMatch(i,j:longint):Boolean;
  2605. Var
  2606. Found : boolean;
  2607. Begin
  2608. Found:=true;
  2609. While Found and (i<=LenPat) Do
  2610. Begin
  2611. Case Pattern[i] of
  2612. '?' : Found:=(j<=LenName);
  2613. '*' : Begin
  2614. {find the next character in pattern, different of ? and *}
  2615. while Found and (i<LenPat) do
  2616. begin
  2617. inc(i);
  2618. case Pattern[i] of
  2619. '*' : ;
  2620. '?' : begin
  2621. inc(j);
  2622. Found:=(j<=LenName);
  2623. end;
  2624. else
  2625. Found:=false;
  2626. end;
  2627. end;
  2628. {Now, find in name the character which i points to, if the * or ?
  2629. wasn't the last character in the pattern, else, use up all the
  2630. chars in name}
  2631. Found:=true;
  2632. if (i<=LenPat) then
  2633. begin
  2634. repeat
  2635. {find a letter (not only first !) which maches pattern[i]}
  2636. while (j<=LenName) and (name[j]<>pattern[i]) do
  2637. inc (j);
  2638. if (j<LenName) then
  2639. begin
  2640. if DoFnMatch(i+1,j+1) then
  2641. begin
  2642. i:=LenPat;
  2643. j:=LenName;{we can stop}
  2644. Found:=true;
  2645. end
  2646. else
  2647. inc(j);{We didn't find one, need to look further}
  2648. end;
  2649. until (j>=LenName);
  2650. end
  2651. else
  2652. j:=LenName;{we can stop}
  2653. end;
  2654. else {not a wildcard character in pattern}
  2655. Found:=(j<=LenName) and (pattern[i]=name[j]);
  2656. end;
  2657. inc(i);
  2658. inc(j);
  2659. end;
  2660. DoFnMatch:=Found and (j>LenName);
  2661. end;
  2662. Begin {start FNMatch}
  2663. LenPat:=Length(Pattern);
  2664. LenName:=Length(Name);
  2665. FNMatch:=DoFNMatch(1,1);
  2666. End;
  2667. Procedure Globfree(var p : pglob);
  2668. {
  2669. Release memory occupied by pglob structure, and names in it.
  2670. sets p to nil.
  2671. }
  2672. var
  2673. temp : pglob;
  2674. begin
  2675. while p<>nil do
  2676. begin
  2677. temp:=p^.next;
  2678. if p^.name<>nil then
  2679. freemem(p^.name,strlen(p^.name));
  2680. dispose(p);
  2681. p:=temp;
  2682. end;
  2683. end;
  2684. Function Glob(Const path:pathstr):pglob;
  2685. {
  2686. Fills a tglob structure with entries matching path,
  2687. and returns a pointer to it. Returns nil on error,
  2688. linuxerror is set accordingly.
  2689. }
  2690. var
  2691. temp : string[255];
  2692. thedir : pdir;
  2693. buffer : pdirent;
  2694. root,run : pglob;
  2695. begin
  2696. { Get directory }
  2697. if dirname(path)='' then
  2698. temp:='.'
  2699. else
  2700. temp:=dirname(path);
  2701. temp:=temp+#0;
  2702. thedir:=opendir(@temp[1]);
  2703. if thedir=nil then
  2704. begin
  2705. glob:=nil;
  2706. linuxerror:=errno;
  2707. exit;
  2708. end;
  2709. temp:=basename(path,'');{ get the pattern }
  2710. if thedir^.fd<0 then
  2711. begin
  2712. linuxerror:=errno;
  2713. glob:=nil;
  2714. exit;
  2715. end;
  2716. {get the entries}
  2717. new(root);
  2718. root^.next:=nil;
  2719. root^.name:=nil;
  2720. run:=root;
  2721. repeat
  2722. buffer:=Sys_readdir(thedir);
  2723. if buffer<>nil then
  2724. begin
  2725. if fnmatch(temp,strpas(@(buffer^.name[0]))) then
  2726. begin
  2727. { get memory for pglob }
  2728. new(run^.next);
  2729. if run^.next=nil then
  2730. begin
  2731. linuxerror:=Sys_ENOMEM;
  2732. globfree(root);
  2733. glob:=nil;
  2734. exit;
  2735. end
  2736. else
  2737. begin
  2738. run:=run^.next;
  2739. run^.next:=nil;
  2740. end;
  2741. { Get memory for name }
  2742. getmem(run^.name,strlen(@(buffer^.name[0]))+1);
  2743. if run^.name=nil then
  2744. begin
  2745. linuxerror:=Sys_ENOMEM;
  2746. globfree(root);
  2747. glob:=nil;
  2748. exit;
  2749. end;
  2750. move(buffer^.name[0],run^.name^,strlen(@(buffer^.name[0]))+1);
  2751. end;{ if fnmatch }
  2752. end { buffer <> nil }
  2753. else
  2754. begin
  2755. run:=root;
  2756. if root^.next<>nil then
  2757. root:=root^.next;{ put root on first entry}
  2758. if run<>nil then
  2759. begin
  2760. run^.next:=nil;
  2761. globfree(run);
  2762. end;
  2763. end;
  2764. until buffer=nil;
  2765. if root^.name=nil then
  2766. begin
  2767. dispose(root);
  2768. linuxerror:=0;
  2769. glob:=nil;
  2770. end
  2771. else
  2772. glob:=root;
  2773. end;
  2774. {--------------------------------
  2775. FiledescriptorSets
  2776. --------------------------------}
  2777. Procedure FD_Zero(var fds:fdSet);
  2778. {
  2779. Clear the set of filedescriptors
  2780. }
  2781. begin
  2782. FillChar(fds,sizeof(fdSet),0);
  2783. end;
  2784. Procedure FD_Clr(fd:longint;var fds:fdSet);
  2785. {
  2786. Remove fd from the set of filedescriptors
  2787. }
  2788. begin
  2789. fds[fd shr 5]:=fds[fd shr 5] and (not (1 shl (fd and 31)));
  2790. end;
  2791. Procedure FD_Set(fd:longint;var fds:fdSet);
  2792. {
  2793. Add fd to the set of filedescriptors
  2794. }
  2795. begin
  2796. fds[fd shr 5]:=fds[fd shr 5] or (1 shl (fd and 31));
  2797. end;
  2798. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  2799. {
  2800. Test if fd is part of the set of filedescriptors
  2801. }
  2802. begin
  2803. FD_IsSet:=((fds[fd shr 5] and (1 shl (fd and 31)))<>0);
  2804. end;
  2805. Function GetFS (var T:Text):longint;
  2806. {
  2807. Get File Descriptor of a text file.
  2808. }
  2809. begin
  2810. if textrec(t).mode=fmclosed then
  2811. exit(-1)
  2812. else
  2813. GETFS:=textrec(t).Handle
  2814. end;
  2815. Function GetFS(Var F:File):longint;
  2816. {
  2817. Get File Descriptor of an unTyped file.
  2818. }
  2819. begin
  2820. { Handle and mode are on the same place in textrec and filerec. }
  2821. if filerec(f).mode=fmclosed then
  2822. exit(-1)
  2823. else
  2824. GETFS:=filerec(f).Handle
  2825. end;
  2826. {--------------------------------
  2827. Stat.Mode Macro's
  2828. --------------------------------}
  2829. Function S_ISLNK(m:word):boolean;
  2830. {
  2831. Check mode field of inode for link.
  2832. }
  2833. begin
  2834. S_ISLNK:=(m and STAT_IFMT)=STAT_IFLNK;
  2835. end;
  2836. Function S_ISREG(m:word):boolean;
  2837. {
  2838. Check mode field of inode for regular file.
  2839. }
  2840. begin
  2841. S_ISREG:=(m and STAT_IFMT)=STAT_IFREG;
  2842. end;
  2843. Function S_ISDIR(m:word):boolean;
  2844. {
  2845. Check mode field of inode for directory.
  2846. }
  2847. begin
  2848. S_ISDIR:=(m and STAT_IFMT)=STAT_IFDIR;
  2849. end;
  2850. Function S_ISCHR(m:word):boolean;
  2851. {
  2852. Check mode field of inode for character device.
  2853. }
  2854. begin
  2855. S_ISCHR:=(m and STAT_IFMT)=STAT_IFCHR;
  2856. end;
  2857. Function S_ISBLK(m:word):boolean;
  2858. {
  2859. Check mode field of inode for block device.
  2860. }
  2861. begin
  2862. S_ISBLK:=(m and STAT_IFMT)=STAT_IFBLK;
  2863. end;
  2864. Function S_ISFIFO(m:word):boolean;
  2865. {
  2866. Check mode field of inode for named pipe (FIFO).
  2867. }
  2868. begin
  2869. S_ISFIFO:=(m and STAT_IFMT)=STAT_IFIFO;
  2870. end;
  2871. Function S_ISSOCK(m:word):boolean;
  2872. {
  2873. Check mode field of inode for socket.
  2874. }
  2875. begin
  2876. S_ISSOCK:=(m and STAT_IFMT)=STAT_IFSOCK;
  2877. end;
  2878. {--------------------------------
  2879. Port IO functions
  2880. --------------------------------}
  2881. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  2882. {
  2883. Set permissions on NUM ports starting with port FROM to VALUE
  2884. this works ONLY as root.
  2885. }
  2886. Var
  2887. Sr : Syscallregs;
  2888. begin
  2889. Sr.Reg2:=From;
  2890. Sr.Reg3:=Num;
  2891. Sr.Reg4:=Value;
  2892. IOPerm:=Syscall(Syscall_nr_ioperm,sr)=0;
  2893. LinuxError:=Errno;
  2894. end;
  2895. {$IFDEF I386}
  2896. {$asmmode direct}
  2897. Procedure WritePort (Port : Longint; Value : Byte);
  2898. {
  2899. Writes 'Value' to port 'Port'
  2900. }
  2901. begin
  2902. asm
  2903. movl 8(%ebp),%edx
  2904. movb 12(%ebp),%al
  2905. outb %al,%dx
  2906. end;
  2907. end;
  2908. Procedure WritePort (Port : Longint; Value : Word);
  2909. {
  2910. Writes 'Value' to port 'Port'
  2911. }
  2912. begin
  2913. asm
  2914. movl 8(%ebp),%edx
  2915. movw 12(%ebp),%ax
  2916. outw %ax,%dx
  2917. end ['EAX','EBX'];
  2918. end;
  2919. Procedure WritePort (Port : Longint; Value : Longint);
  2920. {
  2921. Writes 'Value' to port 'Port'
  2922. }
  2923. begin
  2924. asm
  2925. movl 8(%ebp),%edx
  2926. movl 12(%ebp),%eax
  2927. outl %eax,%dx
  2928. end ['EAX','EBX'];
  2929. end;
  2930. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  2931. {
  2932. Writes 'Count' longints from 'Buf' to Port
  2933. }
  2934. begin
  2935. asm
  2936. movl 16(%ebp),%ecx
  2937. movl 12(%ebp),%esi
  2938. movl 8(%ebp),%edx
  2939. cld
  2940. rep
  2941. outsl
  2942. end ['ECX','ESI','EDX'];
  2943. end;
  2944. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  2945. {
  2946. Writes 'Count' words from 'Buf' to Port
  2947. }
  2948. begin
  2949. asm
  2950. movl 16(%ebp),%ecx
  2951. movl 12(%ebp),%esi
  2952. movl 8(%ebp),%edx
  2953. cld
  2954. rep
  2955. outsw
  2956. end ['ECX','ESI','EDX'];
  2957. end;
  2958. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  2959. {
  2960. Writes 'Count' bytes from 'Buf' to Port
  2961. }
  2962. begin
  2963. asm
  2964. movl 16(%ebp),%ecx
  2965. movl 12(%ebp),%esi
  2966. movl 8(%ebp),%edx
  2967. cld
  2968. rep
  2969. outsb
  2970. end ['ECX','ESI','EDX'];
  2971. end;
  2972. Procedure ReadPort (Port : Longint; Var Value : Byte);
  2973. {
  2974. Reads 'Value' from port 'Port'
  2975. }
  2976. begin
  2977. asm
  2978. movl 8(%ebp),%edx
  2979. inb %dx,%al
  2980. andl $255,%eax
  2981. movl %eax,12(%ebp)
  2982. end ['EAX','EBX'];
  2983. end;
  2984. Procedure ReadPort (Port : Longint; Var Value : Word);
  2985. {
  2986. Reads 'Value' from port 'Port'
  2987. }
  2988. begin
  2989. asm
  2990. movl 8(%ebp),%edx
  2991. inw %dx,%ax
  2992. andl $65535,%eax
  2993. movl %eax,12(%ebp)
  2994. end ['EAX','EBX'];
  2995. end;
  2996. Procedure ReadPort (Port : Longint; Var Value : Longint);
  2997. {
  2998. Reads 'Value' from port 'Port'
  2999. }
  3000. begin
  3001. asm
  3002. movl 8(%ebp),%edx
  3003. inl %dx,%eax
  3004. movl %eax,12(%ebp)
  3005. end ['EAX','EBX'];
  3006. end;
  3007. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  3008. {
  3009. Reads 'Count' longints from port 'Port' to 'Buf'.
  3010. }
  3011. begin
  3012. asm
  3013. movl 16(%ebp),%ecx
  3014. movl 12(%ebp),%edi
  3015. movl 8(%ebp),%edx
  3016. cld
  3017. rep
  3018. insl
  3019. end ['ECX','ESI','EDX'];
  3020. end;
  3021. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  3022. {
  3023. Reads 'Count' words from port 'Port' to 'Buf'.
  3024. }
  3025. begin
  3026. asm
  3027. movl 16(%ebp),%ecx
  3028. movl 12(%ebp),%edi
  3029. movl 8(%ebp),%edx
  3030. cld
  3031. rep
  3032. insw
  3033. end ['ECX','ESI','EDX'];
  3034. end;
  3035. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  3036. {
  3037. Reads 'Count' bytes from port 'Port' to 'Buf'.
  3038. }
  3039. begin
  3040. asm
  3041. movl 16(%ebp),%ecx
  3042. movl 12(%ebp),%edi
  3043. movl 8(%ebp),%edx
  3044. cld
  3045. rep
  3046. insb
  3047. end ['ECX','ESI','EDX'];
  3048. end;
  3049. {$ENDIF}
  3050. Begin
  3051. InitEpochToLocal;
  3052. End.
  3053. {
  3054. $Log$
  3055. Revision 1.39 1999-05-30 11:37:27 peter
  3056. * clone function like the libc version
  3057. + sigraise, exitprocess
  3058. Revision 1.38 1999/05/26 11:05:24 michael
  3059. * fcntl needs file as Var argument
  3060. Revision 1.37 1999/05/10 09:13:41 peter
  3061. * fixed typo
  3062. Revision 1.36 1999/05/08 19:47:24 peter
  3063. * check ioresult after getdir calls
  3064. Revision 1.35 1999/04/22 14:48:55 peter
  3065. * fixed direct asm
  3066. Revision 1.34 1999/03/29 16:03:10 peter
  3067. + clone()
  3068. Revision 1.33 1999/03/11 12:02:03 peter
  3069. * s_is<x> functions have now word para instead of integer
  3070. Revision 1.32 1999/02/22 12:50:53 peter
  3071. * fixed dup() to have var para
  3072. Revision 1.31 1999/02/22 11:47:42 peter
  3073. * fixed juliantogregorian (mailinglist)
  3074. Revision 1.29 1999/02/02 21:19:54 michael
  3075. Corrected wrong mode error in fdopen
  3076. Revision 1.28 1999/01/20 13:29:09 peter
  3077. * utimbuf alias added
  3078. Revision 1.27 1998/12/11 18:08:57 peter
  3079. * fixed assignstream
  3080. Revision 1.26 1998/11/24 15:30:12 michael
  3081. * Bugfix in assignstream. . Now wait is performed upon close
  3082. Revision 1.25 1998/11/16 10:21:28 peter
  3083. * fixes for H+
  3084. Revision 1.24 1998/11/10 14:57:53 peter
  3085. * renamed rename -> FRename
  3086. Revision 1.23 1998/10/30 15:47:11 peter
  3087. * fixed glob, which did not reset the root
  3088. Revision 1.22 1998/10/23 00:05:32 peter
  3089. * getenv with envp=nil check
  3090. Revision 1.21 1998/10/15 08:31:11 peter
  3091. * type aliases using delphi typenaming
  3092. Revision 1.20 1998/10/11 12:23:11 michael
  3093. + Implemented Rename
  3094. Revision 1.19 1998/09/18 09:56:33 peter
  3095. * merged
  3096. Revision 1.18.2.1 1998/09/18 09:53:46 peter
  3097. * fixed winsize record
  3098. Revision 1.18 1998/09/08 13:01:51 michael
  3099. + Signal call now correctly implemented
  3100. Revision 1.17 1998/08/19 00:50:31 peter
  3101. * 'i<>0 and ' needs brackets
  3102. Revision 1.16 1998/08/16 10:23:28 michael
  3103. fixed typos
  3104. Revision 1.15 1998/08/16 09:12:14 michael
  3105. Corrected fexpand behaviour.
  3106. Revision 1.14 1998/08/14 12:01:04 carl
  3107. * ifdef i386 for ports access
  3108. Revision 1.13 1998/08/12 11:10:25 michael
  3109. Added settimeofday function
  3110. Revision 1.12 1998/07/28 09:27:06 michael
  3111. restored previous version. A bug in the compiler prevents compilation.
  3112. Revision 1.10 1998/06/16 08:21:58 michael
  3113. * PClose didn't flush textfiles before closing. Now it does
  3114. Revision 1.9 1998/06/03 11:55:33 michael
  3115. + Added IO port calls
  3116. Revision 1.8 1998/05/06 18:45:32 peter
  3117. * fixed the shell() bug (the correct code was also in Popen) moved the
  3118. argv generation to CreateShellArgv
  3119. + Execve with pchar instead of string
  3120. Revision 1.7 1998/05/06 12:35:26 michael
  3121. + Removed log from before restored version.
  3122. Revision 1.6 1998/04/15 11:23:53 michael
  3123. + Added some calls to make common API more efficient
  3124. Revision 1.5 1998/04/10 15:23:03 michael
  3125. + Pclose now returns exit status of process
  3126. Revision 1.4 1998/04/07 13:08:29 michael
  3127. + Added flock for file locking
  3128. Revision 1.3 1998/04/07 12:27:41 peter
  3129. * fixed fexpand('..')
  3130. Revision 1.2 1998/04/04 17:07:17 michael
  3131. + Fixed AssignStream, it completely refused to work
  3132. }