posix.texi 117 KB

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