posix.texi 139 KB

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