posix.texi 124 KB

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