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