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