linuxold.inc 72 KB

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