linux.pp 71 KB

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