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