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