unix.pp 69 KB

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