unix.pp 69 KB

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