posix.texi 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007,
  4. @c 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2017, 2021 Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node POSIX
  7. @section @acronym{POSIX} System Calls and Networking
  8. @cindex POSIX
  9. @menu
  10. * Conventions:: Conventions employed by the POSIX interface.
  11. * Ports and File Descriptors:: Scheme ``ports'' and Unix file descriptors
  12. have different representations.
  13. * File System:: stat, chown, chmod, etc.
  14. * User Information:: Retrieving a user's GECOS (/etc/passwd) entry.
  15. * Time:: gettimeofday, localtime, strftime, etc.
  16. * Runtime Environment:: Accessing and modifying Guile's environment.
  17. * Processes:: getuid, getpid, etc.
  18. * Signals:: sigaction, kill, pause, alarm, setitimer, etc.
  19. * Terminals and Ptys:: ttyname, tcsetpgrp, etc.
  20. * Pipes:: Communicating data between processes.
  21. * Networking:: gethostbyaddr, getnetent, socket, bind, listen.
  22. * System Identification:: Obtaining information about the system.
  23. * Locales:: setlocale, etc.
  24. * Encryption::
  25. @end menu
  26. @node Conventions
  27. @subsection @acronym{POSIX} Interface Conventions
  28. These interfaces provide access to operating system facilities.
  29. They provide a simple wrapping around the underlying C interfaces
  30. to make usage from Scheme more convenient. They are also used
  31. to implement the Guile port of scsh (@pxref{The Scheme shell (scsh)}).
  32. Generally there is a single procedure for each corresponding Unix
  33. facility. There are some exceptions, such as procedures implemented for
  34. speed and convenience in Scheme with no primitive Unix equivalent,
  35. e.g.@: @code{copy-file}.
  36. The interfaces are intended as far as possible to be portable across
  37. different versions of Unix. In some cases procedures which can't be
  38. implemented on particular systems may become no-ops, or perform limited
  39. actions. In other cases they may throw errors.
  40. General naming conventions are as follows:
  41. @itemize @bullet
  42. @item
  43. The Scheme name is often identical to the name of the underlying Unix
  44. facility.
  45. @item
  46. Underscores in Unix procedure names are converted to hyphens.
  47. @item
  48. Procedures which destructively modify Scheme data have exclamation
  49. marks appended, e.g., @code{recv!}.
  50. @item
  51. Predicates (returning only @code{#t} or @code{#f}) have question marks
  52. appended, e.g., @code{access?}.
  53. @item
  54. Some names are changed to avoid conflict with dissimilar interfaces
  55. defined by scsh, e.g., @code{primitive-fork}.
  56. @item
  57. Unix preprocessor names such as @code{EPERM} or @code{R_OK} are converted
  58. to Scheme variables of the same name (underscores are not replaced
  59. with hyphens).
  60. @end itemize
  61. Unexpected conditions are generally handled by raising exceptions.
  62. There are a few procedures which return a special value if they don't
  63. succeed, e.g., @code{getenv} returns @code{#f} if it the requested
  64. string is not found in the environment. These cases are noted in
  65. the documentation.
  66. For ways to deal with exceptions, see @ref{Exceptions}.
  67. @cindex @code{errno}
  68. Errors which the C library would report by returning a null pointer or
  69. through some other means are reported by raising a @code{system-error}
  70. exception with @code{scm-error} (@pxref{Error Reporting}). The
  71. @var{data} parameter is a list containing the Unix @code{errno} value
  72. (an integer). For example,
  73. @example
  74. (define (my-handler key func fmt fmtargs data)
  75. (display key) (newline)
  76. (display func) (newline)
  77. (apply format #t fmt fmtargs) (newline)
  78. (display data) (newline))
  79. (catch 'system-error
  80. (lambda () (dup2 -123 -456))
  81. my-handler)
  82. @print{}
  83. system-error
  84. dup2
  85. Bad file descriptor
  86. (9)
  87. @end example
  88. @sp 1
  89. @defun system-error-errno arglist
  90. @cindex @code{errno}
  91. Return the @code{errno} value from a list which is the arguments to an
  92. exception handler. If the exception is not a @code{system-error},
  93. then the return is @code{#f}. For example,
  94. @example
  95. (catch
  96. 'system-error
  97. (lambda ()
  98. (mkdir "/this-ought-to-fail-if-I'm-not-root"))
  99. (lambda stuff
  100. (let ((errno (system-error-errno stuff)))
  101. (cond
  102. ((= errno EACCES)
  103. (display "You're not allowed to do that."))
  104. ((= errno EEXIST)
  105. (display "Already exists."))
  106. (#t
  107. (display (strerror errno))))
  108. (newline))))
  109. @end example
  110. @end defun
  111. @node Ports and File Descriptors
  112. @subsection Ports and File Descriptors
  113. @cindex file descriptor
  114. Conventions generally follow those of scsh, @ref{The Scheme shell (scsh)}.
  115. Each open file port has an associated operating system file descriptor.
  116. File descriptors are generally not useful in Scheme programs; however
  117. they may be needed when interfacing with foreign code and the Unix
  118. environment.
  119. A file descriptor can be extracted from a port and a new port can be
  120. created from a file descriptor. However a file descriptor is just an
  121. integer and the garbage collector doesn't recognize it as a reference
  122. to the port. If all other references to the port were dropped, then
  123. it's likely that the garbage collector would free the port, with the
  124. side-effect of closing the file descriptor prematurely.
  125. To assist the programmer in avoiding this problem, each port has an
  126. associated @dfn{revealed count} which can be used to keep track of how many
  127. times the underlying file descriptor has been stored in other places.
  128. If a port's revealed count is greater than zero, the file descriptor
  129. will not be closed when the port is garbage collected. A programmer
  130. can therefore ensure that the revealed count will be greater than
  131. zero if the file descriptor is needed elsewhere.
  132. For the simple case where a file descriptor is ``imported'' once to become
  133. a port, it does not matter if the file descriptor is closed when the
  134. port is garbage collected. There is no need to maintain a revealed
  135. count. Likewise when ``exporting'' a file descriptor to the external
  136. environment, setting the revealed count is not required provided the
  137. port is kept open (i.e., is pointed to by a live Scheme binding) while
  138. the file descriptor is in use.
  139. To correspond with traditional Unix behaviour, three file descriptors
  140. (0, 1, and 2) are automatically imported when a program starts up and
  141. assigned to the initial values of the current/standard input, output,
  142. and error ports, respectively. The revealed count for each is
  143. initially set to one, so that dropping references to one of these
  144. ports will not result in its garbage collection: it could be retrieved
  145. with @code{fdopen} or @code{fdes->ports}.
  146. Guile's ports can be buffered. This means that writing a byte to a file
  147. port goes to the internal buffer first, and only when the buffer is full
  148. (or the user invokes @code{force-output} on the port) is the data
  149. actually written to the file descriptor. Likewise on input, bytes are
  150. read in from the file descriptor in blocks and placed in a buffer.
  151. Reading a character via @code{read-char} first goes to the buffer,
  152. filling it as needed. Usually read buffering is more or less
  153. transparent, but write buffering can sometimes cause writes to be
  154. delayed unexpectedly, if you forget to call @code{force-output}.
  155. @xref{Buffering}, for more on how to control port buffers.
  156. Note however that some procedures (e.g., @code{recv!}) will accept ports
  157. as arguments, but will actually operate directly on the file descriptor
  158. underlying the port. Any port buffering is ignored, including the
  159. buffer which implements @code{peek-char} and @code{unread-char}.
  160. @deffn {Scheme Procedure} port-revealed port
  161. @deffnx {C Function} scm_port_revealed (port)
  162. Return the revealed count for @var{port}.
  163. @end deffn
  164. @deffn {Scheme Procedure} set-port-revealed! port rcount
  165. @deffnx {C Function} scm_set_port_revealed_x (port, rcount)
  166. Sets the revealed count for a @var{port} to @var{rcount}.
  167. The return value is unspecified.
  168. @end deffn
  169. @deffn {Scheme Procedure} fileno port
  170. @deffnx {C Function} scm_fileno (port)
  171. Return the integer file descriptor underlying @var{port}. Does
  172. not change its revealed count.
  173. @end deffn
  174. @deffn {Scheme Procedure} port->fdes port
  175. Returns the integer file descriptor underlying @var{port}. As a
  176. side effect the revealed count of @var{port} is incremented.
  177. @end deffn
  178. @deffn {Scheme Procedure} fdopen fdes modes
  179. @deffnx {C Function} scm_fdopen (fdes, modes)
  180. Return a new port based on the file descriptor @var{fdes}. Modes are
  181. given by the string @var{modes}. The revealed count of the port is
  182. initialized to zero. The @var{modes} string is the same as that
  183. accepted by @code{open-file} (@pxref{File Ports, open-file}).
  184. @end deffn
  185. @deffn {Scheme Procedure} fdes->ports fdes
  186. @deffnx {C Function} scm_fdes_to_ports (fdes)
  187. Return a list of existing ports which have @var{fdes} as an
  188. underlying file descriptor, without changing their revealed
  189. counts.
  190. @end deffn
  191. @deffn {Scheme Procedure} fdes->inport fdes
  192. Returns an existing input port which has @var{fdes} as its underlying file
  193. descriptor, if one exists, and increments its revealed count.
  194. Otherwise, returns a new input port with a revealed count of 1.
  195. @end deffn
  196. @deffn {Scheme Procedure} fdes->outport fdes
  197. Returns an existing output port which has @var{fdes} as its underlying file
  198. descriptor, if one exists, and increments its revealed count.
  199. Otherwise, returns a new output port with a revealed count of 1.
  200. @end deffn
  201. @deffn {Scheme Procedure} primitive-move->fdes port fdes
  202. @deffnx {C Function} scm_primitive_move_to_fdes (port, fdes)
  203. Moves the underlying file descriptor for @var{port} to the integer
  204. value @var{fdes} without changing the revealed count of @var{port}.
  205. Any other ports already using this descriptor will be automatically
  206. shifted to new descriptors and their revealed counts reset to zero.
  207. The return value is @code{#f} if the file descriptor already had the
  208. required value or @code{#t} if it was moved.
  209. @end deffn
  210. @deffn {Scheme Procedure} move->fdes port fdes
  211. Moves the underlying file descriptor for @var{port} to the integer
  212. value @var{fdes} and sets its revealed count to one. Any other ports
  213. already using this descriptor will be automatically
  214. shifted to new descriptors and their revealed counts reset to zero.
  215. The return value is unspecified.
  216. @end deffn
  217. @deffn {Scheme Procedure} release-port-handle port
  218. Decrements the revealed count for a port.
  219. @end deffn
  220. @deffn {Scheme Procedure} fsync port_or_fd
  221. @deffnx {C Function} scm_fsync (port_or_fd)
  222. Copies any unwritten data for the specified output file descriptor to disk.
  223. If @var{port_or_fd} is a port, its buffer is flushed before the underlying
  224. file descriptor is fsync'd.
  225. The return value is unspecified.
  226. @end deffn
  227. @deffn {Scheme Procedure} open path flags [mode]
  228. @deffnx {C Function} scm_open (path, flags, mode)
  229. Open the file named by @var{path} for reading and/or writing.
  230. @var{flags} is an integer specifying how the file should be opened.
  231. @var{mode} is an integer specifying the permission bits of the file,
  232. if it needs to be created, before the umask (@pxref{Processes}) is
  233. applied. The default is 666 (Unix itself has no default).
  234. @var{flags} can be constructed by combining variables using @code{logior}.
  235. Basic flags are:
  236. @defvar O_RDONLY
  237. Open the file read-only.
  238. @end defvar
  239. @defvar O_WRONLY
  240. Open the file write-only.
  241. @end defvar
  242. @defvar O_RDWR
  243. Open the file read/write.
  244. @end defvar
  245. @defvar O_APPEND
  246. Append to the file instead of truncating.
  247. @end defvar
  248. @defvar O_CREAT
  249. Create the file if it does not already exist.
  250. @end defvar
  251. @xref{File Status Flags,,,libc,The GNU C Library Reference Manual},
  252. for additional flags.
  253. @end deffn
  254. @deffn {Scheme Procedure} open-fdes path flags [mode]
  255. @deffnx {C Function} scm_open_fdes (path, flags, mode)
  256. Similar to @code{open} but return a file descriptor instead of
  257. a port.
  258. @end deffn
  259. @deffn {Scheme Procedure} close fd_or_port
  260. @deffnx {C Function} scm_close (fd_or_port)
  261. Similar to @code{close-port} (@pxref{Ports, close-port}),
  262. but also works on file descriptors. A side
  263. effect of closing a file descriptor is that any ports using that file
  264. descriptor are moved to a different file descriptor and have
  265. their revealed counts set to zero.
  266. @end deffn
  267. @deffn {Scheme Procedure} close-fdes fd
  268. @deffnx {C Function} scm_close_fdes (fd)
  269. A simple wrapper for the @code{close} system call. Close file
  270. descriptor @var{fd}, which must be an integer. Unlike @code{close},
  271. the file descriptor will be closed even if a port is using it. The
  272. return value is unspecified.
  273. @end deffn
  274. @deffn {Scheme Procedure} pipe
  275. @deffnx {C Function} scm_pipe ()
  276. @cindex pipe
  277. Return a newly created pipe: a pair of ports which are linked together
  278. on the local machine. The @acronym{CAR} is the input port and the
  279. @acronym{CDR} is the output port. Data written (and flushed) to the
  280. output port can be read from the input port. Pipes are commonly used
  281. for communication with a newly forked child process. The need to flush
  282. the output port can be avoided by making it unbuffered using
  283. @code{setvbuf} (@pxref{Buffering}).
  284. @defvar PIPE_BUF
  285. A write of up to @code{PIPE_BUF} many bytes to a pipe is atomic,
  286. meaning when done it goes into the pipe instantaneously and as a
  287. contiguous block (@pxref{Pipe Atomicity,, Atomicity of Pipe I/O, libc,
  288. The GNU C Library Reference Manual}).
  289. @end defvar
  290. Note that the output port is likely to block if too much data has been
  291. written but not yet read from the input port. Typically the capacity
  292. is @code{PIPE_BUF} bytes.
  293. @end deffn
  294. The next group of procedures perform a @code{dup2}
  295. system call, if @var{newfd} (an
  296. integer) is supplied, otherwise a @code{dup}. The file descriptor to be
  297. duplicated can be supplied as an integer or contained in a port. The
  298. type of value returned varies depending on which procedure is used.
  299. All procedures also have the side effect when performing @code{dup2} that any
  300. ports using @var{newfd} are moved to a different file descriptor and have
  301. their revealed counts set to zero.
  302. @deffn {Scheme Procedure} dup->fdes fd_or_port [fd]
  303. @deffnx {C Function} scm_dup_to_fdes (fd_or_port, fd)
  304. Return a new integer file descriptor referring to the open file
  305. designated by @var{fd_or_port}, which must be either an open
  306. file port or a file descriptor.
  307. @end deffn
  308. @deffn {Scheme Procedure} dup->inport port/fd [newfd]
  309. Returns a new input port using the new file descriptor.
  310. @end deffn
  311. @deffn {Scheme Procedure} dup->outport port/fd [newfd]
  312. Returns a new output port using the new file descriptor.
  313. @end deffn
  314. @deffn {Scheme Procedure} dup port/fd [newfd]
  315. Returns a new port if @var{port/fd} is a port, with the same mode as the
  316. supplied port, otherwise returns an integer file descriptor.
  317. @end deffn
  318. @deffn {Scheme Procedure} dup->port port/fd mode [newfd]
  319. Returns a new port using the new file descriptor. @var{mode} supplies a
  320. mode string for the port (@pxref{File Ports, open-file}).
  321. @end deffn
  322. @deffn {Scheme Procedure} duplicate-port port modes
  323. Returns a new port which is opened on a duplicate of the file
  324. descriptor underlying @var{port}, with mode string @var{modes}
  325. as for @ref{File Ports, open-file}. The two ports
  326. will share a file position and file status flags.
  327. Unexpected behaviour can result if both ports are subsequently used
  328. and the original and/or duplicate ports are buffered.
  329. The mode string can include @code{0} to obtain an unbuffered duplicate
  330. port.
  331. This procedure is equivalent to @code{(dup->port @var{port} @var{modes})}.
  332. @end deffn
  333. @deffn {Scheme Procedure} redirect-port old_port new_port
  334. @deffnx {C Function} scm_redirect_port (old_port, new_port)
  335. This procedure takes two ports and duplicates the underlying file
  336. descriptor from @var{old_port} into @var{new_port}. The
  337. current file descriptor in @var{new_port} will be closed.
  338. After the redirection the two ports will share a file position
  339. and file status flags.
  340. The return value is unspecified.
  341. Unexpected behaviour can result if both ports are subsequently used
  342. and the original and/or duplicate ports are buffered.
  343. This procedure does not have any side effects on other ports or
  344. revealed counts.
  345. @end deffn
  346. @deffn {Scheme Procedure} dup2 oldfd newfd
  347. @deffnx {C Function} scm_dup2 (oldfd, newfd)
  348. A simple wrapper for the @code{dup2} system call.
  349. Copies the file descriptor @var{oldfd} to descriptor
  350. number @var{newfd}, replacing the previous meaning
  351. of @var{newfd}. Both @var{oldfd} and @var{newfd} must
  352. be integers.
  353. Unlike for @code{dup->fdes} or @code{primitive-move->fdes}, no attempt
  354. is made to move away ports which are using @var{newfd}.
  355. The return value is unspecified.
  356. @end deffn
  357. @deffn {Scheme Procedure} port-for-each proc
  358. @deffnx {C Function} scm_port_for_each (SCM proc)
  359. @deffnx {C Function} scm_c_port_for_each (void (*proc)(void *, SCM), void *data)
  360. Apply @var{proc} to each port in the Guile port table
  361. (FIXME: what is the Guile port table?)
  362. in turn. The return value is unspecified. More specifically,
  363. @var{proc} is applied exactly once to every port that exists in the
  364. system at the time @code{port-for-each} is invoked. Changes to the
  365. port table while @code{port-for-each} is running have no effect as far
  366. as @code{port-for-each} is concerned.
  367. The C function @code{scm_port_for_each} takes a Scheme procedure
  368. encoded as a @code{SCM} value, while @code{scm_c_port_for_each} takes
  369. a pointer to a C function and passes along a arbitrary @var{data}
  370. cookie.
  371. @end deffn
  372. @deffn {Scheme Procedure} fcntl port/fd cmd [value]
  373. @deffnx {C Function} scm_fcntl (object, cmd, value)
  374. Apply @var{cmd} on @var{port/fd}, either a port or file descriptor.
  375. The @var{value} argument is used by the @code{SET} commands described
  376. below, it's an integer value.
  377. Values for @var{cmd} are:
  378. @defvar F_DUPFD
  379. Duplicate the file descriptor, the same as @code{dup->fdes} above
  380. does.
  381. @end defvar
  382. @defvar F_GETFD
  383. @defvarx F_SETFD
  384. Get or set flags associated with the file descriptor. The only flag
  385. is the following,
  386. @defvar FD_CLOEXEC
  387. ``Close on exec'', meaning the file descriptor will be closed on an
  388. @code{exec} call (a successful such call). For example to set that
  389. flag,
  390. @example
  391. (fcntl port F_SETFD FD_CLOEXEC)
  392. @end example
  393. Or better, set it but leave any other possible future flags unchanged,
  394. @example
  395. (fcntl port F_SETFD (logior FD_CLOEXEC
  396. (fcntl port F_GETFD)))
  397. @end example
  398. @end defvar
  399. @end defvar
  400. @defvar F_GETFL
  401. @defvarx F_SETFL
  402. Get or set flags associated with the open file. These flags are
  403. @code{O_RDONLY} etc described under @code{open} above.
  404. A common use is to set @code{O_NONBLOCK} on a network socket. The
  405. following sets that flag, and leaves other flags unchanged.
  406. @example
  407. (fcntl sock F_SETFL (logior O_NONBLOCK
  408. (fcntl sock F_GETFL)))
  409. @end example
  410. @end defvar
  411. @defvar F_GETOWN
  412. @defvarx F_SETOWN
  413. Get or set the process ID of a socket's owner, for @code{SIGIO} signals.
  414. @end defvar
  415. @end deffn
  416. @deffn {Scheme Procedure} flock file operation
  417. @deffnx {C Function} scm_flock (file, operation)
  418. @cindex file locking
  419. Apply or remove an advisory lock on an open file.
  420. @var{operation} specifies the action to be done:
  421. @defvar LOCK_SH
  422. Shared lock. More than one process may hold a shared lock
  423. for a given file at a given time.
  424. @end defvar
  425. @defvar LOCK_EX
  426. Exclusive lock. Only one process may hold an exclusive lock
  427. for a given file at a given time.
  428. @end defvar
  429. @defvar LOCK_UN
  430. Unlock the file.
  431. @end defvar
  432. @defvar LOCK_NB
  433. Don't block when locking. This is combined with one of the other
  434. operations using @code{logior} (@pxref{Bitwise Operations}). If
  435. @code{flock} would block an @code{EWOULDBLOCK} error is thrown
  436. (@pxref{Conventions}).
  437. @end defvar
  438. The return value is not specified. @var{file} may be an open
  439. file descriptor or an open file descriptor port.
  440. Note that @code{flock} does not lock files across NFS.
  441. @end deffn
  442. @deffn {Scheme Procedure} select reads writes excepts [secs [usecs]]
  443. @deffnx {C Function} scm_select (reads, writes, excepts, secs, usecs)
  444. This procedure has a variety of uses: waiting for the ability
  445. to provide input, accept output, or the existence of
  446. exceptional conditions on a collection of ports or file
  447. descriptors, or waiting for a timeout to occur.
  448. When an error occurs, this procedure throws a @code{system-error}
  449. exception (@pxref{Conventions, @code{system-error}}). Note that
  450. @code{select} may return early for other reasons, for example due to
  451. pending interrupts. @xref{Asyncs}, for more on interrupts.
  452. @var{reads}, @var{writes} and @var{excepts} can be lists or
  453. vectors, with each member a port or a file descriptor.
  454. The value returned is a list of three corresponding
  455. lists or vectors containing only the members which meet the
  456. specified requirement. The ability of port buffers to
  457. provide input or accept output is taken into account.
  458. Ordering of the input lists or vectors is not preserved.
  459. The optional arguments @var{secs} and @var{usecs} specify the
  460. timeout. Either @var{secs} can be specified alone, as
  461. either an integer or a real number, or both @var{secs} and
  462. @var{usecs} can be specified as integers, in which case
  463. @var{usecs} is an additional timeout expressed in
  464. microseconds. If @var{secs} is omitted or is @code{#f} then
  465. select will wait for as long as it takes for one of the other
  466. conditions to be satisfied.
  467. The scsh version of @code{select} differs as follows:
  468. Only vectors are accepted for the first three arguments.
  469. The @var{usecs} argument is not supported.
  470. Multiple values are returned instead of a list.
  471. Duplicates in the input vectors appear only once in output.
  472. An additional @code{select!} interface is provided.
  473. @end deffn
  474. While it is sometimes necessary to operate at the level of file
  475. descriptors, this is an operation whose correctness can only be
  476. considered as part of a whole program. So for example while the effects
  477. of @code{(string-set! x 34 #\y)} are limited to the bits of code that
  478. can access @var{x}, @code{(close-fdes 34)} mutates the state of the
  479. entire process. In particular if another thread is using file
  480. descriptor 34 then their state might be corrupted; and another thread
  481. which opens a file might cause file descriptor 34 to be re-used, so that
  482. corruption could manifest itself in a strange way.
  483. @cindex fdes finalizers
  484. @cindex file descriptor finalizers
  485. @cindex finalizers, file descriptor
  486. However when working with file descriptors, it's common to want to
  487. associate information with the file descriptor, perhaps in a side table.
  488. To support this use case and to allow user code to remove an association
  489. when a file descriptor is closed, Guile offers @dfn{fdes finalizers}.
  490. As the name indicates, fdes finalizers are finalizers -- they can run in
  491. response to garbage collection, and they can also run in response to
  492. explicit calls to @code{close-port}, @code{close-fdes}, or the like. As
  493. such they inherit many of the pitfalls of finalizers: they may be
  494. invoked from concurrent threads, or not at all. @xref{Foreign Object
  495. Memory Management}, for more on finalizers.
  496. To use fdes finalizers, import their module;
  497. @example
  498. (use-modules (ice-9 fdes-finalizers))
  499. @end example
  500. @deffn {Scheme Procedure} add-fdes-finalizer! fdes finalizer
  501. @deffnx {Scheme Procedure} remove-fdes-finalizer! fdes finalizer
  502. Add or remove a finalizer for @var{fdes}. A finalizer is a procedure
  503. that is called by Guile when a file descriptor is closed. The file
  504. descriptor being closed is passed as the one argument to the finalizer.
  505. If a finalizer has been added multiple times to a file descriptor, to
  506. remove it would require that number of calls to
  507. @code{remove-fdes-finalizer!}.
  508. The finalizers added to a file descriptor are called by Guile in an
  509. unspecified order, and their return values are ignored.
  510. @end deffn
  511. @node File System
  512. @subsection File System
  513. @cindex file system
  514. These procedures allow querying and setting file system attributes
  515. (such as owner,
  516. permissions, sizes and types of files); deleting, copying, renaming and
  517. linking files; creating and removing directories and querying their
  518. contents; syncing the file system and creating special files.
  519. @deffn {Scheme Procedure} access? path how
  520. @deffnx {C Function} scm_access (path, how)
  521. Test accessibility of a file under the real UID and GID of the calling
  522. process. The return is @code{#t} if @var{path} exists and the
  523. permissions requested by @var{how} are all allowed, or @code{#f} if
  524. not.
  525. @var{how} is an integer which is one of the following values, or a
  526. bitwise-OR (@code{logior}) of multiple values.
  527. @defvar R_OK
  528. Test for read permission.
  529. @end defvar
  530. @defvar W_OK
  531. Test for write permission.
  532. @end defvar
  533. @defvar X_OK
  534. Test for execute permission.
  535. @end defvar
  536. @defvar F_OK
  537. Test for existence of the file. This is implied by each of the other
  538. tests, so there's no need to combine it with them.
  539. @end defvar
  540. It's important to note that @code{access?} does not simply indicate
  541. what will happen on attempting to read or write a file. In normal
  542. circumstances it does, but in a set-UID or set-GID program it doesn't
  543. because @code{access?} tests the real ID, whereas an open or execute
  544. attempt uses the effective ID.
  545. A program which will never run set-UID/GID can ignore the difference
  546. between real and effective IDs, but for maximum generality, especially
  547. in library functions, it's best not to use @code{access?} to predict
  548. the result of an open or execute, instead simply attempt that and
  549. catch any exception.
  550. The main use for @code{access?} is to let a set-UID/GID program
  551. determine what the invoking user would have been allowed to do,
  552. without the greater (or perhaps lesser) privileges afforded by the
  553. effective ID. For more on this, see @ref{Testing File Access,,, libc,
  554. The GNU C Library Reference Manual}.
  555. @end deffn
  556. @findex fstat
  557. @deffn {Scheme Procedure} stat object [exception-on-error?]
  558. @deffnx {C Function} scm_stat (object, exception_on_error)
  559. Return an object containing various information about the file
  560. determined by @var{object}. @var{object} can be a string containing
  561. a file name or a port or integer file descriptor which is open
  562. on a file (in which case @code{fstat} is used as the underlying
  563. system call).
  564. If the optional @var{exception_on_error} argument is true, which
  565. is the default, an exception will be raised if the underlying
  566. system call returns an error, for example if the file is not
  567. found or is not readable. Otherwise, an error will cause
  568. @code{stat} to return @code{#f}.
  569. The object returned by @code{stat} can be passed as a single
  570. parameter to the following procedures, all of which return
  571. integers:
  572. @deffn {Scheme Procedure} stat:dev st
  573. The device number containing the file.
  574. @end deffn
  575. @deffn {Scheme Procedure} stat:ino st
  576. The file serial number, which distinguishes this file from all
  577. other files on the same device.
  578. @end deffn
  579. @deffn {Scheme Procedure} stat:mode st
  580. The mode of the file. This is an integer which incorporates file type
  581. information and file permission bits. See also @code{stat:type} and
  582. @code{stat:perms} below.
  583. @end deffn
  584. @deffn {Scheme Procedure} stat:nlink st
  585. The number of hard links to the file.
  586. @end deffn
  587. @deffn {Scheme Procedure} stat:uid st
  588. The user ID of the file's owner.
  589. @end deffn
  590. @deffn {Scheme Procedure} stat:gid st
  591. The group ID of the file.
  592. @end deffn
  593. @deffn {Scheme Procedure} stat:rdev st
  594. Device ID; this entry is defined only for character or block special
  595. files. On some systems this field is not available at all, in which
  596. case @code{stat:rdev} returns @code{#f}.
  597. @end deffn
  598. @deffn {Scheme Procedure} stat:size st
  599. The size of a regular file in bytes.
  600. @end deffn
  601. @deffn {Scheme Procedure} stat:atime st
  602. The last access time for the file, in seconds.
  603. @end deffn
  604. @deffn {Scheme Procedure} stat:mtime st
  605. The last modification time for the file, in seconds.
  606. @end deffn
  607. @deffn {Scheme Procedure} stat:ctime st
  608. The last modification time for the attributes of the file, in seconds.
  609. @end deffn
  610. @deffn {Scheme Procedure} stat:atimensec st
  611. @deffnx {Scheme Procedure} stat:mtimensec st
  612. @deffnx {Scheme Procedure} stat:ctimensec st
  613. The fractional part of a file's access, modification, or attribute modification
  614. time, in nanoseconds. Nanosecond timestamps are only available on some operating
  615. systems and file systems. If Guile cannot retrieve nanosecond-level timestamps
  616. for a file, these fields will be set to 0.
  617. @end deffn
  618. @deffn {Scheme Procedure} stat:blksize st
  619. The optimal block size for reading or writing the file, in bytes. On
  620. some systems this field is not available, in which case
  621. @code{stat:blksize} returns a sensible suggested block size.
  622. @end deffn
  623. @deffn {Scheme Procedure} stat:blocks st
  624. The amount of disk space that the file occupies measured in units of
  625. 512 byte blocks. On some systems this field is not available, in
  626. which case @code{stat:blocks} returns @code{#f}.
  627. @end deffn
  628. In addition, the following procedures return the information
  629. from @code{stat:mode} in a more convenient form:
  630. @deffn {Scheme Procedure} stat:type st
  631. A symbol representing the type of file. Possible values are
  632. @samp{regular}, @samp{directory}, @samp{symlink},
  633. @samp{block-special}, @samp{char-special}, @samp{fifo}, @samp{socket},
  634. and @samp{unknown}.
  635. @end deffn
  636. @deffn {Scheme Procedure} stat:perms st
  637. An integer representing the access permission bits.
  638. @end deffn
  639. @end deffn
  640. @deffn {Scheme Procedure} lstat path
  641. @deffnx {C Function} scm_lstat (path)
  642. Similar to @code{stat}, but does not follow symbolic links, i.e.,
  643. it will return information about a symbolic link itself, not the
  644. file it points to. @var{path} must be a string.
  645. @end deffn
  646. @deffn {Scheme Procedure} readlink path
  647. @deffnx {C Function} scm_readlink (path)
  648. Return the value of the symbolic link named by @var{path} (a
  649. string), i.e., the file that the link points to.
  650. @end deffn
  651. @findex fchown
  652. @findex lchown
  653. @deffn {Scheme Procedure} chown object owner group
  654. @deffnx {C Function} scm_chown (object, owner, group)
  655. Change the ownership and group of the file referred to by @var{object}
  656. to the integer values @var{owner} and @var{group}. @var{object} can
  657. be a string containing a file name or, if the platform supports
  658. @code{fchown} (@pxref{File Owner,,,libc,The GNU C Library Reference
  659. Manual}), a port or integer file descriptor which is open on the file.
  660. The return value is unspecified.
  661. If @var{object} is a symbolic link, either the
  662. ownership of the link or the ownership of the referenced file will be
  663. changed depending on the operating system (lchown is
  664. unsupported at present). If @var{owner} or @var{group} is specified
  665. as @code{-1}, then that ID is not changed.
  666. @end deffn
  667. @findex fchmod
  668. @deffn {Scheme Procedure} chmod object mode
  669. @deffnx {C Function} scm_chmod (object, mode)
  670. Changes the permissions of the file referred to by @var{object}.
  671. @var{object} can be a string containing a file name or a port or integer file
  672. descriptor which is open on a file (in which case @code{fchmod} is used
  673. as the underlying system call).
  674. @var{mode} specifies
  675. the new permissions as a decimal number, e.g., @code{(chmod "foo" #o755)}.
  676. The return value is unspecified.
  677. @end deffn
  678. @deffn {Scheme Procedure} utime pathname [actime [modtime [actimens [modtimens [flags]]]]]
  679. @deffnx {C Function} scm_utime (pathname, actime, modtime, actimens, modtimens, flags)
  680. @code{utime} sets the access and modification times for the
  681. file named by @var{pathname}. If @var{actime} or @var{modtime} is
  682. not supplied, then the current time is used. @var{actime} and
  683. @var{modtime} must be integer time values as returned by the
  684. @code{current-time} procedure.
  685. The optional @var{actimens} and @var{modtimens} are nanoseconds
  686. to add @var{actime} and @var{modtime}. Nanosecond precision is
  687. only supported on some combinations of file systems and operating
  688. systems.
  689. @lisp
  690. (utime "foo" (- (current-time) 3600))
  691. @end lisp
  692. will set the access time to one hour in the past and the
  693. modification time to the current time.
  694. @vindex AT_SYMLINK_NOFOLLOW
  695. Last, @var{flags} may be either @code{0} or the
  696. @code{AT_SYMLINK_NOFOLLOW} constant, to set the time of
  697. @var{pathname} even if it is a symbolic link.
  698. @end deffn
  699. @findex unlink
  700. @deffn {Scheme Procedure} delete-file str
  701. @deffnx {C Function} scm_delete_file (str)
  702. Deletes (or ``unlinks'') the file whose path is specified by
  703. @var{str}.
  704. @end deffn
  705. @deffn {Scheme Procedure} copy-file oldfile newfile
  706. @deffnx {C Function} scm_copy_file (oldfile, newfile)
  707. Copy the file specified by @var{oldfile} to @var{newfile}.
  708. The return value is unspecified.
  709. @end deffn
  710. @deffn {Scheme Procedure} sendfile out in count [offset]
  711. @deffnx {C Function} scm_sendfile (out, in, count, offset)
  712. Send @var{count} bytes from @var{in} to @var{out}, both of which
  713. must be either open file ports or file descriptors. When
  714. @var{offset} is omitted, start reading from @var{in}'s current
  715. position; otherwise, start reading at @var{offset}. Return
  716. the number of bytes actually sent.
  717. When @var{in} is a port, it is often preferable to specify @var{offset},
  718. because @var{in}'s offset as a port may be different from the offset of
  719. its underlying file descriptor.
  720. On systems that support it, such as GNU/Linux, this procedure uses the
  721. @code{sendfile} libc function, which usually corresponds to a system
  722. call. This is faster than doing a series of @code{read} and
  723. @code{write} system calls. A typical application is to send a file over
  724. a socket.
  725. In some cases, the @code{sendfile} libc function may return
  726. @code{EINVAL} or @code{ENOSYS}. In that case, Guile's @code{sendfile}
  727. procedure automatically falls back to doing a series of @code{read} and
  728. @code{write} calls.
  729. In other cases, the libc function may send fewer bytes than
  730. @var{count}---for instance because @var{out} is a slow or limited
  731. device, such as a pipe. When that happens, Guile's @code{sendfile}
  732. automatically retries until exactly @var{count} bytes were sent or an
  733. error occurs.
  734. @end deffn
  735. @findex rename
  736. @deffn {Scheme Procedure} rename-file oldname newname
  737. @deffnx {C Function} scm_rename (oldname, newname)
  738. Renames the file specified by @var{oldname} to @var{newname}.
  739. The return value is unspecified.
  740. @end deffn
  741. @deffn {Scheme Procedure} link oldpath newpath
  742. @deffnx {C Function} scm_link (oldpath, newpath)
  743. Creates a new name @var{newpath} in the file system for the
  744. file named by @var{oldpath}. If @var{oldpath} is a symbolic
  745. link, the link may or may not be followed depending on the
  746. system.
  747. @end deffn
  748. @deffn {Scheme Procedure} symlink oldpath newpath
  749. @deffnx {C Function} scm_symlink (oldpath, newpath)
  750. Create a symbolic link named @var{newpath} with the value (i.e., pointing to)
  751. @var{oldpath}. The return value is unspecified.
  752. @end deffn
  753. @deffn {Scheme Procedure} mkdir path [mode]
  754. @deffnx {C Function} scm_mkdir (path, mode)
  755. Create a new directory named by @var{path}. If @var{mode} is omitted
  756. then the permissions of the directory are set to @code{#o777}
  757. masked with the current umask (@pxref{Processes, @code{umask}}).
  758. Otherwise they are set to the value specified with @var{mode}.
  759. The return value is unspecified.
  760. @end deffn
  761. @deffn {Scheme Procedure} rmdir path
  762. @deffnx {C Function} scm_rmdir (path)
  763. Remove the existing directory named by @var{path}. The directory must
  764. be empty for this to succeed. The return value is unspecified.
  765. @end deffn
  766. @deffn {Scheme Procedure} opendir dirname
  767. @deffnx {C Function} scm_opendir (dirname)
  768. @cindex directory contents
  769. Open the directory specified by @var{dirname} and return a directory
  770. stream.
  771. Before using this and the procedures below, make sure to see the
  772. higher-level procedures for directory traversal that are available
  773. (@pxref{File Tree Walk}).
  774. @end deffn
  775. @deffn {Scheme Procedure} directory-stream? object
  776. @deffnx {C Function} scm_directory_stream_p (object)
  777. Return a boolean indicating whether @var{object} is a directory
  778. stream as returned by @code{opendir}.
  779. @end deffn
  780. @deffn {Scheme Procedure} readdir stream
  781. @deffnx {C Function} scm_readdir (stream)
  782. Return (as a string) the next directory entry from the directory stream
  783. @var{stream}. If there is no remaining entry to be read then the
  784. end of file object is returned.
  785. @end deffn
  786. @deffn {Scheme Procedure} rewinddir stream
  787. @deffnx {C Function} scm_rewinddir (stream)
  788. Reset the directory port @var{stream} so that the next call to
  789. @code{readdir} will return the first directory entry.
  790. @end deffn
  791. @deffn {Scheme Procedure} closedir stream
  792. @deffnx {C Function} scm_closedir (stream)
  793. Close the directory stream @var{stream}.
  794. The return value is unspecified.
  795. @end deffn
  796. Here is an example showing how to display all the entries in a
  797. directory:
  798. @lisp
  799. (define dir (opendir "/usr/lib"))
  800. (do ((entry (readdir dir) (readdir dir)))
  801. ((eof-object? entry))
  802. (display entry)(newline))
  803. (closedir dir)
  804. @end lisp
  805. @deffn {Scheme Procedure} sync
  806. @deffnx {C Function} scm_sync ()
  807. Flush the operating system disk buffers.
  808. The return value is unspecified.
  809. @end deffn
  810. @deffn {Scheme Procedure} mknod path type perms dev
  811. @deffnx {C Function} scm_mknod (path, type, perms, dev)
  812. @cindex device file
  813. Creates a new special file, such as a file corresponding to a device.
  814. @var{path} specifies the name of the file. @var{type} should be one
  815. of the following symbols: @samp{regular}, @samp{directory},
  816. @samp{symlink}, @samp{block-special}, @samp{char-special},
  817. @samp{fifo}, or @samp{socket}. @var{perms} (an integer) specifies the
  818. file permissions. @var{dev} (an integer) specifies which device the
  819. special file refers to. Its exact interpretation depends on the kind
  820. of special file being created.
  821. E.g.,
  822. @lisp
  823. (mknod "/dev/fd0" 'block-special #o660 (+ (* 2 256) 2))
  824. @end lisp
  825. The return value is unspecified.
  826. @end deffn
  827. @deffn {Scheme Procedure} tmpnam
  828. @deffnx {C Function} scm_tmpnam ()
  829. @cindex temporary file
  830. Return an auto-generated name of a temporary file, a file which
  831. doesn't already exist. The name includes a path, it's usually in
  832. @file{/tmp} but that's system dependent.
  833. Care must be taken when using @code{tmpnam}. In between choosing the
  834. name and creating the file another program might use that name, or an
  835. attacker might even make it a symlink pointing at something important
  836. and causing you to overwrite that.
  837. The safe way is to create the file using @code{open} with
  838. @code{O_EXCL} to avoid any overwriting. A loop can try again with
  839. another name if the file exists (error @code{EEXIST}).
  840. @code{mkstemp} below does that.
  841. @end deffn
  842. @deffn {Scheme Procedure} mkstemp tmpl [mode]
  843. @cindex temporary file
  844. Create a new unique file in the file system and return a new buffered
  845. port open for reading and writing to the file.
  846. @var{tmpl} is a string specifying where the file should be created: it
  847. must end with @samp{XXXXXX}. The name of the newly created file will be
  848. the same as @var{tmpl}, but with those @samp{X}s changed, and can be
  849. determined by calling @code{port-filename} on the returned port.
  850. Note that the newly created file is not deleted automatically by Guile;
  851. probably the caller should arrange to call @code{delete-file} when the
  852. file is no longer needed.
  853. POSIX doesn't specify the permissions mode of the file. On GNU and most
  854. systems it's @code{#o600}; an application can use @code{chmod} to relax
  855. that if desired. For example @code{#o666} less @code{umask}, which is
  856. usual for ordinary file creation,
  857. @example
  858. (let ((port (mkstemp "/tmp/myfile-XXXXXX")))
  859. (chmod port (logand #o666 (lognot (umask))))
  860. ...)
  861. @end example
  862. The optional @var{mode} argument specifies a mode with which to open the
  863. new file, as a string in the same format that @code{open-file} takes.
  864. It defaults to @code{"w+"}.
  865. @end deffn
  866. @deffn {Scheme Procedure} tmpfile
  867. @deffnx {C Function} scm_tmpfile ()
  868. Return an input/output port to a unique temporary file
  869. named using the path prefix @code{P_tmpdir} defined in
  870. @file{stdio.h}.
  871. The file is automatically deleted when the port is closed
  872. or the program terminates.
  873. @end deffn
  874. @deffn {Scheme Procedure} mkdtemp tmpl
  875. @deffnx {C Function} scm_mkdtemp (tmpl)
  876. @cindex temporary directory
  877. Create a new directory named in accordance with the template string
  878. @var{tmpl}.
  879. @var{tmpl} is a string specifying the directory's name. The last six
  880. characters of @var{tmpl} must be @samp{XXXXXX}. Upon successful
  881. execution, the name of the new directory is returned which has the same
  882. form as @var{tmpl} but with the @samp{XXXXXX} characters modified to
  883. ensure the directory name is unique.
  884. The permissions of the directory created are OS dependent, but, are
  885. usually @code{#o700}.
  886. An error may be thrown if the template has the wrong format or if the
  887. directory cannot be created.
  888. @end deffn
  889. @deffn {Scheme Procedure} dirname filename
  890. @deffnx {C Function} scm_dirname (filename)
  891. Return the directory name component of the file name
  892. @var{filename}. If @var{filename} does not contain a directory
  893. component, @code{.} is returned.
  894. @end deffn
  895. @deffn {Scheme Procedure} basename filename [suffix]
  896. @deffnx {C Function} scm_basename (filename, suffix)
  897. Return the base name of the file name @var{filename}. The
  898. base name is the file name without any directory components.
  899. If @var{suffix} is provided, and is equal to the end of
  900. @var{basename}, it is removed also.
  901. @lisp
  902. (basename "/tmp/test.xml" ".xml")
  903. @result{} "test"
  904. @end lisp
  905. @end deffn
  906. @deffn {Scheme Procedure} canonicalize-path path
  907. @deffnx {C Function} scm_canonicalize_path (path)
  908. Return the canonical (absolute) path of @var{path}.
  909. A canonical path has no @code{.} or @code{..} components,
  910. nor any repeated path separators (@code{/}) nor symlinks.
  911. Raises an error if any component of @var{path} does not
  912. exist.
  913. @lisp
  914. (canonicalize-path "test.xml")
  915. @result{} "/tmp/test.xml"
  916. @end lisp
  917. @end deffn
  918. @deffn {Scheme Procedure} file-exists? filename
  919. Return @code{#t} if the file named @var{filename} exists, @code{#f} if
  920. not.
  921. @end deffn
  922. @cindex file name separator
  923. @cindex absolute file name
  924. Many operating systems, such as GNU, use @code{/} (forward slash) to
  925. separate the components of a file name; any file name starting with
  926. @code{/} is considered an @dfn{absolute file name}. These conventions
  927. are specified by the POSIX Base Definitions, which refer to conforming
  928. file names as ``pathnames''. Some operating systems use a different
  929. convention; in particular, Windows uses @code{\} (backslash) as the file
  930. name separator, and also has the notion of @dfn{volume names} like
  931. @code{C:\} for absolute file names. The following procedures and
  932. variables provide support for portable file name manipulations.
  933. @deffn {Scheme Procedure} system-file-name-convention
  934. Return either @code{posix} or @code{windows}, depending on
  935. what kind of system this Guile is running on.
  936. @end deffn
  937. @deffn {Scheme Procedure} file-name-separator? c
  938. Return true if character @var{c} is a file name separator on the host
  939. platform.
  940. @end deffn
  941. @deffn {Scheme Procedure} absolute-file-name? file-name
  942. Return true if @var{file-name} denotes an absolute file name on the host
  943. platform.
  944. @end deffn
  945. @defvr {Scheme Variable} file-name-separator-string
  946. The preferred file name separator.
  947. Note that on MinGW builds for Windows, both @code{/} and @code{\} are
  948. valid separators. Thus, programs should not assume that
  949. @code{file-name-separator-string} is the @emph{only} file name
  950. separator---e.g., when extracting the components of a file name.
  951. @end defvr
  952. @node User Information
  953. @subsection User Information
  954. @cindex user information
  955. @cindex password file
  956. @cindex group file
  957. The facilities in this section provide an interface to the user and
  958. group database.
  959. They should be used with care since they are not reentrant.
  960. The following functions accept an object representing user information
  961. and return a selected component:
  962. @deffn {Scheme Procedure} passwd:name pw
  963. The name of the userid.
  964. @end deffn
  965. @deffn {Scheme Procedure} passwd:passwd pw
  966. The encrypted passwd.
  967. @end deffn
  968. @deffn {Scheme Procedure} passwd:uid pw
  969. The user id number.
  970. @end deffn
  971. @deffn {Scheme Procedure} passwd:gid pw
  972. The group id number.
  973. @end deffn
  974. @deffn {Scheme Procedure} passwd:gecos pw
  975. The full name.
  976. @end deffn
  977. @deffn {Scheme Procedure} passwd:dir pw
  978. The home directory.
  979. @end deffn
  980. @deffn {Scheme Procedure} passwd:shell pw
  981. The login shell.
  982. @end deffn
  983. @sp 1
  984. @deffn {Scheme Procedure} getpwuid uid
  985. Look up an integer userid in the user database.
  986. @end deffn
  987. @deffn {Scheme Procedure} getpwnam name
  988. Look up a user name string in the user database.
  989. @end deffn
  990. @deffn {Scheme Procedure} setpwent
  991. Initializes a stream used by @code{getpwent} to read from the user database.
  992. The next use of @code{getpwent} will return the first entry. The
  993. return value is unspecified.
  994. @end deffn
  995. @deffn {Scheme Procedure} getpwent
  996. Read the next entry in the user database stream. The return is a
  997. passwd user object as above, or @code{#f} when no more entries.
  998. @end deffn
  999. @deffn {Scheme Procedure} endpwent
  1000. Closes the stream used by @code{getpwent}. The return value is unspecified.
  1001. @end deffn
  1002. @deffn {Scheme Procedure} setpw [arg]
  1003. @deffnx {C Function} scm_setpwent (arg)
  1004. If called with a true argument, initialize or reset the password data
  1005. stream. Otherwise, close the stream. The @code{setpwent} and
  1006. @code{endpwent} procedures are implemented on top of this.
  1007. @end deffn
  1008. @deffn {Scheme Procedure} getpw [user]
  1009. @deffnx {C Function} scm_getpwuid (user)
  1010. Look up an entry in the user database. @var{user} can be an integer,
  1011. a string, or omitted, giving the behaviour of getpwuid, getpwnam
  1012. or getpwent respectively.
  1013. @end deffn
  1014. The following functions accept an object representing group information
  1015. and return a selected component:
  1016. @deffn {Scheme Procedure} group:name gr
  1017. The group name.
  1018. @end deffn
  1019. @deffn {Scheme Procedure} group:passwd gr
  1020. The encrypted group password.
  1021. @end deffn
  1022. @deffn {Scheme Procedure} group:gid gr
  1023. The group id number.
  1024. @end deffn
  1025. @deffn {Scheme Procedure} group:mem gr
  1026. A list of userids which have this group as a supplementary group.
  1027. @end deffn
  1028. @sp 1
  1029. @deffn {Scheme Procedure} getgrgid gid
  1030. Look up an integer group id in the group database.
  1031. @end deffn
  1032. @deffn {Scheme Procedure} getgrnam name
  1033. Look up a group name in the group database.
  1034. @end deffn
  1035. @deffn {Scheme Procedure} setgrent
  1036. Initializes a stream used by @code{getgrent} to read from the group database.
  1037. The next use of @code{getgrent} will return the first entry.
  1038. The return value is unspecified.
  1039. @end deffn
  1040. @deffn {Scheme Procedure} getgrent
  1041. Return the next entry in the group database, using the stream set by
  1042. @code{setgrent}.
  1043. @end deffn
  1044. @deffn {Scheme Procedure} endgrent
  1045. Closes the stream used by @code{getgrent}.
  1046. The return value is unspecified.
  1047. @end deffn
  1048. @deffn {Scheme Procedure} setgr [arg]
  1049. @deffnx {C Function} scm_setgrent (arg)
  1050. If called with a true argument, initialize or reset the group data
  1051. stream. Otherwise, close the stream. The @code{setgrent} and
  1052. @code{endgrent} procedures are implemented on top of this.
  1053. @end deffn
  1054. @deffn {Scheme Procedure} getgr [group]
  1055. @deffnx {C Function} scm_getgrgid (group)
  1056. Look up an entry in the group database. @var{group} can be an integer,
  1057. a string, or omitted, giving the behaviour of getgrgid, getgrnam
  1058. or getgrent respectively.
  1059. @end deffn
  1060. In addition to the accessor procedures for the user database, the
  1061. following shortcut procedure is also available.
  1062. @deffn {Scheme Procedure} getlogin
  1063. @deffnx {C Function} scm_getlogin ()
  1064. Return a string containing the name of the user logged in on
  1065. the controlling terminal of the process, or @code{#f} if this
  1066. information cannot be obtained.
  1067. @end deffn
  1068. @node Time
  1069. @subsection Time
  1070. @cindex time
  1071. @deffn {Scheme Procedure} current-time
  1072. @deffnx {C Function} scm_current_time ()
  1073. Return the number of seconds since 1970-01-01 00:00:00 @acronym{UTC},
  1074. excluding leap seconds.
  1075. @end deffn
  1076. @deffn {Scheme Procedure} gettimeofday
  1077. @deffnx {C Function} scm_gettimeofday ()
  1078. Return a pair containing the number of seconds and microseconds
  1079. since 1970-01-01 00:00:00 @acronym{UTC}, excluding leap seconds. Note:
  1080. whether true microsecond resolution is available depends on the
  1081. operating system.
  1082. @end deffn
  1083. The following procedures either accept an object representing a broken down
  1084. time and return a selected component, or accept an object representing
  1085. a broken down time and a value and set the component to the value.
  1086. The numbers in parentheses give the usual range.
  1087. @deffn {Scheme Procedure} tm:sec tm
  1088. @deffnx {Scheme Procedure} set-tm:sec tm val
  1089. Seconds (0-59).
  1090. @end deffn
  1091. @deffn {Scheme Procedure} tm:min tm
  1092. @deffnx {Scheme Procedure} set-tm:min tm val
  1093. Minutes (0-59).
  1094. @end deffn
  1095. @deffn {Scheme Procedure} tm:hour tm
  1096. @deffnx {Scheme Procedure} set-tm:hour tm val
  1097. Hours (0-23).
  1098. @end deffn
  1099. @deffn {Scheme Procedure} tm:mday tm
  1100. @deffnx {Scheme Procedure} set-tm:mday tm val
  1101. Day of the month (1-31).
  1102. @end deffn
  1103. @deffn {Scheme Procedure} tm:mon tm
  1104. @deffnx {Scheme Procedure} set-tm:mon tm val
  1105. Month (0-11).
  1106. @end deffn
  1107. @deffn {Scheme Procedure} tm:year tm
  1108. @deffnx {Scheme Procedure} set-tm:year tm val
  1109. Year (70-), the year minus 1900.
  1110. @end deffn
  1111. @deffn {Scheme Procedure} tm:wday tm
  1112. @deffnx {Scheme Procedure} set-tm:wday tm val
  1113. Day of the week (0-6) with Sunday represented as 0.
  1114. @end deffn
  1115. @deffn {Scheme Procedure} tm:yday tm
  1116. @deffnx {Scheme Procedure} set-tm:yday tm val
  1117. Day of the year (0-364, 365 in leap years).
  1118. @end deffn
  1119. @deffn {Scheme Procedure} tm:isdst tm
  1120. @deffnx {Scheme Procedure} set-tm:isdst tm val
  1121. Daylight saving indicator (0 for ``no'', greater than 0 for ``yes'', less than
  1122. 0 for ``unknown'').
  1123. @end deffn
  1124. @deffn {Scheme Procedure} tm:gmtoff tm
  1125. @deffnx {Scheme Procedure} set-tm:gmtoff tm val
  1126. Time zone offset in seconds west of @acronym{UTC} (-46800 to 43200).
  1127. For example on East coast USA (zone @samp{EST+5}) this would be 18000
  1128. (ie.@: @m{5\times60\times60,5*60*60}) in winter, or 14400
  1129. (ie.@: @m{4\times60\times60,4*60*60}) during daylight savings.
  1130. Note @code{tm:gmtoff} is not the same as @code{tm_gmtoff} in the C
  1131. @code{tm} structure. @code{tm_gmtoff} is seconds east and hence the
  1132. negative of the value here.
  1133. @end deffn
  1134. @deffn {Scheme Procedure} tm:zone tm
  1135. @deffnx {Scheme Procedure} set-tm:zone tm val
  1136. Time zone label (a string), not necessarily unique.
  1137. @end deffn
  1138. @sp 1
  1139. @deffn {Scheme Procedure} localtime time [zone]
  1140. @deffnx {C Function} scm_localtime (time, zone)
  1141. @cindex local time
  1142. Return an object representing the broken down components of
  1143. @var{time}, an integer like the one returned by
  1144. @code{current-time}. The time zone for the calculation is
  1145. optionally specified by @var{zone} (a string), otherwise the
  1146. @env{TZ} environment variable or the system default is used.
  1147. @end deffn
  1148. @deffn {Scheme Procedure} gmtime time
  1149. @deffnx {C Function} scm_gmtime (time)
  1150. Return an object representing the broken down components of
  1151. @var{time}, an integer like the one returned by
  1152. @code{current-time}. The values are calculated for @acronym{UTC}.
  1153. @end deffn
  1154. @deffn {Scheme Procedure} mktime sbd-time [zone]
  1155. @deffnx {C Function} scm_mktime (sbd_time, zone)
  1156. For a broken down time object @var{sbd-time}, return a pair the
  1157. @code{car} of which is an integer time like @code{current-time}, and
  1158. the @code{cdr} of which is a new broken down time with normalized
  1159. fields.
  1160. @var{zone} is a timezone string, or the default is the @env{TZ}
  1161. environment variable or the system default (@pxref{TZ Variable,,
  1162. Specifying the Time Zone with @env{TZ}, libc, GNU C Library Reference
  1163. Manual}). @var{sbd-time} is taken to be in that @var{zone}.
  1164. The following fields of @var{sbd-time} are used: @code{tm:year},
  1165. @code{tm:mon}, @code{tm:mday}, @code{tm:hour}, @code{tm:min},
  1166. @code{tm:sec}, @code{tm:isdst}. The values can be outside their usual
  1167. ranges. For example @code{tm:hour} normally goes up to 23, but a
  1168. value say 33 would mean 9 the following day.
  1169. @code{tm:isdst} in @var{sbd-time} says whether the time given is with
  1170. daylight savings or not. This is ignored if @var{zone} doesn't have
  1171. any daylight savings adjustment amount.
  1172. The broken down time in the return normalizes the values of
  1173. @var{sbd-time} by bringing them into their usual ranges, and using the
  1174. actual daylight savings rule for that time in @var{zone} (which may
  1175. differ from what @var{sbd-time} had). The easiest way to think of
  1176. this is that @var{sbd-time} plus @var{zone} converts to the integer
  1177. UTC time, then a @code{localtime} is applied to get the normal
  1178. presentation of that time, in @var{zone}.
  1179. @end deffn
  1180. @deffn {Scheme Procedure} tzset
  1181. @deffnx {C Function} scm_tzset ()
  1182. Initialize the timezone from the @env{TZ} environment variable
  1183. or the system default. It's not usually necessary to call this procedure
  1184. since it's done automatically by other procedures that depend on the
  1185. timezone.
  1186. @end deffn
  1187. @deffn {Scheme Procedure} strftime format tm
  1188. @deffnx {C Function} scm_strftime (format, tm)
  1189. @cindex time formatting
  1190. Return a string which is broken-down time structure @var{tm} formatted
  1191. according to the given @var{format} string.
  1192. @var{format} contains field specifications introduced by a @samp{%}
  1193. character. See @ref{Formatting Calendar Time,,, libc, The GNU C
  1194. Library Reference Manual}, or @samp{man 3 strftime}, for the available
  1195. formatting.
  1196. @lisp
  1197. (strftime "%c" (localtime (current-time)))
  1198. @result{} "Mon Mar 11 20:17:43 2002"
  1199. @end lisp
  1200. If @code{setlocale} has been called (@pxref{Locales}), month and day
  1201. names are from the current locale and in the locale character set.
  1202. @end deffn
  1203. @deffn {Scheme Procedure} strptime format string
  1204. @deffnx {C Function} scm_strptime (format, string)
  1205. @cindex time parsing
  1206. Performs the reverse action to @code{strftime}, parsing
  1207. @var{string} according to the specification supplied in
  1208. @var{format}. The interpretation of month and day names is
  1209. dependent on the current locale. The value returned is a pair.
  1210. The @acronym{CAR} has an object with time components
  1211. in the form returned by @code{localtime} or @code{gmtime},
  1212. but the time zone components
  1213. are not usefully set.
  1214. The @acronym{CDR} reports the number of characters from @var{string}
  1215. which were used for the conversion.
  1216. @end deffn
  1217. @defvar internal-time-units-per-second
  1218. The value of this variable is the number of time units per second
  1219. reported by the following procedures.
  1220. @end defvar
  1221. @deffn {Scheme Procedure} times
  1222. @deffnx {C Function} scm_times ()
  1223. Return an object with information about real and processor
  1224. time. The following procedures accept such an object as an
  1225. argument and return a selected component:
  1226. @deffn {Scheme Procedure} tms:clock tms
  1227. The current real time, expressed as time units relative to an
  1228. arbitrary base.
  1229. @end deffn
  1230. @deffn {Scheme Procedure} tms:utime tms
  1231. The CPU time units used by the calling process.
  1232. @end deffn
  1233. @deffn {Scheme Procedure} tms:stime tms
  1234. The CPU time units used by the system on behalf of the calling
  1235. process.
  1236. @end deffn
  1237. @deffn {Scheme Procedure} tms:cutime tms
  1238. The CPU time units used by terminated child processes of the
  1239. calling process, whose status has been collected (e.g., using
  1240. @code{waitpid}).
  1241. @end deffn
  1242. @deffn {Scheme Procedure} tms:cstime tms
  1243. Similarly, the CPU times units used by the system on behalf of
  1244. terminated child processes.
  1245. @end deffn
  1246. @end deffn
  1247. @deffn {Scheme Procedure} get-internal-real-time
  1248. @deffnx {C Function} scm_get_internal_real_time ()
  1249. Return the number of time units since the interpreter was
  1250. started.
  1251. @end deffn
  1252. @deffn {Scheme Procedure} get-internal-run-time
  1253. @deffnx {C Function} scm_get_internal_run_time ()
  1254. Return the number of time units of processor time used by the
  1255. interpreter. Both @emph{system} and @emph{user} time are
  1256. included but subprocesses are not.
  1257. @end deffn
  1258. @node Runtime Environment
  1259. @subsection Runtime Environment
  1260. @deffn {Scheme Procedure} program-arguments
  1261. @deffnx {Scheme Procedure} command-line
  1262. @deffnx {Scheme Procedure} set-program-arguments
  1263. @deffnx {C Function} scm_program_arguments ()
  1264. @deffnx {C Function} scm_set_program_arguments_scm (lst)
  1265. @cindex command line
  1266. @cindex program arguments
  1267. Get the command line arguments passed to Guile, or set new arguments.
  1268. The arguments are a list of strings, the first of which is the invoked
  1269. program name. This is just @nicode{"guile"} (or the executable path)
  1270. when run interactively, or it's the script name when running a script
  1271. with @option{-s} (@pxref{Invoking Guile}).
  1272. @example
  1273. guile -L /my/extra/dir -s foo.scm abc def
  1274. (program-arguments) @result{} ("foo.scm" "abc" "def")
  1275. @end example
  1276. @code{set-program-arguments} allows a library module or similar to
  1277. modify the arguments, for example to strip options it recognises,
  1278. leaving the rest for the mainline.
  1279. The argument list is held in a fluid, which means it's separate for
  1280. each thread. Neither the list nor the strings within it are copied at
  1281. any point and normally should not be mutated.
  1282. The two names @code{program-arguments} and @code{command-line} are an
  1283. historical accident, they both do exactly the same thing. The name
  1284. @code{scm_set_program_arguments_scm} has an extra @code{_scm} on the
  1285. end to avoid clashing with the C function below.
  1286. @end deffn
  1287. @deftypefn {C Function} void scm_set_program_arguments (int argc, char **argv, char *first)
  1288. @cindex command line
  1289. @cindex program arguments
  1290. Set the list of command line arguments for @code{program-arguments}
  1291. and @code{command-line} above.
  1292. @var{argv} is an array of null-terminated strings, as in a C
  1293. @code{main} function. @var{argc} is the number of strings in
  1294. @var{argv}, or if it's negative then a @code{NULL} in @var{argv} marks
  1295. its end.
  1296. @var{first} is an extra string put at the start of the arguments, or
  1297. @code{NULL} for no such extra. This is a convenient way to pass the
  1298. program name after advancing @var{argv} to strip option arguments.
  1299. Eg.@:
  1300. @example
  1301. @{
  1302. char *progname = argv[0];
  1303. for (argv++; argv[0] != NULL && argv[0][0] == '-'; argv++)
  1304. @{
  1305. /* munch option ... */
  1306. @}
  1307. /* remaining args for scheme level use */
  1308. scm_set_program_arguments (-1, argv, progname);
  1309. @}
  1310. @end example
  1311. This sort of thing is often done at startup under
  1312. @code{scm_boot_guile} with options handled at the C level removed.
  1313. The given strings are all copied, so the C data is not accessed again
  1314. once @code{scm_set_program_arguments} returns.
  1315. @end deftypefn
  1316. @deffn {Scheme Procedure} getenv name
  1317. @deffnx {C Function} scm_getenv (name)
  1318. @cindex environment
  1319. Looks up the string @var{name} in the current environment. The return
  1320. value is @code{#f} unless a string of the form @code{NAME=VALUE} is
  1321. found, in which case the string @code{VALUE} is returned.
  1322. @end deffn
  1323. @deffn {Scheme Procedure} setenv name value
  1324. Modifies the environment of the current process, which is
  1325. also the default environment inherited by child processes.
  1326. If @var{value} is @code{#f}, then @var{name} is removed from the
  1327. environment. Otherwise, the string @var{name}=@var{value} is added
  1328. to the environment, replacing any existing string with name matching
  1329. @var{name}.
  1330. The return value is unspecified.
  1331. @end deffn
  1332. @deffn {Scheme Procedure} unsetenv name
  1333. Remove variable @var{name} from the environment. The
  1334. name can not contain a @samp{=} character.
  1335. @end deffn
  1336. @deffn {Scheme Procedure} environ [env]
  1337. @deffnx {C Function} scm_environ (env)
  1338. If @var{env} is omitted, return the current environment (in the
  1339. Unix sense) as a list of strings. Otherwise set the current
  1340. environment, which is also the default environment for child
  1341. processes, to the supplied list of strings. Each member of
  1342. @var{env} should be of the form @var{name}=@var{value} and values of
  1343. @var{name} should not be duplicated. If @var{env} is supplied
  1344. then the return value is unspecified.
  1345. @end deffn
  1346. @deffn {Scheme Procedure} putenv str
  1347. @deffnx {C Function} scm_putenv (str)
  1348. Modifies the environment of the current process, which is
  1349. also the default environment inherited by child processes.
  1350. If @var{str} is of the form @code{NAME=VALUE} then it will be written
  1351. directly into the environment, replacing any existing environment string
  1352. with
  1353. name matching @code{NAME}. If @var{str} does not contain an equal
  1354. sign, then any existing string with name matching @var{str} will
  1355. be removed.
  1356. The return value is unspecified.
  1357. @end deffn
  1358. @node Processes
  1359. @subsection Processes
  1360. @cindex processes
  1361. @cindex child processes
  1362. @findex cd
  1363. @deffn {Scheme Procedure} chdir str
  1364. @deffnx {C Function} scm_chdir (str)
  1365. @cindex current directory
  1366. Change the current working directory to @var{str}.
  1367. The return value is unspecified.
  1368. @end deffn
  1369. @findex pwd
  1370. @deffn {Scheme Procedure} getcwd
  1371. @deffnx {C Function} scm_getcwd ()
  1372. Return the name of the current working directory.
  1373. @end deffn
  1374. @deffn {Scheme Procedure} umask [mode]
  1375. @deffnx {C Function} scm_umask (mode)
  1376. If @var{mode} is omitted, returns a decimal number representing the
  1377. current file creation mask. Otherwise the file creation mask is set
  1378. to @var{mode} and the previous value is returned. @xref{Setting
  1379. Permissions,,Assigning File Permissions,libc,The GNU C Library
  1380. Reference Manual}, for more on how to use umasks.
  1381. E.g., @code{(umask #o022)} sets the mask to octal 22/decimal 18.
  1382. @end deffn
  1383. @deffn {Scheme Procedure} chroot path
  1384. @deffnx {C Function} scm_chroot (path)
  1385. Change the root directory to that specified in @var{path}.
  1386. This directory will be used for path names beginning with
  1387. @file{/}. The root directory is inherited by all children
  1388. of the current process. Only the superuser may change the
  1389. root directory.
  1390. @end deffn
  1391. @deffn {Scheme Procedure} getpid
  1392. @deffnx {C Function} scm_getpid ()
  1393. Return an integer representing the current process ID.
  1394. @end deffn
  1395. @deffn {Scheme Procedure} getgroups
  1396. @deffnx {C Function} scm_getgroups ()
  1397. Return a vector of integers representing the current
  1398. supplementary group IDs.
  1399. @end deffn
  1400. @deffn {Scheme Procedure} getppid
  1401. @deffnx {C Function} scm_getppid ()
  1402. Return an integer representing the process ID of the parent
  1403. process.
  1404. @end deffn
  1405. @deffn {Scheme Procedure} getuid
  1406. @deffnx {C Function} scm_getuid ()
  1407. Return an integer representing the current real user ID.
  1408. @end deffn
  1409. @deffn {Scheme Procedure} getgid
  1410. @deffnx {C Function} scm_getgid ()
  1411. Return an integer representing the current real group ID.
  1412. @end deffn
  1413. @deffn {Scheme Procedure} geteuid
  1414. @deffnx {C Function} scm_geteuid ()
  1415. Return an integer representing the current effective user ID.
  1416. If the system does not support effective IDs, then the real ID
  1417. is returned. @code{(provided? 'EIDs)} reports whether the
  1418. system supports effective IDs.
  1419. @end deffn
  1420. @deffn {Scheme Procedure} getegid
  1421. @deffnx {C Function} scm_getegid ()
  1422. Return an integer representing the current effective group ID.
  1423. If the system does not support effective IDs, then the real ID
  1424. is returned. @code{(provided? 'EIDs)} reports whether the
  1425. system supports effective IDs.
  1426. @end deffn
  1427. @deffn {Scheme Procedure} setgroups vec
  1428. @deffnx {C Function} scm_setgroups (vec)
  1429. Set the current set of supplementary group IDs to the integers in the
  1430. given vector @var{vec}. The return value is unspecified.
  1431. Generally only the superuser can set the process group IDs
  1432. (@pxref{Setting Groups, Setting the Group IDs,, libc, The GNU C
  1433. Library Reference Manual}).
  1434. @end deffn
  1435. @deffn {Scheme Procedure} setuid id
  1436. @deffnx {C Function} scm_setuid (id)
  1437. Sets both the real and effective user IDs to the integer @var{id}, provided
  1438. the process has appropriate privileges.
  1439. The return value is unspecified.
  1440. @end deffn
  1441. @deffn {Scheme Procedure} setgid id
  1442. @deffnx {C Function} scm_setgid (id)
  1443. Sets both the real and effective group IDs to the integer @var{id}, provided
  1444. the process has appropriate privileges.
  1445. The return value is unspecified.
  1446. @end deffn
  1447. @deffn {Scheme Procedure} seteuid id
  1448. @deffnx {C Function} scm_seteuid (id)
  1449. Sets the effective user ID to the integer @var{id}, provided the process
  1450. has appropriate privileges. If effective IDs are not supported, the
  1451. real ID is set instead---@code{(provided? 'EIDs)} reports whether the
  1452. system supports effective IDs.
  1453. The return value is unspecified.
  1454. @end deffn
  1455. @deffn {Scheme Procedure} setegid id
  1456. @deffnx {C Function} scm_setegid (id)
  1457. Sets the effective group ID to the integer @var{id}, provided the process
  1458. has appropriate privileges. If effective IDs are not supported, the
  1459. real ID is set instead---@code{(provided? 'EIDs)} reports whether the
  1460. system supports effective IDs.
  1461. The return value is unspecified.
  1462. @end deffn
  1463. @deffn {Scheme Procedure} getpgrp
  1464. @deffnx {C Function} scm_getpgrp ()
  1465. Return an integer representing the current process group ID.
  1466. This is the @acronym{POSIX} definition, not @acronym{BSD}.
  1467. @end deffn
  1468. @deffn {Scheme Procedure} setpgid pid pgid
  1469. @deffnx {C Function} scm_setpgid (pid, pgid)
  1470. Move the process @var{pid} into the process group @var{pgid}. @var{pid} or
  1471. @var{pgid} must be integers: they can be zero to indicate the ID of the
  1472. current process.
  1473. Fails on systems that do not support job control.
  1474. The return value is unspecified.
  1475. @end deffn
  1476. @deffn {Scheme Procedure} setsid
  1477. @deffnx {C Function} scm_setsid ()
  1478. Creates a new session. The current process becomes the session leader
  1479. and is put in a new process group. The process will be detached
  1480. from its controlling terminal if it has one.
  1481. The return value is an integer representing the new process group ID.
  1482. @end deffn
  1483. @deffn {Scheme Procedure} getsid pid
  1484. @deffnx {C Function} scm_getsid (pid)
  1485. Returns the session ID of process @var{pid}. (The session
  1486. ID of a process is the process group ID of its session leader.)
  1487. @end deffn
  1488. @deffn {Scheme Procedure} waitpid pid [options]
  1489. @deffnx {C Function} scm_waitpid (pid, options)
  1490. This procedure collects status information from a child process which
  1491. has terminated or (optionally) stopped. Normally it will
  1492. suspend the calling process until this can be done. If more than one
  1493. child process is eligible then one will be chosen by the operating system.
  1494. The value of @var{pid} determines the behaviour:
  1495. @table @asis
  1496. @item @var{pid} greater than 0
  1497. Request status information from the specified child process.
  1498. @item @var{pid} equal to -1 or @code{WAIT_ANY}
  1499. @vindex WAIT_ANY
  1500. Request status information for any child process.
  1501. @item @var{pid} equal to 0 or @code{WAIT_MYPGRP}
  1502. @vindex WAIT_MYPGRP
  1503. Request status information for any child process in the current process
  1504. group.
  1505. @item @var{pid} less than -1
  1506. Request status information for any child process whose process group ID
  1507. is @minus{}@var{pid}.
  1508. @end table
  1509. The @var{options} argument, if supplied, should be the bitwise OR of the
  1510. values of zero or more of the following variables:
  1511. @defvar WNOHANG
  1512. Return immediately even if there are no child processes to be collected.
  1513. @end defvar
  1514. @defvar WUNTRACED
  1515. Report status information for stopped processes as well as terminated
  1516. processes.
  1517. @end defvar
  1518. The return value is a pair containing:
  1519. @enumerate
  1520. @item
  1521. The process ID of the child process, or 0 if @code{WNOHANG} was
  1522. specified and no process was collected.
  1523. @item
  1524. The integer status value (@pxref{Process Completion Status,,, libc, The
  1525. GNU C Library Reference Manual}).
  1526. @end enumerate
  1527. @end deffn
  1528. The following three
  1529. functions can be used to decode the integer status value returned by
  1530. @code{waitpid}.
  1531. @deffn {Scheme Procedure} status:exit-val status
  1532. @deffnx {C Function} scm_status_exit_val (status)
  1533. Return the exit status value, as would be set if a process
  1534. ended normally through a call to @code{exit} or @code{_exit},
  1535. if any, otherwise @code{#f}.
  1536. @end deffn
  1537. @deffn {Scheme Procedure} status:term-sig status
  1538. @deffnx {C Function} scm_status_term_sig (status)
  1539. Return the signal number which terminated the process, if any,
  1540. otherwise @code{#f}.
  1541. @end deffn
  1542. @deffn {Scheme Procedure} status:stop-sig status
  1543. @deffnx {C Function} scm_status_stop_sig (status)
  1544. Return the signal number which stopped the process, if any,
  1545. otherwise @code{#f}.
  1546. @end deffn
  1547. @deffn {Scheme Procedure} system [cmd]
  1548. @deffnx {C Function} scm_system (cmd)
  1549. Execute @var{cmd} using the operating system's ``command
  1550. processor''. Under Unix this is usually the default shell
  1551. @code{sh}. The value returned is @var{cmd}'s exit status as
  1552. returned by @code{waitpid}, which can be interpreted using the
  1553. functions above.
  1554. If @code{system} is called without arguments, return a boolean
  1555. indicating whether the command processor is available.
  1556. @end deffn
  1557. @deffn {Scheme Procedure} system* arg1 arg2 @dots{}
  1558. @deffnx {C Function} scm_system_star (args)
  1559. Execute the command indicated by @var{arg1} @var{arg2} @enddots{}. The
  1560. first element must be a string indicating the command to be executed,
  1561. and the remaining items must be strings representing each of the
  1562. arguments to that command.
  1563. This function returns the exit status of the command as provided by
  1564. @code{waitpid}. This value can be handled with @code{status:exit-val}
  1565. and the related functions.
  1566. @code{system*} is similar to @code{system}, but accepts only one
  1567. string per-argument, and performs no shell interpretation. The
  1568. command is executed using fork and execlp. Accordingly this function
  1569. may be safer than @code{system} in situations where shell
  1570. interpretation is not required.
  1571. Example: (system* "echo" "foo" "bar")
  1572. @end deffn
  1573. @deffn {Scheme Procedure} quit [status]
  1574. @deffnx {Scheme Procedure} exit [status]
  1575. Terminate the current process with proper unwinding of the Scheme stack.
  1576. The exit status zero if @var{status} is not supplied. If @var{status}
  1577. is supplied, and it is an integer, that integer is used as the exit
  1578. status. If @var{status} is @code{#t} or @code{#f}, the exit status is
  1579. @var{EXIT_SUCCESS} or @var{EXIT_FAILURE}, respectively.
  1580. The procedure @code{exit} is an alias of @code{quit}. They have the
  1581. same functionality.
  1582. @end deffn
  1583. @defvr {Scheme Variable} EXIT_SUCCESS
  1584. @defvrx {Scheme Variable} EXIT_FAILURE
  1585. These constants represent the standard exit codes for success (zero) or
  1586. failure (one.)
  1587. @end defvr
  1588. @deffn {Scheme Procedure} primitive-exit [status]
  1589. @deffnx {Scheme Procedure} primitive-_exit [status]
  1590. @deffnx {C Function} scm_primitive_exit (status)
  1591. @deffnx {C Function} scm_primitive__exit (status)
  1592. Terminate the current process without unwinding the Scheme stack. The
  1593. exit status is @var{status} if supplied, otherwise zero.
  1594. @code{primitive-exit} uses the C @code{exit} function and hence runs
  1595. usual C level cleanups (flush output streams, call @code{atexit}
  1596. functions, etc, see @ref{Normal Termination,,, libc, The GNU C Library
  1597. Reference Manual})).
  1598. @code{primitive-_exit} is the @code{_exit} system call
  1599. (@pxref{Termination Internals,,, libc, The GNU C Library Reference
  1600. Manual}). This terminates the program immediately, with neither
  1601. Scheme-level nor C-level cleanups.
  1602. The typical use for @code{primitive-_exit} is from a child process
  1603. created with @code{primitive-fork}. For example in a Gdk program the
  1604. child process inherits the X server connection and a C-level
  1605. @code{atexit} cleanup which will close that connection. But closing
  1606. in the child would upset the protocol in the parent, so
  1607. @code{primitive-_exit} should be used to exit without that.
  1608. @end deffn
  1609. @deffn {Scheme Procedure} execl filename arg @dots{}
  1610. @deffnx {C Function} scm_execl (filename, args)
  1611. Executes the file named by @var{filename} as a new process image.
  1612. The remaining arguments are supplied to the process; from a C program
  1613. they are accessible as the @code{argv} argument to @code{main}.
  1614. Conventionally the first @var{arg} is the same as @var{filename}.
  1615. All arguments must be strings.
  1616. If @var{arg} is missing, @var{filename} is executed with a null
  1617. argument list, which may have system-dependent side-effects.
  1618. This procedure is currently implemented using the @code{execv} system
  1619. call, but we call it @code{execl} because of its Scheme calling interface.
  1620. @end deffn
  1621. @deffn {Scheme Procedure} execlp filename arg @dots{}
  1622. @deffnx {C Function} scm_execlp (filename, args)
  1623. Similar to @code{execl}, however if
  1624. @var{filename} does not contain a slash
  1625. then the file to execute will be located by searching the
  1626. directories listed in the @code{PATH} environment variable.
  1627. This procedure is currently implemented using the @code{execvp} system
  1628. call, but we call it @code{execlp} because of its Scheme calling interface.
  1629. @end deffn
  1630. @deffn {Scheme Procedure} execle filename env arg @dots{}
  1631. @deffnx {C Function} scm_execle (filename, env, args)
  1632. Similar to @code{execl}, but the environment of the new process is
  1633. specified by @var{env}, which must be a list of strings as returned by the
  1634. @code{environ} procedure.
  1635. This procedure is currently implemented using the @code{execve} system
  1636. call, but we call it @code{execle} because of its Scheme calling interface.
  1637. @end deffn
  1638. @deffn {Scheme Procedure} primitive-fork
  1639. @deffnx {C Function} scm_fork ()
  1640. Creates a new ``child'' process by duplicating the current ``parent'' process.
  1641. In the child the return value is 0. In the parent the return value is
  1642. the integer process ID of the child.
  1643. Note that it is unsafe to fork a process that has multiple threads
  1644. running, as only the thread that calls @code{primitive-fork} will
  1645. persist in the child. Any resources that other threads held, such as
  1646. locked mutexes or open file descriptors, are lost. Indeed,
  1647. @acronym{POSIX} specifies that only async-signal-safe procedures are
  1648. safe to call after a multithreaded fork, which is a very limited set.
  1649. Guile issues a warning if it detects a fork from a multi-threaded
  1650. program.
  1651. If you are going to @code{exec} soon after forking, the procedures in
  1652. @code{(ice-9 popen)} may be useful to you, as they fork and exec within
  1653. an async-signal-safe function carefully written to ensure robust program
  1654. behavior, even in the presence of threads. @xref{Pipes}, for more.
  1655. This procedure has been renamed from @code{fork} to avoid a naming conflict
  1656. with the scsh fork.
  1657. @end deffn
  1658. @deffn {Scheme Procedure} nice incr
  1659. @deffnx {C Function} scm_nice (incr)
  1660. @cindex process priority
  1661. Increment the priority of the current process by @var{incr}. A higher
  1662. priority value means that the process runs less often.
  1663. The return value is unspecified.
  1664. @end deffn
  1665. @deffn {Scheme Procedure} setpriority which who prio
  1666. @deffnx {C Function} scm_setpriority (which, who, prio)
  1667. @vindex PRIO_PROCESS
  1668. @vindex PRIO_PGRP
  1669. @vindex PRIO_USER
  1670. Set the scheduling priority of the process, process group
  1671. or user, as indicated by @var{which} and @var{who}. @var{which}
  1672. is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}
  1673. or @code{PRIO_USER}, and @var{who} is interpreted relative to
  1674. @var{which} (a process identifier for @code{PRIO_PROCESS},
  1675. process group identifier for @code{PRIO_PGRP}, and a user
  1676. identifier for @code{PRIO_USER}. A zero value of @var{who}
  1677. denotes the current process, process group, or user.
  1678. @var{prio} is a value in the range [@minus{}20,20]. The default
  1679. priority is 0; lower priorities (in numerical terms) cause more
  1680. favorable scheduling. Sets the priority of all of the specified
  1681. processes. Only the super-user may lower priorities. The return
  1682. value is not specified.
  1683. @end deffn
  1684. @deffn {Scheme Procedure} getpriority which who
  1685. @deffnx {C Function} scm_getpriority (which, who)
  1686. @vindex PRIO_PROCESS
  1687. @vindex PRIO_PGRP
  1688. @vindex PRIO_USER
  1689. Return the scheduling priority of the process, process group
  1690. or user, as indicated by @var{which} and @var{who}. @var{which}
  1691. is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}
  1692. or @code{PRIO_USER}, and @var{who} should be interpreted depending on
  1693. @var{which} (a process identifier for @code{PRIO_PROCESS},
  1694. process group identifier for @code{PRIO_PGRP}, and a user
  1695. identifier for @code{PRIO_USER}). A zero value of @var{who}
  1696. denotes the current process, process group, or user. Return
  1697. the highest priority (lowest numerical value) of any of the
  1698. specified processes.
  1699. @end deffn
  1700. @cindex affinity, CPU
  1701. @deffn {Scheme Procedure} getaffinity pid
  1702. @deffnx {C Function} scm_getaffinity (pid)
  1703. Return a bitvector representing the CPU affinity mask for
  1704. process @var{pid}. Each CPU the process has affinity with
  1705. has its corresponding bit set in the returned bitvector.
  1706. The number of bits set is a good estimate of how many CPUs
  1707. Guile can use without stepping on other processes' toes.
  1708. Currently this procedure is only defined on GNU variants
  1709. (@pxref{CPU Affinity, @code{sched_getaffinity},, libc, The
  1710. GNU C Library Reference Manual}).
  1711. @end deffn
  1712. @deffn {Scheme Procedure} setaffinity pid mask
  1713. @deffnx {C Function} scm_setaffinity (pid, mask)
  1714. Install the CPU affinity mask @var{mask}, a bitvector, for
  1715. the process or thread with ID @var{pid}. The return value
  1716. is unspecified.
  1717. Currently this procedure is only defined on GNU variants
  1718. (@pxref{CPU Affinity, @code{sched_setaffinity},, libc, The
  1719. GNU C Library Reference Manual}).
  1720. @end deffn
  1721. @xref{Threads}, for information on how get the number of processors
  1722. available on a system.
  1723. @node Signals
  1724. @subsection Signals
  1725. @cindex signal
  1726. The following procedures raise, handle and wait for signals.
  1727. Scheme code signal handlers are run via an async (@pxref{Asyncs}), so
  1728. they're called in the handler's thread at the next safe opportunity.
  1729. Generally this is after any currently executing primitive procedure
  1730. finishes (which could be a long time for primitives that wait for an
  1731. external event).
  1732. @deffn {Scheme Procedure} kill pid sig
  1733. @deffnx {C Function} scm_kill (pid, sig)
  1734. Sends a signal to the specified process or group of processes.
  1735. @var{pid} specifies the processes to which the signal is sent:
  1736. @table @asis
  1737. @item @var{pid} greater than 0
  1738. The process whose identifier is @var{pid}.
  1739. @item @var{pid} equal to 0
  1740. All processes in the current process group.
  1741. @item @var{pid} less than -1
  1742. The process group whose identifier is -@var{pid}
  1743. @item @var{pid} equal to -1
  1744. If the process is privileged, all processes except for some special
  1745. system processes. Otherwise, all processes with the current effective
  1746. user ID.
  1747. @end table
  1748. @var{sig} should be specified using a variable corresponding to
  1749. the Unix symbolic name, e.g.,
  1750. @defvar SIGHUP
  1751. Hang-up signal.
  1752. @end defvar
  1753. @defvar SIGINT
  1754. Interrupt signal.
  1755. @end defvar
  1756. A full list of signals on the GNU system may be found in @ref{Standard
  1757. Signals,,,libc,The GNU C Library Reference Manual}.
  1758. @end deffn
  1759. @deffn {Scheme Procedure} raise sig
  1760. @deffnx {C Function} scm_raise (sig)
  1761. Sends a specified signal @var{sig} to the current process, where
  1762. @var{sig} is as described for the @code{kill} procedure.
  1763. @end deffn
  1764. @deffn {Scheme Procedure} sigaction signum [handler [flags [thread]]]
  1765. @deffnx {C Function} scm_sigaction (signum, handler, flags)
  1766. @deffnx {C Function} scm_sigaction_for_thread (signum, handler, flags, thread)
  1767. Install or report the signal handler for a specified signal.
  1768. @var{signum} is the signal number, which can be specified using the value
  1769. of variables such as @code{SIGINT}.
  1770. If @var{handler} is omitted, @code{sigaction} returns a pair: the
  1771. @acronym{CAR} is the current signal hander, which will be either an
  1772. integer with the value @code{SIG_DFL} (default action) or
  1773. @code{SIG_IGN} (ignore), or the Scheme procedure which handles the
  1774. signal, or @code{#f} if a non-Scheme procedure handles the signal.
  1775. The @acronym{CDR} contains the current @code{sigaction} flags for the
  1776. handler.
  1777. If @var{handler} is provided, it is installed as the new handler for
  1778. @var{signum}. @var{handler} can be a Scheme procedure taking one
  1779. argument, or the value of @code{SIG_DFL} (default action) or
  1780. @code{SIG_IGN} (ignore), or @code{#f} to restore whatever signal handler
  1781. was installed before @code{sigaction} was first used. When a scheme
  1782. procedure has been specified, that procedure will run in the given
  1783. @var{thread}. When no thread has been given, the thread that made this
  1784. call to @code{sigaction} is used.
  1785. @var{flags} is a @code{logior} (@pxref{Bitwise Operations}) of the
  1786. following (where provided by the system), or @code{0} for none.
  1787. @defvar SA_NOCLDSTOP
  1788. By default, @code{SIGCHLD} is signalled when a child process stops
  1789. (ie.@: receives @code{SIGSTOP}), and when a child process terminates.
  1790. With the @code{SA_NOCLDSTOP} flag, @code{SIGCHLD} is only signalled
  1791. for termination, not stopping.
  1792. @code{SA_NOCLDSTOP} has no effect on signals other than
  1793. @code{SIGCHLD}.
  1794. @end defvar
  1795. @defvar SA_RESTART
  1796. If a signal occurs while in a system call, deliver the signal then
  1797. restart the system call (as opposed to returning an @code{EINTR} error
  1798. from that call).
  1799. @end defvar
  1800. Guile handles signals asynchronously. When it receives a signal, the
  1801. synchronous signal handler just records the fact that a signal was
  1802. received and sets a flag to tell the relevant Guile thread that it has a
  1803. pending signal. When the Guile thread checks the pending-interrupt
  1804. flag, it will arrange to run the asynchronous part of the signal
  1805. handler, which is the handler attached by @code{sigaction}.
  1806. This strategy has some perhaps-unexpected interactions with the
  1807. @code{SA_RESTART} flag, though: because the synchronous handler doesn't
  1808. do very much, and notably it doesn't run the Guile handler, it's
  1809. impossible to interrupt a thread stuck in a long-running system call via
  1810. a signal handler that is installed with @code{SA_RESTART}: the
  1811. synchronous handler just records the pending interrupt, but then the
  1812. system call resumes and Guile doesn't have a chance to actually check
  1813. the flag and run the asynchronous handler. That's just how it is.
  1814. The return value is a pair with information about the old handler as
  1815. described above.
  1816. This interface does not provide access to the ``signal blocking''
  1817. facility. Maybe this is not needed, since the thread support may
  1818. provide solutions to the problem of consistent access to data
  1819. structures.
  1820. @end deffn
  1821. @deffn {Scheme Procedure} restore-signals
  1822. @deffnx {C Function} scm_restore_signals ()
  1823. Return all signal handlers to the values they had before any call to
  1824. @code{sigaction} was made. The return value is unspecified.
  1825. @end deffn
  1826. @deffn {Scheme Procedure} alarm i
  1827. @deffnx {C Function} scm_alarm (i)
  1828. Set a timer to raise a @code{SIGALRM} signal after the specified
  1829. number of seconds (an integer). It's advisable to install a signal
  1830. handler for
  1831. @code{SIGALRM} beforehand, since the default action is to terminate
  1832. the process.
  1833. The return value indicates the time remaining for the previous alarm,
  1834. if any. The new value replaces the previous alarm. If there was
  1835. no previous alarm, the return value is zero.
  1836. @end deffn
  1837. @deffn {Scheme Procedure} pause
  1838. @deffnx {C Function} scm_pause ()
  1839. Pause the current process (thread?) until a signal arrives whose
  1840. action is to either terminate the current process or invoke a
  1841. handler procedure. The return value is unspecified.
  1842. @end deffn
  1843. @deffn {Scheme Procedure} sleep secs
  1844. @deffnx {Scheme Procedure} usleep usecs
  1845. @deffnx {C Function} scm_sleep (secs)
  1846. @deffnx {C Function} scm_usleep (usecs)
  1847. Wait the given period @var{secs} seconds or @var{usecs} microseconds
  1848. (both integers). If a signal arrives the wait stops and the return
  1849. value is the time remaining, in seconds or microseconds respectively.
  1850. If the period elapses with no signal the return is zero.
  1851. On most systems the process scheduler is not microsecond accurate and
  1852. the actual period slept by @code{usleep} might be rounded to a system
  1853. clock tick boundary, which might be 10 milliseconds for instance.
  1854. See @code{scm_std_sleep} and @code{scm_std_usleep} for equivalents at
  1855. the C level (@pxref{Blocking}).
  1856. @end deffn
  1857. @deffn {Scheme Procedure} getitimer which_timer
  1858. @deffnx {Scheme Procedure} setitimer which_timer interval_seconds interval_microseconds value_seconds value_microseconds
  1859. @deffnx {C Function} scm_getitimer (which_timer)
  1860. @deffnx {C Function} scm_setitimer (which_timer, interval_seconds, interval_microseconds, value_seconds, value_microseconds)
  1861. Get or set the periods programmed in certain system timers.
  1862. These timers have two settings. The first setting, the interval, is the
  1863. value at which the timer will be reset when the current timer expires.
  1864. The second is the current value of the timer, indicating when the next
  1865. expiry will be signalled.
  1866. @var{which_timer} is one of the following values:
  1867. @defvar ITIMER_REAL
  1868. A real-time timer, counting down elapsed real time. At zero it raises
  1869. @code{SIGALRM}. This is like @code{alarm} above, but with a higher
  1870. resolution period.
  1871. @end defvar
  1872. @defvar ITIMER_VIRTUAL
  1873. A virtual-time timer, counting down while the current process is
  1874. actually using CPU. At zero it raises @code{SIGVTALRM}.
  1875. @end defvar
  1876. @defvar ITIMER_PROF
  1877. A profiling timer, counting down while the process is running (like
  1878. @code{ITIMER_VIRTUAL}) and also while system calls are running on the
  1879. process's behalf. At zero it raises a @code{SIGPROF}.
  1880. This timer is intended for profiling where a program is spending its
  1881. time (by looking where it is when the timer goes off).
  1882. @end defvar
  1883. @code{getitimer} returns the restart timer value and its current value,
  1884. as a list containing two pairs. Each pair is a time in seconds and
  1885. microseconds: @code{((@var{interval_secs} . @var{interval_usecs})
  1886. (@var{value_secs} . @var{value_usecs}))}.
  1887. @code{setitimer} sets the timer values similarly, in seconds and
  1888. microseconds (which must be integers). The interval value can be zero
  1889. to have the timer run down just once. The return value is the timer's
  1890. previous setting, in the same form as @code{getitimer} returns.
  1891. @example
  1892. (setitimer ITIMER_REAL
  1893. 5 500000 ;; Raise SIGALRM every 5.5 seconds
  1894. 2 0) ;; with the first SIGALRM in 2 seconds
  1895. @end example
  1896. Although the timers are programmed in microseconds, the actual
  1897. accuracy might not be that high.
  1898. Note that @code{ITIMER_PROF} and @code{ITIMER_VIRTUAL} are not
  1899. functional on all platforms and may always error when called.
  1900. @code{(provided? 'ITIMER_PROF)} and @code{(provided? 'ITIMER_VIRTUAL)}
  1901. can be used to test if the those itimers are supported on the given
  1902. host. @code{ITIMER_REAL} is supported on all platforms that support
  1903. @code{setitimer}.
  1904. @end deffn
  1905. @node Terminals and Ptys
  1906. @subsection Terminals and Ptys
  1907. @deffn {Scheme Procedure} isatty? port
  1908. @deffnx {C Function} scm_isatty_p (port)
  1909. @cindex terminal
  1910. Return @code{#t} if @var{port} is using a serial non--file
  1911. device, otherwise @code{#f}.
  1912. @end deffn
  1913. @deffn {Scheme Procedure} ttyname port
  1914. @deffnx {C Function} scm_ttyname (port)
  1915. @cindex terminal
  1916. Return a string with the name of the serial terminal device
  1917. underlying @var{port}.
  1918. @end deffn
  1919. @deffn {Scheme Procedure} ctermid
  1920. @deffnx {C Function} scm_ctermid ()
  1921. @cindex terminal
  1922. Return a string containing the file name of the controlling
  1923. terminal for the current process.
  1924. @end deffn
  1925. @deffn {Scheme Procedure} tcgetpgrp port
  1926. @deffnx {C Function} scm_tcgetpgrp (port)
  1927. @cindex process group
  1928. Return the process group ID of the foreground process group
  1929. associated with the terminal open on the file descriptor
  1930. underlying @var{port}.
  1931. If there is no foreground process group, the return value is a
  1932. number greater than 1 that does not match the process group ID
  1933. of any existing process group. This can happen if all of the
  1934. processes in the job that was formerly the foreground job have
  1935. terminated, and no other job has yet been moved into the
  1936. foreground.
  1937. @end deffn
  1938. @deffn {Scheme Procedure} tcsetpgrp port pgid
  1939. @deffnx {C Function} scm_tcsetpgrp (port, pgid)
  1940. @cindex process group
  1941. Set the foreground process group ID for the terminal used by the file
  1942. descriptor underlying @var{port} to the integer @var{pgid}.
  1943. The calling process
  1944. must be a member of the same session as @var{pgid} and must have the same
  1945. controlling terminal. The return value is unspecified.
  1946. @end deffn
  1947. @node Pipes
  1948. @subsection Pipes
  1949. @cindex pipe
  1950. The following procedures are similar to the @code{popen} and
  1951. @code{pclose} system routines. The code is in a separate ``popen''
  1952. module@footnote{This module is only available on systems where the
  1953. @code{popen} feature is provided (@pxref{Common Feature Symbols}).}:
  1954. @lisp
  1955. (use-modules (ice-9 popen))
  1956. @end lisp
  1957. @findex popen
  1958. @deffn {Scheme Procedure} open-pipe command mode
  1959. @deffnx {Scheme Procedure} open-pipe* mode prog [args...]
  1960. Execute a command in a subprocess, with a pipe to it or from it, or
  1961. with pipes in both directions.
  1962. @code{open-pipe} runs the shell @var{command} using @samp{/bin/sh -c}.
  1963. @code{open-pipe*} executes @var{prog} directly, with the optional
  1964. @var{args} arguments (all strings).
  1965. @var{mode} should be one of the following values. @code{OPEN_READ} is
  1966. an input pipe, ie.@: to read from the subprocess. @code{OPEN_WRITE}
  1967. is an output pipe, ie.@: to write to it.
  1968. @defvar OPEN_READ
  1969. @defvarx OPEN_WRITE
  1970. @defvarx OPEN_BOTH
  1971. @end defvar
  1972. For an input pipe, the child's standard output is the pipe and
  1973. standard input is inherited from @code{current-input-port}. For an
  1974. output pipe, the child's standard input is the pipe and standard
  1975. output is inherited from @code{current-output-port}. In all cases
  1976. the child's standard error is inherited from
  1977. @code{current-error-port} (@pxref{Default Ports}).
  1978. If those @code{current-X-ports} are not files of some kind, and hence
  1979. don't have file descriptors for the child, then @file{/dev/null} is
  1980. used instead.
  1981. Care should be taken with @code{OPEN_BOTH}, a deadlock will occur if
  1982. both parent and child are writing, and waiting until the write completes
  1983. before doing any reading. Each direction has @code{PIPE_BUF} bytes of
  1984. buffering (@pxref{Buffering}), which will be enough for small writes,
  1985. but not for say putting a big file through a filter.
  1986. @end deffn
  1987. @deffn {Scheme Procedure} open-input-pipe command
  1988. Equivalent to @code{open-pipe} with mode @code{OPEN_READ}.
  1989. @lisp
  1990. (let* ((port (open-input-pipe "date --utc"))
  1991. (str (read-line port))) ; from (ice-9 rdelim)
  1992. (close-pipe port)
  1993. str)
  1994. @result{} "Mon Mar 11 20:10:44 UTC 2002"
  1995. @end lisp
  1996. @end deffn
  1997. @deffn {Scheme Procedure} open-output-pipe command
  1998. Equivalent to @code{open-pipe} with mode @code{OPEN_WRITE}.
  1999. @lisp
  2000. (let ((port (open-output-pipe "lpr")))
  2001. (display "Something for the line printer.\n" port)
  2002. (if (not (eqv? 0 (status:exit-val (close-pipe port))))
  2003. (error "Cannot print")))
  2004. @end lisp
  2005. @end deffn
  2006. @deffn {Scheme Procedure} open-input-output-pipe command
  2007. Equivalent to @code{open-pipe} with mode @code{OPEN_BOTH}.
  2008. @end deffn
  2009. @findex pclose
  2010. @deffn {Scheme Procedure} close-pipe port
  2011. Close a pipe created by @code{open-pipe}, wait for the process to
  2012. terminate, and return the wait status code. The status is as per
  2013. @code{waitpid} and can be decoded with @code{status:exit-val} etc
  2014. (@pxref{Processes})
  2015. @end deffn
  2016. @sp 1
  2017. @code{waitpid WAIT_ANY} should not be used when pipes are open, since
  2018. it can reap a pipe's child process, causing an error from a subsequent
  2019. @code{close-pipe}.
  2020. @code{close-port} (@pxref{Ports}) can close a pipe, but it doesn't reap
  2021. the child process.
  2022. The garbage collector will close a pipe no longer in use, and reap the
  2023. child process with @code{waitpid}. If the child hasn't yet terminated
  2024. the garbage collector doesn't block, but instead checks again in the
  2025. next GC.
  2026. Many systems have per-user and system-wide limits on the number of
  2027. processes, and a system-wide limit on the number of pipes, so pipes
  2028. should be closed explicitly when no longer needed, rather than letting
  2029. the garbage collector pick them up at some later time.
  2030. @deffn {Scheme Procedure} pipeline @var{commands}
  2031. Execute a pipeline of @var{commands}, where each command is a
  2032. list of a program and its arguments as strings, returning an input
  2033. port to the end of the pipeline, an output port to the beginning of the
  2034. pipeline and a list of PIDs of the processes executing the @var{commands}.
  2035. @example
  2036. (let ((commands '(("git" "ls-files")
  2037. ("tar" "-cf-" "-T-")
  2038. ("sha1sum" "-")))
  2039. (success? (lambda (pid)
  2040. (zero?
  2041. (status:exit-val (cdr (waitpid pid)))))))
  2042. (receive (from to pids) (pipeline commands)
  2043. (let* ((sha1 (read-delimited " " from))
  2044. (index (list-index (negate success?) (reverse pids))))
  2045. (close to)
  2046. (close from)
  2047. (if (not index)
  2048. sha1
  2049. (string-append "pipeline failed in command: "
  2050. (string-join (list-ref commands index)))))))
  2051. @result{} "52f99d234503fca8c84ef94b1005a3a28d8b3bc1"
  2052. @end example
  2053. @end deffn
  2054. @node Networking
  2055. @subsection Networking
  2056. @cindex network
  2057. @menu
  2058. * Network Address Conversion::
  2059. * Network Databases::
  2060. * Network Socket Address::
  2061. * Network Sockets and Communication::
  2062. * Internet Socket Examples::
  2063. @end menu
  2064. @node Network Address Conversion
  2065. @subsubsection Network Address Conversion
  2066. @cindex network address
  2067. This section describes procedures which convert internet addresses
  2068. between numeric and string formats.
  2069. @subsubheading IPv4 Address Conversion
  2070. @cindex IPv4
  2071. An IPv4 Internet address is a 4-byte value, represented in Guile as an
  2072. integer in host byte order, so that say ``0.0.0.1'' is 1, or
  2073. ``1.0.0.0'' is 16777216.
  2074. Some underlying C functions use network byte order for addresses,
  2075. Guile converts as necessary so that at the Scheme level its host byte
  2076. order everywhere.
  2077. @defvar INADDR_ANY
  2078. For a server, this can be used with @code{bind} (@pxref{Network
  2079. Sockets and Communication}) to allow connections from any interface on
  2080. the machine.
  2081. @end defvar
  2082. @defvar INADDR_BROADCAST
  2083. The broadcast address on the local network.
  2084. @end defvar
  2085. @defvar INADDR_LOOPBACK
  2086. The address of the local host using the loopback device, ie.@:
  2087. @samp{127.0.0.1}.
  2088. @end defvar
  2089. @c INADDR_NONE is defined in the code, but serves no purpose.
  2090. @c inet_addr() returns it as an error indication, but that function
  2091. @c isn't provided, for the good reason that inet_aton() does the same
  2092. @c job and gives an unambiguous error indication. (INADDR_NONE is a
  2093. @c valid 4-byte value, in glibc it's the same as INADDR_BROADCAST.)
  2094. @c
  2095. @c @defvar INADDR_NONE
  2096. @c No address.
  2097. @c @end defvar
  2098. @deffn {Scheme Procedure} inet-netof address
  2099. @deffnx {C Function} scm_inet_netof (address)
  2100. Return the network number part of the given IPv4
  2101. Internet address. E.g.,
  2102. @lisp
  2103. (inet-netof 2130706433) @result{} 127
  2104. @end lisp
  2105. @end deffn
  2106. @deffn {Scheme Procedure} inet-lnaof address
  2107. @deffnx {C Function} scm_lnaof (address)
  2108. Return the local-address-with-network part of the given
  2109. IPv4 Internet address, using the obsolete class A/B/C system.
  2110. E.g.,
  2111. @lisp
  2112. (inet-lnaof 2130706433) @result{} 1
  2113. @end lisp
  2114. @end deffn
  2115. @deffn {Scheme Procedure} inet-makeaddr net lna
  2116. @deffnx {C Function} scm_inet_makeaddr (net, lna)
  2117. Make an IPv4 Internet address by combining the network number
  2118. @var{net} with the local-address-within-network number
  2119. @var{lna}. E.g.,
  2120. @lisp
  2121. (inet-makeaddr 127 1) @result{} 2130706433
  2122. @end lisp
  2123. @end deffn
  2124. @subsubheading IPv6 Address Conversion
  2125. @cindex IPv6
  2126. An IPv6 Internet address is a 16-byte value, represented in Guile as
  2127. an integer in host byte order, so that say ``::1'' is 1.
  2128. @deffn {Scheme Procedure} inet-ntop family address
  2129. @deffnx {C Function} scm_inet_ntop (family, address)
  2130. Convert a network address from an integer to a printable string.
  2131. @var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,
  2132. @lisp
  2133. (inet-ntop AF_INET 2130706433) @result{} "127.0.0.1"
  2134. (inet-ntop AF_INET6 (- (expt 2 128) 1))
  2135. @result{} "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  2136. @end lisp
  2137. @end deffn
  2138. @deffn {Scheme Procedure} inet-pton family address
  2139. @deffnx {C Function} scm_inet_pton (family, address)
  2140. Convert a string containing a printable network address to an integer
  2141. address. @var{family} can be @code{AF_INET} or @code{AF_INET6}.
  2142. E.g.,
  2143. @lisp
  2144. (inet-pton AF_INET "127.0.0.1") @result{} 2130706433
  2145. (inet-pton AF_INET6 "::1") @result{} 1
  2146. @end lisp
  2147. @end deffn
  2148. @node Network Databases
  2149. @subsubsection Network Databases
  2150. @cindex network database
  2151. This section describes procedures which query various network databases.
  2152. Care should be taken when using the database routines since they are not
  2153. reentrant.
  2154. @subsubheading @code{getaddrinfo}
  2155. @cindex @code{addrinfo} object type
  2156. @cindex host name lookup
  2157. @cindex service name lookup
  2158. The @code{getaddrinfo} procedure maps host and service names to socket addresses
  2159. and associated information in a protocol-independent way.
  2160. @deffn {Scheme Procedure} getaddrinfo name service [hint_flags [hint_family [hint_socktype [hint_protocol]]]]
  2161. @deffnx {C Function} scm_getaddrinfo (name, service, hint_flags, hint_family, hint_socktype, hint_protocol)
  2162. Return a list of @code{addrinfo} structures containing
  2163. a socket address and associated information for host @var{name}
  2164. and/or @var{service} to be used in creating a socket with
  2165. which to address the specified service.
  2166. @example
  2167. (let* ((ai (car (getaddrinfo "www.gnu.org" "http")))
  2168. (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
  2169. (addrinfo:protocol ai))))
  2170. (connect s (addrinfo:addr ai))
  2171. s)
  2172. @end example
  2173. When @var{service} is omitted or is @code{#f}, return
  2174. network-level addresses for @var{name}. When @var{name}
  2175. is @code{#f} @var{service} must be provided and service
  2176. locations local to the caller are returned.
  2177. Additional hints can be provided. When specified,
  2178. @var{hint_flags} should be a bitwise-or of zero or more
  2179. constants among the following:
  2180. @table @code
  2181. @item AI_PASSIVE
  2182. Socket address is intended for @code{bind}.
  2183. @item AI_CANONNAME
  2184. Request for canonical host name, available via
  2185. @code{addrinfo:canonname}. This makes sense mainly when
  2186. DNS lookups are involved.
  2187. @item AI_NUMERICHOST
  2188. Specifies that @var{name} is a numeric host address string
  2189. (e.g., @code{"127.0.0.1"}), meaning that name resolution
  2190. will not be used.
  2191. @item AI_NUMERICSERV
  2192. Likewise, specifies that @var{service} is a numeric port
  2193. string (e.g., @code{"80"}).
  2194. @item AI_ADDRCONFIG
  2195. Return only addresses configured on the local system It is
  2196. highly recommended to provide this flag when the returned
  2197. socket addresses are to be used to make connections;
  2198. otherwise, some of the returned addresses could be unreachable
  2199. or use a protocol that is not supported.
  2200. @item AI_V4MAPPED
  2201. When looking up IPv6 addresses, return mapped IPv4 addresses if
  2202. there is no IPv6 address available at all.
  2203. @item AI_ALL
  2204. If this flag is set along with @code{AI_V4MAPPED} when looking up IPv6
  2205. addresses, return all IPv6 addresses as well as all IPv4 addresses, the latter
  2206. mapped to IPv6 format.
  2207. @end table
  2208. When given, @var{hint_family} should specify the requested
  2209. address family, e.g., @code{AF_INET6}. Similarly,
  2210. @var{hint_socktype} should specify the requested socket type
  2211. (e.g., @code{SOCK_DGRAM}), and @var{hint_protocol} should
  2212. specify the requested protocol (its value is interpreted
  2213. as in calls to @code{socket}).
  2214. On error, an exception with key @code{getaddrinfo-error} is
  2215. thrown, with an error code (an integer) as its argument:
  2216. @example
  2217. (catch 'getaddrinfo-error
  2218. (lambda ()
  2219. (getaddrinfo "www.gnu.org" "gopher"))
  2220. (lambda (key errcode)
  2221. (cond ((= errcode EAI_SERVICE)
  2222. (display "doesn't know about Gopher!\n"))
  2223. ((= errcode EAI_NONAME)
  2224. (display "www.gnu.org not found\\n"))
  2225. (else
  2226. (format #t "something wrong: ~a\n"
  2227. (gai-strerror errcode))))))
  2228. @end example
  2229. Error codes are:
  2230. @table @code
  2231. @item EAI_AGAIN
  2232. The name or service could not be resolved at this time. Future
  2233. attempts may succeed.
  2234. @item EAI_BADFLAGS
  2235. @var{hint_flags} contains an invalid value.
  2236. @item EAI_FAIL
  2237. A non-recoverable error occurred when attempting to
  2238. resolve the name.
  2239. @item EAI_FAMILY
  2240. @var{hint_family} was not recognized.
  2241. @item EAI_NONAME
  2242. Either @var{name} does not resolve for the supplied parameters,
  2243. or neither @var{name} nor @var{service} were supplied.
  2244. @item EAI_NODATA
  2245. This non-POSIX error code can be returned on some systems (GNU
  2246. and Darwin, at least), for example when @var{name} is known
  2247. but requests that were made turned out no data. Error handling
  2248. code should be prepared to handle it when it is defined.
  2249. @item EAI_SERVICE
  2250. @var{service} was not recognized for the specified socket type.
  2251. @item EAI_SOCKTYPE
  2252. @var{hint_socktype} was not recognized.
  2253. @item EAI_SYSTEM
  2254. A system error occurred. In C, the error code can be found in
  2255. @code{errno}; this value is not accessible from Scheme, but in
  2256. practice it provides little information about the actual error
  2257. cause.
  2258. @c See <http://bugs.gnu.org/13958>.
  2259. @end table
  2260. Users are encouraged to read the
  2261. @url{http://www.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html,
  2262. "POSIX specification} for more details.
  2263. @end deffn
  2264. The following procedures take an @code{addrinfo} object as returned by
  2265. @code{getaddrinfo}:
  2266. @deffn {Scheme Procedure} addrinfo:flags ai
  2267. Return flags for @var{ai} as a bitwise or of @code{AI_} values (see above).
  2268. @end deffn
  2269. @deffn {Scheme Procedure} addrinfo:fam ai
  2270. Return the address family of @var{ai} (a @code{AF_} value).
  2271. @end deffn
  2272. @deffn {Scheme Procedure} addrinfo:socktype ai
  2273. Return the socket type for @var{ai} (a @code{SOCK_} value).
  2274. @end deffn
  2275. @deffn {Scheme Procedure} addrinfo:protocol ai
  2276. Return the protocol of @var{ai}.
  2277. @end deffn
  2278. @deffn {Scheme Procedure} addrinfo:addr ai
  2279. Return the socket address associated with @var{ai} as a @code{sockaddr}
  2280. object (@pxref{Network Socket Address}).
  2281. @end deffn
  2282. @deffn {Scheme Procedure} addrinfo:canonname ai
  2283. Return a string for the canonical name associated with @var{ai} if
  2284. the @code{AI_CANONNAME} flag was supplied.
  2285. @end deffn
  2286. @subsubheading The Host Database
  2287. @cindex @file{/etc/hosts}
  2288. @cindex network database
  2289. A @dfn{host object} is a structure that represents what is known about a
  2290. network host, and is the usual way of representing a system's network
  2291. identity inside software.
  2292. The following functions accept a host object and return a selected
  2293. component:
  2294. @deffn {Scheme Procedure} hostent:name host
  2295. The ``official'' hostname for @var{host}.
  2296. @end deffn
  2297. @deffn {Scheme Procedure} hostent:aliases host
  2298. A list of aliases for @var{host}.
  2299. @end deffn
  2300. @deffn {Scheme Procedure} hostent:addrtype host
  2301. The host address type, one of the @code{AF} constants, such as
  2302. @code{AF_INET} or @code{AF_INET6}.
  2303. @end deffn
  2304. @deffn {Scheme Procedure} hostent:length host
  2305. The length of each address for @var{host}, in bytes.
  2306. @end deffn
  2307. @deffn {Scheme Procedure} hostent:addr-list host
  2308. The list of network addresses associated with @var{host}. For
  2309. @code{AF_INET} these are integer IPv4 address (@pxref{Network Address
  2310. Conversion}).
  2311. @end deffn
  2312. The following procedures can be used to search the host database. However,
  2313. @code{getaddrinfo} should be preferred over them since it's more generic and
  2314. thread-safe.
  2315. @deffn {Scheme Procedure} gethost [host]
  2316. @deffnx {Scheme Procedure} gethostbyname hostname
  2317. @deffnx {Scheme Procedure} gethostbyaddr address
  2318. @deffnx {C Function} scm_gethost (host)
  2319. Look up a host by name or address, returning a host object. The
  2320. @code{gethost} procedure will accept either a string name or an integer
  2321. address; if given no arguments, it behaves like @code{gethostent} (see
  2322. below). If a name or address is supplied but the address can not be
  2323. found, an error will be thrown to one of the keys:
  2324. @code{host-not-found}, @code{try-again}, @code{no-recovery} or
  2325. @code{no-data}, corresponding to the equivalent @code{h_error} values.
  2326. Unusual conditions may result in errors thrown to the
  2327. @code{system-error} or @code{misc_error} keys.
  2328. @lisp
  2329. (gethost "www.gnu.org")
  2330. @result{} #("www.gnu.org" () 2 4 (3353880842))
  2331. (gethostbyname "www.emacs.org")
  2332. @result{} #("emacs.org" ("www.emacs.org") 2 4 (1073448978))
  2333. @end lisp
  2334. @end deffn
  2335. The following procedures may be used to step through the host
  2336. database from beginning to end.
  2337. @deffn {Scheme Procedure} sethostent [stayopen]
  2338. Initialize an internal stream from which host objects may be read. This
  2339. procedure must be called before any calls to @code{gethostent}, and may
  2340. also be called afterward to reset the host entry stream. If
  2341. @var{stayopen} is supplied and is not @code{#f}, the database is not
  2342. closed by subsequent @code{gethostbyname} or @code{gethostbyaddr} calls,
  2343. possibly giving an efficiency gain.
  2344. @end deffn
  2345. @deffn {Scheme Procedure} gethostent
  2346. Return the next host object from the host database, or @code{#f} if
  2347. there are no more hosts to be found (or an error has been encountered).
  2348. This procedure may not be used before @code{sethostent} has been called.
  2349. @end deffn
  2350. @deffn {Scheme Procedure} endhostent
  2351. Close the stream used by @code{gethostent}. The return value is unspecified.
  2352. @end deffn
  2353. @deffn {Scheme Procedure} sethost [stayopen]
  2354. @deffnx {C Function} scm_sethost (stayopen)
  2355. If @var{stayopen} is omitted, this is equivalent to @code{endhostent}.
  2356. Otherwise it is equivalent to @code{sethostent stayopen}.
  2357. @end deffn
  2358. @subsubheading The Network Database
  2359. @cindex network database
  2360. The following functions accept an object representing a network
  2361. and return a selected component:
  2362. @deffn {Scheme Procedure} netent:name net
  2363. The ``official'' network name.
  2364. @end deffn
  2365. @deffn {Scheme Procedure} netent:aliases net
  2366. A list of aliases for the network.
  2367. @end deffn
  2368. @deffn {Scheme Procedure} netent:addrtype net
  2369. The type of the network number. Currently, this returns only
  2370. @code{AF_INET}.
  2371. @end deffn
  2372. @deffn {Scheme Procedure} netent:net net
  2373. The network number.
  2374. @end deffn
  2375. The following procedures are used to search the network database:
  2376. @deffn {Scheme Procedure} getnet [net]
  2377. @deffnx {Scheme Procedure} getnetbyname net-name
  2378. @deffnx {Scheme Procedure} getnetbyaddr net-number
  2379. @deffnx {C Function} scm_getnet (net)
  2380. Look up a network by name or net number in the network database. The
  2381. @var{net-name} argument must be a string, and the @var{net-number}
  2382. argument must be an integer. @code{getnet} will accept either type of
  2383. argument, behaving like @code{getnetent} (see below) if no arguments are
  2384. given.
  2385. @end deffn
  2386. The following procedures may be used to step through the network
  2387. database from beginning to end.
  2388. @deffn {Scheme Procedure} setnetent [stayopen]
  2389. Initialize an internal stream from which network objects may be read. This
  2390. procedure must be called before any calls to @code{getnetent}, and may
  2391. also be called afterward to reset the net entry stream. If
  2392. @var{stayopen} is supplied and is not @code{#f}, the database is not
  2393. closed by subsequent @code{getnetbyname} or @code{getnetbyaddr} calls,
  2394. possibly giving an efficiency gain.
  2395. @end deffn
  2396. @deffn {Scheme Procedure} getnetent
  2397. Return the next entry from the network database.
  2398. @end deffn
  2399. @deffn {Scheme Procedure} endnetent
  2400. Close the stream used by @code{getnetent}. The return value is unspecified.
  2401. @end deffn
  2402. @deffn {Scheme Procedure} setnet [stayopen]
  2403. @deffnx {C Function} scm_setnet (stayopen)
  2404. If @var{stayopen} is omitted, this is equivalent to @code{endnetent}.
  2405. Otherwise it is equivalent to @code{setnetent stayopen}.
  2406. @end deffn
  2407. @subsubheading The Protocol Database
  2408. @cindex @file{/etc/protocols}
  2409. @cindex protocols
  2410. @cindex network protocols
  2411. The following functions accept an object representing a protocol
  2412. and return a selected component:
  2413. @deffn {Scheme Procedure} protoent:name protocol
  2414. The ``official'' protocol name.
  2415. @end deffn
  2416. @deffn {Scheme Procedure} protoent:aliases protocol
  2417. A list of aliases for the protocol.
  2418. @end deffn
  2419. @deffn {Scheme Procedure} protoent:proto protocol
  2420. The protocol number.
  2421. @end deffn
  2422. The following procedures are used to search the protocol database:
  2423. @deffn {Scheme Procedure} getproto [protocol]
  2424. @deffnx {Scheme Procedure} getprotobyname name
  2425. @deffnx {Scheme Procedure} getprotobynumber number
  2426. @deffnx {C Function} scm_getproto (protocol)
  2427. Look up a network protocol by name or by number. @code{getprotobyname}
  2428. takes a string argument, and @code{getprotobynumber} takes an integer
  2429. argument. @code{getproto} will accept either type, behaving like
  2430. @code{getprotoent} (see below) if no arguments are supplied.
  2431. @end deffn
  2432. The following procedures may be used to step through the protocol
  2433. database from beginning to end.
  2434. @deffn {Scheme Procedure} setprotoent [stayopen]
  2435. Initialize an internal stream from which protocol objects may be read. This
  2436. procedure must be called before any calls to @code{getprotoent}, and may
  2437. also be called afterward to reset the protocol entry stream. If
  2438. @var{stayopen} is supplied and is not @code{#f}, the database is not
  2439. closed by subsequent @code{getprotobyname} or @code{getprotobynumber} calls,
  2440. possibly giving an efficiency gain.
  2441. @end deffn
  2442. @deffn {Scheme Procedure} getprotoent
  2443. Return the next entry from the protocol database.
  2444. @end deffn
  2445. @deffn {Scheme Procedure} endprotoent
  2446. Close the stream used by @code{getprotoent}. The return value is unspecified.
  2447. @end deffn
  2448. @deffn {Scheme Procedure} setproto [stayopen]
  2449. @deffnx {C Function} scm_setproto (stayopen)
  2450. If @var{stayopen} is omitted, this is equivalent to @code{endprotoent}.
  2451. Otherwise it is equivalent to @code{setprotoent stayopen}.
  2452. @end deffn
  2453. @subsubheading The Service Database
  2454. @cindex @file{/etc/services}
  2455. @cindex services
  2456. @cindex network services
  2457. The following functions accept an object representing a service
  2458. and return a selected component:
  2459. @deffn {Scheme Procedure} servent:name serv
  2460. The ``official'' name of the network service.
  2461. @end deffn
  2462. @deffn {Scheme Procedure} servent:aliases serv
  2463. A list of aliases for the network service.
  2464. @end deffn
  2465. @deffn {Scheme Procedure} servent:port serv
  2466. The Internet port used by the service.
  2467. @end deffn
  2468. @deffn {Scheme Procedure} servent:proto serv
  2469. The protocol used by the service. A service may be listed many times
  2470. in the database under different protocol names.
  2471. @end deffn
  2472. The following procedures are used to search the service database:
  2473. @deffn {Scheme Procedure} getserv [name [protocol]]
  2474. @deffnx {Scheme Procedure} getservbyname name protocol
  2475. @deffnx {Scheme Procedure} getservbyport port protocol
  2476. @deffnx {C Function} scm_getserv (name, protocol)
  2477. Look up a network service by name or by service number, and return a
  2478. network service object. The @var{protocol} argument specifies the name
  2479. of the desired protocol; if the protocol found in the network service
  2480. database does not match this name, a system error is signalled.
  2481. The @code{getserv} procedure will take either a service name or number
  2482. as its first argument; if given no arguments, it behaves like
  2483. @code{getservent} (see below).
  2484. @lisp
  2485. (getserv "imap" "tcp")
  2486. @result{} #("imap2" ("imap") 143 "tcp")
  2487. (getservbyport 88 "udp")
  2488. @result{} #("kerberos" ("kerberos5" "krb5") 88 "udp")
  2489. @end lisp
  2490. @end deffn
  2491. The following procedures may be used to step through the service
  2492. database from beginning to end.
  2493. @deffn {Scheme Procedure} setservent [stayopen]
  2494. Initialize an internal stream from which service objects may be read. This
  2495. procedure must be called before any calls to @code{getservent}, and may
  2496. also be called afterward to reset the service entry stream. If
  2497. @var{stayopen} is supplied and is not @code{#f}, the database is not
  2498. closed by subsequent @code{getservbyname} or @code{getservbyport} calls,
  2499. possibly giving an efficiency gain.
  2500. @end deffn
  2501. @deffn {Scheme Procedure} getservent
  2502. Return the next entry from the services database.
  2503. @end deffn
  2504. @deffn {Scheme Procedure} endservent
  2505. Close the stream used by @code{getservent}. The return value is unspecified.
  2506. @end deffn
  2507. @deffn {Scheme Procedure} setserv [stayopen]
  2508. @deffnx {C Function} scm_setserv (stayopen)
  2509. If @var{stayopen} is omitted, this is equivalent to @code{endservent}.
  2510. Otherwise it is equivalent to @code{setservent stayopen}.
  2511. @end deffn
  2512. @node Network Socket Address
  2513. @subsubsection Network Socket Address
  2514. @cindex socket address
  2515. @cindex network socket address
  2516. @tpindex Socket address
  2517. A @dfn{socket address} object identifies a socket endpoint for
  2518. communication. In the case of @code{AF_INET} for instance, the socket
  2519. address object comprises the host address (or interface on the host)
  2520. and a port number which specifies a particular open socket in a
  2521. running client or server process. A socket address object can be
  2522. created with,
  2523. @deffn {Scheme Procedure} make-socket-address AF_INET ipv4addr port
  2524. @deffnx {Scheme Procedure} make-socket-address AF_INET6 ipv6addr port [flowinfo [scopeid]]
  2525. @deffnx {Scheme Procedure} make-socket-address AF_UNIX path
  2526. @deffnx {C Function} scm_make_socket_address (family, address, arglist)
  2527. Return a new socket address object. The first argument is the address
  2528. family, one of the @code{AF} constants, then the arguments vary
  2529. according to the family.
  2530. For @code{AF_INET} the arguments are an IPv4 network address number
  2531. (@pxref{Network Address Conversion}), and a port number.
  2532. For @code{AF_INET6} the arguments are an IPv6 network address number
  2533. and a port number. Optional @var{flowinfo} and @var{scopeid}
  2534. arguments may be given (both integers, default 0).
  2535. For @code{AF_UNIX} the argument is a filename (a string).
  2536. The C function @code{scm_make_socket_address} takes the @var{family}
  2537. and @var{address} arguments directly, then @var{arglist} is a list of
  2538. further arguments, being the port for IPv4, port and optional flowinfo
  2539. and scopeid for IPv6, or the empty list @code{SCM_EOL} for Unix
  2540. domain.
  2541. @end deffn
  2542. @noindent
  2543. The following functions access the fields of a socket address object,
  2544. @deffn {Scheme Procedure} sockaddr:fam sa
  2545. Return the address family from socket address object @var{sa}. This
  2546. is one of the @code{AF} constants (e.g.@: @code{AF_INET}).
  2547. @end deffn
  2548. @deffn {Scheme Procedure} sockaddr:path sa
  2549. For an @code{AF_UNIX} socket address object @var{sa}, return the
  2550. filename.
  2551. @end deffn
  2552. @deffn {Scheme Procedure} sockaddr:addr sa
  2553. For an @code{AF_INET} or @code{AF_INET6} socket address object
  2554. @var{sa}, return the network address number.
  2555. @end deffn
  2556. @deffn {Scheme Procedure} sockaddr:port sa
  2557. For an @code{AF_INET} or @code{AF_INET6} socket address object
  2558. @var{sa}, return the port number.
  2559. @end deffn
  2560. @deffn {Scheme Procedure} sockaddr:flowinfo sa
  2561. For an @code{AF_INET6} socket address object @var{sa}, return the
  2562. flowinfo value.
  2563. @end deffn
  2564. @deffn {Scheme Procedure} sockaddr:scopeid sa
  2565. For an @code{AF_INET6} socket address object @var{sa}, return the
  2566. scope ID value.
  2567. @end deffn
  2568. @tpindex @code{struct sockaddr}
  2569. @tpindex @code{sockaddr}
  2570. The functions below convert to and from the C @code{struct sockaddr}
  2571. (@pxref{Address Formats,,, libc, The GNU C Library Reference Manual}).
  2572. That structure is a generic type, an application can cast to or from
  2573. @code{struct sockaddr_in}, @code{struct sockaddr_in6} or @code{struct
  2574. sockaddr_un} according to the address family.
  2575. In a @code{struct sockaddr} taken or returned, the byte ordering in
  2576. the fields follows the C conventions (@pxref{Byte Order,, Byte Order
  2577. Conversion, libc, The GNU C Library Reference Manual}). This means
  2578. network byte order for @code{AF_INET} host address
  2579. (@code{sin_addr.s_addr}) and port number (@code{sin_port}), and
  2580. @code{AF_INET6} port number (@code{sin6_port}). But at the Scheme
  2581. level these values are taken or returned in host byte order, so the
  2582. port is an ordinary integer, and the host address likewise is an
  2583. ordinary integer (as described in @ref{Network Address Conversion}).
  2584. @deftypefn {C Function} {struct sockaddr *} scm_c_make_socket_address (SCM family, SCM address, SCM args, size_t *outsize)
  2585. Return a newly-@code{malloc}ed @code{struct sockaddr} created from
  2586. arguments like those taken by @code{scm_make_socket_address} above.
  2587. The size (in bytes) of the @code{struct sockaddr} return is stored
  2588. into @code{*@var{outsize}}. An application must call @code{free} to
  2589. release the returned structure when no longer required.
  2590. @end deftypefn
  2591. @deftypefn {C Function} SCM scm_from_sockaddr (const struct sockaddr *address, unsigned address_size)
  2592. Return a Scheme socket address object from the C @var{address}
  2593. structure. @var{address_size} is the size in bytes of @var{address}.
  2594. @end deftypefn
  2595. @deftypefn {C Function} {struct sockaddr *} scm_to_sockaddr (SCM address, size_t *address_size)
  2596. Return a newly-@code{malloc}ed @code{struct sockaddr} from a Scheme
  2597. level socket address object.
  2598. The size (in bytes) of the @code{struct sockaddr} return is stored
  2599. into @code{*@var{outsize}}. An application must call @code{free} to
  2600. release the returned structure when no longer required.
  2601. @end deftypefn
  2602. @node Network Sockets and Communication
  2603. @subsubsection Network Sockets and Communication
  2604. @cindex socket
  2605. @cindex network socket
  2606. Socket ports can be created using @code{socket} and @code{socketpair}.
  2607. The ports are initially unbuffered, to make reading and writing to the
  2608. same port more reliable. A buffer can be added to the port using
  2609. @code{setvbuf} (@pxref{Buffering}).
  2610. Most systems have limits on how many files and sockets can be open, so
  2611. it's strongly recommended that socket ports be closed explicitly when
  2612. no longer required (@pxref{Ports}).
  2613. Some of the underlying C functions take values in network byte order,
  2614. but the convention in Guile is that at the Scheme level everything is
  2615. ordinary host byte order and conversions are made automatically where
  2616. necessary.
  2617. @deffn {Scheme Procedure} socket family style proto
  2618. @deffnx {C Function} scm_socket (family, style, proto)
  2619. Return a new socket port of the type specified by @var{family},
  2620. @var{style} and @var{proto}. All three parameters are integers. The
  2621. possible values for @var{family} are as follows, where supported by
  2622. the system,
  2623. @defvar PF_UNIX
  2624. @defvarx PF_INET
  2625. @defvarx PF_INET6
  2626. @end defvar
  2627. The possible values for @var{style} are as follows, again where
  2628. supported by the system,
  2629. @defvar SOCK_STREAM
  2630. @defvarx SOCK_DGRAM
  2631. @defvarx SOCK_RAW
  2632. @defvarx SOCK_RDM
  2633. @defvarx SOCK_SEQPACKET
  2634. @end defvar
  2635. @var{proto} can be obtained from a protocol name using
  2636. @code{getprotobyname} (@pxref{Network Databases}). A value of zero
  2637. means the default protocol, which is usually right.
  2638. A socket cannot by used for communication until it has been connected
  2639. somewhere, usually with either @code{connect} or @code{accept} below.
  2640. @end deffn
  2641. @deffn {Scheme Procedure} socketpair family style proto
  2642. @deffnx {C Function} scm_socketpair (family, style, proto)
  2643. Return a pair, the @code{car} and @code{cdr} of which are two unnamed
  2644. socket ports connected to each other. The connection is full-duplex,
  2645. so data can be transferred in either direction between the two.
  2646. @var{family}, @var{style} and @var{proto} are as per @code{socket}
  2647. above. But many systems only support socket pairs in the
  2648. @code{PF_UNIX} family. Zero is likely to be the only meaningful value
  2649. for @var{proto}.
  2650. @end deffn
  2651. @deffn {Scheme Procedure} getsockopt sock level optname
  2652. @deffnx {Scheme Procedure} setsockopt sock level optname value
  2653. @deffnx {C Function} scm_getsockopt (sock, level, optname)
  2654. @deffnx {C Function} scm_setsockopt (sock, level, optname, value)
  2655. Get or set an option on socket port @var{sock}. @code{getsockopt}
  2656. returns the current value. @code{setsockopt} sets a value and the
  2657. return is unspecified.
  2658. @var{level} is an integer specifying a protocol layer, either
  2659. @code{SOL_SOCKET} for socket level options, or a protocol number from
  2660. the @code{IPPROTO} constants or @code{getprotoent} (@pxref{Network
  2661. Databases}).
  2662. @defvar SOL_SOCKET
  2663. @defvarx IPPROTO_IP
  2664. @defvarx IPPROTO_TCP
  2665. @defvarx IPPROTO_UDP
  2666. @end defvar
  2667. @var{optname} is an integer specifying an option within the protocol
  2668. layer.
  2669. For @code{SOL_SOCKET} level the following @var{optname}s are defined
  2670. (when provided by the system). For their meaning see
  2671. @ref{Socket-Level Options,,, libc, The GNU C Library Reference
  2672. Manual}, or @command{man 7 socket}.
  2673. @defvar SO_DEBUG
  2674. @defvarx SO_REUSEADDR
  2675. @defvarx SO_STYLE
  2676. @defvarx SO_TYPE
  2677. @defvarx SO_ERROR
  2678. @defvarx SO_DONTROUTE
  2679. @defvarx SO_BROADCAST
  2680. @defvarx SO_SNDBUF
  2681. @defvarx SO_RCVBUF
  2682. @defvarx SO_KEEPALIVE
  2683. @defvarx SO_OOBINLINE
  2684. @defvarx SO_NO_CHECK
  2685. @defvarx SO_PRIORITY
  2686. @defvarx SO_REUSEPORT
  2687. The @var{value} taken or returned is an integer.
  2688. @end defvar
  2689. @defvar SO_LINGER
  2690. The @var{value} taken or returned is a pair of integers
  2691. @code{(@var{ENABLE} . @var{TIMEOUT})}. On old systems without timeout
  2692. support (ie.@: without @code{struct linger}), only @var{ENABLE} has an
  2693. effect but the value in Guile is always a pair.
  2694. @end defvar
  2695. @c Note that we refer only to ``man ip'' here. On GNU/Linux it's
  2696. @c ``man 7 ip'' but on NetBSD it's ``man 4 ip''.
  2697. @c
  2698. For IP level (@code{IPPROTO_IP}) the following @var{optname}s are
  2699. defined (when provided by the system). See @command{man ip} for what
  2700. they mean.
  2701. @defvar IP_MULTICAST_IF
  2702. This sets the source interface used by multicast traffic.
  2703. @end defvar
  2704. @defvar IP_MULTICAST_TTL
  2705. This sets the default TTL for multicast traffic. This defaults
  2706. to 1 and should be increased to allow traffic to pass beyond the
  2707. local network.
  2708. @end defvar
  2709. @defvar IP_ADD_MEMBERSHIP
  2710. @defvarx IP_DROP_MEMBERSHIP
  2711. These can be used only with @code{setsockopt}, not @code{getsockopt}.
  2712. @var{value} is a pair @code{(@var{MULTIADDR} . @var{INTERFACEADDR})}
  2713. of integer IPv4 addresses (@pxref{Network Address Conversion}).
  2714. @var{MULTIADDR} is a multicast address to be added to or dropped from
  2715. the interface @var{INTERFACEADDR}. @var{INTERFACEADDR} can be
  2716. @code{INADDR_ANY} to have the system select the interface.
  2717. @var{INTERFACEADDR} can also be an interface index number, on systems
  2718. supporting that.
  2719. @end defvar
  2720. @end deffn
  2721. For @code{IPPROTO_TCP} level the following @var{optname}s are defined
  2722. (when provided by the system). For their meaning see @command{man 7
  2723. tcp}.
  2724. @defvar TCP_NODELAY
  2725. @defvarx TCP_CORK
  2726. The @var{value} taken or returned is an integer.
  2727. @end defvar
  2728. @deffn {Scheme Procedure} shutdown sock how
  2729. @deffnx {C Function} scm_shutdown (sock, how)
  2730. Sockets can be closed simply by using @code{close-port}. The
  2731. @code{shutdown} procedure allows reception or transmission on a
  2732. connection to be shut down individually, according to the parameter
  2733. @var{how}:
  2734. @table @asis
  2735. @item 0
  2736. Stop receiving data for this socket. If further data arrives, reject it.
  2737. @item 1
  2738. Stop trying to transmit data from this socket. Discard any
  2739. data waiting to be sent. Stop looking for acknowledgement of
  2740. data already sent; don't retransmit it if it is lost.
  2741. @item 2
  2742. Stop both reception and transmission.
  2743. @end table
  2744. The return value is unspecified.
  2745. @end deffn
  2746. @deffn {Scheme Procedure} connect sock sockaddr
  2747. @deffnx {Scheme Procedure} connect sock AF_INET ipv4addr port
  2748. @deffnx {Scheme Procedure} connect sock AF_INET6 ipv6addr port [flowinfo [scopeid]]
  2749. @deffnx {Scheme Procedure} connect sock AF_UNIX path
  2750. @deffnx {C Function} scm_connect (sock, fam, address, args)
  2751. Initiate a connection on socket port @var{sock} to a given address. The
  2752. destination is either a socket address object, or arguments the same as
  2753. @code{make-socket-address} would take to make such an object
  2754. (@pxref{Network Socket Address}). Return true unless the socket was
  2755. configured as non-blocking and the connection could not be made
  2756. immediately.
  2757. @example
  2758. (connect sock AF_INET INADDR_LOOPBACK 23)
  2759. (connect sock (make-socket-address AF_INET INADDR_LOOPBACK 23))
  2760. @end example
  2761. @end deffn
  2762. @deffn {Scheme Procedure} bind sock sockaddr
  2763. @deffnx {Scheme Procedure} bind sock AF_INET ipv4addr port
  2764. @deffnx {Scheme Procedure} bind sock AF_INET6 ipv6addr port [flowinfo [scopeid]]
  2765. @deffnx {Scheme Procedure} bind sock AF_UNIX path
  2766. @deffnx {C Function} scm_bind (sock, fam, address, args)
  2767. Bind socket port @var{sock} to the given address. The address is
  2768. either a socket address object, or arguments the same as
  2769. @code{make-socket-address} would take to make such an object
  2770. (@pxref{Network Socket Address}). The return value is unspecified.
  2771. Generally a socket is only explicitly bound to a particular address
  2772. when making a server, i.e.@: to listen on a particular port. For an
  2773. outgoing connection the system will assign a local address
  2774. automatically, if not already bound.
  2775. @example
  2776. (bind sock AF_INET INADDR_ANY 12345)
  2777. (bind sock (make-socket-address AF_INET INADDR_ANY 12345))
  2778. @end example
  2779. @end deffn
  2780. @deffn {Scheme Procedure} listen sock backlog
  2781. @deffnx {C Function} scm_listen (sock, backlog)
  2782. Enable @var{sock} to accept connection
  2783. requests. @var{backlog} is an integer specifying
  2784. the maximum length of the queue for pending connections.
  2785. If the queue fills, new clients will fail to connect until
  2786. the server calls @code{accept} to accept a connection from
  2787. the queue.
  2788. The return value is unspecified.
  2789. @end deffn
  2790. @deffn {Scheme Procedure} accept sock [flags]
  2791. @deffnx {C Function} scm_accept (sock)
  2792. Accept a connection from socket port @var{sock} which has been enabled
  2793. for listening with @code{listen} above.
  2794. If there are no incoming connections in the queue, there are two
  2795. possible behaviors, depending on whether @var{sock} has been configured
  2796. for non-blocking operation or not:
  2797. @itemize
  2798. @item
  2799. If there is no connection waiting and the socket was set to non-blocking
  2800. mode with the @code{O_NONBLOCK} port option (@pxref{Ports and File
  2801. Descriptors,@code{fcntl}}), return @code{#f} directly.
  2802. @item
  2803. Otherwise wait until a connection is available.
  2804. @end itemize
  2805. The return value is a pair. The @code{car} is a new socket port,
  2806. connected and ready to communicate. The @code{cdr} is a socket address
  2807. object (@pxref{Network Socket Address}) which is where the remote
  2808. connection is from (like @code{getpeername} below).
  2809. @var{flags}, if given, may include @code{SOCK_CLOEXEC} or
  2810. @code{SOCK_NONBLOCK}, which like @code{O_CLOEXEC} and @code{O_NONBLOCK}
  2811. apply to the newly accepted socket.
  2812. All communication takes place using the new socket returned. The
  2813. given @var{sock} remains bound and listening, and @code{accept} may be
  2814. called on it again to get another incoming connection when desired.
  2815. @end deffn
  2816. @deffn {Scheme Procedure} getsockname sock
  2817. @deffnx {C Function} scm_getsockname (sock)
  2818. Return a socket address object which is the where @var{sock} is bound
  2819. locally. @var{sock} may have obtained its local address from
  2820. @code{bind} (above), or if a @code{connect} is done with an otherwise
  2821. unbound socket (which is usual) then the system will have assigned an
  2822. address.
  2823. Note that on many systems the address of a socket in the
  2824. @code{AF_UNIX} namespace cannot be read.
  2825. @end deffn
  2826. @deffn {Scheme Procedure} getpeername sock
  2827. @deffnx {C Function} scm_getpeername (sock)
  2828. Return a socket address object which is where @var{sock} is connected
  2829. to, i.e.@: the remote endpoint.
  2830. Note that on many systems the address of a socket in the
  2831. @code{AF_UNIX} namespace cannot be read.
  2832. @end deffn
  2833. @deffn {Scheme Procedure} recv! sock buf [flags]
  2834. @deffnx {C Function} scm_recv (sock, buf, flags)
  2835. Receive data from a socket port.
  2836. @var{sock} must already
  2837. be bound to the address from which data is to be received.
  2838. @var{buf} is a bytevector into which
  2839. the data will be written. The size of @var{buf} limits
  2840. the amount of
  2841. data which can be received: in the case of packet
  2842. protocols, if a packet larger than this limit is encountered
  2843. then some data
  2844. will be irrevocably lost.
  2845. @vindex MSG_OOB
  2846. @vindex MSG_PEEK
  2847. @vindex MSG_DONTROUTE
  2848. The optional @var{flags} argument is a value or bitwise OR of
  2849. @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
  2850. The value returned is the number of bytes read from the
  2851. socket.
  2852. Note that the data is read directly from the socket file
  2853. descriptor:
  2854. any unread buffered port data is ignored.
  2855. @end deffn
  2856. @deffn {Scheme Procedure} send sock message [flags]
  2857. @deffnx {C Function} scm_send (sock, message, flags)
  2858. @vindex MSG_OOB
  2859. @vindex MSG_PEEK
  2860. @vindex MSG_DONTROUTE
  2861. Transmit bytevector @var{message} on socket port @var{sock}.
  2862. @var{sock} must already be bound to a destination address. The value
  2863. returned is the number of bytes transmitted---it's possible for this
  2864. to be less than the length of @var{message} if the socket is set to be
  2865. non-blocking. The optional @var{flags} argument is a value or bitwise
  2866. OR of @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
  2867. Note that the data is written directly to the socket
  2868. file descriptor:
  2869. any unflushed buffered port data is ignored.
  2870. @end deffn
  2871. @deffn {Scheme Procedure} recvfrom! sock buf [flags [start [end]]]
  2872. @deffnx {C Function} scm_recvfrom (sock, buf, flags, start, end)
  2873. Receive data from socket port @var{sock}, returning the originating
  2874. address as well as the data. This function is usually for datagram
  2875. sockets, but can be used on stream-oriented sockets too.
  2876. The data received is stored in bytevector @var{buf}, using
  2877. either the whole bytevector or just the region between the optional
  2878. @var{start} and @var{end} positions. The size of @var{buf}
  2879. limits the amount of data that can be received. For datagram
  2880. protocols if a packet larger than this is received then excess
  2881. bytes are irrevocably lost.
  2882. The return value is a pair. The @code{car} is the number of bytes
  2883. read. The @code{cdr} is a socket address object (@pxref{Network
  2884. Socket Address}) which is where the data came from, or @code{#f} if
  2885. the origin is unknown.
  2886. @vindex MSG_OOB
  2887. @vindex MSG_PEEK
  2888. @vindex MSG_DONTROUTE
  2889. The optional @var{flags} argument is a or bitwise-OR (@code{logior})
  2890. of @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
  2891. Data is read directly from the socket file descriptor, any buffered
  2892. port data is ignored.
  2893. @c This was linux kernel 2.6.15 and glibc 2.3.6, not sure what any
  2894. @c specs are supposed to say about recvfrom threading.
  2895. @c
  2896. On a GNU/Linux system @code{recvfrom!} is not multi-threading, all
  2897. threads stop while a @code{recvfrom!} call is in progress. An
  2898. application may need to use @code{select}, @code{O_NONBLOCK} or
  2899. @code{MSG_DONTWAIT} to avoid this.
  2900. @end deffn
  2901. @deffn {Scheme Procedure} sendto sock message sockaddr [flags]
  2902. @deffnx {Scheme Procedure} sendto sock message AF_INET ipv4addr port [flags]
  2903. @deffnx {Scheme Procedure} sendto sock message AF_INET6 ipv6addr port [flowinfo [scopeid [flags]]]
  2904. @deffnx {Scheme Procedure} sendto sock message AF_UNIX path [flags]
  2905. @deffnx {C Function} scm_sendto (sock, message, fam, address, args_and_flags)
  2906. Transmit bytevector @var{message} as a datagram socket port
  2907. @var{sock}. The destination is specified either as a socket address
  2908. object, or as arguments the same as would be taken by
  2909. @code{make-socket-address} to create such an object (@pxref{Network
  2910. Socket Address}).
  2911. The destination address may be followed by an optional @var{flags}
  2912. argument which is a @code{logior} (@pxref{Bitwise Operations}) of
  2913. @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
  2914. The value returned is the number of bytes transmitted --
  2915. it's possible for
  2916. this to be less than the length of @var{message} if the
  2917. socket is
  2918. set to be non-blocking.
  2919. Note that the data is written directly to the socket
  2920. file descriptor:
  2921. any unflushed buffered port data is ignored.
  2922. @end deffn
  2923. @node Internet Socket Examples
  2924. @subsubsection Network Socket Examples
  2925. @cindex network examples
  2926. @cindex socket examples
  2927. The following give examples of how to use network sockets.
  2928. @subsubheading Internet Socket Client Example
  2929. @cindex socket client example
  2930. The following example demonstrates an Internet socket client.
  2931. It connects to the HTTP daemon running on the local machine and
  2932. returns the contents of the root index URL.
  2933. @example
  2934. (let ((s (socket PF_INET SOCK_STREAM 0)))
  2935. (connect s AF_INET (inet-pton AF_INET "127.0.0.1") 80)
  2936. (display "GET / HTTP/1.0\r\n\r\n" s)
  2937. (do ((line (read-line s) (read-line s)))
  2938. ((eof-object? line))
  2939. (display line)
  2940. (newline)))
  2941. @end example
  2942. @subsubheading Internet Socket Server Example
  2943. @cindex socket server example
  2944. The following example shows a simple Internet server which listens on
  2945. port 2904 for incoming connections and sends a greeting back to the
  2946. client.
  2947. @example
  2948. (let ((s (socket PF_INET SOCK_STREAM 0)))
  2949. (setsockopt s SOL_SOCKET SO_REUSEADDR 1)
  2950. ;; @r{Specific address?}
  2951. ;; @r{(bind s AF_INET (inet-pton AF_INET "127.0.0.1") 2904)}
  2952. (bind s AF_INET INADDR_ANY 2904)
  2953. (listen s 5)
  2954. (simple-format #t "Listening for clients in pid: ~S" (getpid))
  2955. (newline)
  2956. (while #t
  2957. (let* ((client-connection (accept s))
  2958. (client-details (cdr client-connection))
  2959. (client (car client-connection)))
  2960. (simple-format #t "Got new client connection: ~S"
  2961. client-details)
  2962. (newline)
  2963. (simple-format #t "Client address: ~S"
  2964. (gethostbyaddr
  2965. (sockaddr:addr client-details)))
  2966. (newline)
  2967. ;; @r{Send back the greeting to the client port}
  2968. (display "Hello client\r\n" client)
  2969. (close client))))
  2970. @end example
  2971. @node System Identification
  2972. @subsection System Identification
  2973. @cindex system name
  2974. This section lists the various procedures Guile provides for accessing
  2975. information about the system it runs on.
  2976. @deffn {Scheme Procedure} uname
  2977. @deffnx {C Function} scm_uname ()
  2978. Return an object with some information about the computer
  2979. system the program is running on.
  2980. The following procedures accept an object as returned by @code{uname}
  2981. and return a selected component (all of which are strings).
  2982. @deffn {Scheme Procedure} utsname:sysname un
  2983. The name of the operating system.
  2984. @end deffn
  2985. @deffn {Scheme Procedure} utsname:nodename un
  2986. The network name of the computer.
  2987. @end deffn
  2988. @deffn {Scheme Procedure} utsname:release un
  2989. The current release level of the operating system implementation.
  2990. @end deffn
  2991. @deffn {Scheme Procedure} utsname:version un
  2992. The current version level within the release of the operating system.
  2993. @end deffn
  2994. @deffn {Scheme Procedure} utsname:machine un
  2995. A description of the hardware.
  2996. @end deffn
  2997. @end deffn
  2998. @deffn {Scheme Procedure} gethostname
  2999. @deffnx {C Function} scm_gethostname ()
  3000. @cindex host name
  3001. Return the host name of the current processor.
  3002. @end deffn
  3003. @deffn {Scheme Procedure} sethostname name
  3004. @deffnx {C Function} scm_sethostname (name)
  3005. Set the host name of the current processor to @var{name}. May
  3006. only be used by the superuser. The return value is not
  3007. specified.
  3008. @end deffn
  3009. @node Locales
  3010. @subsection Locales
  3011. @cindex locale
  3012. @deffn {Scheme Procedure} setlocale category [locale]
  3013. @deffnx {C Function} scm_setlocale (category, locale)
  3014. Get or set the current locale, used for various internationalizations.
  3015. Locales are strings, such as @samp{sv_SE}.
  3016. If @var{locale} is given then the locale for the given @var{category}
  3017. is set and the new value returned. If @var{locale} is not given then
  3018. the current value is returned. @var{category} should be one of the
  3019. following values (@pxref{Locale Categories, Categories of Activities
  3020. that Locales Affect,, libc, The GNU C Library Reference Manual}):
  3021. @defvar LC_ALL
  3022. @defvarx LC_COLLATE
  3023. @defvarx LC_CTYPE
  3024. @defvarx LC_MESSAGES
  3025. @defvarx LC_MONETARY
  3026. @defvarx LC_NUMERIC
  3027. @defvarx LC_TIME
  3028. @end defvar
  3029. @cindex @code{LANG}
  3030. A common usage is @samp{(setlocale LC_ALL "")}, which initializes all
  3031. categories based on standard environment variables (@code{LANG} etc).
  3032. For full details on categories and locale names @pxref{Locales,,
  3033. Locales and Internationalization, libc, The GNU C Library Reference
  3034. Manual}.
  3035. Note that @code{setlocale} affects locale settings for the whole
  3036. process. @xref{i18n Introduction, locale objects and
  3037. @code{make-locale}}, for a thread-safe alternative.
  3038. @end deffn
  3039. @node Encryption
  3040. @subsection Encryption
  3041. @cindex encryption
  3042. Please note that the procedures in this section are not suited for
  3043. strong encryption, they are only interfaces to the well-known and
  3044. common system library functions of the same name. They are just as good
  3045. (or bad) as the underlying functions, so you should refer to your system
  3046. documentation before using them (@pxref{crypt,, Encrypting Passwords,
  3047. libc, The GNU C Library Reference Manual}).
  3048. @deffn {Scheme Procedure} crypt key salt
  3049. @deffnx {C Function} scm_crypt (key, salt)
  3050. Encrypt @var{key}, with the addition of @var{salt} (both strings),
  3051. using the @code{crypt} C library call.
  3052. @end deffn
  3053. Although @code{getpass} is not an encryption procedure per se, it
  3054. appears here because it is often used in combination with @code{crypt}:
  3055. @deffn {Scheme Procedure} getpass prompt
  3056. @deffnx {C Function} scm_getpass (prompt)
  3057. @cindex password
  3058. Display @var{prompt} to the standard error output and read
  3059. a password from @file{/dev/tty}. If this file is not
  3060. accessible, it reads from standard input. The password may be
  3061. up to 127 characters in length. Additional characters and the
  3062. terminating newline character are discarded. While reading
  3063. the password, echoing and the generation of signals by special
  3064. characters is disabled.
  3065. @end deffn
  3066. @c Local Variables:
  3067. @c TeX-master: "guile.texi"
  3068. @c End: