2
0

linuxold.inc 72 KB

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