linux.pp 71 KB

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