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