linuxold.inc 72 KB

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