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