linux.pp 72 KB

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