2
0

linux.pp 71 KB

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