posix.texi 130 KB

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