unix.pp 70 KB

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