unix.pp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. BSD parts (c) 2000 by Marco van de Voort
  6. members of the Free Pascal development team.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY;without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. Unit Unix;
  14. Interface
  15. { Get Types and Constants }
  16. {$i sysconst.inc}
  17. {$i systypes.inc}
  18. { Get System call numbers and error-numbers}
  19. {$i sysnr.inc}
  20. {$i errno.inc}
  21. {$I signal.inc}
  22. var
  23. ErrNo,
  24. LinuxError : Longint;
  25. {********************
  26. Process
  27. ********************}
  28. const
  29. { For getting/setting priority }
  30. Prio_Process = 0;
  31. Prio_PGrp = 1;
  32. Prio_User = 2;
  33. {$ifdef Solaris}
  34. WNOHANG = $100;
  35. WUNTRACED = $4;
  36. {$ELSE}
  37. WNOHANG = $1;
  38. WUNTRACED = $2;
  39. __WCLONE = $80000000;
  40. {$ENDIF}
  41. {********************
  42. File
  43. ********************}
  44. Const
  45. P_IN = 1;
  46. P_OUT = 2;
  47. Const
  48. LOCK_SH = 1;
  49. LOCK_EX = 2;
  50. LOCK_UN = 8;
  51. LOCK_NB = 4;
  52. Type
  53. Tpipe = array[1..2] of longint;
  54. pglob = ^tglob;
  55. tglob = record
  56. name : pchar;
  57. next : pglob;
  58. end;
  59. ComStr = String[255];
  60. PathStr = String[255];
  61. DirStr = String[255];
  62. NameStr = String[255];
  63. ExtStr = String[255];
  64. const
  65. { For testing access rights }
  66. R_OK = 4;
  67. W_OK = 2;
  68. X_OK = 1;
  69. F_OK = 0;
  70. { For File control mechanism }
  71. F_GetFd = 1;
  72. F_SetFd = 2;
  73. F_GetFl = 3;
  74. F_SetFl = 4;
  75. {$ifdef Solaris}
  76. F_DupFd = 0;
  77. F_Dup2Fd = 9;
  78. F_GetOwn = 23;
  79. F_SetOwn = 24;
  80. F_GetLk = 14;
  81. F_SetLk = 6;
  82. F_SetLkW = 7;
  83. F_FreeSp = 11;
  84. {$else}
  85. F_GetLk = 5;
  86. F_SetLk = 6;
  87. F_SetLkW = 7;
  88. F_SetOwn = 8;
  89. F_GetOwn = 9;
  90. {$endif}
  91. {********************
  92. IOCtl(TermIOS)
  93. ********************}
  94. {Is too freebsd/Linux specific}
  95. {$I termios.inc}
  96. {********************
  97. Info
  98. ********************}
  99. Type
  100. UTimBuf = packed record{in BSD array[0..1] of timeval, but this is
  101. backwards compatible with linux version}
  102. actime,
  103. {$ifdef BSD}
  104. uactime, {BSD Micro seconds}
  105. {$endif}
  106. modtime
  107. {$ifdef BSD}
  108. ,
  109. umodtime {BSD Micro seconds}
  110. {$endif}
  111. : longint;
  112. end;
  113. UTimeBuf=UTimBuf;
  114. TUTimeBuf=UTimeBuf;
  115. PUTimeBuf=^UTimeBuf;
  116. TSysinfo = packed record
  117. uptime : longint;
  118. loads : array[1..3] of longint;
  119. totalram,
  120. freeram,
  121. sharedram,
  122. bufferram,
  123. totalswap,
  124. freeswap : longint;
  125. procs : integer;
  126. s : string[18];
  127. end;
  128. PSysInfo = ^TSysInfo;
  129. {******************************************************************************
  130. Procedure/Functions
  131. ******************************************************************************}
  132. {$ifdef bsd}
  133. function Do_SysCall(sysnr:longint):longint;
  134. function Do_Syscall(sysnr,param1:integer):longint;
  135. function Do_SysCall(sysnr,param1:LONGINT):longint;
  136. function Do_SysCall(sysnr,param1,param2:LONGINT):longint;
  137. function Do_SysCall(sysnr,param1,param2,param3:LONGINT):longint;
  138. function Do_SysCall(sysnr,param1,param2,param3,param4:LONGINT):longint;
  139. function Do_SysCall(sysnr,param1,param2,param3,param4,param5:LONGINT):longint;
  140. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6:LONGINT):longint;
  141. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6,param7:LONGINT):longint;
  142. {$else}
  143. Function SysCall(callnr:longint;var regs:SysCallregs):longint;
  144. {$endif}
  145. {**************************
  146. Time/Date Handling
  147. ***************************}
  148. var
  149. tzdaylight : boolean;
  150. tzseconds : longint;
  151. tzname : array[boolean] of pchar;
  152. { timezone support }
  153. procedure GetLocalTimezone(timer:longint;var leap_correct,leap_hit:longint);
  154. procedure GetLocalTimezone(timer:longint);
  155. procedure ReadTimezoneFile(fn:string);
  156. function GetTimezoneFile:string;
  157. Procedure GetTimeOfDay(var tv:timeval);
  158. Function GetTimeOfDay:longint;
  159. Function GetEpochTime: longint;
  160. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  161. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  162. procedure GetTime(var hour,min,sec,msec,usec:word);
  163. procedure GetTime(var hour,min,sec,sec100:word);
  164. procedure GetTime(var hour,min,sec:word);
  165. Procedure GetDate(Var Year,Month,Day:Word);
  166. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  167. function SetTime(Hour,Min,Sec:word) : Boolean;
  168. function SetDate(Year,Month,Day:Word) : Boolean;
  169. function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  170. {**************************
  171. Process Handling
  172. ***************************}
  173. function CreateShellArgV(const prog:string):ppchar;
  174. function CreateShellArgV(const prog:Ansistring):ppchar;
  175. Procedure Execve(Path: pathstr;args:ppchar;ep:ppchar);
  176. Procedure Execve(Path: AnsiString;args:ppchar;ep:ppchar);
  177. Procedure Execve(path: pchar;args:ppchar;ep:ppchar);
  178. Procedure Execv(const path:pathstr;args:ppchar);
  179. Procedure Execv(const path: AnsiString;args:ppchar);
  180. Procedure Execvp(Path: Pathstr;Args:ppchar;Ep:ppchar);
  181. Procedure Execvp(Path: AnsiString; Args:ppchar;Ep:ppchar);
  182. Procedure Execl(const Todo: String);
  183. Procedure Execl(const Todo: Ansistring);
  184. Procedure Execle(Todo: String;Ep:ppchar);
  185. Procedure Execle(Todo: AnsiString;Ep:ppchar);
  186. Procedure Execlp(Todo: string;Ep:ppchar);
  187. Procedure Execlp(Todo: Ansistring;Ep:ppchar);
  188. Function Shell(const Command:String):Longint;
  189. Function Shell(const Command:AnsiString):Longint;
  190. Function Fork:longint;
  191. {Clone for FreeBSD is copied from the LinuxThread port, and rfork based}
  192. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  193. Procedure ExitProcess(val:longint);
  194. Function WaitPid(Pid:longint;Status:pointer;Options:Longint):Longint; {=>PID (Status Valid), 0 (No Status), -1: Error, special case errno=EINTR }
  195. Function WaitProcess(Pid:longint):Longint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  196. Procedure Nice(N:integer);
  197. function WEXITSTATUS(Status: Integer): Integer;
  198. function WTERMSIG(Status: Integer): Integer;
  199. function WSTOPSIG(Status: Integer): Integer;
  200. Function WIFEXITED(Status: Integer): Boolean;
  201. Function WIFSTOPPED(Status: Integer): Boolean;
  202. Function WIFSIGNALED(Status: Integer): Boolean;
  203. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  204. Function W_STOPCODE(Signal: Integer): Integer;
  205. {$ifdef bsd}
  206. Function GetPriority(Which,Who:longint):longint;
  207. procedure SetPriority(Which,Who,What:longint);
  208. {$else}
  209. Function GetPriority(Which,Who:Integer):integer;
  210. Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
  211. {$endif}
  212. Function GetPid:LongInt;
  213. Function GetPPid:LongInt;
  214. Function GetUid:Longint;
  215. Function GetEUid:Longint;
  216. Function GetGid:Longint;
  217. Function GetEGid:Longint;
  218. {**************************
  219. File Handling
  220. ***************************}
  221. Function fdOpen(pathname:string;flags:longint):longint;
  222. Function fdOpen(pathname:string;flags,mode:longint):longint;
  223. Function fdOpen(pathname:pchar;flags:longint):longint;
  224. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  225. Function fdClose(fd:longint):boolean;
  226. Function fdRead(fd:longint;var buf;size:longint):longint;
  227. Function fdWrite(fd:longint;const buf;size:longint):longint;
  228. Function fdTruncate(fd,size:longint):boolean;
  229. Function fdSeek (fd,pos,seektype :longint): longint;
  230. Function fdFlush (fd : Longint) : Boolean;
  231. Function Link(OldPath,NewPath:pathstr):boolean;
  232. Function SymLink(OldPath,NewPath:pathstr):boolean;
  233. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  234. Function ReadLink(name:pathstr):pathstr;
  235. Function UnLink(Path:pathstr):boolean;
  236. Function UnLink(Path:pchar):Boolean;
  237. Function FReName (OldName,NewName : Pchar) : Boolean;
  238. Function FReName (OldName,NewName : String) : Boolean;
  239. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  240. Function Chmod(path:pathstr;Newmode:longint):boolean;
  241. Function Utime(path:pathstr;utim:utimebuf):boolean;
  242. {$ifdef BSD}
  243. Function Access(Path:Pathstr ;mode:longint):boolean;
  244. {$else}
  245. Function Access(Path:Pathstr ;mode:integer):boolean;
  246. {$endif}
  247. Function Umask(Mask:Integer):integer;
  248. Function Flock (fd,mode : longint) : boolean;
  249. Function Flock (var T : text;mode : longint) : boolean;
  250. Function Flock (var F : File;mode : longint) : boolean;
  251. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  252. Function FStat(Fd:longint;Var Info:stat):Boolean;
  253. Function FStat(var F:Text;Var Info:stat):Boolean;
  254. Function FStat(var F:File;Var Info:stat):Boolean;
  255. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  256. Function StatFS(Path:Pathstr;Var Info:tstatfs):Boolean;
  257. Function StatFS(Fd: Longint;Var Info:tstatfs):Boolean;
  258. {$ifdef bsd}
  259. Function Fcntl(Fd:longint;Cmd:longint):longint;
  260. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  261. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  262. Procedure Fcntl(var Fd:Text;Cmd:longint;Arg:Longint);
  263. {$else}
  264. Function Fcntl(Fd:longint;Cmd:Integer):integer;
  265. Procedure Fcntl(Fd:longint;Cmd:Integer;Arg:Longint);
  266. Function Fcntl(var Fd:Text;Cmd:Integer):integer;
  267. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  268. {$endif}
  269. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  270. Function Dup(var oldfile,newfile:text):Boolean;
  271. Function Dup(var oldfile,newfile:file):Boolean;
  272. Function Dup2(oldfile,newfile:longint):Boolean;
  273. Function Dup2(var oldfile,newfile:text):Boolean;
  274. Function Dup2(var oldfile,newfile:file):Boolean;
  275. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
  276. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  277. Function SelectText(var T:Text;TimeOut :PTimeVal):Longint;
  278. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  279. {**************************
  280. Directory Handling
  281. ***************************}
  282. Function OpenDir(f:pchar):pdir;
  283. Function OpenDir(f: String):pdir;
  284. function CloseDir(p:pdir):integer;
  285. Function ReadDir(p:pdir):pdirent;
  286. procedure SeekDir(p:pdir;off:longint);
  287. function TellDir(p:pdir):longint;
  288. {**************************
  289. Pipe/Fifo/Stream
  290. ***************************}
  291. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  292. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  293. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  294. Function PClose(Var F:text) : longint;
  295. Function PClose(Var F:file) : longint;
  296. Procedure POpen(var F:text;const Prog:String;rw:char);
  297. Procedure POpen(var F:file;const Prog:String;rw:char);
  298. Function mkFifo(pathname:string;mode:longint):boolean;
  299. function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  300. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  301. {**************************
  302. General information
  303. ***************************}
  304. Function GetEnv(P:string):Pchar;
  305. {$ifndef BSD}
  306. Function GetDomainName:String;
  307. Function GetHostName:String;
  308. Function Sysinfo(var Info:TSysinfo):Boolean;
  309. Function Uname(var unamerec:utsname):Boolean;
  310. {$endif}
  311. {**************************
  312. Signal
  313. ***************************}
  314. Procedure SigAction(Signum:longint;Act,OldAct:PSigActionRec );
  315. Procedure SigProcMask (How:longint;SSet,OldSSet:PSigSet);
  316. Function SigPending:SigSet;
  317. Procedure SigSuspend(Mask:Sigset);
  318. Function Signal(Signum:longint;Handler:SignalHandler):SignalHandler;
  319. Function Kill(Pid:longint;Sig:longint):integer;
  320. Procedure SigRaise(Sig:integer);
  321. {$ifndef BSD}
  322. Function Alarm(Sec : Longint) : longint;
  323. Procedure Pause;
  324. {$endif}
  325. Function NanoSleep(const req : timespec;var rem : timespec) : longint;
  326. {**************************
  327. IOCtl/Termios Functions
  328. ***************************}
  329. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  330. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  331. Function TCSetAttr(fd:longint;OptAct:longint;const tios:TermIOS):boolean;
  332. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  333. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  334. Procedure CFMakeRaw(var tios:TermIOS);
  335. Function TCSendBreak(fd,duration:longint):boolean;
  336. Function TCSetPGrp(fd,id:longint):boolean;
  337. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  338. Function TCFlush(fd,qsel:longint):boolean;
  339. Function TCDrain(fd:longint):boolean;
  340. Function TCFlow(fd,act:longint):boolean;
  341. Function IsATTY(Handle:Longint):Boolean;
  342. Function IsATTY(f:text):Boolean;
  343. function TTYname(Handle:Longint):string;
  344. function TTYname(var F:Text):string;
  345. {**************************
  346. Memory functions
  347. ***************************}
  348. const
  349. PROT_READ = $1; { page can be read }
  350. PROT_WRITE = $2; { page can be written }
  351. PROT_EXEC = $4; { page can be executed }
  352. PROT_NONE = $0; { page can not be accessed }
  353. MAP_SHARED = $1; { Share changes }
  354. // MAP_PRIVATE = $2; { Changes are private }
  355. MAP_TYPE = $f; { Mask for type of mapping }
  356. MAP_FIXED = $10; { Interpret addr exactly }
  357. // MAP_ANONYMOUS = $20; { don't use a file }
  358. MAP_GROWSDOWN = $100; { stack-like segment }
  359. MAP_DENYWRITE = $800; { ETXTBSY }
  360. MAP_EXECUTABLE = $1000; { mark it as an executable }
  361. MAP_LOCKED = $2000; { pages are locked }
  362. MAP_NORESERVE = $4000; { don't check for reservations }
  363. type
  364. tmmapargs=record
  365. address : longint;
  366. size : longint;
  367. prot : longint;
  368. flags : longint;
  369. fd : longint;
  370. offset : longint;
  371. end;
  372. function MMap(const m:tmmapargs):longint;
  373. function MUnMap (P : Pointer; Size : Longint) : Boolean;
  374. {**************************
  375. Port IO functions
  376. ***************************}
  377. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  378. {$ifndef BSD}
  379. Function IoPL(Level : longint) : Boolean;
  380. {$endif}
  381. {$ifdef i386}
  382. Procedure WritePort (Port : Longint; Value : Byte);
  383. Procedure WritePort (Port : Longint; Value : Word);
  384. Procedure WritePort (Port : Longint; Value : Longint);
  385. Procedure WritePortB (Port : Longint; Value : Byte);
  386. Procedure WritePortW (Port : Longint; Value : Word);
  387. Procedure WritePortL (Port : Longint; Value : Longint);
  388. Procedure WritePortL (Port : Longint; Var Buf; Count: longint);
  389. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  390. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  391. Procedure ReadPort (Port : Longint; Var Value : Byte);
  392. Procedure ReadPort (Port : Longint; Var Value : Word);
  393. Procedure ReadPort (Port : Longint; Var Value : Longint);
  394. function ReadPortB (Port : Longint): Byte;
  395. function ReadPortW (Port : Longint): Word;
  396. function ReadPortL (Port : Longint): LongInt;
  397. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  398. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  399. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  400. {$endif}
  401. {**************************
  402. Utility functions
  403. ***************************}
  404. Function Octal(l:longint):longint;
  405. Function FExpand(Const Path: PathStr):PathStr;
  406. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  407. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  408. Function Dirname(Const path:pathstr):pathstr;
  409. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  410. Function FNMatch(const Pattern,Name:string):Boolean;
  411. Function Glob(Const path:pathstr):pglob;
  412. Procedure Globfree(var p:pglob);
  413. Function StringToPPChar(Var S:String):ppchar;
  414. Function StringToPPChar(Var S:AnsiString):ppchar;
  415. Function StringToPPChar(S : Pchar):ppchar;
  416. Function GetFS(var T:Text):longint;
  417. Function GetFS(Var F:File):longint;
  418. {Filedescriptorsets}
  419. Procedure FD_Zero(var fds:fdSet);
  420. Procedure FD_Clr(fd:longint;var fds:fdSet);
  421. Procedure FD_Set(fd:longint;var fds:fdSet);
  422. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  423. {Stat.Mode Types}
  424. Function S_ISLNK(m:word):boolean;
  425. Function S_ISREG(m:word):boolean;
  426. Function S_ISDIR(m:word):boolean;
  427. Function S_ISCHR(m:word):boolean;
  428. Function S_ISBLK(m:word):boolean;
  429. Function S_ISFIFO(m:word):boolean;
  430. Function S_ISSOCK(m:word):boolean;
  431. {******************************************************************************
  432. Implementation
  433. ******************************************************************************}
  434. Implementation
  435. Uses Strings;
  436. { Get the definitions of textrec and filerec }
  437. {$i textrec.inc}
  438. {$i filerec.inc}
  439. { Raw System calls are in Syscalls.inc}
  440. {$i syscalls.inc}
  441. {$i unixsysc.inc} {Syscalls only used in unit Unix/Linux}
  442. {******************************************************************************
  443. Process related calls
  444. ******************************************************************************}
  445. { Most calls of WaitPID do not handle the result correctly, this funktion treats errors more correctly }
  446. Function WaitProcess(Pid:longint):Longint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  447. var r,s : LongInt;
  448. begin
  449. repeat
  450. s:=$7F00;
  451. r:=WaitPid(Pid,@s,0);
  452. until (r<>-1) or (LinuxError<>Sys_EINTR);
  453. if (r=-1) or (r=0) then // 0 is not a valid return and should never occur (it means status invalid when using WNOHANG)
  454. WaitProcess:=-1 // return -1 to indicate an error
  455. else
  456. begin
  457. {$ifndef Solaris}
  458. WaitProcess:=s; // s<0 should not occur, but wie return also a negativ value
  459. {$else}
  460. if (s and $FF)=0 then // Only this is a valid returncode
  461. WaitProcess:=s shr 8
  462. else if (s>0) then // Until now there is not use of the highest bit , but check this for the future
  463. WaitProcess:=-s // normal case
  464. else
  465. WaitProcess:=s; // s<0 should not occur, but wie return also a negativ value
  466. {$endif}
  467. end;
  468. end;
  469. function InternalCreateShellArgV(cmd:pChar; len:longint):ppchar;
  470. {
  471. Create an argv which executes a command in a shell using /bin/sh -c
  472. }
  473. const Shell = '/bin/sh'#0'-c'#0;
  474. var
  475. pp,p : ppchar;
  476. // temp : string; !! Never pass a local var back!!
  477. begin
  478. getmem(pp,4*4);
  479. p:=pp;
  480. p^:=@Shell[1];
  481. inc(p);
  482. p^:=@Shell[9];
  483. inc(p);
  484. getmem(p^,len+1);
  485. move(cmd^,p^^,len);
  486. pchar(p^)[len]:=#0;
  487. inc(p);
  488. p^:=Nil;
  489. InternalCreateShellArgV:=pp;
  490. end;
  491. function CreateShellArgV(const prog:string):ppchar;
  492. begin
  493. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog));
  494. end;
  495. function CreateShellArgV(const prog:Ansistring):ppchar;
  496. {
  497. Create an argv which executes a command in a shell using /bin/sh -c
  498. using a AnsiString;
  499. }
  500. begin
  501. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog)); // if ppc works like delphi this also work when @prog[1] is invalid (len=0)
  502. end;
  503. procedure FreeShellArgV(p:ppchar);
  504. begin
  505. if (p<>nil) then begin
  506. freemem(p[2]);
  507. freemem(p);
  508. end;
  509. end;
  510. Procedure Execve(Path: AnsiString;args:ppchar;ep:ppchar);
  511. {
  512. overloaded ansistring version.
  513. }
  514. begin
  515. ExecVE(PChar(Path),args,ep);
  516. end;
  517. Procedure Execv(const path: AnsiString;args:ppchar);
  518. {
  519. Overloaded ansistring version.
  520. }
  521. begin
  522. ExecVe(Path,Args,envp)
  523. end;
  524. Procedure Execvp(Path: AnsiString; Args:ppchar;Ep:ppchar);
  525. {
  526. Overloaded ansistring version
  527. }
  528. var
  529. thepath : Ansistring;
  530. begin
  531. if path[1]<>'/' then
  532. begin
  533. Thepath:=strpas(getenv('PATH'));
  534. if thepath='' then
  535. thepath:='.';
  536. Path:=FSearch(path,thepath)
  537. end
  538. else
  539. Path:='';
  540. if Path='' then
  541. linuxerror:=Sys_enoent
  542. else
  543. Execve(Path,args,ep);{On error linuxerror will get set there}
  544. end;
  545. Procedure Execv(const path:pathstr;args:ppchar);
  546. {
  547. Replaces the current program by the program specified in path,
  548. arguments in args are passed to Execve.
  549. the current environment is passed on.
  550. }
  551. begin
  552. Execve(path,args,envp); {On error linuxerror will get set there}
  553. end;
  554. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  555. {
  556. This does the same as Execve, only it searches the PATH environment
  557. for the place of the Executable, except when Path starts with a slash.
  558. if the PATH environment variable is unavailable, the path is set to '.'
  559. }
  560. var
  561. thepath : string;
  562. begin
  563. if path[1]<>'/' then
  564. begin
  565. Thepath:=strpas(getenv('PATH'));
  566. if thepath='' then
  567. thepath:='.';
  568. Path:=FSearch(path,thepath)
  569. end
  570. else
  571. Path:='';
  572. if Path='' then
  573. linuxerror:=Sys_enoent
  574. else
  575. Execve(Path,args,ep);{On error linuxerror will get set there}
  576. end;
  577. Procedure Execle(Todo:string;Ep:ppchar);
  578. {
  579. This procedure takes the string 'Todo', parses it for command and
  580. command options, and Executes the command with the given options.
  581. The string 'Todo' shoud be of the form 'command options', options
  582. separated by commas.
  583. the PATH environment is not searched for 'command'.
  584. The specified environment(in 'ep') is passed on to command
  585. }
  586. var
  587. p : ppchar;
  588. begin
  589. p:=StringToPPChar(ToDo);
  590. if (p=nil) or (p^=nil) then
  591. exit;
  592. ExecVE(p^,p,EP);
  593. end;
  594. Procedure Execle(Todo:AnsiString;Ep:ppchar);
  595. {
  596. This procedure takes the string 'Todo', parses it for command and
  597. command options, and Executes the command with the given options.
  598. The string 'Todo' shoud be of the form 'command options', options
  599. separated by commas.
  600. the PATH environment is not searched for 'command'.
  601. The specified environment(in 'ep') is passed on to command
  602. }
  603. var
  604. p : ppchar;
  605. begin
  606. p:=StringToPPChar(ToDo);
  607. if (p=nil) or (p^=nil) then
  608. exit;
  609. ExecVE(p^,p,EP);
  610. end;
  611. Procedure Execl(const Todo:string);
  612. {
  613. This procedure takes the string 'Todo', parses it for command and
  614. command options, and Executes the command with the given options.
  615. The string 'Todo' shoud be of the form 'command options', options
  616. separated by commas.
  617. the PATH environment is not searched for 'command'.
  618. The current environment is passed on to command
  619. }
  620. begin
  621. ExecLE(ToDo,EnvP);
  622. end;
  623. Procedure Execlp(Todo:string;Ep:ppchar);
  624. {
  625. This procedure takes the string 'Todo', parses it for command and
  626. command options, and Executes the command with the given options.
  627. The string 'Todo' shoud be of the form 'command options', options
  628. separated by commas.
  629. the PATH environment is searched for 'command'.
  630. The specified environment (in 'ep') is passed on to command
  631. }
  632. var
  633. p : ppchar;
  634. begin
  635. p:=StringToPPchar(todo);
  636. if (p=nil) or (p^=nil) then
  637. exit;
  638. ExecVP(StrPas(p^),p,EP);
  639. end;
  640. Procedure Execlp(Todo: Ansistring;Ep:ppchar);
  641. {
  642. Overloaded ansistring version.
  643. }
  644. var
  645. p : ppchar;
  646. begin
  647. p:=StringToPPchar(todo);
  648. if (p=nil) or (p^=nil) then
  649. exit;
  650. ExecVP(StrPas(p^),p,EP);
  651. end;
  652. Function Shell(const Command:String):Longint;
  653. {
  654. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  655. The current environment is passed to the shell.
  656. It waits for the shell to exit, and returns its exit status.
  657. If the Exec call failed exit status 127 is reported.
  658. }
  659. { Changed the structure:
  660. - the previous version returns an undefinied value if fork fails
  661. - it returns the status of Waitpid instead of the Process returnvalue (see the doc to Shell)
  662. - it uses exit(127) not ExitProc (The Result in pp386: going on Compiling in 2 processes!)
  663. - ShellArgs are now released
  664. - The Old CreateShellArg gives back pointers to a local var
  665. }
  666. var
  667. p : ppchar;
  668. pid : longint;
  669. begin
  670. p:=CreateShellArgv(command);
  671. pid:=fork;
  672. if pid=0 then // We are in the Child
  673. begin
  674. {This is the child.}
  675. Execve(p^,p,envp);
  676. ExitProcess(127); // was Exit(127)
  677. end
  678. else if (pid<>-1) then // Successfull started
  679. Shell:=WaitProcess(pid) {Linuxerror is set there}
  680. else // no success
  681. Shell:=-1; // indicate an error
  682. FreeShellArgV(p);
  683. end;
  684. Function Shell(const Command:AnsiString):Longint;
  685. {
  686. AnsiString version of Shell
  687. }
  688. var
  689. p : ppchar;
  690. pid : longint;
  691. begin { Changes as above }
  692. p:=CreateShellArgv(command);
  693. pid:=fork;
  694. if pid=0 then // We are in the Child
  695. begin
  696. Execve(p^,p,envp);
  697. ExitProcess(127); // was exit(127)!! We must exit the Process, not the function
  698. end
  699. else if (pid<>-1) then // Successfull started
  700. Shell:=WaitProcess(pid) {Linuxerror is set there}
  701. else // no success
  702. Shell:=-1;
  703. FreeShellArgV(p);
  704. end;
  705. function WEXITSTATUS(Status: Integer): Integer;
  706. begin
  707. WEXITSTATUS:=(Status and $FF00) shr 8;
  708. end;
  709. function WTERMSIG(Status: Integer): Integer;
  710. begin
  711. WTERMSIG:=(Status and $7F);
  712. end;
  713. function WSTOPSIG(Status: Integer): Integer;
  714. begin
  715. WSTOPSIG:=WEXITSTATUS(Status);
  716. end;
  717. Function WIFEXITED(Status: Integer): Boolean;
  718. begin
  719. WIFEXITED:=(WTERMSIG(Status)=0);
  720. end;
  721. Function WIFSTOPPED(Status: Integer): Boolean;
  722. begin
  723. WIFSTOPPED:=((Status and $FF)=$7F);
  724. end;
  725. Function WIFSIGNALED(Status: Integer): Boolean;
  726. begin
  727. WIFSIGNALED:=(not WIFSTOPPED(Status)) and
  728. (not WIFEXITED(Status));
  729. end;
  730. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  731. begin
  732. W_EXITCODE:=(ReturnCode shl 8) or Signal;
  733. end;
  734. Function W_STOPCODE(Signal: Integer): Integer;
  735. begin
  736. W_STOPCODE:=(Signal shl 8) or $7F;
  737. end;
  738. {******************************************************************************
  739. Date and Time related calls
  740. ******************************************************************************}
  741. Const
  742. {Date Translation}
  743. C1970=2440588;
  744. D0 = 1461;
  745. D1 = 146097;
  746. D2 =1721119;
  747. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  748. Var
  749. Century,XYear: LongInt;
  750. Begin
  751. If Month<=2 Then
  752. Begin
  753. Dec(Year);
  754. Inc(Month,12);
  755. End;
  756. Dec(Month,3);
  757. Century:=(longint(Year Div 100)*D1) shr 2;
  758. XYear:=(longint(Year Mod 100)*D0) shr 2;
  759. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  760. End;
  761. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  762. Var
  763. YYear,XYear,Temp,TempMonth : LongInt;
  764. Begin
  765. Temp:=((JulianDN-D2) shl 2)-1;
  766. JulianDN:=Temp Div D1;
  767. XYear:=(Temp Mod D1) or 3;
  768. YYear:=(XYear Div D0);
  769. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  770. Day:=((Temp Mod 153)+5) Div 5;
  771. TempMonth:=Temp Div 153;
  772. If TempMonth>=10 Then
  773. Begin
  774. inc(YYear);
  775. dec(TempMonth,12);
  776. End;
  777. inc(TempMonth,3);
  778. Month := TempMonth;
  779. Year:=YYear+(JulianDN*100);
  780. end;
  781. Function GetEpochTime: longint;
  782. {
  783. Get the number of seconds since 00:00, January 1 1970, GMT
  784. the time NOT corrected any way
  785. }
  786. begin
  787. GetEpochTime:=GetTimeOfDay;
  788. end;
  789. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  790. {
  791. Transforms Epoch time into local time (hour, minute,seconds)
  792. }
  793. Var
  794. DateNum: LongInt;
  795. Begin
  796. inc(Epoch,TZSeconds);
  797. Datenum:=(Epoch Div 86400) + c1970;
  798. JulianToGregorian(DateNum,Year,Month,day);
  799. Epoch:=Abs(Epoch Mod 86400);
  800. Hour:=Epoch Div 3600;
  801. Epoch:=Epoch Mod 3600;
  802. Minute:=Epoch Div 60;
  803. Second:=Epoch Mod 60;
  804. End;
  805. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  806. {
  807. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  808. (seconds since 00:00, january 1 1970, corrected for local time zone)
  809. }
  810. Begin
  811. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  812. (LongInt(Hour)*3600)+(Minute*60)+Second-TZSeconds;
  813. End;
  814. procedure GetTime(var hour,min,sec,msec,usec:word);
  815. {
  816. Gets the current time, adjusted to local time
  817. }
  818. var
  819. year,day,month:Word;
  820. t : timeval;
  821. begin
  822. gettimeofday(t);
  823. EpochToLocal(t.sec,year,month,day,hour,min,sec);
  824. msec:=t.usec div 1000;
  825. usec:=t.usec mod 1000;
  826. end;
  827. procedure GetTime(var hour,min,sec,sec100:word);
  828. {
  829. Gets the current time, adjusted to local time
  830. }
  831. var
  832. usec : word;
  833. begin
  834. gettime(hour,min,sec,sec100,usec);
  835. sec100:=sec100 div 10;
  836. end;
  837. Procedure GetTime(Var Hour,Min,Sec:Word);
  838. {
  839. Gets the current time, adjusted to local time
  840. }
  841. var
  842. msec,usec : Word;
  843. Begin
  844. gettime(hour,min,sec,msec,usec);
  845. End;
  846. Procedure GetDate(Var Year,Month,Day:Word);
  847. {
  848. Gets the current date, adjusted to local time
  849. }
  850. var
  851. hour,minute,second : word;
  852. Begin
  853. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  854. End;
  855. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  856. {
  857. Gets the current date, adjusted to local time
  858. }
  859. Begin
  860. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  861. End;
  862. {$ifndef BSD}
  863. {$ifdef linux}
  864. Function stime (t : longint) : Boolean;
  865. var
  866. sr : Syscallregs;
  867. begin
  868. sr.reg2:=longint(@t);
  869. SysCall(Syscall_nr_stime,sr);
  870. linuxerror:=errno;
  871. stime:=linuxerror=0;
  872. end;
  873. {$endif}
  874. {$endif}
  875. {$ifdef BSD}
  876. Function stime (t : longint) : Boolean;
  877. begin
  878. end;
  879. {$endif}
  880. Function SetTime(Hour,Min,Sec:word) : boolean;
  881. var
  882. Year, Month, Day : Word;
  883. begin
  884. GetDate (Year, Month, Day);
  885. SetTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Min, Sec ) );
  886. end;
  887. Function SetDate(Year,Month,Day:Word) : boolean;
  888. var
  889. Hour, Minute, Second, Sec100 : Word;
  890. begin
  891. GetTime ( Hour, Minute, Second, Sec100 );
  892. SetDate:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  893. end;
  894. Function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  895. begin
  896. SetDateTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  897. end;
  898. { Include timezone handling routines which use /usr/share/timezone info }
  899. {$i timezone.inc}
  900. {******************************************************************************
  901. FileSystem calls
  902. ******************************************************************************}
  903. Function fdOpen(pathname:string;flags:longint):longint;
  904. begin
  905. pathname:=pathname+#0;
  906. fdOpen:=Sys_Open(@pathname[1],flags,438);
  907. LinuxError:=Errno;
  908. end;
  909. Function fdOpen(pathname:string;flags,mode:longint):longint;
  910. begin
  911. pathname:=pathname+#0;
  912. fdOpen:=Sys_Open(@pathname[1],flags,mode);
  913. LinuxError:=Errno;
  914. end;
  915. Procedure Execl(const Todo:Ansistring);
  916. {
  917. Overloaded AnsiString Version of ExecL.
  918. }
  919. begin
  920. ExecLE(ToDo,EnvP);
  921. end;
  922. Function fdOpen(pathname:pchar;flags:longint):longint;
  923. begin
  924. fdOpen:=Sys_Open(pathname,flags,0);
  925. LinuxError:=Errno;
  926. end;
  927. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  928. begin
  929. fdOpen:=Sys_Open(pathname,flags,mode);
  930. LinuxError:=Errno;
  931. end;
  932. Function fdClose(fd:longint):boolean;
  933. begin
  934. fdClose:=(Sys_Close(fd)=0);
  935. LinuxError:=Errno;
  936. end;
  937. Function fdRead(fd:longint;var buf;size:longint):longint;
  938. begin
  939. fdRead:=Sys_Read(fd,pchar(@buf),size);
  940. LinuxError:=Errno;
  941. end;
  942. Function fdWrite(fd:longint;const buf;size:longint):longint;
  943. begin
  944. fdWrite:=Sys_Write(fd,pchar(@buf),size);
  945. LinuxError:=Errno;
  946. end;
  947. Function fdSeek (fd,pos,seektype :longint): longint;
  948. {
  949. Do a Seek on a file descriptor fd to position pos, starting from seektype
  950. }
  951. begin
  952. fdseek:=Sys_LSeek (fd,pos,seektype);
  953. LinuxError:=Errno;
  954. end;
  955. {$ifdef BSD}
  956. Function Fcntl(Fd:longint;Cmd:longint):longint;
  957. {
  958. Read or manipulate a file.(See also fcntl (2) )
  959. Possible values for Cmd are :
  960. F_GetFd,F_GetFl,F_GetOwn
  961. Errors are reported in Linuxerror;
  962. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  963. }
  964. begin
  965. if (cmd in [F_GetFd,F_GetFl,F_GetOwn]) then
  966. begin
  967. Linuxerror:=sys_fcntl(fd,cmd,0);
  968. if linuxerror=-1 then
  969. begin
  970. linuxerror:=errno;
  971. fcntl:=0;
  972. end
  973. else
  974. begin
  975. fcntl:=linuxerror;
  976. linuxerror:=0;
  977. end;
  978. end
  979. else
  980. begin
  981. linuxerror:=Sys_einval;
  982. Fcntl:=0;
  983. end;
  984. end;
  985. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  986. {
  987. Read or manipulate a file. (See also fcntl (2) )
  988. Possible values for Cmd are :
  989. F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
  990. Errors are reported in Linuxerror;
  991. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  992. F_DupFD is not allowed, due to the structure of Files in Pascal.
  993. }
  994. begin
  995. if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn]) then
  996. begin
  997. sys_fcntl(fd,cmd,arg);
  998. LinuxError:=ErrNo;
  999. end
  1000. else
  1001. linuxerror:=Sys_einval;
  1002. end;
  1003. {$endif}
  1004. {$ifdef BSD}
  1005. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  1006. {$else}
  1007. Function Fcntl(var Fd:Text;Cmd:integer):integer;
  1008. {$endif}
  1009. begin
  1010. Fcntl := Fcntl(textrec(Fd).handle, Cmd);
  1011. end;
  1012. {$ifdef BSD}
  1013. Procedure Fcntl(var Fd:Text;Cmd,Arg:Longint);
  1014. {$else}
  1015. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  1016. {$endif}
  1017. begin
  1018. Fcntl(textrec(Fd).handle, Cmd, Arg);
  1019. end;
  1020. Function Flock (var T : text;mode : longint) : boolean;
  1021. begin
  1022. Flock:=Flock(TextRec(T).Handle,mode);
  1023. end;
  1024. Function Flock (var F : File;mode : longint) : boolean;
  1025. begin
  1026. Flock:=Flock(FileRec(F).Handle,mode);
  1027. end;
  1028. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  1029. {
  1030. Get all information on a file, and return it in Info.
  1031. }
  1032. begin
  1033. path:=path+#0;
  1034. FStat:=(Sys_stat(@(path[1]),Info)=0);
  1035. LinuxError:=errno;
  1036. end;
  1037. Function FStat(var F:Text;Var Info:stat):Boolean;
  1038. {
  1039. Get all information on a text file, and return it in info.
  1040. }
  1041. begin
  1042. FStat:=Fstat(TextRec(F).Handle,INfo);
  1043. end;
  1044. Function FStat(var F:File;Var Info:stat):Boolean;
  1045. {
  1046. Get all information on a untyped file, and return it in info.
  1047. }
  1048. begin
  1049. FStat:=Fstat(FileRec(F).Handle,Info);
  1050. end;
  1051. Function SymLink(OldPath,newPath:pathstr):boolean;
  1052. {
  1053. Proceduces a soft link from new to old.
  1054. }
  1055. begin
  1056. oldpath:=oldpath+#0;
  1057. newpath:=newpath+#0;
  1058. Symlink:=Sys_symlink(pchar(@(oldpath[1])),pchar(@(newpath[1])))=0;
  1059. linuxerror:=errno;
  1060. end;
  1061. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  1062. {
  1063. Read a link (where it points to)
  1064. }
  1065. begin
  1066. Readlink:=Sys_readlink(Name,LinkName,maxlen);
  1067. linuxerror:=errno;
  1068. end;
  1069. Function ReadLink(Name:pathstr):pathstr;
  1070. {
  1071. Read a link (where it points to)
  1072. }
  1073. var
  1074. LinkName : pathstr;
  1075. i : longint;
  1076. begin
  1077. Name:=Name+#0;
  1078. i:=ReadLink(@Name[1],@LinkName[1],high(linkname));
  1079. if i>0 then
  1080. begin
  1081. linkname[0]:=chr(i);
  1082. ReadLink:=LinkName;
  1083. end
  1084. else
  1085. ReadLink:='';
  1086. end;
  1087. Function UnLink(Path:pathstr):boolean;
  1088. {
  1089. Removes the file in 'Path' (that is, it decreases the link count with one.
  1090. if the link count is zero, the file is removed from the disk.
  1091. }
  1092. begin
  1093. path:=path+#0;
  1094. Unlink:=Sys_unlink(pchar(@(path[1])))=0;
  1095. linuxerror:=errno;
  1096. end;
  1097. Function UnLink(Path:pchar):Boolean;
  1098. {
  1099. Removes the file in 'Path' (that is, it decreases the link count with one.
  1100. if the link count is zero, the file is removed from the disk.
  1101. }
  1102. begin
  1103. Unlink:=(Sys_unlink(path)=0);
  1104. linuxerror:=errno;
  1105. end;
  1106. Function FRename (OldName,NewName : Pchar) : Boolean;
  1107. begin
  1108. FRename:=Sys_rename(OldName,NewName)=0;
  1109. LinuxError:=Errno;
  1110. end;
  1111. Function FRename (OldName,NewName : String) : Boolean;
  1112. begin
  1113. OldName:=OldName+#0;
  1114. NewName:=NewName+#0;
  1115. FRename:=FRename (@OldName[1],@NewName[1]);
  1116. end;
  1117. Function Dup(var oldfile,newfile:text):Boolean;
  1118. {
  1119. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  1120. oldfile.
  1121. After which the two textfiles are, in effect, the same, except
  1122. that they don't share the same buffer, and don't share the same
  1123. close_on_exit flag.
  1124. }
  1125. begin
  1126. flush(oldfile);{ We cannot share buffers, so we flush them. }
  1127. textrec(newfile):=textrec(oldfile);
  1128. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  1129. Dup:=Dup(textrec(oldfile).handle,textrec(newfile).handle);
  1130. end;
  1131. Function Dup(var oldfile,newfile:file):Boolean;
  1132. {
  1133. Copies the filedescriptor oldfile to newfile
  1134. }
  1135. begin
  1136. filerec(newfile):=filerec(oldfile);
  1137. Dup:=Dup(filerec(oldfile).handle,filerec(newfile).handle);
  1138. end;
  1139. Function Dup2(var oldfile,newfile:text):Boolean;
  1140. {
  1141. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  1142. oldfile. It closes newfile if it was still open.
  1143. After which the two textfiles are, in effect, the same, except
  1144. that they don't share the same buffer, and don't share the same
  1145. close_on_exit flag.
  1146. }
  1147. var
  1148. tmphandle : word;
  1149. begin
  1150. case TextRec(oldfile).mode of
  1151. fmOutput, fmInOut, fmAppend :
  1152. flush(oldfile);{ We cannot share buffers, so we flush them. }
  1153. end;
  1154. case TextRec(newfile).mode of
  1155. fmOutput, fmInOut, fmAppend :
  1156. flush(newfile);
  1157. end;
  1158. tmphandle:=textrec(newfile).handle;
  1159. textrec(newfile):=textrec(oldfile);
  1160. textrec(newfile).handle:=tmphandle;
  1161. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  1162. Dup2:=Dup2(textrec(oldfile).handle,textrec(newfile).handle);
  1163. end;
  1164. Function Dup2(var oldfile,newfile:file):Boolean;
  1165. {
  1166. Copies the filedescriptor oldfile to newfile
  1167. }
  1168. begin
  1169. filerec(newfile):=filerec(oldfile);
  1170. Dup2:=Dup2(filerec(oldfile).handle,filerec(newfile).handle);
  1171. end;
  1172. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  1173. {
  1174. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  1175. have changed.
  1176. This function allows specification of a timeout as a longint.
  1177. }
  1178. var
  1179. p : PTimeVal;
  1180. tv : TimeVal;
  1181. begin
  1182. if TimeOut=-1 then
  1183. p:=nil
  1184. else
  1185. begin
  1186. tv.Sec:=Timeout div 1000;
  1187. tv.Usec:=(Timeout mod 1000)*1000;
  1188. p:=@tv;
  1189. end;
  1190. Select:=Select(N,Readfds,WriteFds,ExceptFds,p);
  1191. end;
  1192. Function SelectText(var T:Text;TimeOut :PTimeval):Longint;
  1193. Var
  1194. F:FDSet;
  1195. begin
  1196. if textrec(t).mode=fmclosed then
  1197. begin
  1198. LinuxError:=Sys_EBADF;
  1199. exit(-1);
  1200. end;
  1201. FD_Zero(f);
  1202. FD_Set(textrec(T).handle,f);
  1203. if textrec(T).mode=fminput then
  1204. SelectText:=select(textrec(T).handle+1,@f,nil,nil,TimeOut)
  1205. else
  1206. SelectText:=select(textrec(T).handle+1,nil,@f,nil,TimeOut);
  1207. end;
  1208. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  1209. var
  1210. p : PTimeVal;
  1211. tv : TimeVal;
  1212. begin
  1213. if TimeOut=-1 then
  1214. p:=nil
  1215. else
  1216. begin
  1217. tv.Sec:=Timeout div 1000;
  1218. tv.Usec:=(Timeout mod 1000)*1000;
  1219. p:=@tv;
  1220. end;
  1221. SelectText:=SelectText(T,p);
  1222. end;
  1223. {******************************************************************************
  1224. Directory
  1225. ******************************************************************************}
  1226. Function OpenDir(F:String):PDir;
  1227. begin
  1228. F:=F+#0;
  1229. OpenDir:=OpenDir(@F[1]);
  1230. LinuxError:=ErrNo;
  1231. end;
  1232. procedure SeekDir(p:pdir;off:longint);
  1233. begin
  1234. if p=nil then
  1235. begin
  1236. errno:=Sys_EBADF;
  1237. exit;
  1238. end;
  1239. {$ifndef bsd}
  1240. p^.nextoff:=Sys_lseek(p^.fd,off,seek_set);
  1241. {$endif}
  1242. p^.size:=0;
  1243. p^.loc:=0;
  1244. end;
  1245. function TellDir(p:pdir):longint;
  1246. begin
  1247. if p=nil then
  1248. begin
  1249. errno:=Sys_EBADF;
  1250. telldir:=-1;
  1251. exit;
  1252. end;
  1253. telldir:=Sys_lseek(p^.fd,0,seek_cur)
  1254. { We could try to use the nextoff field here, but on my 1.2.13
  1255. kernel, this gives nothing... This may have to do with
  1256. the readdir implementation of libc... I also didn't find any trace of
  1257. the field in the kernel code itself, So I suspect it is an artifact of libc.
  1258. Michael. }
  1259. end;
  1260. Function ReadDir(P:pdir):pdirent;
  1261. begin
  1262. ReadDir:=Sys_ReadDir(p);
  1263. LinuxError:=Errno;
  1264. end;
  1265. {******************************************************************************
  1266. Pipes/Fifo
  1267. ******************************************************************************}
  1268. Procedure OpenPipe(var F:Text);
  1269. begin
  1270. case textrec(f).mode of
  1271. fmoutput :
  1272. if textrec(f).userdata[1]<>P_OUT then
  1273. textrec(f).mode:=fmclosed;
  1274. fminput :
  1275. if textrec(f).userdata[1]<>P_IN then
  1276. textrec(f).mode:=fmclosed;
  1277. else
  1278. textrec(f).mode:=fmclosed;
  1279. end;
  1280. end;
  1281. Procedure IOPipe(var F:text);
  1282. begin
  1283. case textrec(f).mode of
  1284. fmoutput :
  1285. begin
  1286. { first check if we need something to write, else we may
  1287. get a SigPipe when Close() is called (PFV) }
  1288. if textrec(f).bufpos>0 then
  1289. Sys_write(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  1290. end;
  1291. fminput :
  1292. textrec(f).bufend:=Sys_read(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  1293. end;
  1294. textrec(f).bufpos:=0;
  1295. end;
  1296. Procedure FlushPipe(var F:Text);
  1297. begin
  1298. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  1299. IOPipe(f);
  1300. textrec(f).bufpos:=0;
  1301. end;
  1302. Procedure ClosePipe(var F:text);
  1303. begin
  1304. textrec(f).mode:=fmclosed;
  1305. Sys_close(textrec(f).handle);
  1306. end;
  1307. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  1308. {
  1309. Sets up a pair of file variables, which act as a pipe. The first one can
  1310. be read from, the second one can be written to.
  1311. If the operation was unsuccesful, linuxerror is set.
  1312. }
  1313. var
  1314. f_in,f_out : longint;
  1315. begin
  1316. if not AssignPipe(f_in,f_out) then
  1317. begin
  1318. AssignPipe:=false;
  1319. exit;
  1320. end;
  1321. { Set up input }
  1322. Assign(Pipe_in,'');
  1323. Textrec(Pipe_in).Handle:=f_in;
  1324. Textrec(Pipe_in).Mode:=fmInput;
  1325. Textrec(Pipe_in).userdata[1]:=P_IN;
  1326. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  1327. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  1328. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  1329. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  1330. { Set up output }
  1331. Assign(Pipe_out,'');
  1332. Textrec(Pipe_out).Handle:=f_out;
  1333. Textrec(Pipe_out).Mode:=fmOutput;
  1334. Textrec(Pipe_out).userdata[1]:=P_OUT;
  1335. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  1336. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  1337. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  1338. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  1339. AssignPipe:=true;
  1340. end;
  1341. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  1342. {
  1343. Sets up a pair of file variables, which act as a pipe. The first one can
  1344. be read from, the second one can be written to.
  1345. If the operation was unsuccesful, linuxerror is set.
  1346. }
  1347. var
  1348. f_in,f_out : longint;
  1349. begin
  1350. if not AssignPipe(f_in,f_out) then
  1351. begin
  1352. AssignPipe:=false;
  1353. exit;
  1354. end;
  1355. { Set up input }
  1356. Assign(Pipe_in,'');
  1357. Filerec(Pipe_in).Handle:=f_in;
  1358. Filerec(Pipe_in).Mode:=fmInput;
  1359. Filerec(Pipe_in).recsize:=1;
  1360. Filerec(Pipe_in).userdata[1]:=P_IN;
  1361. { Set up output }
  1362. Assign(Pipe_out,'');
  1363. Filerec(Pipe_out).Handle:=f_out;
  1364. Filerec(Pipe_out).Mode:=fmoutput;
  1365. Filerec(Pipe_out).recsize:=1;
  1366. Filerec(Pipe_out).userdata[1]:=P_OUT;
  1367. AssignPipe:=true;
  1368. end;
  1369. Procedure PCloseText(Var F:text);
  1370. {
  1371. May not use @PClose due overloading
  1372. }
  1373. begin
  1374. PClose(f);
  1375. end;
  1376. Procedure POpen(var F:text;const Prog:String;rw:char);
  1377. {
  1378. Starts the program in 'Prog' and makes it's input or out put the
  1379. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1380. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1381. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1382. read from 'f'.
  1383. }
  1384. var
  1385. pipi,
  1386. pipo : text;
  1387. pid : longint;
  1388. pl : ^longint;
  1389. pp : ppchar;
  1390. begin
  1391. LinuxError:=0;
  1392. rw:=upcase(rw);
  1393. if not (rw in ['R','W']) then
  1394. begin
  1395. LinuxError:=Sys_enoent;
  1396. exit;
  1397. end;
  1398. AssignPipe(pipi,pipo);
  1399. if Linuxerror<>0 then
  1400. exit;
  1401. pid:=fork;
  1402. if linuxerror<>0 then
  1403. begin
  1404. close(pipi);
  1405. close(pipo);
  1406. exit;
  1407. end;
  1408. if pid=0 then
  1409. begin
  1410. { We're in the child }
  1411. if rw='W' then
  1412. begin
  1413. close(pipo);
  1414. dup2(pipi,input);
  1415. close(pipi);
  1416. if linuxerror<>0 then
  1417. halt(127);
  1418. end
  1419. else
  1420. begin
  1421. close(pipi);
  1422. dup2(pipo,output);
  1423. close(pipo);
  1424. if linuxerror<>0 then
  1425. halt(127);
  1426. end;
  1427. pp:=createshellargv(prog);
  1428. Execve(pp^,pp,envp);
  1429. halt(127);
  1430. end
  1431. else
  1432. begin
  1433. { We're in the parent }
  1434. if rw='W' then
  1435. begin
  1436. close(pipi);
  1437. f:=pipo;
  1438. textrec(f).bufptr:=@textrec(f).buffer;
  1439. end
  1440. else
  1441. begin
  1442. close(pipo);
  1443. f:=pipi;
  1444. textrec(f).bufptr:=@textrec(f).buffer;
  1445. end;
  1446. {Save the process ID - needed when closing }
  1447. pl:=@(textrec(f).userdata[2]);
  1448. pl^:=pid;
  1449. textrec(f).closefunc:=@PCloseText;
  1450. end;
  1451. end;
  1452. Procedure POpen(var F:file;const Prog:String;rw:char);
  1453. {
  1454. Starts the program in 'Prog' and makes it's input or out put the
  1455. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1456. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1457. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1458. read from 'f'.
  1459. }
  1460. var
  1461. pipi,
  1462. pipo : file;
  1463. pid : longint;
  1464. pl : ^longint;
  1465. p,pp : ppchar;
  1466. temp : string[255];
  1467. begin
  1468. LinuxError:=0;
  1469. rw:=upcase(rw);
  1470. if not (rw in ['R','W']) then
  1471. begin
  1472. LinuxError:=Sys_enoent;
  1473. exit;
  1474. end;
  1475. AssignPipe(pipi,pipo);
  1476. if Linuxerror<>0 then
  1477. exit;
  1478. pid:=fork;
  1479. if linuxerror<>0 then
  1480. begin
  1481. close(pipi);
  1482. close(pipo);
  1483. exit;
  1484. end;
  1485. if pid=0 then
  1486. begin
  1487. { We're in the child }
  1488. if rw='W' then
  1489. begin
  1490. close(pipo);
  1491. dup2(filerec(pipi).handle,stdinputhandle);
  1492. close(pipi);
  1493. if linuxerror<>0 then
  1494. halt(127);
  1495. end
  1496. else
  1497. begin
  1498. close(pipi);
  1499. dup2(filerec(pipo).handle,stdoutputhandle);
  1500. close(pipo);
  1501. if linuxerror<>0 then
  1502. halt(127);
  1503. end;
  1504. getmem(pp,sizeof(pchar)*4);
  1505. temp:='/bin/sh'#0'-c'#0+prog+#0;
  1506. p:=pp;
  1507. p^:=@temp[1];
  1508. inc(p);
  1509. p^:=@temp[9];
  1510. inc(p);
  1511. p^:=@temp[12];
  1512. inc(p);
  1513. p^:=Nil;
  1514. Execve('/bin/sh',pp,envp);
  1515. halt(127);
  1516. end
  1517. else
  1518. begin
  1519. { We're in the parent }
  1520. if rw='W' then
  1521. begin
  1522. close(pipi);
  1523. f:=pipo;
  1524. end
  1525. else
  1526. begin
  1527. close(pipo);
  1528. f:=pipi;
  1529. end;
  1530. {Save the process ID - needed when closing }
  1531. pl:=@(filerec(f).userdata[2]);
  1532. pl^:=pid;
  1533. end;
  1534. end;
  1535. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  1536. {
  1537. Starts the program in 'Prog' and makes its input and output the
  1538. other end of two pipes, which are the stdin and stdout of a program
  1539. specified in 'Prog'.
  1540. streamout can be used to write to the program, streamin can be used to read
  1541. the output of the program. See the following diagram :
  1542. Parent Child
  1543. STreamout --> Input
  1544. Streamin <-- Output
  1545. Return value is the process ID of the process being spawned, or -1 in case of failure.
  1546. }
  1547. var
  1548. pipi,
  1549. pipo : text;
  1550. pid : longint;
  1551. pl : ^Longint;
  1552. begin
  1553. LinuxError:=0;
  1554. AssignStream:=-1;
  1555. AssignPipe(streamin,pipo);
  1556. if Linuxerror<>0 then
  1557. exit;
  1558. AssignPipe(pipi,streamout);
  1559. if Linuxerror<>0 then
  1560. exit;
  1561. pid:=fork;
  1562. if linuxerror<>0 then
  1563. begin
  1564. close(pipi);
  1565. close(pipo);
  1566. close (streamin);
  1567. close (streamout);
  1568. exit;
  1569. end;
  1570. if pid=0 then
  1571. begin
  1572. { We're in the child }
  1573. { Close what we don't need }
  1574. close(streamout);
  1575. close(streamin);
  1576. dup2(pipi,input);
  1577. if linuxerror<>0 then
  1578. halt(127);
  1579. close(pipi);
  1580. dup2(pipo,output);
  1581. if linuxerror<>0 then
  1582. halt (127);
  1583. close(pipo);
  1584. Execl(Prog);
  1585. halt(127);
  1586. end
  1587. else
  1588. begin
  1589. { we're in the parent}
  1590. close(pipo);
  1591. close(pipi);
  1592. {Save the process ID - needed when closing }
  1593. pl:=@(textrec(StreamIn).userdata[2]);
  1594. pl^:=pid;
  1595. textrec(StreamIn).closefunc:=@PCloseText;
  1596. {Save the process ID - needed when closing }
  1597. pl:=@(textrec(StreamOut).userdata[2]);
  1598. pl^:=pid;
  1599. textrec(StreamOut).closefunc:=@PCloseText;
  1600. AssignStream:=Pid;
  1601. end;
  1602. end;
  1603. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  1604. {
  1605. Starts the program in 'prog' and makes its input, output and error output the
  1606. other end of three pipes, which are the stdin, stdout and stderr of a program
  1607. specified in 'prog'.
  1608. StreamOut can be used to write to the program, StreamIn can be used to read
  1609. the output of the program, StreamErr reads the error output of the program.
  1610. See the following diagram :
  1611. Parent Child
  1612. StreamOut --> StdIn (input)
  1613. StreamIn <-- StdOut (output)
  1614. StreamErr <-- StdErr (error output)
  1615. }
  1616. var
  1617. PipeIn, PipeOut, PipeErr: text;
  1618. pid: LongInt;
  1619. pl: ^LongInt;
  1620. begin
  1621. LinuxError := 0;
  1622. AssignStream := -1;
  1623. // Assign pipes
  1624. AssignPipe(StreamIn, PipeOut);
  1625. if LinuxError <> 0 then exit;
  1626. AssignPipe(StreamErr, PipeErr);
  1627. if LinuxError <> 0 then begin
  1628. Close(StreamIn);
  1629. Close(PipeOut);
  1630. exit;
  1631. end;
  1632. AssignPipe(PipeIn, StreamOut);
  1633. if LinuxError <> 0 then begin
  1634. Close(StreamIn);
  1635. Close(PipeOut);
  1636. Close(StreamErr);
  1637. Close(PipeErr);
  1638. exit;
  1639. end;
  1640. // Fork
  1641. pid := Fork;
  1642. if LinuxError <> 0 then begin
  1643. Close(StreamIn);
  1644. Close(PipeOut);
  1645. Close(StreamErr);
  1646. Close(PipeErr);
  1647. Close(PipeIn);
  1648. Close(StreamOut);
  1649. exit;
  1650. end;
  1651. if pid = 0 then begin
  1652. // *** We are in the child ***
  1653. // Close what we don not need
  1654. Close(StreamOut);
  1655. Close(StreamIn);
  1656. Close(StreamErr);
  1657. // Connect pipes
  1658. dup2(PipeIn, Input);
  1659. if LinuxError <> 0 then Halt(127);
  1660. Close(PipeIn);
  1661. dup2(PipeOut, Output);
  1662. if LinuxError <> 0 then Halt(127);
  1663. Close(PipeOut);
  1664. dup2(PipeErr, StdErr);
  1665. if LinuxError <> 0 then Halt(127);
  1666. Close(PipeErr);
  1667. // Execute program
  1668. Execl(Prog);
  1669. Halt(127);
  1670. end else begin
  1671. // *** We are in the parent ***
  1672. Close(PipeErr);
  1673. Close(PipeOut);
  1674. Close(PipeIn);
  1675. // Save the process ID - needed when closing
  1676. pl := @(TextRec(StreamIn).userdata[2]);
  1677. pl^ := pid;
  1678. TextRec(StreamIn).closefunc := @PCloseText;
  1679. // Save the process ID - needed when closing
  1680. pl := @(TextRec(StreamOut).userdata[2]);
  1681. pl^ := pid;
  1682. TextRec(StreamOut).closefunc := @PCloseText;
  1683. // Save the process ID - needed when closing
  1684. pl := @(TextRec(StreamErr).userdata[2]);
  1685. pl^ := pid;
  1686. TextRec(StreamErr).closefunc := @PCloseText;
  1687. AssignStream := pid;
  1688. end;
  1689. end;
  1690. {******************************************************************************
  1691. General information calls
  1692. ******************************************************************************}
  1693. Function GetEnv(P:string):Pchar;
  1694. {
  1695. Searches the environment for a string with name p and
  1696. returns a pchar to it's value.
  1697. A pchar is used to accomodate for strings of length > 255
  1698. }
  1699. var
  1700. ep : ppchar;
  1701. found : boolean;
  1702. Begin
  1703. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1704. ep:=envp;
  1705. found:=false;
  1706. if ep<>nil then
  1707. begin
  1708. while (not found) and (ep^<>nil) do
  1709. begin
  1710. if strlcomp(@p[1],(ep^),length(p))=0 then
  1711. found:=true
  1712. else
  1713. inc(ep);
  1714. end;
  1715. end;
  1716. if found then
  1717. getenv:=ep^+length(p)
  1718. else
  1719. getenv:=nil;
  1720. end;
  1721. {$ifndef bsd}
  1722. Function GetDomainName:String;
  1723. {
  1724. Get machines domain name. Returns empty string if not set.
  1725. }
  1726. Var
  1727. Sysn : utsname;
  1728. begin
  1729. Uname(Sysn);
  1730. linuxerror:=errno;
  1731. If linuxerror<>0 then
  1732. getdomainname:=''
  1733. else
  1734. getdomainname:=strpas(@Sysn.domainname[0]);
  1735. end;
  1736. Function GetHostName:String;
  1737. {
  1738. Get machines name. Returns empty string if not set.
  1739. }
  1740. Var
  1741. Sysn : utsname;
  1742. begin
  1743. uname(Sysn);
  1744. linuxerror:=errno;
  1745. If linuxerror<>0 then
  1746. gethostname:=''
  1747. else
  1748. gethostname:=strpas(@Sysn.nodename[0]);
  1749. end;
  1750. {$endif}
  1751. {******************************************************************************
  1752. Signal handling calls
  1753. ******************************************************************************}
  1754. procedure SigRaise(sig:integer);
  1755. begin
  1756. Kill(GetPid,Sig);
  1757. end;
  1758. {******************************************************************************
  1759. IOCtl and Termios calls
  1760. ******************************************************************************}
  1761. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  1762. begin
  1763. {$ifndef BSD}
  1764. TCGetAttr:=IOCtl(fd,TCGETS,@tios);
  1765. {$else}
  1766. TCGETAttr:=IoCtl(Fd,TIOCGETA,@tios);
  1767. {$endif}
  1768. end;
  1769. Function TCSetAttr(fd:longint;OptAct:longint;const tios:TermIOS):boolean;
  1770. var
  1771. nr:longint;
  1772. begin
  1773. {$ifndef BSD}
  1774. case OptAct of
  1775. TCSANOW : nr:=TCSETS;
  1776. TCSADRAIN : nr:=TCSETSW;
  1777. TCSAFLUSH : nr:=TCSETSF;
  1778. {$else}
  1779. case OptAct of
  1780. TCSANOW : nr:=TIOCSETA;
  1781. TCSADRAIN : nr:=TIOCSETAW;
  1782. TCSAFLUSH : nr:=TIOCSETAF;
  1783. {$endif}
  1784. else
  1785. begin
  1786. ErrNo:=Sys_EINVAL;
  1787. TCSetAttr:=false;
  1788. exit;
  1789. end;
  1790. end;
  1791. TCSetAttr:=IOCtl(fd,nr,@Tios);
  1792. end;
  1793. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  1794. begin
  1795. {$ifndef BSD}
  1796. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  1797. {$else}
  1798. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  1799. {$endif}
  1800. end;
  1801. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  1802. begin
  1803. {$ifndef BSD}
  1804. CFSetISpeed(tios,speed);
  1805. {$else}
  1806. tios.c_ospeed:=speed;
  1807. {$endif}
  1808. end;
  1809. Procedure CFMakeRaw(var tios:TermIOS);
  1810. begin
  1811. {$ifndef BSD}
  1812. with tios do
  1813. begin
  1814. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  1815. INLCR or IGNCR or ICRNL or IXON));
  1816. c_oflag:=c_oflag and (not OPOST);
  1817. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  1818. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  1819. end;
  1820. {$else}
  1821. with tios do
  1822. begin
  1823. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  1824. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  1825. IGNPAR));
  1826. c_iflag:=c_iflag OR IGNBRK;
  1827. c_oflag:=c_oflag and (not OPOST);
  1828. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  1829. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  1830. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  1831. c_cc[VMIN]:=1;
  1832. c_cc[VTIME]:=0;
  1833. end;
  1834. {$endif}
  1835. end;
  1836. Function TCSendBreak(fd,duration:longint):boolean;
  1837. begin
  1838. {$ifndef BSD}
  1839. TCSendBreak:=IOCtl(fd,TCSBRK,pointer(duration));
  1840. {$else}
  1841. TCSendBreak:=IOCtl(fd,TIOCSBRK,0);
  1842. {$endif}
  1843. end;
  1844. Function TCSetPGrp(fd,id:longint):boolean;
  1845. begin
  1846. TCSetPGrp:=IOCtl(fd,TIOCSPGRP,pointer(id));
  1847. end;
  1848. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  1849. begin
  1850. TCGetPGrp:=IOCtl(fd,TIOCGPGRP,@id);
  1851. end;
  1852. Function TCDrain(fd:longint):boolean;
  1853. begin
  1854. {$ifndef BSD}
  1855. TCDrain:=IOCtl(fd,TCSBRK,pointer(1));
  1856. {$else}
  1857. TCDrain:=IOCtl(fd,TIOCDRAIN,0); {Should set timeout to 1 first?}
  1858. {$endif}
  1859. end;
  1860. Function TCFlow(fd,act:longint):boolean;
  1861. begin
  1862. {$ifndef BSD}
  1863. TCFlow:=IOCtl(fd,TCXONC,pointer(act));
  1864. {$else}
  1865. case act OF
  1866. TCOOFF : TCFlow:=Ioctl(fd,TIOCSTOP,0);
  1867. TCOOn : TCFlow:=IOctl(Fd,TIOCStart,0);
  1868. TCIOFF : {N/I}
  1869. end;
  1870. {$endif}
  1871. end;
  1872. Function TCFlush(fd,qsel:longint):boolean;
  1873. begin
  1874. {$ifndef BSD}
  1875. TCFlush:=IOCtl(fd,TCFLSH,pointer(qsel));
  1876. {$else}
  1877. TCFlush:=IOCtl(fd,TIOCFLUSH,pointer(qsel));
  1878. {$endif}
  1879. end;
  1880. Function IsATTY(Handle:Longint):Boolean;
  1881. {
  1882. Check if the filehandle described by 'handle' is a TTY (Terminal)
  1883. }
  1884. var
  1885. t : Termios;
  1886. begin
  1887. IsAtty:=TCGetAttr(Handle,t);
  1888. end;
  1889. Function IsATTY(f: text):Boolean;
  1890. {
  1891. Idem as previous, only now for text variables.
  1892. }
  1893. begin
  1894. IsATTY:=IsaTTY(textrec(f).handle);
  1895. end;
  1896. function TTYName(Handle:Longint):string;
  1897. {
  1898. Return the name of the current tty described by handle f.
  1899. returns empty string in case of an error.
  1900. }
  1901. var
  1902. mydev,
  1903. myino : longint;
  1904. st : stat;
  1905. function mysearch(n:string): boolean;
  1906. {searches recursively for the device in the directory given by n,
  1907. returns true if found and sets the name of the device in ttyname}
  1908. var dirstream : pdir;
  1909. d : pdirent;
  1910. name : string;
  1911. st : stat;
  1912. begin
  1913. dirstream:=opendir(n);
  1914. if (linuxerror<>0) then
  1915. exit;
  1916. d:=Readdir(dirstream);
  1917. while (d<>nil) do
  1918. begin
  1919. name:=n+'/'+strpas(@(d^.name));
  1920. fstat(name,st);
  1921. if linuxerror=0 then
  1922. begin
  1923. if ((st.mode and $E000)=$4000) and { if it is a directory }
  1924. (strpas(@(d^.name))<>'.') and { but not ., .. and fd subdirs }
  1925. (strpas(@(d^.name))<>'..') and
  1926. (strpas(@(d^.name))<>'') and
  1927. (strpas(@(d^.name))<>'fd') then
  1928. begin {we found a directory, search inside it}
  1929. if mysearch(name) then
  1930. begin {the device is here}
  1931. closedir(dirstream); {then don't continue searching}
  1932. mysearch:=true;
  1933. exit;
  1934. end;
  1935. end
  1936. else if (d^.ino=myino) and (st.dev=mydev) then
  1937. begin
  1938. closedir(dirstream);
  1939. ttyname:=name;
  1940. mysearch:=true;
  1941. exit;
  1942. end;
  1943. end;
  1944. d:=Readdir(dirstream);
  1945. end;
  1946. closedir(dirstream);
  1947. mysearch:=false;
  1948. end;
  1949. begin
  1950. TTYName:='';
  1951. fstat(handle,st);
  1952. if (errno<>0) and isatty (handle) then
  1953. exit;
  1954. mydev:=st.dev;
  1955. myino:=st.ino;
  1956. mysearch('/dev');
  1957. end;
  1958. function TTYName(var F:Text):string;
  1959. {
  1960. Idem as previous, only now for text variables;
  1961. }
  1962. begin
  1963. TTYName:=TTYName(textrec(f).handle);
  1964. end;
  1965. {******************************************************************************
  1966. Utility calls
  1967. ******************************************************************************}
  1968. Function Octal(l:longint):longint;
  1969. {
  1970. Convert an octal specified number to decimal;
  1971. }
  1972. var
  1973. octnr,
  1974. oct : longint;
  1975. begin
  1976. octnr:=0;
  1977. oct:=0;
  1978. while (l>0) do
  1979. begin
  1980. oct:=oct or ((l mod 10) shl octnr);
  1981. l:=l div 10;
  1982. inc(octnr,3);
  1983. end;
  1984. Octal:=oct;
  1985. end;
  1986. Function StringToPPChar(S: PChar):ppchar;
  1987. var
  1988. nr : longint;
  1989. Buf : ^char;
  1990. p : ppchar;
  1991. begin
  1992. buf:=s;
  1993. nr:=0;
  1994. while(buf^<>#0) do
  1995. begin
  1996. while (buf^ in [' ',#8,#10]) do
  1997. inc(buf);
  1998. inc(nr);
  1999. while not (buf^ in [' ',#0,#8,#10]) do
  2000. inc(buf);
  2001. end;
  2002. getmem(p,nr*4);
  2003. StringToPPChar:=p;
  2004. if p=nil then
  2005. begin
  2006. LinuxError:=sys_enomem;
  2007. exit;
  2008. end;
  2009. buf:=s;
  2010. while (buf^<>#0) do
  2011. begin
  2012. while (buf^ in [' ',#8,#10]) do
  2013. begin
  2014. buf^:=#0;
  2015. inc(buf);
  2016. end;
  2017. p^:=buf;
  2018. inc(p);
  2019. p^:=nil;
  2020. while not (buf^ in [' ',#0,#8,#10]) do
  2021. inc(buf);
  2022. end;
  2023. end;
  2024. {
  2025. function FExpand (const Path: PathStr): PathStr;
  2026. - declared in fexpand.inc
  2027. }
  2028. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  2029. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  2030. {$I fexpand.inc}
  2031. {$UNDEF FPC_FEXPAND_GETENVPCHAR}
  2032. {$UNDEF FPC_FEXPAND_TILDE}
  2033. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  2034. {
  2035. Searches for a file 'path' in the list of direcories in 'dirlist'.
  2036. returns an empty string if not found. Wildcards are NOT allowed.
  2037. If dirlist is empty, it is set to '.'
  2038. }
  2039. Var
  2040. NewDir : PathStr;
  2041. p1 : Longint;
  2042. Info : Stat;
  2043. Begin
  2044. {Replace ':' with ';'}
  2045. for p1:=1to length(dirlist) do
  2046. if dirlist[p1]=':' then
  2047. dirlist[p1]:=';';
  2048. {Check for WildCards}
  2049. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  2050. FSearch:='' {No wildcards allowed in these things.}
  2051. Else
  2052. Begin
  2053. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  2054. Repeat
  2055. p1:=Pos(';',DirList);
  2056. If p1=0 Then
  2057. p1:=255;
  2058. NewDir:=Copy(DirList,1,P1 - 1);
  2059. if NewDir[Length(NewDir)]<>'/' then
  2060. NewDir:=NewDir+'/';
  2061. NewDir:=NewDir+Path;
  2062. Delete(DirList,1,p1);
  2063. if FStat(NewDir,Info) then
  2064. Begin
  2065. If Pos('./',NewDir)=1 Then
  2066. Delete(NewDir,1,2);
  2067. {DOS strips off an initial .\}
  2068. End
  2069. Else
  2070. NewDir:='';
  2071. Until (DirList='') or (Length(NewDir) > 0);
  2072. FSearch:=NewDir;
  2073. End;
  2074. End;
  2075. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  2076. Var
  2077. DotPos,SlashPos,i : longint;
  2078. Begin
  2079. SlashPos:=0;
  2080. DotPos:=256;
  2081. i:=Length(Path);
  2082. While (i>0) and (SlashPos=0) Do
  2083. Begin
  2084. If (DotPos=256) and (Path[i]='.') Then
  2085. begin
  2086. DotPos:=i;
  2087. end;
  2088. If (Path[i]='/') Then
  2089. SlashPos:=i;
  2090. Dec(i);
  2091. End;
  2092. Ext:=Copy(Path,DotPos,255);
  2093. Dir:=Copy(Path,1,SlashPos);
  2094. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  2095. End;
  2096. Function Dirname(Const path:pathstr):pathstr;
  2097. {
  2098. This function returns the directory part of a complete path.
  2099. Unless the directory is root '/', The last character is not
  2100. a slash.
  2101. }
  2102. var
  2103. Dir : PathStr;
  2104. Name : NameStr;
  2105. Ext : ExtStr;
  2106. begin
  2107. FSplit(Path,Dir,Name,Ext);
  2108. if length(Dir)>1 then
  2109. Delete(Dir,length(Dir),1);
  2110. DirName:=Dir;
  2111. end;
  2112. Function StringToPPChar(Var S:String):ppchar;
  2113. {
  2114. Create a PPChar to structure of pchars which are the arguments specified
  2115. in the string S. Especially usefull for creating an ArgV for Exec-calls
  2116. Note that the string S is destroyed by this call.
  2117. }
  2118. begin
  2119. S:=S+#0;
  2120. StringToPPChar:=StringToPPChar(@S[1]);
  2121. end;
  2122. Function StringToPPChar(Var S:AnsiString):ppchar;
  2123. {
  2124. Create a PPChar to structure of pchars which are the arguments specified
  2125. in the string S. Especially usefull for creating an ArgV for Exec-calls
  2126. }
  2127. begin
  2128. StringToPPChar:=StringToPPChar(PChar(S));
  2129. end;
  2130. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  2131. {
  2132. This function returns the filename part of a complete path. If suf is
  2133. supplied, it is cut off the filename.
  2134. }
  2135. var
  2136. Dir : PathStr;
  2137. Name : NameStr;
  2138. Ext : ExtStr;
  2139. begin
  2140. FSplit(Path,Dir,Name,Ext);
  2141. if Suf<>Ext then
  2142. Name:=Name+Ext;
  2143. BaseName:=Name;
  2144. end;
  2145. Function FNMatch(const Pattern,Name:string):Boolean;
  2146. Var
  2147. LenPat,LenName : longint;
  2148. Function DoFNMatch(i,j:longint):Boolean;
  2149. Var
  2150. Found : boolean;
  2151. Begin
  2152. Found:=true;
  2153. While Found and (i<=LenPat) Do
  2154. Begin
  2155. Case Pattern[i] of
  2156. '?' : Found:=(j<=LenName);
  2157. '*' : Begin
  2158. {find the next character in pattern, different of ? and *}
  2159. while Found and (i<LenPat) do
  2160. begin
  2161. inc(i);
  2162. case Pattern[i] of
  2163. '*' : ;
  2164. '?' : begin
  2165. inc(j);
  2166. Found:=(j<=LenName);
  2167. end;
  2168. else
  2169. Found:=false;
  2170. end;
  2171. end;
  2172. {Now, find in name the character which i points to, if the * or ?
  2173. wasn't the last character in the pattern, else, use up all the
  2174. chars in name}
  2175. Found:=true;
  2176. if (i<=LenPat) then
  2177. begin
  2178. repeat
  2179. {find a letter (not only first !) which maches pattern[i]}
  2180. while (j<=LenName) and (name[j]<>pattern[i]) do
  2181. inc (j);
  2182. if (j<LenName) then
  2183. begin
  2184. if DoFnMatch(i+1,j+1) then
  2185. begin
  2186. i:=LenPat;
  2187. j:=LenName;{we can stop}
  2188. Found:=true;
  2189. end
  2190. else
  2191. inc(j);{We didn't find one, need to look further}
  2192. end;
  2193. until (j>=LenName);
  2194. end
  2195. else
  2196. j:=LenName;{we can stop}
  2197. end;
  2198. else {not a wildcard character in pattern}
  2199. Found:=(j<=LenName) and (pattern[i]=name[j]);
  2200. end;
  2201. inc(i);
  2202. inc(j);
  2203. end;
  2204. DoFnMatch:=Found and (j>LenName);
  2205. end;
  2206. Begin {start FNMatch}
  2207. LenPat:=Length(Pattern);
  2208. LenName:=Length(Name);
  2209. FNMatch:=DoFNMatch(1,1);
  2210. End;
  2211. Procedure Globfree(var p : pglob);
  2212. {
  2213. Release memory occupied by pglob structure, and names in it.
  2214. sets p to nil.
  2215. }
  2216. var
  2217. temp : pglob;
  2218. begin
  2219. while assigned(p) do
  2220. begin
  2221. temp:=p^.next;
  2222. if assigned(p^.name) then
  2223. freemem(p^.name);
  2224. dispose(p);
  2225. p:=temp;
  2226. end;
  2227. end;
  2228. Function Glob(Const path:pathstr):pglob;
  2229. {
  2230. Fills a tglob structure with entries matching path,
  2231. and returns a pointer to it. Returns nil on error,
  2232. linuxerror is set accordingly.
  2233. }
  2234. var
  2235. temp,
  2236. temp2 : string[255];
  2237. thedir : pdir;
  2238. buffer : pdirent;
  2239. root,
  2240. current : pglob;
  2241. begin
  2242. { Get directory }
  2243. temp:=dirname(path);
  2244. if temp='' then
  2245. temp:='.';
  2246. temp:=temp+#0;
  2247. thedir:=opendir(@temp[1]);
  2248. if thedir=nil then
  2249. begin
  2250. glob:=nil;
  2251. linuxerror:=errno;
  2252. exit;
  2253. end;
  2254. temp:=basename(path,''); { get the pattern }
  2255. if thedir^.fd<0 then
  2256. begin
  2257. linuxerror:=errno;
  2258. glob:=nil;
  2259. exit;
  2260. end;
  2261. {get the entries}
  2262. root:=nil;
  2263. current:=nil;
  2264. repeat
  2265. buffer:=Sys_readdir(thedir);
  2266. if buffer=nil then
  2267. break;
  2268. temp2:=strpas(@(buffer^.name[0]));
  2269. if fnmatch(temp,temp2) then
  2270. begin
  2271. if root=nil then
  2272. begin
  2273. new(root);
  2274. current:=root;
  2275. end
  2276. else
  2277. begin
  2278. new(current^.next);
  2279. current:=current^.next;
  2280. end;
  2281. if current=nil then
  2282. begin
  2283. linuxerror:=Sys_ENOMEM;
  2284. globfree(root);
  2285. break;
  2286. end;
  2287. current^.next:=nil;
  2288. getmem(current^.name,length(temp2)+1);
  2289. if current^.name=nil then
  2290. begin
  2291. linuxerror:=Sys_ENOMEM;
  2292. globfree(root);
  2293. break;
  2294. end;
  2295. move(buffer^.name[0],current^.name^,length(temp2)+1);
  2296. end;
  2297. until false;
  2298. closedir(thedir);
  2299. glob:=root;
  2300. end;
  2301. {--------------------------------
  2302. FiledescriptorSets
  2303. --------------------------------}
  2304. Procedure FD_Zero(var fds:fdSet);
  2305. {
  2306. Clear the set of filedescriptors
  2307. }
  2308. begin
  2309. FillChar(fds,sizeof(fdSet),0);
  2310. end;
  2311. Procedure FD_Clr(fd:longint;var fds:fdSet);
  2312. {
  2313. Remove fd from the set of filedescriptors
  2314. }
  2315. begin
  2316. fds[fd shr 5]:=fds[fd shr 5] and (not (1 shl (fd and 31)));
  2317. end;
  2318. Procedure FD_Set(fd:longint;var fds:fdSet);
  2319. {
  2320. Add fd to the set of filedescriptors
  2321. }
  2322. begin
  2323. fds[fd shr 5]:=fds[fd shr 5] or (1 shl (fd and 31));
  2324. end;
  2325. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  2326. {
  2327. Test if fd is part of the set of filedescriptors
  2328. }
  2329. begin
  2330. FD_IsSet:=((fds[fd shr 5] and (1 shl (fd and 31)))<>0);
  2331. end;
  2332. Function GetFS (var T:Text):longint;
  2333. {
  2334. Get File Descriptor of a text file.
  2335. }
  2336. begin
  2337. if textrec(t).mode=fmclosed then
  2338. exit(-1)
  2339. else
  2340. GETFS:=textrec(t).Handle
  2341. end;
  2342. Function GetFS(Var F:File):longint;
  2343. {
  2344. Get File Descriptor of an unTyped file.
  2345. }
  2346. begin
  2347. { Handle and mode are on the same place in textrec and filerec. }
  2348. if filerec(f).mode=fmclosed then
  2349. exit(-1)
  2350. else
  2351. GETFS:=filerec(f).Handle
  2352. end;
  2353. {--------------------------------
  2354. Stat.Mode Macro's
  2355. --------------------------------}
  2356. Function S_ISLNK(m:word):boolean;
  2357. {
  2358. Check mode field of inode for link.
  2359. }
  2360. begin
  2361. S_ISLNK:=(m and STAT_IFMT)=STAT_IFLNK;
  2362. end;
  2363. Function S_ISREG(m:word):boolean;
  2364. {
  2365. Check mode field of inode for regular file.
  2366. }
  2367. begin
  2368. S_ISREG:=(m and STAT_IFMT)=STAT_IFREG;
  2369. end;
  2370. Function S_ISDIR(m:word):boolean;
  2371. {
  2372. Check mode field of inode for directory.
  2373. }
  2374. begin
  2375. S_ISDIR:=(m and STAT_IFMT)=STAT_IFDIR;
  2376. end;
  2377. Function S_ISCHR(m:word):boolean;
  2378. {
  2379. Check mode field of inode for character device.
  2380. }
  2381. begin
  2382. S_ISCHR:=(m and STAT_IFMT)=STAT_IFCHR;
  2383. end;
  2384. Function S_ISBLK(m:word):boolean;
  2385. {
  2386. Check mode field of inode for block device.
  2387. }
  2388. begin
  2389. S_ISBLK:=(m and STAT_IFMT)=STAT_IFBLK;
  2390. end;
  2391. Function S_ISFIFO(m:word):boolean;
  2392. {
  2393. Check mode field of inode for named pipe (FIFO).
  2394. }
  2395. begin
  2396. S_ISFIFO:=(m and STAT_IFMT)=STAT_IFIFO;
  2397. end;
  2398. Function S_ISSOCK(m:word):boolean;
  2399. {
  2400. Check mode field of inode for socket.
  2401. }
  2402. begin
  2403. S_ISSOCK:=(m and STAT_IFMT)=STAT_IFSOCK;
  2404. end;
  2405. {--------------------------------
  2406. Memory functions
  2407. --------------------------------}
  2408. {$IFDEF I386}
  2409. Procedure WritePort (Port : Longint; Value : Byte);
  2410. {
  2411. Writes 'Value' to port 'Port'
  2412. }
  2413. begin
  2414. asm
  2415. movl port,%edx
  2416. movb value,%al
  2417. outb %al,%dx
  2418. end ['EAX','EDX'];
  2419. end;
  2420. Procedure WritePort (Port : Longint; Value : Word);
  2421. {
  2422. Writes 'Value' to port 'Port'
  2423. }
  2424. begin
  2425. asm
  2426. movl port,%edx
  2427. movw value,%ax
  2428. outw %ax,%dx
  2429. end ['EAX','EDX'];
  2430. end;
  2431. Procedure WritePort (Port : Longint; Value : Longint);
  2432. {
  2433. Writes 'Value' to port 'Port'
  2434. }
  2435. begin
  2436. asm
  2437. movl port,%edx
  2438. movl value,%eax
  2439. outl %eax,%dx
  2440. end ['EAX','EDX'];
  2441. end;
  2442. Procedure WritePortB (Port : Longint; Value : Byte);
  2443. {
  2444. Writes 'Value' to port 'Port'
  2445. }
  2446. begin
  2447. asm
  2448. movl port,%edx
  2449. movb value,%al
  2450. outb %al,%dx
  2451. end ['EAX','EDX'];
  2452. end;
  2453. Procedure WritePortW (Port : Longint; Value : Word);
  2454. {
  2455. Writes 'Value' to port 'Port'
  2456. }
  2457. begin
  2458. asm
  2459. movl port,%edx
  2460. movw value,%ax
  2461. outw %ax,%dx
  2462. end ['EAX','EDX'];
  2463. end;
  2464. Procedure WritePortL (Port : Longint; Value : Longint);
  2465. {
  2466. Writes 'Value' to port 'Port'
  2467. }
  2468. begin
  2469. asm
  2470. movl port,%edx
  2471. movl value,%eax
  2472. outl %eax,%dx
  2473. end ['EAX','EDX'];
  2474. end;
  2475. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  2476. {
  2477. Writes 'Count' longints from 'Buf' to Port
  2478. }
  2479. begin
  2480. asm
  2481. movl count,%ecx
  2482. movl buf,%esi
  2483. movl port,%edx
  2484. cld
  2485. rep
  2486. outsl
  2487. end ['ECX','ESI','EDX'];
  2488. end;
  2489. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  2490. {
  2491. Writes 'Count' words from 'Buf' to Port
  2492. }
  2493. begin
  2494. asm
  2495. movl count,%ecx
  2496. movl buf,%esi
  2497. movl port,%edx
  2498. cld
  2499. rep
  2500. outsw
  2501. end ['ECX','ESI','EDX'];
  2502. end;
  2503. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  2504. {
  2505. Writes 'Count' bytes from 'Buf' to Port
  2506. }
  2507. begin
  2508. asm
  2509. movl count,%ecx
  2510. movl buf,%esi
  2511. movl port,%edx
  2512. cld
  2513. rep
  2514. outsb
  2515. end ['ECX','ESI','EDX'];
  2516. end;
  2517. Procedure ReadPort (Port : Longint; Var Value : Byte);
  2518. {
  2519. Reads 'Value' from port 'Port'
  2520. }
  2521. begin
  2522. asm
  2523. movl port,%edx
  2524. inb %dx,%al
  2525. movl value,%edx
  2526. movb %al,(%edx)
  2527. end ['EAX','EDX'];
  2528. end;
  2529. Procedure ReadPort (Port : Longint; Var Value : Word);
  2530. {
  2531. Reads 'Value' from port 'Port'
  2532. }
  2533. begin
  2534. asm
  2535. movl port,%edx
  2536. inw %dx,%ax
  2537. movl value,%edx
  2538. movw %ax,(%edx)
  2539. end ['EAX','EDX'];
  2540. end;
  2541. Procedure ReadPort (Port : Longint; Var Value : Longint);
  2542. {
  2543. Reads 'Value' from port 'Port'
  2544. }
  2545. begin
  2546. asm
  2547. movl port,%edx
  2548. inl %dx,%eax
  2549. movl value,%edx
  2550. movl %eax,(%edx)
  2551. end ['EAX','EDX'];
  2552. end;
  2553. function ReadPortB (Port : Longint): Byte; assembler;
  2554. {
  2555. Reads a byte from port 'Port'
  2556. }
  2557. asm
  2558. xorl %eax,%eax
  2559. movl port,%edx
  2560. inb %dx,%al
  2561. end ['EAX','EDX'];
  2562. function ReadPortW (Port : Longint): Word; assembler;
  2563. {
  2564. Reads a word from port 'Port'
  2565. }
  2566. asm
  2567. xorl %eax,%eax
  2568. movl port,%edx
  2569. inw %dx,%ax
  2570. end ['EAX','EDX'];
  2571. function ReadPortL (Port : Longint): LongInt; assembler;
  2572. {
  2573. Reads a LongInt from port 'Port'
  2574. }
  2575. asm
  2576. movl port,%edx
  2577. inl %dx,%eax
  2578. end ['EAX','EDX'];
  2579. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  2580. {
  2581. Reads 'Count' longints from port 'Port' to 'Buf'.
  2582. }
  2583. begin
  2584. asm
  2585. movl count,%ecx
  2586. movl buf,%edi
  2587. movl port,%edx
  2588. cld
  2589. rep
  2590. insl
  2591. end ['ECX','EDI','EDX'];
  2592. end;
  2593. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  2594. {
  2595. Reads 'Count' words from port 'Port' to 'Buf'.
  2596. }
  2597. begin
  2598. asm
  2599. movl count,%ecx
  2600. movl buf,%edi
  2601. movl port,%edx
  2602. cld
  2603. rep
  2604. insw
  2605. end ['ECX','EDI','EDX'];
  2606. end;
  2607. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  2608. {
  2609. Reads 'Count' bytes from port 'Port' to 'Buf'.
  2610. }
  2611. begin
  2612. asm
  2613. movl count,%ecx
  2614. movl buf,%edi
  2615. movl port,%edx
  2616. cld
  2617. rep
  2618. insb
  2619. end ['ECX','EDI','EDX'];
  2620. end;
  2621. {$ENDIF}
  2622. Initialization
  2623. InitLocalTime;
  2624. finalization
  2625. DoneLocalTime;
  2626. End.
  2627. {
  2628. $Log$
  2629. Revision 1.21 2002-01-02 12:22:54 marco
  2630. * Removed ifdef arround getepoch.
  2631. Revision 1.20 2001/12/26 21:03:57 peter
  2632. * merged fixes from 1.0.x
  2633. Revision 1.19 2001/11/30 07:16:42 marco
  2634. * TTYname fix from Maarten Beekers. Apparantly accidentally not commited the first time.
  2635. Revision 1.18 2001/11/05 21:46:06 michael
  2636. + Added exit status examining functions
  2637. Revision 1.17 2001/10/14 13:33:21 peter
  2638. * start of thread support for linux
  2639. Revision 1.16 2001/09/17 21:36:31 peter
  2640. * merged fixes
  2641. Revision 1.15 2001/08/12 18:05:19 peter
  2642. * Range check fix with epoch (merged)
  2643. Revision 1.14 2001/07/12 12:42:39 marco
  2644. * Fixes to the FreeBSD compability of the datetime patches
  2645. Revision 1.13 2001/07/12 07:20:05 michael
  2646. + Added setdate/time/datetime functions
  2647. Revision 1.12 2001/06/20 15:24:47 marco
  2648. * readlink for Unix fix.
  2649. Revision 1.11 2001/06/19 17:19:50 marco
  2650. * Again uncommented readlink. Merges or typo, I dunno
  2651. Revision 1.10 2001/06/13 22:20:11 hajny
  2652. + platform specific information
  2653. Revision 1.9 2001/06/03 20:19:09 peter
  2654. * FSStat to StatFS
  2655. * StatFS structure to TStatFS
  2656. Revision 1.8 2001/06/02 00:31:31 peter
  2657. * merge unix updates from the 1.0 branch, mostly related to the
  2658. solaris target
  2659. Revision 1.7 2001/04/19 12:57:33 marco
  2660. * Readlink uncommented for FreeBSD.
  2661. Revision 1.6 2001/04/13 22:37:21 peter
  2662. * remove warning
  2663. Revision 1.5 2001/03/27 11:47:25 michael
  2664. + Fixed F_[G,S]etOwn constants. By Alexander Sychev
  2665. Revision 1.4 2001/03/17 16:04:37 hajny
  2666. * FExpand omission fixed
  2667. Revision 1.3 2001/03/16 20:09:58 hajny
  2668. * universal FExpand
  2669. Revision 1.2 2001/01/22 07:25:10 marco
  2670. * IOPERM for FreeBSD. Port routines moved from linsysca to Unix again .
  2671. Revision 1.1 2001/01/21 20:21:41 marco
  2672. * Rename fest II. Rtl OK
  2673. Revision 1.6 2000/12/28 20:42:12 peter
  2674. * ttyname fix from the mailinglist (merged)
  2675. Revision 1.5 2000/10/26 22:51:12 peter
  2676. * nano sleep (merged)
  2677. Revision 1.4 2000/10/11 13:59:16 marco
  2678. * FreeBSD TermIOS support and minor changes to some related files.
  2679. Revision 1.3 2000/10/10 12:02:35 marco
  2680. * Terminal stuff of Linux moved to separate file. I think it is too
  2681. much to do with ifdefs.
  2682. Revision 1.2 2000/09/18 13:14:50 marco
  2683. * Global Linux +bsd to (rtl/freebsd rtl/unix rtl/linux structure)
  2684. Revision 1.7 2000/09/12 08:51:43 marco
  2685. * fixed some small problems left from merging. (waitpid has now last param longint)
  2686. Revision 1.6 2000/09/11 14:05:31 marco
  2687. * FreeBSD support and removed old signalhandling
  2688. Revision 1.5 2000/09/06 20:47:34 peter
  2689. * removed previous fsplit() patch as it's not the correct behaviour for
  2690. LFNs. The code showing the bug could easily be adapted (merged)
  2691. Revision 1.4 2000/09/04 20:17:53 peter
  2692. * fixed previous commit (merged)
  2693. Revision 1.3 2000/09/04 19:38:13 peter
  2694. * fsplit with .. fix from Thomas (merged)
  2695. Revision 1.2 2000/07/13 11:33:48 michael
  2696. + removed logs
  2697. }