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