Commit | Line | Data |
---|---|---|
2da09c3f MV |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Guile Reference Manual. | |
55ae00ea | 3 | @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010 |
2da09c3f MV |
4 | @c Free Software Foundation, Inc. |
5 | @c See the file guile.texi for copying conditions. | |
6 | ||
a0e07ba4 | 7 | @node POSIX |
3229f68b | 8 | @section @acronym{POSIX} System Calls and Networking |
f3dfb8ac | 9 | @cindex POSIX |
a0e07ba4 NJ |
10 | |
11 | @menu | |
12 | * Conventions:: Conventions employed by the POSIX interface. | |
13 | * Ports and File Descriptors:: Scheme ``ports'' and Unix file descriptors | |
7381c1de | 14 | have different representations. |
a0e07ba4 NJ |
15 | * File System:: stat, chown, chmod, etc. |
16 | * User Information:: Retrieving a user's GECOS (/etc/passwd) entry. | |
17 | * Time:: gettimeofday, localtime, strftime, etc. | |
18 | * Runtime Environment:: Accessing and modifying Guile's environment. | |
19 | * Processes:: getuid, getpid, etc. | |
20 | * Signals:: sigaction, kill, pause, alarm, setitimer, etc. | |
21 | * Terminals and Ptys:: ttyname, tcsetpgrp, etc. | |
22 | * Pipes:: Communicating data between processes. | |
23 | * Networking:: gethostbyaddr, getnetent, socket, bind, listen. | |
24 | * System Identification:: Obtaining information about the system. | |
25 | * Locales:: setlocale, etc. | |
5f378d17 | 26 | * Encryption:: |
a0e07ba4 NJ |
27 | @end menu |
28 | ||
29 | @node Conventions | |
3229f68b | 30 | @subsection @acronym{POSIX} Interface Conventions |
a0e07ba4 NJ |
31 | |
32 | These interfaces provide access to operating system facilities. | |
33 | They provide a simple wrapping around the underlying C interfaces | |
34 | to make usage from Scheme more convenient. They are also used | |
7403e409 | 35 | to implement the Guile port of scsh (@pxref{The Scheme shell (scsh)}). |
a0e07ba4 NJ |
36 | |
37 | Generally there is a single procedure for each corresponding Unix | |
38 | facility. There are some exceptions, such as procedures implemented for | |
39 | speed and convenience in Scheme with no primitive Unix equivalent, | |
7403e409 | 40 | e.g.@: @code{copy-file}. |
a0e07ba4 NJ |
41 | |
42 | The interfaces are intended as far as possible to be portable across | |
43 | different versions of Unix. In some cases procedures which can't be | |
44 | implemented on particular systems may become no-ops, or perform limited | |
45 | actions. In other cases they may throw errors. | |
46 | ||
47 | General naming conventions are as follows: | |
48 | ||
49 | @itemize @bullet | |
50 | @item | |
51 | The Scheme name is often identical to the name of the underlying Unix | |
52 | facility. | |
53 | @item | |
54 | Underscores in Unix procedure names are converted to hyphens. | |
55 | @item | |
85a9b4ed | 56 | Procedures which destructively modify Scheme data have exclamation |
a0e07ba4 NJ |
57 | marks appended, e.g., @code{recv!}. |
58 | @item | |
59 | Predicates (returning only @code{#t} or @code{#f}) have question marks | |
60 | appended, e.g., @code{access?}. | |
61 | @item | |
62 | Some names are changed to avoid conflict with dissimilar interfaces | |
63 | defined by scsh, e.g., @code{primitive-fork}. | |
64 | @item | |
65 | Unix preprocessor names such as @code{EPERM} or @code{R_OK} are converted | |
66 | to Scheme variables of the same name (underscores are not replaced | |
67 | with hyphens). | |
68 | @end itemize | |
69 | ||
70 | Unexpected conditions are generally handled by raising exceptions. | |
71 | There are a few procedures which return a special value if they don't | |
72 | succeed, e.g., @code{getenv} returns @code{#f} if it the requested | |
73 | string is not found in the environment. These cases are noted in | |
74 | the documentation. | |
75 | ||
7403e409 | 76 | For ways to deal with exceptions, see @ref{Exceptions}. |
a0e07ba4 | 77 | |
f3dfb8ac | 78 | @cindex @code{errno} |
7403e409 | 79 | Errors which the C library would report by returning a null pointer or |
a0e07ba4 | 80 | through some other means are reported by raising a @code{system-error} |
5982a8e0 KR |
81 | exception with @code{scm-error} (@pxref{Error Reporting}). The |
82 | @var{data} parameter is a list containing the Unix @code{errno} value | |
83 | (an integer). For example, | |
a0e07ba4 | 84 | |
5982a8e0 KR |
85 | @example |
86 | (define (my-handler key func fmt fmtargs data) | |
87 | (display key) (newline) | |
88 | (display func) (newline) | |
89 | (apply format #t fmt fmtargs) (newline) | |
90 | (display data) (newline)) | |
91 | ||
92 | (catch 'system-error | |
93 | (lambda () (dup2 -123 -456)) | |
94 | my-handler) | |
95 | ||
96 | @print{} | |
97 | system-error | |
98 | dup2 | |
99 | Bad file descriptor | |
100 | (9) | |
101 | @end example | |
102 | ||
103 | ||
104 | @sp 1 | |
105 | @defun system-error-errno arglist | |
f3dfb8ac | 106 | @cindex @code{errno} |
5982a8e0 KR |
107 | Return the @code{errno} value from a list which is the arguments to an |
108 | exception handler. If the exception is not a @code{system-error}, | |
109 | then the return is @code{#f}. For example, | |
a0e07ba4 NJ |
110 | |
111 | @example | |
112 | (catch | |
113 | 'system-error | |
114 | (lambda () | |
115 | (mkdir "/this-ought-to-fail-if-I'm-not-root")) | |
116 | (lambda stuff | |
117 | (let ((errno (system-error-errno stuff))) | |
118 | (cond | |
119 | ((= errno EACCES) | |
120 | (display "You're not allowed to do that.")) | |
121 | ((= errno EEXIST) | |
122 | (display "Already exists.")) | |
123 | (#t | |
124 | (display (strerror errno)))) | |
125 | (newline)))) | |
126 | @end example | |
5982a8e0 KR |
127 | @end defun |
128 | ||
a0e07ba4 NJ |
129 | |
130 | @node Ports and File Descriptors | |
3229f68b | 131 | @subsection Ports and File Descriptors |
f3dfb8ac | 132 | @cindex file descriptor |
a0e07ba4 NJ |
133 | |
134 | Conventions generally follow those of scsh, @ref{The Scheme shell (scsh)}. | |
135 | ||
136 | File ports are implemented using low-level operating system I/O | |
7403e409 NJ |
137 | facilities, with optional buffering to improve efficiency; see |
138 | @ref{File Ports}. | |
a0e07ba4 NJ |
139 | |
140 | Note that some procedures (e.g., @code{recv!}) will accept ports as | |
141 | arguments, but will actually operate directly on the file descriptor | |
142 | underlying the port. Any port buffering is ignored, including the | |
143 | buffer which implements @code{peek-char} and @code{unread-char}. | |
144 | ||
145 | The @code{force-output} and @code{drain-input} procedures can be used | |
146 | to clear the buffers. | |
147 | ||
148 | Each open file port has an associated operating system file descriptor. | |
149 | File descriptors are generally not useful in Scheme programs; however | |
150 | they may be needed when interfacing with foreign code and the Unix | |
151 | environment. | |
152 | ||
153 | A file descriptor can be extracted from a port and a new port can be | |
154 | created from a file descriptor. However a file descriptor is just an | |
85a9b4ed | 155 | integer and the garbage collector doesn't recognize it as a reference |
a0e07ba4 NJ |
156 | to the port. If all other references to the port were dropped, then |
157 | it's likely that the garbage collector would free the port, with the | |
158 | side-effect of closing the file descriptor prematurely. | |
159 | ||
160 | To assist the programmer in avoiding this problem, each port has an | |
7403e409 | 161 | associated @dfn{revealed count} which can be used to keep track of how many |
a0e07ba4 NJ |
162 | times the underlying file descriptor has been stored in other places. |
163 | If a port's revealed count is greater than zero, the file descriptor | |
85a9b4ed | 164 | will not be closed when the port is garbage collected. A programmer |
a0e07ba4 NJ |
165 | can therefore ensure that the revealed count will be greater than |
166 | zero if the file descriptor is needed elsewhere. | |
167 | ||
7403e409 | 168 | For the simple case where a file descriptor is ``imported'' once to become |
a0e07ba4 NJ |
169 | a port, it does not matter if the file descriptor is closed when the |
170 | port is garbage collected. There is no need to maintain a revealed | |
7403e409 | 171 | count. Likewise when ``exporting'' a file descriptor to the external |
a0e07ba4 NJ |
172 | environment, setting the revealed count is not required provided the |
173 | port is kept open (i.e., is pointed to by a live Scheme binding) while | |
174 | the file descriptor is in use. | |
175 | ||
7403e409 NJ |
176 | To correspond with traditional Unix behaviour, three file descriptors |
177 | (0, 1, and 2) are automatically imported when a program starts up and | |
178 | assigned to the initial values of the current/standard input, output, | |
179 | and error ports, respectively. The revealed count for each is | |
180 | initially set to one, so that dropping references to one of these | |
181 | ports will not result in its garbage collection: it could be retrieved | |
182 | with @code{fdopen} or @code{fdes->ports}. | |
a0e07ba4 | 183 | |
8f85c0c6 NJ |
184 | @deffn {Scheme Procedure} port-revealed port |
185 | @deffnx {C Function} scm_port_revealed (port) | |
a0e07ba4 NJ |
186 | Return the revealed count for @var{port}. |
187 | @end deffn | |
188 | ||
8f85c0c6 NJ |
189 | @deffn {Scheme Procedure} set-port-revealed! port rcount |
190 | @deffnx {C Function} scm_set_port_revealed_x (port, rcount) | |
7403e409 | 191 | Sets the revealed count for a @var{port} to @var{rcount}. |
a0e07ba4 NJ |
192 | The return value is unspecified. |
193 | @end deffn | |
194 | ||
8f85c0c6 NJ |
195 | @deffn {Scheme Procedure} fileno port |
196 | @deffnx {C Function} scm_fileno (port) | |
a0e07ba4 NJ |
197 | Return the integer file descriptor underlying @var{port}. Does |
198 | not change its revealed count. | |
199 | @end deffn | |
200 | ||
8f85c0c6 | 201 | @deffn {Scheme Procedure} port->fdes port |
a0e07ba4 NJ |
202 | Returns the integer file descriptor underlying @var{port}. As a |
203 | side effect the revealed count of @var{port} is incremented. | |
204 | @end deffn | |
205 | ||
8f85c0c6 NJ |
206 | @deffn {Scheme Procedure} fdopen fdes modes |
207 | @deffnx {C Function} scm_fdopen (fdes, modes) | |
7403e409 NJ |
208 | Return a new port based on the file descriptor @var{fdes}. Modes are |
209 | given by the string @var{modes}. The revealed count of the port is | |
210 | initialized to zero. The @var{modes} string is the same as that | |
211 | accepted by @code{open-file} (@pxref{File Ports, open-file}). | |
a0e07ba4 NJ |
212 | @end deffn |
213 | ||
8f85c0c6 NJ |
214 | @deffn {Scheme Procedure} fdes->ports fd |
215 | @deffnx {C Function} scm_fdes_to_ports (fd) | |
a0e07ba4 NJ |
216 | Return a list of existing ports which have @var{fdes} as an |
217 | underlying file descriptor, without changing their revealed | |
218 | counts. | |
219 | @end deffn | |
220 | ||
8f85c0c6 | 221 | @deffn {Scheme Procedure} fdes->inport fdes |
a0e07ba4 NJ |
222 | Returns an existing input port which has @var{fdes} as its underlying file |
223 | descriptor, if one exists, and increments its revealed count. | |
224 | Otherwise, returns a new input port with a revealed count of 1. | |
225 | @end deffn | |
226 | ||
8f85c0c6 | 227 | @deffn {Scheme Procedure} fdes->outport fdes |
a0e07ba4 NJ |
228 | Returns an existing output port which has @var{fdes} as its underlying file |
229 | descriptor, if one exists, and increments its revealed count. | |
230 | Otherwise, returns a new output port with a revealed count of 1. | |
231 | @end deffn | |
232 | ||
8f85c0c6 NJ |
233 | @deffn {Scheme Procedure} primitive-move->fdes port fd |
234 | @deffnx {C Function} scm_primitive_move_to_fdes (port, fd) | |
a0e07ba4 NJ |
235 | Moves the underlying file descriptor for @var{port} to the integer |
236 | value @var{fdes} without changing the revealed count of @var{port}. | |
237 | Any other ports already using this descriptor will be automatically | |
238 | shifted to new descriptors and their revealed counts reset to zero. | |
239 | The return value is @code{#f} if the file descriptor already had the | |
240 | required value or @code{#t} if it was moved. | |
241 | @end deffn | |
242 | ||
8f85c0c6 | 243 | @deffn {Scheme Procedure} move->fdes port fdes |
a0e07ba4 NJ |
244 | Moves the underlying file descriptor for @var{port} to the integer |
245 | value @var{fdes} and sets its revealed count to one. Any other ports | |
246 | already using this descriptor will be automatically | |
247 | shifted to new descriptors and their revealed counts reset to zero. | |
248 | The return value is unspecified. | |
249 | @end deffn | |
250 | ||
8f85c0c6 | 251 | @deffn {Scheme Procedure} release-port-handle port |
a0e07ba4 NJ |
252 | Decrements the revealed count for a port. |
253 | @end deffn | |
254 | ||
8f85c0c6 NJ |
255 | @deffn {Scheme Procedure} fsync object |
256 | @deffnx {C Function} scm_fsync (object) | |
a0e07ba4 NJ |
257 | Copies any unwritten data for the specified output file descriptor to disk. |
258 | If @var{port/fd} is a port, its buffer is flushed before the underlying | |
259 | file descriptor is fsync'd. | |
260 | The return value is unspecified. | |
261 | @end deffn | |
262 | ||
8f85c0c6 NJ |
263 | @deffn {Scheme Procedure} open path flags [mode] |
264 | @deffnx {C Function} scm_open (path, flags, mode) | |
a0e07ba4 NJ |
265 | Open the file named by @var{path} for reading and/or writing. |
266 | @var{flags} is an integer specifying how the file should be opened. | |
7403e409 NJ |
267 | @var{mode} is an integer specifying the permission bits of the file, |
268 | if it needs to be created, before the umask (@pxref{Processes}) is | |
269 | applied. The default is 666 (Unix itself has no default). | |
a0e07ba4 NJ |
270 | |
271 | @var{flags} can be constructed by combining variables using @code{logior}. | |
272 | Basic flags are: | |
273 | ||
274 | @defvar O_RDONLY | |
275 | Open the file read-only. | |
276 | @end defvar | |
277 | @defvar O_WRONLY | |
278 | Open the file write-only. | |
279 | @end defvar | |
280 | @defvar O_RDWR | |
281 | Open the file read/write. | |
282 | @end defvar | |
283 | @defvar O_APPEND | |
284 | Append to the file instead of truncating. | |
285 | @end defvar | |
286 | @defvar O_CREAT | |
287 | Create the file if it does not already exist. | |
288 | @end defvar | |
289 | ||
7403e409 | 290 | @xref{File Status Flags,,,libc,The GNU C Library Reference Manual}, |
a0e07ba4 NJ |
291 | for additional flags. |
292 | @end deffn | |
293 | ||
8f85c0c6 NJ |
294 | @deffn {Scheme Procedure} open-fdes path flags [mode] |
295 | @deffnx {C Function} scm_open_fdes (path, flags, mode) | |
a0e07ba4 NJ |
296 | Similar to @code{open} but return a file descriptor instead of |
297 | a port. | |
298 | @end deffn | |
299 | ||
8f85c0c6 NJ |
300 | @deffn {Scheme Procedure} close fd_or_port |
301 | @deffnx {C Function} scm_close (fd_or_port) | |
7403e409 | 302 | Similar to @code{close-port} (@pxref{Closing, close-port}), |
8f85c0c6 NJ |
303 | but also works on file descriptors. A side |
304 | effect of closing a file descriptor is that any ports using that file | |
305 | descriptor are moved to a different file descriptor and have | |
306 | their revealed counts set to zero. | |
a0e07ba4 NJ |
307 | @end deffn |
308 | ||
8f85c0c6 NJ |
309 | @deffn {Scheme Procedure} close-fdes fd |
310 | @deffnx {C Function} scm_close_fdes (fd) | |
7403e409 NJ |
311 | A simple wrapper for the @code{close} system call. Close file |
312 | descriptor @var{fd}, which must be an integer. Unlike @code{close}, | |
313 | the file descriptor will be closed even if a port is using it. The | |
314 | return value is unspecified. | |
a0e07ba4 NJ |
315 | @end deffn |
316 | ||
8f85c0c6 | 317 | @deffn {Scheme Procedure} unread-char char [port] |
c16da59f | 318 | @deffnx {C Function} scm_unread_char (char, port) |
7403e409 NJ |
319 | Place @var{char} in @var{port} so that it will be read by the next |
320 | read operation on that port. If called multiple times, the unread | |
321 | characters will be read again in ``last-in, first-out'' order (i.e.@: | |
322 | a stack). If @var{port} is not supplied, the current input port is | |
323 | used. | |
a0e07ba4 NJ |
324 | @end deffn |
325 | ||
8f85c0c6 | 326 | @deffn {Scheme Procedure} unread-string str port |
a0e07ba4 NJ |
327 | Place the string @var{str} in @var{port} so that its characters will be |
328 | read in subsequent read operations. If called multiple times, the | |
329 | unread characters will be read again in last-in first-out order. If | |
330 | @var{port} is not supplied, the current-input-port is used. | |
331 | @end deffn | |
332 | ||
8f85c0c6 NJ |
333 | @deffn {Scheme Procedure} pipe |
334 | @deffnx {C Function} scm_pipe () | |
f3dfb8ac | 335 | @cindex pipe |
a0e07ba4 | 336 | Return a newly created pipe: a pair of ports which are linked |
7403e409 NJ |
337 | together on the local machine. The @acronym{CAR} is the input |
338 | port and the @acronym{CDR} is the output port. Data written (and | |
a0e07ba4 NJ |
339 | flushed) to the output port can be read from the input port. |
340 | Pipes are commonly used for communication with a newly forked | |
341 | child process. The need to flush the output port can be | |
342 | avoided by making it unbuffered using @code{setvbuf}. | |
343 | ||
c6ba64cd KR |
344 | @defvar PIPE_BUF |
345 | A write of up to @code{PIPE_BUF} many bytes to a pipe is atomic, | |
346 | meaning when done it goes into the pipe instantaneously and as a | |
347 | contiguous block (@pxref{Pipe Atomicity,, Atomicity of Pipe I/O, libc, | |
348 | The GNU C Library Reference Manual}). | |
349 | @end defvar | |
350 | ||
351 | Note that the output port is likely to block if too much data has been | |
352 | written but not yet read from the input port. Typically the capacity | |
353 | is @code{PIPE_BUF} bytes. | |
a0e07ba4 NJ |
354 | @end deffn |
355 | ||
356 | The next group of procedures perform a @code{dup2} | |
357 | system call, if @var{newfd} (an | |
358 | integer) is supplied, otherwise a @code{dup}. The file descriptor to be | |
359 | duplicated can be supplied as an integer or contained in a port. The | |
360 | type of value returned varies depending on which procedure is used. | |
361 | ||
362 | All procedures also have the side effect when performing @code{dup2} that any | |
363 | ports using @var{newfd} are moved to a different file descriptor and have | |
364 | their revealed counts set to zero. | |
365 | ||
8f85c0c6 NJ |
366 | @deffn {Scheme Procedure} dup->fdes fd_or_port [fd] |
367 | @deffnx {C Function} scm_dup_to_fdes (fd_or_port, fd) | |
a0e07ba4 NJ |
368 | Return a new integer file descriptor referring to the open file |
369 | designated by @var{fd_or_port}, which must be either an open | |
370 | file port or a file descriptor. | |
371 | @end deffn | |
372 | ||
8f85c0c6 | 373 | @deffn {Scheme Procedure} dup->inport port/fd [newfd] |
a0e07ba4 NJ |
374 | Returns a new input port using the new file descriptor. |
375 | @end deffn | |
376 | ||
8f85c0c6 | 377 | @deffn {Scheme Procedure} dup->outport port/fd [newfd] |
a0e07ba4 NJ |
378 | Returns a new output port using the new file descriptor. |
379 | @end deffn | |
380 | ||
8f85c0c6 | 381 | @deffn {Scheme Procedure} dup port/fd [newfd] |
a0e07ba4 NJ |
382 | Returns a new port if @var{port/fd} is a port, with the same mode as the |
383 | supplied port, otherwise returns an integer file descriptor. | |
384 | @end deffn | |
385 | ||
8f85c0c6 | 386 | @deffn {Scheme Procedure} dup->port port/fd mode [newfd] |
a0e07ba4 NJ |
387 | Returns a new port using the new file descriptor. @var{mode} supplies a |
388 | mode string for the port (@pxref{File Ports, open-file}). | |
389 | @end deffn | |
390 | ||
8f85c0c6 | 391 | @deffn {Scheme Procedure} duplicate-port port modes |
a0e07ba4 NJ |
392 | Returns a new port which is opened on a duplicate of the file |
393 | descriptor underlying @var{port}, with mode string @var{modes} | |
394 | as for @ref{File Ports, open-file}. The two ports | |
395 | will share a file position and file status flags. | |
396 | ||
397 | Unexpected behaviour can result if both ports are subsequently used | |
398 | and the original and/or duplicate ports are buffered. | |
399 | The mode string can include @code{0} to obtain an unbuffered duplicate | |
400 | port. | |
401 | ||
402 | This procedure is equivalent to @code{(dup->port @var{port} @var{modes})}. | |
403 | @end deffn | |
404 | ||
8f85c0c6 NJ |
405 | @deffn {Scheme Procedure} redirect-port old new |
406 | @deffnx {C Function} scm_redirect_port (old, new) | |
a0e07ba4 NJ |
407 | This procedure takes two ports and duplicates the underlying file |
408 | descriptor from @var{old-port} into @var{new-port}. The | |
409 | current file descriptor in @var{new-port} will be closed. | |
410 | After the redirection the two ports will share a file position | |
411 | and file status flags. | |
412 | ||
413 | The return value is unspecified. | |
414 | ||
415 | Unexpected behaviour can result if both ports are subsequently used | |
416 | and the original and/or duplicate ports are buffered. | |
417 | ||
418 | This procedure does not have any side effects on other ports or | |
419 | revealed counts. | |
420 | @end deffn | |
421 | ||
8f85c0c6 NJ |
422 | @deffn {Scheme Procedure} dup2 oldfd newfd |
423 | @deffnx {C Function} scm_dup2 (oldfd, newfd) | |
a0e07ba4 NJ |
424 | A simple wrapper for the @code{dup2} system call. |
425 | Copies the file descriptor @var{oldfd} to descriptor | |
426 | number @var{newfd}, replacing the previous meaning | |
427 | of @var{newfd}. Both @var{oldfd} and @var{newfd} must | |
428 | be integers. | |
7403e409 | 429 | Unlike for @code{dup->fdes} or @code{primitive-move->fdes}, no attempt |
a0e07ba4 NJ |
430 | is made to move away ports which are using @var{newfd}. |
431 | The return value is unspecified. | |
432 | @end deffn | |
433 | ||
8f85c0c6 | 434 | @deffn {Scheme Procedure} port-mode port |
a0e07ba4 NJ |
435 | Return the port modes associated with the open port @var{port}. |
436 | These will not necessarily be identical to the modes used when | |
7403e409 | 437 | the port was opened, since modes such as ``append'' which are |
a0e07ba4 NJ |
438 | used only during port creation are not retained. |
439 | @end deffn | |
440 | ||
8f85c0c6 | 441 | @deffn {Scheme Procedure} port-for-each proc |
c2e15516 MV |
442 | @deffnx {C Function} scm_port_for_each (SCM proc) |
443 | @deffnx {C Function} scm_c_port_for_each (void (*proc)(void *, SCM), void *data) | |
a0e07ba4 | 444 | Apply @var{proc} to each port in the Guile port table |
7403e409 | 445 | (FIXME: what is the Guile port table?) |
a0e07ba4 | 446 | in turn. The return value is unspecified. More specifically, |
7403e409 NJ |
447 | @var{proc} is applied exactly once to every port that exists in the |
448 | system at the time @code{port-for-each} is invoked. Changes to the | |
449 | port table while @code{port-for-each} is running have no effect as far | |
450 | as @code{port-for-each} is concerned. | |
c2e15516 MV |
451 | |
452 | The C function @code{scm_port_for_each} takes a Scheme procedure | |
453 | encoded as a @code{SCM} value, while @code{scm_c_port_for_each} takes | |
454 | a pointer to a C function and passes along a arbitrary @var{data} | |
455 | cookie. | |
a0e07ba4 NJ |
456 | @end deffn |
457 | ||
8f85c0c6 NJ |
458 | @deffn {Scheme Procedure} setvbuf port mode [size] |
459 | @deffnx {C Function} scm_setvbuf (port, mode, size) | |
f3dfb8ac | 460 | @cindex port buffering |
a0e07ba4 | 461 | Set the buffering mode for @var{port}. @var{mode} can be: |
2ce02471 NJ |
462 | |
463 | @defvar _IONBF | |
a0e07ba4 | 464 | non-buffered |
2ce02471 NJ |
465 | @end defvar |
466 | @defvar _IOLBF | |
a0e07ba4 | 467 | line buffered |
2ce02471 NJ |
468 | @end defvar |
469 | @defvar _IOFBF | |
a0e07ba4 NJ |
470 | block buffered, using a newly allocated buffer of @var{size} bytes. |
471 | If @var{size} is omitted, a default size will be used. | |
2ce02471 | 472 | @end defvar |
a0e07ba4 NJ |
473 | @end deffn |
474 | ||
23f2b9a3 | 475 | @deffn {Scheme Procedure} fcntl port/fd cmd [value] |
8f85c0c6 | 476 | @deffnx {C Function} scm_fcntl (object, cmd, value) |
23f2b9a3 KR |
477 | Apply @var{cmd} on @var{port/fd}, either a port or file descriptor. |
478 | The @var{value} argument is used by the @code{SET} commands described | |
479 | below, it's an integer value. | |
a0e07ba4 | 480 | |
1b09b607 | 481 | Values for @var{cmd} are: |
a0e07ba4 | 482 | |
2ce02471 | 483 | @defvar F_DUPFD |
1b09b607 KR |
484 | Duplicate the file descriptor, the same as @code{dup->fdes} above |
485 | does. | |
2ce02471 | 486 | @end defvar |
1b09b607 | 487 | |
2ce02471 | 488 | @defvar F_GETFD |
1b09b607 KR |
489 | @defvarx F_SETFD |
490 | Get or set flags associated with the file descriptor. The only flag | |
491 | is the following, | |
492 | ||
493 | @defvar FD_CLOEXEC | |
494 | ``Close on exec'', meaning the file descriptor will be closed on an | |
495 | @code{exec} call (a successful such call). For example to set that | |
496 | flag, | |
497 | ||
498 | @example | |
499 | (fcntl port F_SETFD FD_CLOEXEC) | |
500 | @end example | |
23f2b9a3 KR |
501 | |
502 | Or better, set it but leave any other possible future flags unchanged, | |
503 | ||
504 | @example | |
505 | (fcntl port F_SETFD (logior FD_CLOEXEC | |
506 | (fcntl port F_GETFD))) | |
507 | @end example | |
2ce02471 | 508 | @end defvar |
2ce02471 | 509 | @end defvar |
1b09b607 | 510 | |
2ce02471 | 511 | @defvar F_GETFL |
1b09b607 KR |
512 | @defvarx F_SETFL |
513 | Get or set flags associated with the open file. These flags are | |
514 | @code{O_RDONLY} etc described under @code{open} above. | |
515 | ||
516 | A common use is to set @code{O_NONBLOCK} on a network socket. The | |
517 | following sets that flag, and leaves other flags unchanged. | |
518 | ||
519 | @example | |
23f2b9a3 KR |
520 | (fcntl sock F_SETFL (logior O_NONBLOCK |
521 | (fcntl sock F_GETFL))) | |
1b09b607 | 522 | @end example |
2ce02471 | 523 | @end defvar |
1b09b607 | 524 | |
2ce02471 | 525 | @defvar F_GETOWN |
1b09b607 KR |
526 | @defvarx F_SETOWN |
527 | Get or set the process ID of a socket's owner, for @code{SIGIO} signals. | |
2ce02471 | 528 | @end defvar |
a0e07ba4 NJ |
529 | @end deffn |
530 | ||
8f85c0c6 NJ |
531 | @deffn {Scheme Procedure} flock file operation |
532 | @deffnx {C Function} scm_flock (file, operation) | |
f3dfb8ac | 533 | @cindex file locking |
a0e07ba4 NJ |
534 | Apply or remove an advisory lock on an open file. |
535 | @var{operation} specifies the action to be done: | |
2ce02471 NJ |
536 | |
537 | @defvar LOCK_SH | |
a0e07ba4 NJ |
538 | Shared lock. More than one process may hold a shared lock |
539 | for a given file at a given time. | |
2ce02471 NJ |
540 | @end defvar |
541 | @defvar LOCK_EX | |
a0e07ba4 NJ |
542 | Exclusive lock. Only one process may hold an exclusive lock |
543 | for a given file at a given time. | |
2ce02471 NJ |
544 | @end defvar |
545 | @defvar LOCK_UN | |
a0e07ba4 | 546 | Unlock the file. |
2ce02471 NJ |
547 | @end defvar |
548 | @defvar LOCK_NB | |
67bcd110 KR |
549 | Don't block when locking. This is combined with one of the other |
550 | operations using @code{logior} (@pxref{Bitwise Operations}). If | |
551 | @code{flock} would block an @code{EWOULDBLOCK} error is thrown | |
552 | (@pxref{Conventions}). | |
2ce02471 NJ |
553 | @end defvar |
554 | ||
a0e07ba4 | 555 | The return value is not specified. @var{file} may be an open |
85a9b4ed | 556 | file descriptor or an open file descriptor port. |
67bcd110 KR |
557 | |
558 | Note that @code{flock} does not lock files across NFS. | |
a0e07ba4 NJ |
559 | @end deffn |
560 | ||
8f85c0c6 NJ |
561 | @deffn {Scheme Procedure} select reads writes excepts [secs [usecs]] |
562 | @deffnx {C Function} scm_select (reads, writes, excepts, secs, usecs) | |
a0e07ba4 | 563 | This procedure has a variety of uses: waiting for the ability |
85a9b4ed | 564 | to provide input, accept output, or the existence of |
a0e07ba4 NJ |
565 | exceptional conditions on a collection of ports or file |
566 | descriptors, or waiting for a timeout to occur. | |
567 | It also returns if interrupted by a signal. | |
568 | ||
569 | @var{reads}, @var{writes} and @var{excepts} can be lists or | |
570 | vectors, with each member a port or a file descriptor. | |
571 | The value returned is a list of three corresponding | |
572 | lists or vectors containing only the members which meet the | |
573 | specified requirement. The ability of port buffers to | |
574 | provide input or accept output is taken into account. | |
575 | Ordering of the input lists or vectors is not preserved. | |
576 | ||
577 | The optional arguments @var{secs} and @var{usecs} specify the | |
578 | timeout. Either @var{secs} can be specified alone, as | |
579 | either an integer or a real number, or both @var{secs} and | |
580 | @var{usecs} can be specified as integers, in which case | |
581 | @var{usecs} is an additional timeout expressed in | |
582 | microseconds. If @var{secs} is omitted or is @code{#f} then | |
583 | select will wait for as long as it takes for one of the other | |
584 | conditions to be satisfied. | |
585 | ||
586 | The scsh version of @code{select} differs as follows: | |
587 | Only vectors are accepted for the first three arguments. | |
588 | The @var{usecs} argument is not supported. | |
589 | Multiple values are returned instead of a list. | |
590 | Duplicates in the input vectors appear only once in output. | |
591 | An additional @code{select!} interface is provided. | |
592 | @end deffn | |
593 | ||
594 | @node File System | |
3229f68b | 595 | @subsection File System |
f3dfb8ac | 596 | @cindex file system |
a0e07ba4 NJ |
597 | |
598 | These procedures allow querying and setting file system attributes | |
599 | (such as owner, | |
600 | permissions, sizes and types of files); deleting, copying, renaming and | |
601 | linking files; creating and removing directories and querying their | |
602 | contents; syncing the file system and creating special files. | |
603 | ||
8f85c0c6 NJ |
604 | @deffn {Scheme Procedure} access? path how |
605 | @deffnx {C Function} scm_access (path, how) | |
ad1c1f18 KR |
606 | Test accessibility of a file under the real UID and GID of the calling |
607 | process. The return is @code{#t} if @var{path} exists and the | |
608 | permissions requested by @var{how} are all allowed, or @code{#f} if | |
609 | not. | |
a0e07ba4 | 610 | |
ad1c1f18 KR |
611 | @var{how} is an integer which is one of the following values, or a |
612 | bitwise-OR (@code{logior}) of multiple values. | |
a0e07ba4 NJ |
613 | |
614 | @defvar R_OK | |
ad1c1f18 | 615 | Test for read permission. |
a0e07ba4 NJ |
616 | @end defvar |
617 | @defvar W_OK | |
ad1c1f18 | 618 | Test for write permission. |
a0e07ba4 NJ |
619 | @end defvar |
620 | @defvar X_OK | |
ad1c1f18 | 621 | Test for execute permission. |
a0e07ba4 NJ |
622 | @end defvar |
623 | @defvar F_OK | |
ad1c1f18 KR |
624 | Test for existence of the file. This is implied by each of the other |
625 | tests, so there's no need to combine it with them. | |
a0e07ba4 | 626 | @end defvar |
ad1c1f18 KR |
627 | |
628 | It's important to note that @code{access?} does not simply indicate | |
629 | what will happen on attempting to read or write a file. In normal | |
630 | circumstances it does, but in a set-UID or set-GID program it doesn't | |
631 | because @code{access?} tests the real ID, whereas an open or execute | |
632 | attempt uses the effective ID. | |
633 | ||
634 | A program which will never run set-UID/GID can ignore the difference | |
635 | between real and effective IDs, but for maximum generality, especially | |
1cd9ea69 KR |
636 | in library functions, it's best not to use @code{access?} to predict |
637 | the result of an open or execute, instead simply attempt that and | |
638 | catch any exception. | |
ad1c1f18 KR |
639 | |
640 | The main use for @code{access?} is to let a set-UID/GID program | |
641 | determine what the invoking user would have been allowed to do, | |
642 | without the greater (or perhaps lesser) privileges afforded by the | |
643 | effective ID. For more on this, see @ref{Testing File Access,,, libc, | |
644 | The GNU C Library Reference Manual}. | |
a0e07ba4 NJ |
645 | @end deffn |
646 | ||
647 | @findex fstat | |
8f85c0c6 NJ |
648 | @deffn {Scheme Procedure} stat object |
649 | @deffnx {C Function} scm_stat (object) | |
a0e07ba4 NJ |
650 | Return an object containing various information about the file |
651 | determined by @var{obj}. @var{obj} can be a string containing | |
652 | a file name or a port or integer file descriptor which is open | |
653 | on a file (in which case @code{fstat} is used as the underlying | |
654 | system call). | |
655 | ||
656 | The object returned by @code{stat} can be passed as a single | |
657 | parameter to the following procedures, all of which return | |
658 | integers: | |
659 | ||
2ce02471 | 660 | @deffn {Scheme Procedure} stat:dev st |
5c3917e7 | 661 | The device number containing the file. |
2ce02471 NJ |
662 | @end deffn |
663 | @deffn {Scheme Procedure} stat:ino st | |
a0e07ba4 NJ |
664 | The file serial number, which distinguishes this file from all |
665 | other files on the same device. | |
2ce02471 NJ |
666 | @end deffn |
667 | @deffn {Scheme Procedure} stat:mode st | |
5c3917e7 KR |
668 | The mode of the file. This is an integer which incorporates file type |
669 | information and file permission bits. See also @code{stat:type} and | |
a0e07ba4 | 670 | @code{stat:perms} below. |
2ce02471 NJ |
671 | @end deffn |
672 | @deffn {Scheme Procedure} stat:nlink st | |
a0e07ba4 | 673 | The number of hard links to the file. |
2ce02471 NJ |
674 | @end deffn |
675 | @deffn {Scheme Procedure} stat:uid st | |
a0e07ba4 | 676 | The user ID of the file's owner. |
2ce02471 NJ |
677 | @end deffn |
678 | @deffn {Scheme Procedure} stat:gid st | |
a0e07ba4 | 679 | The group ID of the file. |
2ce02471 NJ |
680 | @end deffn |
681 | @deffn {Scheme Procedure} stat:rdev st | |
f5f7888d KR |
682 | Device ID; this entry is defined only for character or block special |
683 | files. On some systems this field is not available at all, in which | |
684 | case @code{stat:rdev} returns @code{#f}. | |
2ce02471 NJ |
685 | @end deffn |
686 | @deffn {Scheme Procedure} stat:size st | |
a0e07ba4 | 687 | The size of a regular file in bytes. |
2ce02471 NJ |
688 | @end deffn |
689 | @deffn {Scheme Procedure} stat:atime st | |
06bfe276 | 690 | The last access time for the file, in seconds. |
2ce02471 NJ |
691 | @end deffn |
692 | @deffn {Scheme Procedure} stat:mtime st | |
06bfe276 | 693 | The last modification time for the file, in seconds. |
2ce02471 NJ |
694 | @end deffn |
695 | @deffn {Scheme Procedure} stat:ctime st | |
06bfe276 AW |
696 | The last modification time for the attributes of the file, in seconds. |
697 | @end deffn | |
698 | @deffn {Scheme Procedure} stat:atimensec st | |
699 | @deffnx {Scheme Procedure} stat:mtimensec st | |
700 | @deffnx {Scheme Procedure} stat:ctimensec st | |
701 | The fractional part of a file's access, modification, or attribute modification | |
702 | time, in nanoseconds. Nanosecond timestamps are only available on some operating | |
477e4219 | 703 | systems and file systems. If Guile cannot retrieve nanosecond-level timestamps |
06bfe276 | 704 | for a file, these fields will be set to 0. |
2ce02471 NJ |
705 | @end deffn |
706 | @deffn {Scheme Procedure} stat:blksize st | |
f5f7888d KR |
707 | The optimal block size for reading or writing the file, in bytes. On |
708 | some systems this field is not available, in which case | |
709 | @code{stat:blksize} returns a sensible suggested block size. | |
2ce02471 NJ |
710 | @end deffn |
711 | @deffn {Scheme Procedure} stat:blocks st | |
f5f7888d KR |
712 | The amount of disk space that the file occupies measured in units of |
713 | 512 byte blocks. On some systems this field is not available, in | |
714 | which case @code{stat:blocks} returns @code{#f}. | |
2ce02471 | 715 | @end deffn |
a0e07ba4 NJ |
716 | |
717 | In addition, the following procedures return the information | |
5c3917e7 | 718 | from @code{stat:mode} in a more convenient form: |
a0e07ba4 | 719 | |
2ce02471 | 720 | @deffn {Scheme Procedure} stat:type st |
a0e07ba4 | 721 | A symbol representing the type of file. Possible values are |
7403e409 NJ |
722 | @samp{regular}, @samp{directory}, @samp{symlink}, |
723 | @samp{block-special}, @samp{char-special}, @samp{fifo}, @samp{socket}, | |
724 | and @samp{unknown}. | |
2ce02471 NJ |
725 | @end deffn |
726 | @deffn {Scheme Procedure} stat:perms st | |
a0e07ba4 | 727 | An integer representing the access permission bits. |
2ce02471 | 728 | @end deffn |
a0e07ba4 NJ |
729 | @end deffn |
730 | ||
8f85c0c6 NJ |
731 | @deffn {Scheme Procedure} lstat str |
732 | @deffnx {C Function} scm_lstat (str) | |
a0e07ba4 NJ |
733 | Similar to @code{stat}, but does not follow symbolic links, i.e., |
734 | it will return information about a symbolic link itself, not the | |
735 | file it points to. @var{path} must be a string. | |
736 | @end deffn | |
737 | ||
8f85c0c6 NJ |
738 | @deffn {Scheme Procedure} readlink path |
739 | @deffnx {C Function} scm_readlink (path) | |
a0e07ba4 NJ |
740 | Return the value of the symbolic link named by @var{path} (a |
741 | string), i.e., the file that the link points to. | |
742 | @end deffn | |
743 | ||
744 | @findex fchown | |
745 | @findex lchown | |
8f85c0c6 NJ |
746 | @deffn {Scheme Procedure} chown object owner group |
747 | @deffnx {C Function} scm_chown (object, owner, group) | |
7403e409 NJ |
748 | Change the ownership and group of the file referred to by @var{object} |
749 | to the integer values @var{owner} and @var{group}. @var{object} can | |
750 | be a string containing a file name or, if the platform supports | |
751 | @code{fchown} (@pxref{File Owner,,,libc,The GNU C Library Reference | |
752 | Manual}), a port or integer file descriptor which is open on the file. | |
753 | The return value is unspecified. | |
a0e07ba4 NJ |
754 | |
755 | If @var{object} is a symbolic link, either the | |
756 | ownership of the link or the ownership of the referenced file will be | |
757 | changed depending on the operating system (lchown is | |
758 | unsupported at present). If @var{owner} or @var{group} is specified | |
759 | as @code{-1}, then that ID is not changed. | |
760 | @end deffn | |
761 | ||
762 | @findex fchmod | |
8f85c0c6 NJ |
763 | @deffn {Scheme Procedure} chmod object mode |
764 | @deffnx {C Function} scm_chmod (object, mode) | |
a0e07ba4 NJ |
765 | Changes the permissions of the file referred to by @var{obj}. |
766 | @var{obj} can be a string containing a file name or a port or integer file | |
767 | descriptor which is open on a file (in which case @code{fchmod} is used | |
768 | as the underlying system call). | |
769 | @var{mode} specifies | |
770 | the new permissions as a decimal number, e.g., @code{(chmod "foo" #o755)}. | |
771 | The return value is unspecified. | |
772 | @end deffn | |
773 | ||
06bfe276 AW |
774 | @deffn {Scheme Procedure} utime pathname [actime [modtime [actimens [modtimens [flags]]]]] |
775 | @deffnx {C Function} scm_utime (pathname, actime, modtime, actimens, modtimens, flags) | |
a0e07ba4 NJ |
776 | @code{utime} sets the access and modification times for the |
777 | file named by @var{path}. If @var{actime} or @var{modtime} is | |
778 | not supplied, then the current time is used. @var{actime} and | |
779 | @var{modtime} must be integer time values as returned by the | |
780 | @code{current-time} procedure. | |
06bfe276 AW |
781 | |
782 | The optional @var{actimens} and @var{modtimens} are nanoseconds | |
783 | to add @var{actime} and @var{modtime}. Nanosecond precision is | |
477e4219 | 784 | only supported on some combinations of file systems and operating |
06bfe276 | 785 | systems. |
a0e07ba4 NJ |
786 | @lisp |
787 | (utime "foo" (- (current-time) 3600)) | |
788 | @end lisp | |
789 | will set the access time to one hour in the past and the | |
790 | modification time to the current time. | |
791 | @end deffn | |
792 | ||
793 | @findex unlink | |
8f85c0c6 NJ |
794 | @deffn {Scheme Procedure} delete-file str |
795 | @deffnx {C Function} scm_delete_file (str) | |
7403e409 NJ |
796 | Deletes (or ``unlinks'') the file whose path is specified by |
797 | @var{str}. | |
a0e07ba4 NJ |
798 | @end deffn |
799 | ||
8f85c0c6 NJ |
800 | @deffn {Scheme Procedure} copy-file oldfile newfile |
801 | @deffnx {C Function} scm_copy_file (oldfile, newfile) | |
7403e409 | 802 | Copy the file specified by @var{oldfile} to @var{newfile}. |
a0e07ba4 NJ |
803 | The return value is unspecified. |
804 | @end deffn | |
805 | ||
806 | @findex rename | |
8f85c0c6 NJ |
807 | @deffn {Scheme Procedure} rename-file oldname newname |
808 | @deffnx {C Function} scm_rename (oldname, newname) | |
a0e07ba4 NJ |
809 | Renames the file specified by @var{oldname} to @var{newname}. |
810 | The return value is unspecified. | |
811 | @end deffn | |
812 | ||
8f85c0c6 NJ |
813 | @deffn {Scheme Procedure} link oldpath newpath |
814 | @deffnx {C Function} scm_link (oldpath, newpath) | |
a0e07ba4 NJ |
815 | Creates a new name @var{newpath} in the file system for the |
816 | file named by @var{oldpath}. If @var{oldpath} is a symbolic | |
817 | link, the link may or may not be followed depending on the | |
818 | system. | |
819 | @end deffn | |
820 | ||
8f85c0c6 NJ |
821 | @deffn {Scheme Procedure} symlink oldpath newpath |
822 | @deffnx {C Function} scm_symlink (oldpath, newpath) | |
7403e409 NJ |
823 | Create a symbolic link named @var{newpath} with the value (i.e., pointing to) |
824 | @var{oldpath}. The return value is unspecified. | |
a0e07ba4 NJ |
825 | @end deffn |
826 | ||
8f85c0c6 NJ |
827 | @deffn {Scheme Procedure} mkdir path [mode] |
828 | @deffnx {C Function} scm_mkdir (path, mode) | |
a0e07ba4 NJ |
829 | Create a new directory named by @var{path}. If @var{mode} is omitted |
830 | then the permissions of the directory file are set using the current | |
7403e409 NJ |
831 | umask (@pxref{Processes}). Otherwise they are set to the decimal |
832 | value specified with @var{mode}. The return value is unspecified. | |
a0e07ba4 NJ |
833 | @end deffn |
834 | ||
8f85c0c6 NJ |
835 | @deffn {Scheme Procedure} rmdir path |
836 | @deffnx {C Function} scm_rmdir (path) | |
a0e07ba4 NJ |
837 | Remove the existing directory named by @var{path}. The directory must |
838 | be empty for this to succeed. The return value is unspecified. | |
839 | @end deffn | |
840 | ||
8f85c0c6 NJ |
841 | @deffn {Scheme Procedure} opendir dirname |
842 | @deffnx {C Function} scm_opendir (dirname) | |
f3dfb8ac | 843 | @cindex directory contents |
7403e409 | 844 | Open the directory specified by @var{dirname} and return a directory |
a0e07ba4 NJ |
845 | stream. |
846 | @end deffn | |
847 | ||
7403e409 NJ |
848 | @deffn {Scheme Procedure} directory-stream? object |
849 | @deffnx {C Function} scm_directory_stream_p (object) | |
a0e07ba4 NJ |
850 | Return a boolean indicating whether @var{object} is a directory |
851 | stream as returned by @code{opendir}. | |
852 | @end deffn | |
853 | ||
7403e409 NJ |
854 | @deffn {Scheme Procedure} readdir stream |
855 | @deffnx {C Function} scm_readdir (stream) | |
a0e07ba4 NJ |
856 | Return (as a string) the next directory entry from the directory stream |
857 | @var{stream}. If there is no remaining entry to be read then the | |
858 | end of file object is returned. | |
859 | @end deffn | |
860 | ||
7403e409 NJ |
861 | @deffn {Scheme Procedure} rewinddir stream |
862 | @deffnx {C Function} scm_rewinddir (stream) | |
a0e07ba4 NJ |
863 | Reset the directory port @var{stream} so that the next call to |
864 | @code{readdir} will return the first directory entry. | |
865 | @end deffn | |
866 | ||
7403e409 NJ |
867 | @deffn {Scheme Procedure} closedir stream |
868 | @deffnx {C Function} scm_closedir (stream) | |
a0e07ba4 NJ |
869 | Close the directory stream @var{stream}. |
870 | The return value is unspecified. | |
871 | @end deffn | |
872 | ||
bcf009c3 NJ |
873 | Here is an example showing how to display all the entries in a |
874 | directory: | |
875 | ||
876 | @lisp | |
877 | (define dir (opendir "/usr/lib")) | |
878 | (do ((entry (readdir dir) (readdir dir))) | |
879 | ((eof-object? entry)) | |
880 | (display entry)(newline)) | |
881 | (closedir dir) | |
882 | @end lisp | |
883 | ||
8f85c0c6 NJ |
884 | @deffn {Scheme Procedure} sync |
885 | @deffnx {C Function} scm_sync () | |
a0e07ba4 NJ |
886 | Flush the operating system disk buffers. |
887 | The return value is unspecified. | |
888 | @end deffn | |
889 | ||
8f85c0c6 NJ |
890 | @deffn {Scheme Procedure} mknod path type perms dev |
891 | @deffnx {C Function} scm_mknod (path, type, perms, dev) | |
f3dfb8ac | 892 | @cindex device file |
a0e07ba4 | 893 | Creates a new special file, such as a file corresponding to a device. |
7403e409 NJ |
894 | @var{path} specifies the name of the file. @var{type} should be one |
895 | of the following symbols: @samp{regular}, @samp{directory}, | |
896 | @samp{symlink}, @samp{block-special}, @samp{char-special}, | |
897 | @samp{fifo}, or @samp{socket}. @var{perms} (an integer) specifies the | |
898 | file permissions. @var{dev} (an integer) specifies which device the | |
899 | special file refers to. Its exact interpretation depends on the kind | |
900 | of special file being created. | |
a0e07ba4 NJ |
901 | |
902 | E.g., | |
903 | @lisp | |
904 | (mknod "/dev/fd0" 'block-special #o660 (+ (* 2 256) 2)) | |
905 | @end lisp | |
906 | ||
907 | The return value is unspecified. | |
908 | @end deffn | |
909 | ||
8f85c0c6 NJ |
910 | @deffn {Scheme Procedure} tmpnam |
911 | @deffnx {C Function} scm_tmpnam () | |
f3dfb8ac | 912 | @cindex temporary file |
0b0715f1 KR |
913 | Return an auto-generated name of a temporary file, a file which |
914 | doesn't already exist. The name includes a path, it's usually in | |
915 | @file{/tmp} but that's system dependent. | |
916 | ||
917 | Care must be taken when using @code{tmpnam}. In between choosing the | |
918 | name and creating the file another program might use that name, or an | |
919 | attacker might even make it a symlink pointing at something important | |
920 | and causing you to overwrite that. | |
921 | ||
922 | The safe way is to create the file using @code{open} with | |
923 | @code{O_EXCL} to avoid any overwriting. A loop can try again with | |
924 | another name if the file exists (error @code{EEXIST}). | |
925 | @code{mkstemp!} below does that. | |
a0e07ba4 NJ |
926 | @end deffn |
927 | ||
8f85c0c6 NJ |
928 | @deffn {Scheme Procedure} mkstemp! tmpl |
929 | @deffnx {C Function} scm_mkstemp (tmpl) | |
f3dfb8ac | 930 | @cindex temporary file |
0b0715f1 KR |
931 | Create a new unique file in the file system and return a new buffered |
932 | port open for reading and writing to the file. | |
24ec486c | 933 | |
0b0715f1 KR |
934 | @var{tmpl} is a string specifying where the file should be created: it |
935 | must end with @samp{XXXXXX} and those @samp{X}s will be changed in the | |
936 | string to return the name of the file. (@code{port-filename} on the | |
937 | port also gives the name.) | |
24ec486c | 938 | |
0b0715f1 KR |
939 | POSIX doesn't specify the permissions mode of the file, on GNU and |
940 | most systems it's @code{#o600}. An application can use @code{chmod} | |
941 | to relax that if desired. For example @code{#o666} less @code{umask}, | |
942 | which is usual for ordinary file creation, | |
61fdb557 KR |
943 | |
944 | @example | |
0b0715f1 | 945 | (let ((port (mkstemp! (string-copy "/tmp/myfile-XXXXXX")))) |
61fdb557 KR |
946 | (chmod port (logand #o666 (lognot (umask)))) |
947 | ...) | |
948 | @end example | |
a0e07ba4 NJ |
949 | @end deffn |
950 | ||
cd53bd36 TTN |
951 | @deffn {Scheme Procedure} tmpfile |
952 | @deffnx {C Function} scm_tmpfile | |
953 | Return an input/output port to a unique temporary file | |
954 | named using the path prefix @code{P_tmpdir} defined in | |
955 | @file{stdio.h}. | |
956 | The file is automatically deleted when the port is closed | |
957 | or the program terminates. | |
958 | @end deffn | |
959 | ||
8f85c0c6 NJ |
960 | @deffn {Scheme Procedure} dirname filename |
961 | @deffnx {C Function} scm_dirname (filename) | |
a0e07ba4 NJ |
962 | Return the directory name component of the file name |
963 | @var{filename}. If @var{filename} does not contain a directory | |
964 | component, @code{.} is returned. | |
965 | @end deffn | |
966 | ||
8f85c0c6 NJ |
967 | @deffn {Scheme Procedure} basename filename [suffix] |
968 | @deffnx {C Function} scm_basename (filename, suffix) | |
a0e07ba4 NJ |
969 | Return the base name of the file name @var{filename}. The |
970 | base name is the file name without any directory components. | |
85a9b4ed | 971 | If @var{suffix} is provided, and is equal to the end of |
a0e07ba4 | 972 | @var{basename}, it is removed also. |
bcf009c3 NJ |
973 | |
974 | @lisp | |
975 | (basename "/tmp/test.xml" ".xml") | |
976 | @result{} "test" | |
977 | @end lisp | |
a0e07ba4 NJ |
978 | @end deffn |
979 | ||
839e6326 NJ |
980 | @deffn {Scheme Procedure} file-exists? filename |
981 | Return @code{#t} if the file named @var{filename} exists, @code{#f} if | |
982 | not. | |
983 | @end deffn | |
984 | ||
a0e07ba4 NJ |
985 | |
986 | @node User Information | |
3229f68b | 987 | @subsection User Information |
f3dfb8ac KR |
988 | @cindex user information |
989 | @cindex password file | |
990 | @cindex group file | |
a0e07ba4 NJ |
991 | |
992 | The facilities in this section provide an interface to the user and | |
993 | group database. | |
994 | They should be used with care since they are not reentrant. | |
995 | ||
996 | The following functions accept an object representing user information | |
997 | and return a selected component: | |
998 | ||
2ce02471 | 999 | @deffn {Scheme Procedure} passwd:name pw |
a0e07ba4 | 1000 | The name of the userid. |
2ce02471 NJ |
1001 | @end deffn |
1002 | @deffn {Scheme Procedure} passwd:passwd pw | |
a0e07ba4 | 1003 | The encrypted passwd. |
2ce02471 NJ |
1004 | @end deffn |
1005 | @deffn {Scheme Procedure} passwd:uid pw | |
a0e07ba4 | 1006 | The user id number. |
2ce02471 NJ |
1007 | @end deffn |
1008 | @deffn {Scheme Procedure} passwd:gid pw | |
a0e07ba4 | 1009 | The group id number. |
2ce02471 NJ |
1010 | @end deffn |
1011 | @deffn {Scheme Procedure} passwd:gecos pw | |
a0e07ba4 | 1012 | The full name. |
2ce02471 NJ |
1013 | @end deffn |
1014 | @deffn {Scheme Procedure} passwd:dir pw | |
a0e07ba4 | 1015 | The home directory. |
2ce02471 NJ |
1016 | @end deffn |
1017 | @deffn {Scheme Procedure} passwd:shell pw | |
a0e07ba4 | 1018 | The login shell. |
2ce02471 NJ |
1019 | @end deffn |
1020 | @sp 1 | |
a0e07ba4 | 1021 | |
8f85c0c6 | 1022 | @deffn {Scheme Procedure} getpwuid uid |
a0e07ba4 NJ |
1023 | Look up an integer userid in the user database. |
1024 | @end deffn | |
1025 | ||
8f85c0c6 | 1026 | @deffn {Scheme Procedure} getpwnam name |
a0e07ba4 NJ |
1027 | Look up a user name string in the user database. |
1028 | @end deffn | |
1029 | ||
8f85c0c6 | 1030 | @deffn {Scheme Procedure} setpwent |
a0e07ba4 NJ |
1031 | Initializes a stream used by @code{getpwent} to read from the user database. |
1032 | The next use of @code{getpwent} will return the first entry. The | |
1033 | return value is unspecified. | |
1034 | @end deffn | |
1035 | ||
8f85c0c6 | 1036 | @deffn {Scheme Procedure} getpwent |
40296bab KR |
1037 | Read the next entry in the user database stream. The return is a |
1038 | passwd user object as above, or @code{#f} when no more entries. | |
a0e07ba4 NJ |
1039 | @end deffn |
1040 | ||
8f85c0c6 | 1041 | @deffn {Scheme Procedure} endpwent |
a0e07ba4 NJ |
1042 | Closes the stream used by @code{getpwent}. The return value is unspecified. |
1043 | @end deffn | |
1044 | ||
8f85c0c6 NJ |
1045 | @deffn {Scheme Procedure} setpw [arg] |
1046 | @deffnx {C Function} scm_setpwent (arg) | |
a0e07ba4 NJ |
1047 | If called with a true argument, initialize or reset the password data |
1048 | stream. Otherwise, close the stream. The @code{setpwent} and | |
1049 | @code{endpwent} procedures are implemented on top of this. | |
1050 | @end deffn | |
1051 | ||
8f85c0c6 NJ |
1052 | @deffn {Scheme Procedure} getpw [user] |
1053 | @deffnx {C Function} scm_getpwuid (user) | |
a0e07ba4 NJ |
1054 | Look up an entry in the user database. @var{obj} can be an integer, |
1055 | a string, or omitted, giving the behaviour of getpwuid, getpwnam | |
1056 | or getpwent respectively. | |
1057 | @end deffn | |
1058 | ||
1059 | The following functions accept an object representing group information | |
1060 | and return a selected component: | |
1061 | ||
2ce02471 | 1062 | @deffn {Scheme Procedure} group:name gr |
a0e07ba4 | 1063 | The group name. |
2ce02471 NJ |
1064 | @end deffn |
1065 | @deffn {Scheme Procedure} group:passwd gr | |
a0e07ba4 | 1066 | The encrypted group password. |
2ce02471 NJ |
1067 | @end deffn |
1068 | @deffn {Scheme Procedure} group:gid gr | |
a0e07ba4 | 1069 | The group id number. |
2ce02471 NJ |
1070 | @end deffn |
1071 | @deffn {Scheme Procedure} group:mem gr | |
85a9b4ed | 1072 | A list of userids which have this group as a supplementary group. |
2ce02471 NJ |
1073 | @end deffn |
1074 | @sp 1 | |
a0e07ba4 | 1075 | |
8f85c0c6 | 1076 | @deffn {Scheme Procedure} getgrgid gid |
85a9b4ed | 1077 | Look up an integer group id in the group database. |
a0e07ba4 NJ |
1078 | @end deffn |
1079 | ||
8f85c0c6 | 1080 | @deffn {Scheme Procedure} getgrnam name |
a0e07ba4 NJ |
1081 | Look up a group name in the group database. |
1082 | @end deffn | |
1083 | ||
8f85c0c6 | 1084 | @deffn {Scheme Procedure} setgrent |
a0e07ba4 NJ |
1085 | Initializes a stream used by @code{getgrent} to read from the group database. |
1086 | The next use of @code{getgrent} will return the first entry. | |
1087 | The return value is unspecified. | |
1088 | @end deffn | |
1089 | ||
8f85c0c6 | 1090 | @deffn {Scheme Procedure} getgrent |
a0e07ba4 NJ |
1091 | Return the next entry in the group database, using the stream set by |
1092 | @code{setgrent}. | |
1093 | @end deffn | |
1094 | ||
8f85c0c6 | 1095 | @deffn {Scheme Procedure} endgrent |
a0e07ba4 NJ |
1096 | Closes the stream used by @code{getgrent}. |
1097 | The return value is unspecified. | |
1098 | @end deffn | |
1099 | ||
8f85c0c6 NJ |
1100 | @deffn {Scheme Procedure} setgr [arg] |
1101 | @deffnx {C Function} scm_setgrent (arg) | |
a0e07ba4 NJ |
1102 | If called with a true argument, initialize or reset the group data |
1103 | stream. Otherwise, close the stream. The @code{setgrent} and | |
1104 | @code{endgrent} procedures are implemented on top of this. | |
1105 | @end deffn | |
1106 | ||
8f85c0c6 NJ |
1107 | @deffn {Scheme Procedure} getgr [name] |
1108 | @deffnx {C Function} scm_getgrgid (name) | |
a0e07ba4 NJ |
1109 | Look up an entry in the group database. @var{obj} can be an integer, |
1110 | a string, or omitted, giving the behaviour of getgrgid, getgrnam | |
1111 | or getgrent respectively. | |
1112 | @end deffn | |
1113 | ||
1114 | In addition to the accessor procedures for the user database, the | |
cd28785f | 1115 | following shortcut procedure is also available. |
a0e07ba4 | 1116 | |
8f85c0c6 NJ |
1117 | @deffn {Scheme Procedure} getlogin |
1118 | @deffnx {C Function} scm_getlogin () | |
a0e07ba4 NJ |
1119 | Return a string containing the name of the user logged in on |
1120 | the controlling terminal of the process, or @code{#f} if this | |
1121 | information cannot be obtained. | |
1122 | @end deffn | |
1123 | ||
1124 | ||
1125 | @node Time | |
3229f68b | 1126 | @subsection Time |
f3dfb8ac | 1127 | @cindex time |
a0e07ba4 | 1128 | |
8f85c0c6 NJ |
1129 | @deffn {Scheme Procedure} current-time |
1130 | @deffnx {C Function} scm_current_time () | |
7403e409 | 1131 | Return the number of seconds since 1970-01-01 00:00:00 @acronym{UTC}, |
a0e07ba4 NJ |
1132 | excluding leap seconds. |
1133 | @end deffn | |
1134 | ||
8f85c0c6 NJ |
1135 | @deffn {Scheme Procedure} gettimeofday |
1136 | @deffnx {C Function} scm_gettimeofday () | |
a0e07ba4 | 1137 | Return a pair containing the number of seconds and microseconds |
7403e409 | 1138 | since 1970-01-01 00:00:00 @acronym{UTC}, excluding leap seconds. Note: |
a0e07ba4 NJ |
1139 | whether true microsecond resolution is available depends on the |
1140 | operating system. | |
1141 | @end deffn | |
1142 | ||
1143 | The following procedures either accept an object representing a broken down | |
1144 | time and return a selected component, or accept an object representing | |
1145 | a broken down time and a value and set the component to the value. | |
1146 | The numbers in parentheses give the usual range. | |
1147 | ||
2ce02471 NJ |
1148 | @deffn {Scheme Procedure} tm:sec tm |
1149 | @deffnx {Scheme Procedure} set-tm:sec tm val | |
a0e07ba4 | 1150 | Seconds (0-59). |
2ce02471 NJ |
1151 | @end deffn |
1152 | @deffn {Scheme Procedure} tm:min tm | |
1153 | @deffnx {Scheme Procedure} set-tm:min tm val | |
a0e07ba4 | 1154 | Minutes (0-59). |
2ce02471 NJ |
1155 | @end deffn |
1156 | @deffn {Scheme Procedure} tm:hour tm | |
1157 | @deffnx {Scheme Procedure} set-tm:hour tm val | |
a0e07ba4 | 1158 | Hours (0-23). |
2ce02471 NJ |
1159 | @end deffn |
1160 | @deffn {Scheme Procedure} tm:mday tm | |
1161 | @deffnx {Scheme Procedure} set-tm:mday tm val | |
a0e07ba4 | 1162 | Day of the month (1-31). |
2ce02471 NJ |
1163 | @end deffn |
1164 | @deffn {Scheme Procedure} tm:mon tm | |
1165 | @deffnx {Scheme Procedure} set-tm:mon tm val | |
a0e07ba4 | 1166 | Month (0-11). |
2ce02471 NJ |
1167 | @end deffn |
1168 | @deffn {Scheme Procedure} tm:year tm | |
1169 | @deffnx {Scheme Procedure} set-tm:year tm val | |
a0e07ba4 | 1170 | Year (70-), the year minus 1900. |
2ce02471 NJ |
1171 | @end deffn |
1172 | @deffn {Scheme Procedure} tm:wday tm | |
1173 | @deffnx {Scheme Procedure} set-tm:wday tm val | |
a0e07ba4 | 1174 | Day of the week (0-6) with Sunday represented as 0. |
2ce02471 NJ |
1175 | @end deffn |
1176 | @deffn {Scheme Procedure} tm:yday tm | |
1177 | @deffnx {Scheme Procedure} set-tm:yday tm val | |
a0e07ba4 | 1178 | Day of the year (0-364, 365 in leap years). |
2ce02471 NJ |
1179 | @end deffn |
1180 | @deffn {Scheme Procedure} tm:isdst tm | |
1181 | @deffnx {Scheme Procedure} set-tm:isdst tm val | |
7403e409 NJ |
1182 | Daylight saving indicator (0 for ``no'', greater than 0 for ``yes'', less than |
1183 | 0 for ``unknown''). | |
2ce02471 NJ |
1184 | @end deffn |
1185 | @deffn {Scheme Procedure} tm:gmtoff tm | |
1186 | @deffnx {Scheme Procedure} set-tm:gmtoff tm val | |
7403e409 | 1187 | Time zone offset in seconds west of @acronym{UTC} (-46800 to 43200). |
40296bab KR |
1188 | For example on East coast USA (zone @samp{EST+5}) this would be 18000 |
1189 | (ie.@: @m{5\times60\times60,5*60*60}) in winter, or 14400 | |
1190 | (ie.@: @m{4\times60\times60,4*60*60}) during daylight savings. | |
1191 | ||
1192 | Note @code{tm:gmtoff} is not the same as @code{tm_gmtoff} in the C | |
1193 | @code{tm} structure. @code{tm_gmtoff} is seconds east and hence the | |
1194 | negative of the value here. | |
2ce02471 NJ |
1195 | @end deffn |
1196 | @deffn {Scheme Procedure} tm:zone tm | |
1197 | @deffnx {Scheme Procedure} set-tm:zone tm val | |
a0e07ba4 | 1198 | Time zone label (a string), not necessarily unique. |
2ce02471 NJ |
1199 | @end deffn |
1200 | @sp 1 | |
a0e07ba4 | 1201 | |
8f85c0c6 NJ |
1202 | @deffn {Scheme Procedure} localtime time [zone] |
1203 | @deffnx {C Function} scm_localtime (time, zone) | |
f3dfb8ac | 1204 | @cindex local time |
a0e07ba4 NJ |
1205 | Return an object representing the broken down components of |
1206 | @var{time}, an integer like the one returned by | |
1207 | @code{current-time}. The time zone for the calculation is | |
1208 | optionally specified by @var{zone} (a string), otherwise the | |
7403e409 | 1209 | @env{TZ} environment variable or the system default is used. |
a0e07ba4 NJ |
1210 | @end deffn |
1211 | ||
8f85c0c6 NJ |
1212 | @deffn {Scheme Procedure} gmtime time |
1213 | @deffnx {C Function} scm_gmtime (time) | |
a0e07ba4 NJ |
1214 | Return an object representing the broken down components of |
1215 | @var{time}, an integer like the one returned by | |
7403e409 | 1216 | @code{current-time}. The values are calculated for @acronym{UTC}. |
a0e07ba4 NJ |
1217 | @end deffn |
1218 | ||
82512be0 | 1219 | @deffn {Scheme Procedure} mktime sbd-time [zone] |
8f85c0c6 | 1220 | @deffnx {C Function} scm_mktime (sbd_time, zone) |
b0fb2306 KR |
1221 | For a broken down time object @var{sbd-time}, return a pair the |
1222 | @code{car} of which is an integer time like @code{current-time}, and | |
1223 | the @code{cdr} of which is a new broken down time with normalized | |
1224 | fields. | |
1225 | ||
1226 | @var{zone} is a timezone string, or the default is the @env{TZ} | |
1227 | environment variable or the system default (@pxref{TZ Variable,, | |
1228 | Specifying the Time Zone with @env{TZ}, libc, GNU C Library Reference | |
1229 | Manual}). @var{sbd-time} is taken to be in that @var{zone}. | |
1230 | ||
1231 | The following fields of @var{sbd-time} are used: @code{tm:year}, | |
1232 | @code{tm:mon}, @code{tm:mday}, @code{tm:hour}, @code{tm:min}, | |
1233 | @code{tm:sec}, @code{tm:isdst}. The values can be outside their usual | |
1234 | ranges. For example @code{tm:hour} normally goes up to 23, but a | |
1235 | value say 33 would mean 9 the following day. | |
1236 | ||
1237 | @code{tm:isdst} in @var{sbd-time} says whether the time given is with | |
1238 | daylight savings or not. This is ignored if @var{zone} doesn't have | |
1239 | any daylight savings adjustment amount. | |
1240 | ||
1241 | The broken down time in the return normalizes the values of | |
1242 | @var{sbd-time} by bringing them into their usual ranges, and using the | |
1243 | actual daylight savings rule for that time in @var{zone} (which may | |
1244 | differ from what @var{sbd-time} had). The easiest way to think of | |
1245 | this is that @var{sbd-time} plus @var{zone} converts to the integer | |
1246 | UTC time, then a @code{localtime} is applied to get the normal | |
1247 | presentation of that time, in @var{zone}. | |
a0e07ba4 NJ |
1248 | @end deffn |
1249 | ||
8f85c0c6 NJ |
1250 | @deffn {Scheme Procedure} tzset |
1251 | @deffnx {C Function} scm_tzset () | |
7403e409 | 1252 | Initialize the timezone from the @env{TZ} environment variable |
a0e07ba4 NJ |
1253 | or the system default. It's not usually necessary to call this procedure |
1254 | since it's done automatically by other procedures that depend on the | |
1255 | timezone. | |
1256 | @end deffn | |
1257 | ||
4b08cab6 KR |
1258 | @deffn {Scheme Procedure} strftime format tm |
1259 | @deffnx {C Function} scm_strftime (format, tm) | |
f3dfb8ac | 1260 | @cindex time formatting |
4b08cab6 KR |
1261 | Return a string which is broken-down time structure @var{tm} formatted |
1262 | according to the given @var{format} string. | |
1263 | ||
1264 | @var{format} contains field specifications introduced by a @samp{%} | |
1265 | character. See @ref{Formatting Calendar Time,,, libc, The GNU C | |
1266 | Library Reference Manual}, or @samp{man 3 strftime}, for the available | |
1267 | formatting. | |
bcf009c3 NJ |
1268 | |
1269 | @lisp | |
1270 | (strftime "%c" (localtime (current-time))) | |
1271 | @result{} "Mon Mar 11 20:17:43 2002" | |
1272 | @end lisp | |
4b08cab6 KR |
1273 | |
1274 | If @code{setlocale} has been called (@pxref{Locales}), month and day | |
1275 | names are from the current locale and in the locale character set. | |
a0e07ba4 NJ |
1276 | @end deffn |
1277 | ||
8f85c0c6 NJ |
1278 | @deffn {Scheme Procedure} strptime format string |
1279 | @deffnx {C Function} scm_strptime (format, string) | |
f3dfb8ac | 1280 | @cindex time parsing |
a0e07ba4 NJ |
1281 | Performs the reverse action to @code{strftime}, parsing |
1282 | @var{string} according to the specification supplied in | |
1283 | @var{template}. The interpretation of month and day names is | |
1284 | dependent on the current locale. The value returned is a pair. | |
7403e409 | 1285 | The @acronym{CAR} has an object with time components |
a0e07ba4 NJ |
1286 | in the form returned by @code{localtime} or @code{gmtime}, |
1287 | but the time zone components | |
1288 | are not usefully set. | |
7403e409 | 1289 | The @acronym{CDR} reports the number of characters from @var{string} |
a0e07ba4 NJ |
1290 | which were used for the conversion. |
1291 | @end deffn | |
1292 | ||
1293 | @defvar internal-time-units-per-second | |
1294 | The value of this variable is the number of time units per second | |
1295 | reported by the following procedures. | |
1296 | @end defvar | |
1297 | ||
8f85c0c6 NJ |
1298 | @deffn {Scheme Procedure} times |
1299 | @deffnx {C Function} scm_times () | |
a0e07ba4 NJ |
1300 | Return an object with information about real and processor |
1301 | time. The following procedures accept such an object as an | |
1302 | argument and return a selected component: | |
1303 | ||
2ce02471 | 1304 | @deffn {Scheme Procedure} tms:clock tms |
a0e07ba4 NJ |
1305 | The current real time, expressed as time units relative to an |
1306 | arbitrary base. | |
2ce02471 NJ |
1307 | @end deffn |
1308 | @deffn {Scheme Procedure} tms:utime tms | |
a0e07ba4 | 1309 | The CPU time units used by the calling process. |
2ce02471 NJ |
1310 | @end deffn |
1311 | @deffn {Scheme Procedure} tms:stime tms | |
a0e07ba4 NJ |
1312 | The CPU time units used by the system on behalf of the calling |
1313 | process. | |
2ce02471 NJ |
1314 | @end deffn |
1315 | @deffn {Scheme Procedure} tms:cutime tms | |
a0e07ba4 NJ |
1316 | The CPU time units used by terminated child processes of the |
1317 | calling process, whose status has been collected (e.g., using | |
1318 | @code{waitpid}). | |
2ce02471 NJ |
1319 | @end deffn |
1320 | @deffn {Scheme Procedure} tms:cstime tms | |
a0e07ba4 NJ |
1321 | Similarly, the CPU times units used by the system on behalf of |
1322 | terminated child processes. | |
2ce02471 | 1323 | @end deffn |
a0e07ba4 NJ |
1324 | @end deffn |
1325 | ||
8f85c0c6 NJ |
1326 | @deffn {Scheme Procedure} get-internal-real-time |
1327 | @deffnx {C Function} scm_get_internal_real_time () | |
a0e07ba4 NJ |
1328 | Return the number of time units since the interpreter was |
1329 | started. | |
1330 | @end deffn | |
1331 | ||
8f85c0c6 NJ |
1332 | @deffn {Scheme Procedure} get-internal-run-time |
1333 | @deffnx {C Function} scm_get_internal_run_time () | |
a0e07ba4 NJ |
1334 | Return the number of time units of processor time used by the |
1335 | interpreter. Both @emph{system} and @emph{user} time are | |
1336 | included but subprocesses are not. | |
1337 | @end deffn | |
1338 | ||
1339 | @node Runtime Environment | |
3229f68b | 1340 | @subsection Runtime Environment |
a0e07ba4 | 1341 | |
8f85c0c6 NJ |
1342 | @deffn {Scheme Procedure} program-arguments |
1343 | @deffnx {Scheme Procedure} command-line | |
9a18d8d4 | 1344 | @deffnx {Scheme Procedure} set-program-arguments |
8f85c0c6 | 1345 | @deffnx {C Function} scm_program_arguments () |
9a18d8d4 | 1346 | @deffnx {C Function} scm_set_program_arguments_scm (lst) |
f3dfb8ac KR |
1347 | @cindex command line |
1348 | @cindex program arguments | |
9a18d8d4 KR |
1349 | Get the command line arguments passed to Guile, or set new arguments. |
1350 | ||
1351 | The arguments are a list of strings, the first of which is the invoked | |
1352 | program name. This is just @nicode{"guile"} (or the executable path) | |
1353 | when run interactively, or it's the script name when running a script | |
1354 | with @option{-s} (@pxref{Invoking Guile}). | |
1355 | ||
1356 | @example | |
1357 | guile -L /my/extra/dir -s foo.scm abc def | |
1358 | ||
1359 | (program-arguments) @result{} ("foo.scm" "abc" "def") | |
1360 | @end example | |
1361 | ||
1362 | @code{set-program-arguments} allows a library module or similar to | |
1363 | modify the arguments, for example to strip options it recognises, | |
1364 | leaving the rest for the mainline. | |
1365 | ||
1366 | The argument list is held in a fluid, which means it's separate for | |
1367 | each thread. Neither the list nor the strings within it are copied at | |
1368 | any point and normally should not be mutated. | |
1369 | ||
1370 | The two names @code{program-arguments} and @code{command-line} are an | |
1371 | historical accident, they both do exactly the same thing. The name | |
1372 | @code{scm_set_program_arguments_scm} has an extra @code{_scm} on the | |
1373 | end to avoid clashing with the C function below. | |
a0e07ba4 NJ |
1374 | @end deffn |
1375 | ||
9a18d8d4 KR |
1376 | @deftypefn {C Function} void scm_set_program_arguments (int argc, char **argv, char *first) |
1377 | @cindex command line | |
1378 | @cindex program arguments | |
1379 | Set the list of command line arguments for @code{program-arguments} | |
1380 | and @code{command-line} above. | |
1381 | ||
1382 | @var{argv} is an array of null-terminated strings, as in a C | |
1383 | @code{main} function. @var{argc} is the number of strings in | |
bf5df489 KR |
1384 | @var{argv}, or if it's negative then a @code{NULL} in @var{argv} marks |
1385 | its end. | |
9a18d8d4 KR |
1386 | |
1387 | @var{first} is an extra string put at the start of the arguments, or | |
1388 | @code{NULL} for no such extra. This is a convenient way to pass the | |
1389 | program name after advancing @var{argv} to strip option arguments. | |
bf5df489 | 1390 | Eg.@: |
9a18d8d4 KR |
1391 | |
1392 | @example | |
1393 | @{ | |
1394 | char *progname = argv[0]; | |
9a18d8d4 KR |
1395 | for (argv++; argv[0] != NULL && argv[0][0] == '-'; argv++) |
1396 | @{ | |
1397 | /* munch option ... */ | |
1398 | @} | |
1399 | /* remaining args for scheme level use */ | |
1400 | scm_set_program_arguments (-1, argv, progname); | |
1401 | @} | |
1402 | @end example | |
1403 | ||
1404 | This sort of thing is often done at startup under | |
bf5df489 | 1405 | @code{scm_boot_guile} with options handled at the C level removed. |
9a18d8d4 KR |
1406 | The given strings are all copied, so the C data is not accessed again |
1407 | once @code{scm_set_program_arguments} returns. | |
1408 | @end deftypefn | |
1409 | ||
8f85c0c6 NJ |
1410 | @deffn {Scheme Procedure} getenv nam |
1411 | @deffnx {C Function} scm_getenv (nam) | |
f3dfb8ac | 1412 | @cindex environment |
a0e07ba4 NJ |
1413 | Looks up the string @var{name} in the current environment. The return |
1414 | value is @code{#f} unless a string of the form @code{NAME=VALUE} is | |
1415 | found, in which case the string @code{VALUE} is returned. | |
1416 | @end deffn | |
1417 | ||
8f85c0c6 | 1418 | @deffn {Scheme Procedure} setenv name value |
a0e07ba4 NJ |
1419 | Modifies the environment of the current process, which is |
1420 | also the default environment inherited by child processes. | |
1421 | ||
1422 | If @var{value} is @code{#f}, then @var{name} is removed from the | |
1423 | environment. Otherwise, the string @var{name}=@var{value} is added | |
1424 | to the environment, replacing any existing string with name matching | |
1425 | @var{name}. | |
1426 | ||
1427 | The return value is unspecified. | |
1428 | @end deffn | |
1429 | ||
395b0a34 NJ |
1430 | @deffn {Scheme Procedure} unsetenv name |
1431 | Remove variable @var{name} from the environment. The | |
1432 | name can not contain a @samp{=} character. | |
1433 | @end deffn | |
1434 | ||
8f85c0c6 NJ |
1435 | @deffn {Scheme Procedure} environ [env] |
1436 | @deffnx {C Function} scm_environ (env) | |
a0e07ba4 NJ |
1437 | If @var{env} is omitted, return the current environment (in the |
1438 | Unix sense) as a list of strings. Otherwise set the current | |
1439 | environment, which is also the default environment for child | |
1440 | processes, to the supplied list of strings. Each member of | |
7403e409 NJ |
1441 | @var{env} should be of the form @var{NAME}=@var{VALUE} and values of |
1442 | @var{NAME} should not be duplicated. If @var{env} is supplied | |
a0e07ba4 NJ |
1443 | then the return value is unspecified. |
1444 | @end deffn | |
1445 | ||
8f85c0c6 NJ |
1446 | @deffn {Scheme Procedure} putenv str |
1447 | @deffnx {C Function} scm_putenv (str) | |
a0e07ba4 NJ |
1448 | Modifies the environment of the current process, which is |
1449 | also the default environment inherited by child processes. | |
1450 | ||
1451 | If @var{string} is of the form @code{NAME=VALUE} then it will be written | |
1452 | directly into the environment, replacing any existing environment string | |
1453 | with | |
1454 | name matching @code{NAME}. If @var{string} does not contain an equal | |
1455 | sign, then any existing string with name matching @var{string} will | |
1456 | be removed. | |
1457 | ||
1458 | The return value is unspecified. | |
1459 | @end deffn | |
1460 | ||
1461 | ||
1462 | @node Processes | |
3229f68b | 1463 | @subsection Processes |
f3dfb8ac KR |
1464 | @cindex processes |
1465 | @cindex child processes | |
a0e07ba4 NJ |
1466 | |
1467 | @findex cd | |
8f85c0c6 NJ |
1468 | @deffn {Scheme Procedure} chdir str |
1469 | @deffnx {C Function} scm_chdir (str) | |
f3dfb8ac | 1470 | @cindex current directory |
a0e07ba4 NJ |
1471 | Change the current working directory to @var{path}. |
1472 | The return value is unspecified. | |
1473 | @end deffn | |
1474 | ||
1475 | @findex pwd | |
8f85c0c6 NJ |
1476 | @deffn {Scheme Procedure} getcwd |
1477 | @deffnx {C Function} scm_getcwd () | |
a0e07ba4 NJ |
1478 | Return the name of the current working directory. |
1479 | @end deffn | |
1480 | ||
8f85c0c6 NJ |
1481 | @deffn {Scheme Procedure} umask [mode] |
1482 | @deffnx {C Function} scm_umask (mode) | |
7403e409 NJ |
1483 | If @var{mode} is omitted, returns a decimal number representing the |
1484 | current file creation mask. Otherwise the file creation mask is set | |
1485 | to @var{mode} and the previous value is returned. @xref{Setting | |
1486 | Permissions,,Assigning File Permissions,libc,The GNU C Library | |
1487 | Reference Manual}, for more on how to use umasks. | |
a0e07ba4 | 1488 | |
7403e409 | 1489 | E.g., @code{(umask #o022)} sets the mask to octal 22/decimal 18. |
a0e07ba4 NJ |
1490 | @end deffn |
1491 | ||
8f85c0c6 NJ |
1492 | @deffn {Scheme Procedure} chroot path |
1493 | @deffnx {C Function} scm_chroot (path) | |
a0e07ba4 NJ |
1494 | Change the root directory to that specified in @var{path}. |
1495 | This directory will be used for path names beginning with | |
1496 | @file{/}. The root directory is inherited by all children | |
1497 | of the current process. Only the superuser may change the | |
1498 | root directory. | |
1499 | @end deffn | |
1500 | ||
8f85c0c6 NJ |
1501 | @deffn {Scheme Procedure} getpid |
1502 | @deffnx {C Function} scm_getpid () | |
a0e07ba4 NJ |
1503 | Return an integer representing the current process ID. |
1504 | @end deffn | |
1505 | ||
8f85c0c6 NJ |
1506 | @deffn {Scheme Procedure} getgroups |
1507 | @deffnx {C Function} scm_getgroups () | |
a0e07ba4 | 1508 | Return a vector of integers representing the current |
85a9b4ed | 1509 | supplementary group IDs. |
a0e07ba4 NJ |
1510 | @end deffn |
1511 | ||
8f85c0c6 NJ |
1512 | @deffn {Scheme Procedure} getppid |
1513 | @deffnx {C Function} scm_getppid () | |
a0e07ba4 NJ |
1514 | Return an integer representing the process ID of the parent |
1515 | process. | |
1516 | @end deffn | |
1517 | ||
8f85c0c6 NJ |
1518 | @deffn {Scheme Procedure} getuid |
1519 | @deffnx {C Function} scm_getuid () | |
a0e07ba4 NJ |
1520 | Return an integer representing the current real user ID. |
1521 | @end deffn | |
1522 | ||
8f85c0c6 NJ |
1523 | @deffn {Scheme Procedure} getgid |
1524 | @deffnx {C Function} scm_getgid () | |
a0e07ba4 NJ |
1525 | Return an integer representing the current real group ID. |
1526 | @end deffn | |
1527 | ||
8f85c0c6 NJ |
1528 | @deffn {Scheme Procedure} geteuid |
1529 | @deffnx {C Function} scm_geteuid () | |
a0e07ba4 NJ |
1530 | Return an integer representing the current effective user ID. |
1531 | If the system does not support effective IDs, then the real ID | |
66add4eb | 1532 | is returned. @code{(provided? 'EIDs)} reports whether the |
a0e07ba4 NJ |
1533 | system supports effective IDs. |
1534 | @end deffn | |
1535 | ||
8f85c0c6 NJ |
1536 | @deffn {Scheme Procedure} getegid |
1537 | @deffnx {C Function} scm_getegid () | |
a0e07ba4 NJ |
1538 | Return an integer representing the current effective group ID. |
1539 | If the system does not support effective IDs, then the real ID | |
66add4eb | 1540 | is returned. @code{(provided? 'EIDs)} reports whether the |
a0e07ba4 NJ |
1541 | system supports effective IDs. |
1542 | @end deffn | |
1543 | ||
ef048324 KR |
1544 | @deffn {Scheme Procedure} setgroups vec |
1545 | @deffnx {C Function} scm_setgroups (vec) | |
1546 | Set the current set of supplementary group IDs to the integers in the | |
1547 | given vector @var{vec}. The return value is unspecified. | |
1548 | ||
1549 | Generally only the superuser can set the process group IDs | |
1550 | (@pxref{Setting Groups, Setting the Group IDs,, libc, The GNU C | |
1551 | Library Reference Manual}). | |
1552 | @end deffn | |
1553 | ||
8f85c0c6 NJ |
1554 | @deffn {Scheme Procedure} setuid id |
1555 | @deffnx {C Function} scm_setuid (id) | |
a0e07ba4 NJ |
1556 | Sets both the real and effective user IDs to the integer @var{id}, provided |
1557 | the process has appropriate privileges. | |
1558 | The return value is unspecified. | |
1559 | @end deffn | |
1560 | ||
8f85c0c6 NJ |
1561 | @deffn {Scheme Procedure} setgid id |
1562 | @deffnx {C Function} scm_setgid (id) | |
a0e07ba4 NJ |
1563 | Sets both the real and effective group IDs to the integer @var{id}, provided |
1564 | the process has appropriate privileges. | |
1565 | The return value is unspecified. | |
1566 | @end deffn | |
1567 | ||
8f85c0c6 NJ |
1568 | @deffn {Scheme Procedure} seteuid id |
1569 | @deffnx {C Function} scm_seteuid (id) | |
a0e07ba4 NJ |
1570 | Sets the effective user ID to the integer @var{id}, provided the process |
1571 | has appropriate privileges. If effective IDs are not supported, the | |
7403e409 | 1572 | real ID is set instead---@code{(provided? 'EIDs)} reports whether the |
a0e07ba4 NJ |
1573 | system supports effective IDs. |
1574 | The return value is unspecified. | |
1575 | @end deffn | |
1576 | ||
8f85c0c6 NJ |
1577 | @deffn {Scheme Procedure} setegid id |
1578 | @deffnx {C Function} scm_setegid (id) | |
a0e07ba4 NJ |
1579 | Sets the effective group ID to the integer @var{id}, provided the process |
1580 | has appropriate privileges. If effective IDs are not supported, the | |
7403e409 | 1581 | real ID is set instead---@code{(provided? 'EIDs)} reports whether the |
a0e07ba4 NJ |
1582 | system supports effective IDs. |
1583 | The return value is unspecified. | |
1584 | @end deffn | |
1585 | ||
8f85c0c6 NJ |
1586 | @deffn {Scheme Procedure} getpgrp |
1587 | @deffnx {C Function} scm_getpgrp () | |
a0e07ba4 | 1588 | Return an integer representing the current process group ID. |
7403e409 | 1589 | This is the @acronym{POSIX} definition, not @acronym{BSD}. |
a0e07ba4 NJ |
1590 | @end deffn |
1591 | ||
8f85c0c6 NJ |
1592 | @deffn {Scheme Procedure} setpgid pid pgid |
1593 | @deffnx {C Function} scm_setpgid (pid, pgid) | |
a0e07ba4 NJ |
1594 | Move the process @var{pid} into the process group @var{pgid}. @var{pid} or |
1595 | @var{pgid} must be integers: they can be zero to indicate the ID of the | |
1596 | current process. | |
1597 | Fails on systems that do not support job control. | |
1598 | The return value is unspecified. | |
1599 | @end deffn | |
1600 | ||
8f85c0c6 NJ |
1601 | @deffn {Scheme Procedure} setsid |
1602 | @deffnx {C Function} scm_setsid () | |
a0e07ba4 NJ |
1603 | Creates a new session. The current process becomes the session leader |
1604 | and is put in a new process group. The process will be detached | |
1605 | from its controlling terminal if it has one. | |
1606 | The return value is an integer representing the new process group ID. | |
1607 | @end deffn | |
1608 | ||
211a5b04 NJ |
1609 | @deffn {Scheme Procedure} getsid pid |
1610 | @deffnx {C Function} scm_getsid (pid) | |
1611 | Returns the session ID of process @var{pid}. (The session | |
1612 | ID of a process is the process group ID of its session leader.) | |
1613 | @end deffn | |
1614 | ||
8f85c0c6 NJ |
1615 | @deffn {Scheme Procedure} waitpid pid [options] |
1616 | @deffnx {C Function} scm_waitpid (pid, options) | |
a0e07ba4 NJ |
1617 | This procedure collects status information from a child process which |
1618 | has terminated or (optionally) stopped. Normally it will | |
1619 | suspend the calling process until this can be done. If more than one | |
1620 | child process is eligible then one will be chosen by the operating system. | |
1621 | ||
1622 | The value of @var{pid} determines the behaviour: | |
1623 | ||
7403e409 | 1624 | @table @asis |
a0e07ba4 NJ |
1625 | @item @var{pid} greater than 0 |
1626 | Request status information from the specified child process. | |
7403e409 | 1627 | @item @var{pid} equal to -1 or @code{WAIT_ANY} |
2ce02471 | 1628 | @vindex WAIT_ANY |
a0e07ba4 | 1629 | Request status information for any child process. |
7403e409 | 1630 | @item @var{pid} equal to 0 or @code{WAIT_MYPGRP} |
2ce02471 | 1631 | @vindex WAIT_MYPGRP |
a0e07ba4 NJ |
1632 | Request status information for any child process in the current process |
1633 | group. | |
1634 | @item @var{pid} less than -1 | |
1635 | Request status information for any child process whose process group ID | |
7403e409 | 1636 | is @minus{}@var{pid}. |
a0e07ba4 NJ |
1637 | @end table |
1638 | ||
1639 | The @var{options} argument, if supplied, should be the bitwise OR of the | |
1640 | values of zero or more of the following variables: | |
1641 | ||
1642 | @defvar WNOHANG | |
1643 | Return immediately even if there are no child processes to be collected. | |
1644 | @end defvar | |
1645 | ||
1646 | @defvar WUNTRACED | |
1647 | Report status information for stopped processes as well as terminated | |
1648 | processes. | |
1649 | @end defvar | |
1650 | ||
1651 | The return value is a pair containing: | |
1652 | ||
1653 | @enumerate | |
1654 | @item | |
1655 | The process ID of the child process, or 0 if @code{WNOHANG} was | |
1656 | specified and no process was collected. | |
1657 | @item | |
1658 | The integer status value. | |
1659 | @end enumerate | |
1660 | @end deffn | |
1661 | ||
1662 | The following three | |
1663 | functions can be used to decode the process status code returned | |
1664 | by @code{waitpid}. | |
1665 | ||
8f85c0c6 NJ |
1666 | @deffn {Scheme Procedure} status:exit-val status |
1667 | @deffnx {C Function} scm_status_exit_val (status) | |
a0e07ba4 NJ |
1668 | Return the exit status value, as would be set if a process |
1669 | ended normally through a call to @code{exit} or @code{_exit}, | |
1670 | if any, otherwise @code{#f}. | |
1671 | @end deffn | |
1672 | ||
8f85c0c6 NJ |
1673 | @deffn {Scheme Procedure} status:term-sig status |
1674 | @deffnx {C Function} scm_status_term_sig (status) | |
a0e07ba4 NJ |
1675 | Return the signal number which terminated the process, if any, |
1676 | otherwise @code{#f}. | |
1677 | @end deffn | |
1678 | ||
8f85c0c6 NJ |
1679 | @deffn {Scheme Procedure} status:stop-sig status |
1680 | @deffnx {C Function} scm_status_stop_sig (status) | |
a0e07ba4 NJ |
1681 | Return the signal number which stopped the process, if any, |
1682 | otherwise @code{#f}. | |
1683 | @end deffn | |
1684 | ||
8f85c0c6 NJ |
1685 | @deffn {Scheme Procedure} system [cmd] |
1686 | @deffnx {C Function} scm_system (cmd) | |
7403e409 NJ |
1687 | Execute @var{cmd} using the operating system's ``command |
1688 | processor''. Under Unix this is usually the default shell | |
a0e07ba4 NJ |
1689 | @code{sh}. The value returned is @var{cmd}'s exit status as |
1690 | returned by @code{waitpid}, which can be interpreted using the | |
1691 | functions above. | |
1692 | ||
1693 | If @code{system} is called without arguments, return a boolean | |
1694 | indicating whether the command processor is available. | |
1695 | @end deffn | |
1696 | ||
8141bd98 RB |
1697 | @deffn {Scheme Procedure} system* . args |
1698 | @deffnx {C Function} scm_system_star (args) | |
1699 | Execute the command indicated by @var{args}. The first element must | |
1700 | be a string indicating the command to be executed, and the remaining | |
1701 | items must be strings representing each of the arguments to that | |
1702 | command. | |
1703 | ||
1704 | This function returns the exit status of the command as provided by | |
1705 | @code{waitpid}. This value can be handled with @code{status:exit-val} | |
1706 | and the related functions. | |
1707 | ||
1708 | @code{system*} is similar to @code{system}, but accepts only one | |
1709 | string per-argument, and performs no shell interpretation. The | |
1710 | command is executed using fork and execlp. Accordingly this function | |
1711 | may be safer than @code{system} in situations where shell | |
1712 | interpretation is not required. | |
1713 | ||
1714 | Example: (system* "echo" "foo" "bar") | |
1715 | @end deffn | |
1716 | ||
8f85c0c6 | 1717 | @deffn {Scheme Procedure} primitive-exit [status] |
23f2b9a3 | 1718 | @deffnx {Scheme Procedure} primitive-_exit [status] |
8f85c0c6 | 1719 | @deffnx {C Function} scm_primitive_exit (status) |
23f2b9a3 KR |
1720 | @deffnx {C Function} scm_primitive__exit (status) |
1721 | Terminate the current process without unwinding the Scheme stack. The | |
1722 | exit status is @var{status} if supplied, otherwise zero. | |
1723 | ||
1724 | @code{primitive-exit} uses the C @code{exit} function and hence runs | |
1725 | usual C level cleanups (flush output streams, call @code{atexit} | |
1726 | functions, etc, see @ref{Normal Termination,,, libc, The GNU C Library | |
1727 | Reference Manual})). | |
1728 | ||
1729 | @code{primitive-_exit} is the @code{_exit} system call | |
1730 | (@pxref{Termination Internals,,, libc, The GNU C Library Reference | |
1731 | Manual}). This terminates the program immediately, with neither | |
1732 | Scheme-level nor C-level cleanups. | |
1733 | ||
1734 | The typical use for @code{primitive-_exit} is from a child process | |
1735 | created with @code{primitive-fork}. For example in a Gdk program the | |
1736 | child process inherits the X server connection and a C-level | |
1737 | @code{atexit} cleanup which will close that connection. But closing | |
1738 | in the child would upset the protocol in the parent, so | |
1739 | @code{primitive-_exit} should be used to exit without that. | |
a0e07ba4 NJ |
1740 | @end deffn |
1741 | ||
8f85c0c6 NJ |
1742 | @deffn {Scheme Procedure} execl filename . args |
1743 | @deffnx {C Function} scm_execl (filename, args) | |
a0e07ba4 NJ |
1744 | Executes the file named by @var{path} as a new process image. |
1745 | The remaining arguments are supplied to the process; from a C program | |
85a9b4ed | 1746 | they are accessible as the @code{argv} argument to @code{main}. |
a0e07ba4 NJ |
1747 | Conventionally the first @var{arg} is the same as @var{path}. |
1748 | All arguments must be strings. | |
1749 | ||
1750 | If @var{arg} is missing, @var{path} is executed with a null | |
1751 | argument list, which may have system-dependent side-effects. | |
1752 | ||
1753 | This procedure is currently implemented using the @code{execv} system | |
1754 | call, but we call it @code{execl} because of its Scheme calling interface. | |
1755 | @end deffn | |
1756 | ||
8f85c0c6 NJ |
1757 | @deffn {Scheme Procedure} execlp filename . args |
1758 | @deffnx {C Function} scm_execlp (filename, args) | |
a0e07ba4 NJ |
1759 | Similar to @code{execl}, however if |
1760 | @var{filename} does not contain a slash | |
1761 | then the file to execute will be located by searching the | |
1762 | directories listed in the @code{PATH} environment variable. | |
1763 | ||
1764 | This procedure is currently implemented using the @code{execvp} system | |
1765 | call, but we call it @code{execlp} because of its Scheme calling interface. | |
1766 | @end deffn | |
1767 | ||
8f85c0c6 NJ |
1768 | @deffn {Scheme Procedure} execle filename env . args |
1769 | @deffnx {C Function} scm_execle (filename, env, args) | |
a0e07ba4 NJ |
1770 | Similar to @code{execl}, but the environment of the new process is |
1771 | specified by @var{env}, which must be a list of strings as returned by the | |
1772 | @code{environ} procedure. | |
1773 | ||
1774 | This procedure is currently implemented using the @code{execve} system | |
1775 | call, but we call it @code{execle} because of its Scheme calling interface. | |
1776 | @end deffn | |
1777 | ||
8f85c0c6 NJ |
1778 | @deffn {Scheme Procedure} primitive-fork |
1779 | @deffnx {C Function} scm_fork () | |
7403e409 | 1780 | Creates a new ``child'' process by duplicating the current ``parent'' process. |
a0e07ba4 NJ |
1781 | In the child the return value is 0. In the parent the return value is |
1782 | the integer process ID of the child. | |
1783 | ||
1784 | This procedure has been renamed from @code{fork} to avoid a naming conflict | |
1785 | with the scsh fork. | |
1786 | @end deffn | |
1787 | ||
8f85c0c6 NJ |
1788 | @deffn {Scheme Procedure} nice incr |
1789 | @deffnx {C Function} scm_nice (incr) | |
f3dfb8ac | 1790 | @cindex process priority |
a0e07ba4 NJ |
1791 | Increment the priority of the current process by @var{incr}. A higher |
1792 | priority value means that the process runs less often. | |
1793 | The return value is unspecified. | |
1794 | @end deffn | |
1795 | ||
8f85c0c6 NJ |
1796 | @deffn {Scheme Procedure} setpriority which who prio |
1797 | @deffnx {C Function} scm_setpriority (which, who, prio) | |
2ce02471 NJ |
1798 | @vindex PRIO_PROCESS |
1799 | @vindex PRIO_PGRP | |
1800 | @vindex PRIO_USER | |
a0e07ba4 NJ |
1801 | Set the scheduling priority of the process, process group |
1802 | or user, as indicated by @var{which} and @var{who}. @var{which} | |
1803 | is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP} | |
1804 | or @code{PRIO_USER}, and @var{who} is interpreted relative to | |
1805 | @var{which} (a process identifier for @code{PRIO_PROCESS}, | |
004fe2c8 | 1806 | process group identifier for @code{PRIO_PGRP}, and a user |
a0e07ba4 NJ |
1807 | identifier for @code{PRIO_USER}. A zero value of @var{who} |
1808 | denotes the current process, process group, or user. | |
7403e409 NJ |
1809 | @var{prio} is a value in the range [@minus{}20,20]. The default |
1810 | priority is 0; lower priorities (in numerical terms) cause more | |
1811 | favorable scheduling. Sets the priority of all of the specified | |
1812 | processes. Only the super-user may lower priorities. The return | |
1813 | value is not specified. | |
a0e07ba4 NJ |
1814 | @end deffn |
1815 | ||
8f85c0c6 NJ |
1816 | @deffn {Scheme Procedure} getpriority which who |
1817 | @deffnx {C Function} scm_getpriority (which, who) | |
2ce02471 NJ |
1818 | @vindex PRIO_PROCESS |
1819 | @vindex PRIO_PGRP | |
1820 | @vindex PRIO_USER | |
a0e07ba4 NJ |
1821 | Return the scheduling priority of the process, process group |
1822 | or user, as indicated by @var{which} and @var{who}. @var{which} | |
1823 | is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP} | |
7403e409 | 1824 | or @code{PRIO_USER}, and @var{who} should be interpreted depending on |
a0e07ba4 NJ |
1825 | @var{which} (a process identifier for @code{PRIO_PROCESS}, |
1826 | process group identifier for @code{PRIO_PGRP}, and a user | |
7403e409 | 1827 | identifier for @code{PRIO_USER}). A zero value of @var{who} |
a0e07ba4 NJ |
1828 | denotes the current process, process group, or user. Return |
1829 | the highest priority (lowest numerical value) of any of the | |
1830 | specified processes. | |
1831 | @end deffn | |
1832 | ||
fe613fe2 LC |
1833 | @cindex affinity, CPU |
1834 | ||
1835 | @deffn {Scheme Procedure} getaffinity pid | |
1836 | @deffnx {C Function} scm_getaffinity (pid) | |
1837 | Return a bitvector representing the CPU affinity mask for | |
1838 | process @var{pid}. Each CPU the process has affinity with | |
1839 | has its corresponding bit set in the returned bitvector. | |
1840 | The number of bits set is a good estimate of how many CPUs | |
1841 | Guile can use without stepping on other processes' toes. | |
1842 | ||
1843 | Currently this procedure is only defined on GNU variants. | |
1844 | @end deffn | |
1845 | ||
1846 | @deffn {Scheme Procedure} setaffinity pid mask | |
1847 | @deffnx {C Function} scm_setaffinity (pid, mask) | |
1848 | Install the CPU affinity mask @var{mask}, a bitvector, for | |
1849 | the process or thread with ID @var{pid}. The return value | |
1850 | is unspecified. | |
1851 | ||
1852 | Currently this procedure is only defined on GNU variants. | |
1853 | @end deffn | |
1854 | ||
a0e07ba4 NJ |
1855 | |
1856 | @node Signals | |
3229f68b | 1857 | @subsection Signals |
f3dfb8ac | 1858 | @cindex signal |
a0e07ba4 | 1859 | |
bf5df489 KR |
1860 | The following procedures raise, handle and wait for signals. |
1861 | ||
1862 | Scheme code signal handlers are run via a system async (@pxref{System | |
1863 | asyncs}), so they're called in the handler's thread at the next safe | |
1864 | opportunity. Generally this is after any currently executing | |
1865 | primitive procedure finishes (which could be a long time for | |
1866 | primitives that wait for an external event). | |
a0e07ba4 | 1867 | |
8f85c0c6 NJ |
1868 | @deffn {Scheme Procedure} kill pid sig |
1869 | @deffnx {C Function} scm_kill (pid, sig) | |
a0e07ba4 NJ |
1870 | Sends a signal to the specified process or group of processes. |
1871 | ||
1872 | @var{pid} specifies the processes to which the signal is sent: | |
1873 | ||
7403e409 | 1874 | @table @asis |
a0e07ba4 NJ |
1875 | @item @var{pid} greater than 0 |
1876 | The process whose identifier is @var{pid}. | |
1877 | @item @var{pid} equal to 0 | |
1878 | All processes in the current process group. | |
1879 | @item @var{pid} less than -1 | |
1880 | The process group whose identifier is -@var{pid} | |
1881 | @item @var{pid} equal to -1 | |
1882 | If the process is privileged, all processes except for some special | |
1883 | system processes. Otherwise, all processes with the current effective | |
1884 | user ID. | |
1885 | @end table | |
1886 | ||
1887 | @var{sig} should be specified using a variable corresponding to | |
1888 | the Unix symbolic name, e.g., | |
1889 | ||
1890 | @defvar SIGHUP | |
1891 | Hang-up signal. | |
1892 | @end defvar | |
1893 | ||
1894 | @defvar SIGINT | |
1895 | Interrupt signal. | |
1896 | @end defvar | |
7403e409 NJ |
1897 | |
1898 | A full list of signals on the GNU system may be found in @ref{Standard | |
1899 | Signals,,,libc,The GNU C Library Reference Manual}. | |
a0e07ba4 NJ |
1900 | @end deffn |
1901 | ||
8f85c0c6 NJ |
1902 | @deffn {Scheme Procedure} raise sig |
1903 | @deffnx {C Function} scm_raise (sig) | |
a0e07ba4 | 1904 | Sends a specified signal @var{sig} to the current process, where |
7403e409 | 1905 | @var{sig} is as described for the @code{kill} procedure. |
a0e07ba4 NJ |
1906 | @end deffn |
1907 | ||
b6506f45 | 1908 | @deffn {Scheme Procedure} sigaction signum [handler [flags [thread]]] |
8f85c0c6 | 1909 | @deffnx {C Function} scm_sigaction (signum, handler, flags) |
b6506f45 | 1910 | @deffnx {C Function} scm_sigaction_for_thread (signum, handler, flags, thread) |
a0e07ba4 NJ |
1911 | Install or report the signal handler for a specified signal. |
1912 | ||
1913 | @var{signum} is the signal number, which can be specified using the value | |
1914 | of variables such as @code{SIGINT}. | |
1915 | ||
b6506f45 | 1916 | If @var{handler} is omitted, @code{sigaction} returns a pair: the |
7403e409 NJ |
1917 | @acronym{CAR} is the current signal hander, which will be either an |
1918 | integer with the value @code{SIG_DFL} (default action) or | |
1919 | @code{SIG_IGN} (ignore), or the Scheme procedure which handles the | |
1920 | signal, or @code{#f} if a non-Scheme procedure handles the signal. | |
1921 | The @acronym{CDR} contains the current @code{sigaction} flags for the | |
1922 | handler. | |
a0e07ba4 | 1923 | |
b6506f45 | 1924 | If @var{handler} is provided, it is installed as the new handler for |
0a50eeaa NJ |
1925 | @var{signum}. @var{handler} can be a Scheme procedure taking one |
1926 | argument, or the value of @code{SIG_DFL} (default action) or | |
a0e07ba4 | 1927 | @code{SIG_IGN} (ignore), or @code{#f} to restore whatever signal handler |
b6506f45 MV |
1928 | was installed before @code{sigaction} was first used. When a scheme |
1929 | procedure has been specified, that procedure will run in the given | |
1930 | @var{thread}. When no thread has been given, the thread that made this | |
1931 | call to @code{sigaction} is used. | |
1932 | ||
91f5e9f7 KR |
1933 | @var{flags} is a @code{logior} (@pxref{Bitwise Operations}) of the |
1934 | following (where provided by the system), or @code{0} for none. | |
1935 | ||
1936 | @defvar SA_NOCLDSTOP | |
1937 | By default, @code{SIGCHLD} is signalled when a child process stops | |
1938 | (ie.@: receives @code{SIGSTOP}), and when a child process terminates. | |
1939 | With the @code{SA_NOCLDSTOP} flag, @code{SIGCHLD} is only signalled | |
1940 | for termination, not stopping. | |
1941 | ||
1942 | @code{SA_NOCLDSTOP} has no effect on signals other than | |
1943 | @code{SIGCHLD}. | |
1944 | @end defvar | |
1945 | ||
1946 | @defvar SA_RESTART | |
1947 | If a signal occurs while in a system call, deliver the signal then | |
1948 | restart the system call (as opposed to returning an @code{EINTR} error | |
1949 | from that call). | |
91f5e9f7 KR |
1950 | @end defvar |
1951 | ||
1952 | The return value is a pair with information about the old handler as | |
1953 | described above. | |
a0e07ba4 | 1954 | |
7403e409 | 1955 | This interface does not provide access to the ``signal blocking'' |
a0e07ba4 NJ |
1956 | facility. Maybe this is not needed, since the thread support may |
1957 | provide solutions to the problem of consistent access to data | |
1958 | structures. | |
1959 | @end deffn | |
1960 | ||
8f85c0c6 NJ |
1961 | @deffn {Scheme Procedure} restore-signals |
1962 | @deffnx {C Function} scm_restore_signals () | |
a0e07ba4 NJ |
1963 | Return all signal handlers to the values they had before any call to |
1964 | @code{sigaction} was made. The return value is unspecified. | |
1965 | @end deffn | |
1966 | ||
8f85c0c6 NJ |
1967 | @deffn {Scheme Procedure} alarm i |
1968 | @deffnx {C Function} scm_alarm (i) | |
a0e07ba4 NJ |
1969 | Set a timer to raise a @code{SIGALRM} signal after the specified |
1970 | number of seconds (an integer). It's advisable to install a signal | |
1971 | handler for | |
1972 | @code{SIGALRM} beforehand, since the default action is to terminate | |
1973 | the process. | |
1974 | ||
1975 | The return value indicates the time remaining for the previous alarm, | |
1976 | if any. The new value replaces the previous alarm. If there was | |
1977 | no previous alarm, the return value is zero. | |
1978 | @end deffn | |
1979 | ||
8f85c0c6 NJ |
1980 | @deffn {Scheme Procedure} pause |
1981 | @deffnx {C Function} scm_pause () | |
a0e07ba4 NJ |
1982 | Pause the current process (thread?) until a signal arrives whose |
1983 | action is to either terminate the current process or invoke a | |
1984 | handler procedure. The return value is unspecified. | |
1985 | @end deffn | |
1986 | ||
bf5df489 KR |
1987 | @deffn {Scheme Procedure} sleep secs |
1988 | @deffnx {Scheme Procedure} usleep usecs | |
1989 | @deffnx {C Function} scm_sleep (secs) | |
1990 | @deffnx {C Function} scm_usleep (usecs) | |
1991 | Wait the given period @var{secs} seconds or @var{usecs} microseconds | |
1992 | (both integers). If a signal arrives the wait stops and the return | |
1993 | value is the time remaining, in seconds or microseconds respectively. | |
1994 | If the period elapses with no signal the return is zero. | |
9401323e | 1995 | |
bf5df489 KR |
1996 | On most systems the process scheduler is not microsecond accurate and |
1997 | the actual period slept by @code{usleep} might be rounded to a system | |
1998 | clock tick boundary, which might be 10 milliseconds for instance. | |
9401323e | 1999 | |
bf5df489 KR |
2000 | See @code{scm_std_sleep} and @code{scm_std_usleep} for equivalents at |
2001 | the C level (@pxref{Blocking}). | |
a0e07ba4 NJ |
2002 | @end deffn |
2003 | ||
8f85c0c6 | 2004 | @deffn {Scheme Procedure} getitimer which_timer |
bf5df489 | 2005 | @deffnx {Scheme Procedure} setitimer which_timer interval_seconds interval_microseconds periodic_seconds periodic_microseconds |
8f85c0c6 | 2006 | @deffnx {C Function} scm_getitimer (which_timer) |
bf5df489 KR |
2007 | @deffnx {C Function} scm_setitimer (which_timer, interval_seconds, interval_microseconds, periodic_seconds, periodic_microseconds) |
2008 | Get or set the periods programmed in certain system timers. These | |
2009 | timers have a current interval value which counts down and on reaching | |
2010 | zero raises a signal. An optional periodic value can be set to | |
2011 | restart from there each time, for periodic operation. | |
2012 | @var{which_timer} is one of the following values | |
2013 | ||
2014 | @defvar ITIMER_REAL | |
2015 | A real-time timer, counting down elapsed real time. At zero it raises | |
2016 | @code{SIGALRM}. This is like @code{alarm} above, but with a higher | |
2017 | resolution period. | |
2018 | @end defvar | |
2019 | ||
2020 | @defvar ITIMER_VIRTUAL | |
2021 | A virtual-time timer, counting down while the current process is | |
2022 | actually using CPU. At zero it raises @code{SIGVTALRM}. | |
2023 | @end defvar | |
2024 | ||
2025 | @defvar ITIMER_PROF | |
2026 | A profiling timer, counting down while the process is running (like | |
2027 | @code{ITIMER_VIRTUAL}) and also while system calls are running on the | |
2028 | process's behalf. At zero it raises a @code{SIGPROF}. | |
2029 | ||
2030 | This timer is intended for profiling where a program is spending its | |
2031 | time (by looking where it is when the timer goes off). | |
2032 | @end defvar | |
2033 | ||
2034 | @code{getitimer} returns the current timer value and its programmed | |
2035 | restart value, as a list containing two pairs. Each pair is a time in | |
2036 | seconds and microseconds: @code{((@var{interval_secs} | |
2037 | . @var{interval_usecs}) (@var{periodic_secs} | |
2038 | . @var{periodic_usecs}))}. | |
2039 | ||
2040 | @code{setitimer} sets the timer values similarly, in seconds and | |
2041 | microseconds (which must be integers). The periodic value can be zero | |
2042 | to have the timer run down just once. The return value is the timer's | |
2043 | previous setting, in the same form as @code{getitimer} returns. | |
9401323e | 2044 | |
bf5df489 KR |
2045 | @example |
2046 | (setitimer ITIMER_REAL | |
2047 | 5 500000 ;; first SIGALRM in 5.5 seconds time | |
2048 | 2 0) ;; then repeat every 2 seconds | |
2049 | @end example | |
9401323e | 2050 | |
bf5df489 KR |
2051 | Although the timers are programmed in microseconds, the actual |
2052 | accuracy might not be that high. | |
a0e07ba4 NJ |
2053 | @end deffn |
2054 | ||
2055 | ||
2056 | @node Terminals and Ptys | |
3229f68b | 2057 | @subsection Terminals and Ptys |
a0e07ba4 | 2058 | |
8f85c0c6 NJ |
2059 | @deffn {Scheme Procedure} isatty? port |
2060 | @deffnx {C Function} scm_isatty_p (port) | |
f3dfb8ac | 2061 | @cindex terminal |
a0e07ba4 NJ |
2062 | Return @code{#t} if @var{port} is using a serial non--file |
2063 | device, otherwise @code{#f}. | |
2064 | @end deffn | |
2065 | ||
8f85c0c6 NJ |
2066 | @deffn {Scheme Procedure} ttyname port |
2067 | @deffnx {C Function} scm_ttyname (port) | |
f3dfb8ac | 2068 | @cindex terminal |
a0e07ba4 NJ |
2069 | Return a string with the name of the serial terminal device |
2070 | underlying @var{port}. | |
2071 | @end deffn | |
2072 | ||
8f85c0c6 NJ |
2073 | @deffn {Scheme Procedure} ctermid |
2074 | @deffnx {C Function} scm_ctermid () | |
f3dfb8ac | 2075 | @cindex terminal |
a0e07ba4 NJ |
2076 | Return a string containing the file name of the controlling |
2077 | terminal for the current process. | |
2078 | @end deffn | |
2079 | ||
8f85c0c6 NJ |
2080 | @deffn {Scheme Procedure} tcgetpgrp port |
2081 | @deffnx {C Function} scm_tcgetpgrp (port) | |
f3dfb8ac | 2082 | @cindex process group |
a0e07ba4 NJ |
2083 | Return the process group ID of the foreground process group |
2084 | associated with the terminal open on the file descriptor | |
2085 | underlying @var{port}. | |
2086 | ||
2087 | If there is no foreground process group, the return value is a | |
2088 | number greater than 1 that does not match the process group ID | |
2089 | of any existing process group. This can happen if all of the | |
2090 | processes in the job that was formerly the foreground job have | |
2091 | terminated, and no other job has yet been moved into the | |
2092 | foreground. | |
2093 | @end deffn | |
2094 | ||
8f85c0c6 NJ |
2095 | @deffn {Scheme Procedure} tcsetpgrp port pgid |
2096 | @deffnx {C Function} scm_tcsetpgrp (port, pgid) | |
f3dfb8ac | 2097 | @cindex process group |
a0e07ba4 NJ |
2098 | Set the foreground process group ID for the terminal used by the file |
2099 | descriptor underlying @var{port} to the integer @var{pgid}. | |
2100 | The calling process | |
2101 | must be a member of the same session as @var{pgid} and must have the same | |
2102 | controlling terminal. The return value is unspecified. | |
2103 | @end deffn | |
2104 | ||
2105 | @node Pipes | |
3229f68b | 2106 | @subsection Pipes |
f3dfb8ac | 2107 | @cindex pipe |
a0e07ba4 | 2108 | |
cb62d8e5 | 2109 | The following procedures are similar to the @code{popen} and |
7403e409 | 2110 | @code{pclose} system routines. The code is in a separate ``popen'' |
a0e07ba4 NJ |
2111 | module: |
2112 | ||
aba0dff5 | 2113 | @lisp |
a0e07ba4 | 2114 | (use-modules (ice-9 popen)) |
aba0dff5 | 2115 | @end lisp |
a0e07ba4 NJ |
2116 | |
2117 | @findex popen | |
cb62d8e5 KR |
2118 | @deffn {Scheme Procedure} open-pipe command mode |
2119 | @deffnx {Scheme Procedure} open-pipe* mode prog [args...] | |
2120 | Execute a command in a subprocess, with a pipe to it or from it, or | |
2121 | with pipes in both directions. | |
2122 | ||
2123 | @code{open-pipe} runs the shell @var{command} using @samp{/bin/sh -c}. | |
2124 | @code{open-pipe*} executes @var{prog} directly, with the optional | |
2125 | @var{args} arguments (all strings). | |
2126 | ||
2127 | @var{mode} should be one of the following values. @code{OPEN_READ} is | |
2128 | an input pipe, ie.@: to read from the subprocess. @code{OPEN_WRITE} | |
2129 | is an output pipe, ie.@: to write to it. | |
2130 | ||
2131 | @defvar OPEN_READ | |
2132 | @defvarx OPEN_WRITE | |
2133 | @defvarx OPEN_BOTH | |
2134 | @end defvar | |
2135 | ||
2136 | For an input pipe, the child's standard output is the pipe and | |
2137 | standard input is inherited from @code{current-input-port}. For an | |
2138 | output pipe, the child's standard input is the pipe and standard | |
2139 | output is inherited from @code{current-output-port}. In all cases | |
2140 | cases the child's standard error is inherited from | |
2141 | @code{current-error-port} (@pxref{Default Ports}). | |
2142 | ||
2143 | If those @code{current-X-ports} are not files of some kind, and hence | |
2144 | don't have file descriptors for the child, then @file{/dev/null} is | |
2145 | used instead. | |
7064e449 | 2146 | |
cb62d8e5 KR |
2147 | Care should be taken with @code{OPEN_BOTH}, a deadlock will occur if |
2148 | both parent and child are writing, and waiting until the write | |
2149 | completes before doing any reading. Each direction has | |
2150 | @code{PIPE_BUF} bytes of buffering (@pxref{Ports and File | |
2151 | Descriptors}), which will be enough for small writes, but not for say | |
2152 | putting a big file through a filter. | |
a0e07ba4 NJ |
2153 | @end deffn |
2154 | ||
8f85c0c6 | 2155 | @deffn {Scheme Procedure} open-input-pipe command |
a0e07ba4 | 2156 | Equivalent to @code{open-pipe} with mode @code{OPEN_READ}. |
bcf009c3 NJ |
2157 | |
2158 | @lisp | |
cb62d8e5 KR |
2159 | (let* ((port (open-input-pipe "date --utc")) |
2160 | (str (read-line port))) | |
2161 | (close-pipe port) | |
2162 | str) | |
2163 | @result{} "Mon Mar 11 20:10:44 UTC 2002" | |
bcf009c3 | 2164 | @end lisp |
a0e07ba4 NJ |
2165 | @end deffn |
2166 | ||
8f85c0c6 | 2167 | @deffn {Scheme Procedure} open-output-pipe command |
a0e07ba4 | 2168 | Equivalent to @code{open-pipe} with mode @code{OPEN_WRITE}. |
cb62d8e5 KR |
2169 | |
2170 | @lisp | |
2171 | (let ((port (open-output-pipe "lpr"))) | |
2172 | (display "Something for the line printer.\n" port) | |
2173 | (if (not (eqv? 0 (status:exit-val (close-pipe port)))) | |
2174 | (error "Cannot print"))) | |
2175 | @end lisp | |
a0e07ba4 NJ |
2176 | @end deffn |
2177 | ||
7064e449 MV |
2178 | @deffn {Scheme Procedure} open-input-output-pipe command |
2179 | Equivalent to @code{open-pipe} with mode @code{OPEN_BOTH}. | |
2180 | @end deffn | |
2181 | ||
a0e07ba4 | 2182 | @findex pclose |
8f85c0c6 | 2183 | @deffn {Scheme Procedure} close-pipe port |
cb62d8e5 KR |
2184 | Close a pipe created by @code{open-pipe}, wait for the process to |
2185 | terminate, and return the wait status code. The status is as per | |
2186 | @code{waitpid} and can be decoded with @code{status:exit-val} etc | |
2187 | (@pxref{Processes}) | |
a0e07ba4 NJ |
2188 | @end deffn |
2189 | ||
cb62d8e5 KR |
2190 | @sp 1 |
2191 | @code{waitpid WAIT_ANY} should not be used when pipes are open, since | |
2192 | it can reap a pipe's child process, causing an error from a subsequent | |
2193 | @code{close-pipe}. | |
2194 | ||
2195 | @code{close-port} (@pxref{Closing}) can close a pipe, but it doesn't | |
2196 | reap the child process. | |
2197 | ||
2198 | The garbage collector will close a pipe no longer in use, and reap the | |
2199 | child process with @code{waitpid}. If the child hasn't yet terminated | |
2200 | the garbage collector doesn't block, but instead checks again in the | |
2201 | next GC. | |
2202 | ||
2203 | Many systems have per-user and system-wide limits on the number of | |
2204 | processes, and a system-wide limit on the number of pipes, so pipes | |
2205 | should be closed explicitly when no longer needed, rather than letting | |
2206 | the garbage collector pick them up at some later time. | |
2207 | ||
2208 | ||
a0e07ba4 | 2209 | @node Networking |
3229f68b | 2210 | @subsection Networking |
f3dfb8ac | 2211 | @cindex network |
a0e07ba4 NJ |
2212 | |
2213 | @menu | |
13ed23db KR |
2214 | * Network Address Conversion:: |
2215 | * Network Databases:: | |
2216 | * Network Socket Address:: | |
2217 | * Network Sockets and Communication:: | |
2218 | * Internet Socket Examples:: | |
a0e07ba4 NJ |
2219 | @end menu |
2220 | ||
2221 | @node Network Address Conversion | |
3229f68b | 2222 | @subsubsection Network Address Conversion |
f3dfb8ac | 2223 | @cindex network address |
a0e07ba4 NJ |
2224 | |
2225 | This section describes procedures which convert internet addresses | |
2226 | between numeric and string formats. | |
2227 | ||
3229f68b | 2228 | @subsubheading IPv4 Address Conversion |
f3dfb8ac | 2229 | @cindex IPv4 |
a0e07ba4 | 2230 | |
957f9f62 | 2231 | An IPv4 Internet address is a 4-byte value, represented in Guile as an |
99d16776 KR |
2232 | integer in host byte order, so that say ``0.0.0.1'' is 1, or |
2233 | ``1.0.0.0'' is 16777216. | |
2234 | ||
2235 | Some underlying C functions use network byte order for addresses, | |
2236 | Guile converts as necessary so that at the Scheme level its host byte | |
2237 | order everywhere. | |
957f9f62 | 2238 | |
13ed23db KR |
2239 | @defvar INADDR_ANY |
2240 | For a server, this can be used with @code{bind} (@pxref{Network | |
2241 | Sockets and Communication}) to allow connections from any interface on | |
2242 | the machine. | |
957f9f62 KR |
2243 | @end defvar |
2244 | ||
2245 | @defvar INADDR_BROADCAST | |
2246 | The broadcast address on the local network. | |
2247 | @end defvar | |
2248 | ||
13ed23db KR |
2249 | @defvar INADDR_LOOPBACK |
2250 | The address of the local host using the loopback device, ie.@: | |
2251 | @samp{127.0.0.1}. | |
2252 | @end defvar | |
2253 | ||
957f9f62 KR |
2254 | @c INADDR_NONE is defined in the code, but serves no purpose. |
2255 | @c inet_addr() returns it as an error indication, but that function | |
2256 | @c isn't provided, for the good reason that inet_aton() does the same | |
2257 | @c job and gives an unambiguous error indication. (INADDR_NONE is a | |
2258 | @c valid 4-byte value, in glibc it's the same as INADDR_BROADCAST.) | |
2259 | @c | |
2260 | @c @defvar INADDR_NONE | |
2261 | @c No address. | |
2262 | @c @end defvar | |
2263 | ||
8f85c0c6 NJ |
2264 | @deffn {Scheme Procedure} inet-aton address |
2265 | @deffnx {C Function} scm_inet_aton (address) | |
3452e666 LC |
2266 | This function is deprecated in favor of @code{inet-pton}. |
2267 | ||
a0e07ba4 NJ |
2268 | Convert an IPv4 Internet address from printable string |
2269 | (dotted decimal notation) to an integer. E.g., | |
2270 | ||
2271 | @lisp | |
2272 | (inet-aton "127.0.0.1") @result{} 2130706433 | |
2273 | @end lisp | |
2274 | @end deffn | |
2275 | ||
8f85c0c6 NJ |
2276 | @deffn {Scheme Procedure} inet-ntoa inetid |
2277 | @deffnx {C Function} scm_inet_ntoa (inetid) | |
3452e666 LC |
2278 | This function is deprecated in favor of @code{inet-ntop}. |
2279 | ||
a0e07ba4 NJ |
2280 | Convert an IPv4 Internet address to a printable |
2281 | (dotted decimal notation) string. E.g., | |
2282 | ||
2283 | @lisp | |
2284 | (inet-ntoa 2130706433) @result{} "127.0.0.1" | |
2285 | @end lisp | |
2286 | @end deffn | |
2287 | ||
8f85c0c6 NJ |
2288 | @deffn {Scheme Procedure} inet-netof address |
2289 | @deffnx {C Function} scm_inet_netof (address) | |
a0e07ba4 NJ |
2290 | Return the network number part of the given IPv4 |
2291 | Internet address. E.g., | |
2292 | ||
2293 | @lisp | |
2294 | (inet-netof 2130706433) @result{} 127 | |
2295 | @end lisp | |
2296 | @end deffn | |
2297 | ||
8f85c0c6 NJ |
2298 | @deffn {Scheme Procedure} inet-lnaof address |
2299 | @deffnx {C Function} scm_lnaof (address) | |
a0e07ba4 NJ |
2300 | Return the local-address-with-network part of the given |
2301 | IPv4 Internet address, using the obsolete class A/B/C system. | |
2302 | E.g., | |
2303 | ||
2304 | @lisp | |
2305 | (inet-lnaof 2130706433) @result{} 1 | |
2306 | @end lisp | |
2307 | @end deffn | |
2308 | ||
8f85c0c6 NJ |
2309 | @deffn {Scheme Procedure} inet-makeaddr net lna |
2310 | @deffnx {C Function} scm_inet_makeaddr (net, lna) | |
a0e07ba4 NJ |
2311 | Make an IPv4 Internet address by combining the network number |
2312 | @var{net} with the local-address-within-network number | |
2313 | @var{lna}. E.g., | |
2314 | ||
2315 | @lisp | |
2316 | (inet-makeaddr 127 1) @result{} 2130706433 | |
2317 | @end lisp | |
2318 | @end deffn | |
2319 | ||
3229f68b | 2320 | @subsubheading IPv6 Address Conversion |
f3dfb8ac | 2321 | @cindex IPv6 |
a0e07ba4 | 2322 | |
99d16776 KR |
2323 | An IPv6 Internet address is a 16-byte value, represented in Guile as |
2324 | an integer in host byte order, so that say ``::1'' is 1. | |
2325 | ||
8f85c0c6 NJ |
2326 | @deffn {Scheme Procedure} inet-ntop family address |
2327 | @deffnx {C Function} scm_inet_ntop (family, address) | |
99d16776 | 2328 | Convert a network address from an integer to a printable string. |
a0e07ba4 NJ |
2329 | @var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g., |
2330 | ||
2331 | @lisp | |
2332 | (inet-ntop AF_INET 2130706433) @result{} "127.0.0.1" | |
187a4390 NJ |
2333 | (inet-ntop AF_INET6 (- (expt 2 128) 1)) |
2334 | @result{} "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" | |
a0e07ba4 NJ |
2335 | @end lisp |
2336 | @end deffn | |
2337 | ||
8f85c0c6 NJ |
2338 | @deffn {Scheme Procedure} inet-pton family address |
2339 | @deffnx {C Function} scm_inet_pton (family, address) | |
99d16776 KR |
2340 | Convert a string containing a printable network address to an integer |
2341 | address. @var{family} can be @code{AF_INET} or @code{AF_INET6}. | |
2342 | E.g., | |
a0e07ba4 NJ |
2343 | |
2344 | @lisp | |
2345 | (inet-pton AF_INET "127.0.0.1") @result{} 2130706433 | |
2346 | (inet-pton AF_INET6 "::1") @result{} 1 | |
2347 | @end lisp | |
2348 | @end deffn | |
2349 | ||
2350 | ||
2351 | @node Network Databases | |
3229f68b | 2352 | @subsubsection Network Databases |
f3dfb8ac | 2353 | @cindex network database |
a0e07ba4 NJ |
2354 | |
2355 | This section describes procedures which query various network databases. | |
2356 | Care should be taken when using the database routines since they are not | |
2357 | reentrant. | |
2358 | ||
55ae00ea LC |
2359 | @subsubheading @code{getaddrinfo} |
2360 | ||
2361 | @cindex @code{addrinfo} object type | |
2362 | @cindex host name lookup | |
2363 | @cindex service name lookup | |
2364 | ||
2365 | The @code{getaddrinfo} procedure maps host and service names to socket addresses | |
2366 | and associated information in a protocol-independent way. | |
2367 | ||
2368 | @deffn {Scheme Procedure} getaddrinfo name service [hint_flags [hint_family [hint_socktype [hint_protocol]]]] | |
2369 | @deffnx {C Function} scm_getaddrinfo (name, service, hint_flags, hint_family, hint_socktype, hint_protocol) | |
2370 | Return a list of @code{addrinfo} structures containing | |
2371 | a socket address and associated information for host @var{name} | |
2372 | and/or @var{service} to be used in creating a socket with | |
2373 | which to address the specified service. | |
2374 | ||
2375 | @example | |
2376 | (let* ((ai (car (getaddrinfo "www.gnu.org" "http"))) | |
2377 | (s (socket (addrinfo:fam ai) (addrinfo:socktype ai) | |
d2add8ae | 2378 | (addrinfo:protocol ai)))) |
55ae00ea LC |
2379 | (connect s (addrinfo:addr ai)) |
2380 | s) | |
2381 | @end example | |
2382 | ||
2383 | When @var{service} is omitted or is @code{#f}, return | |
2384 | network-level addresses for @var{name}. When @var{name} | |
2385 | is @code{#f} @var{service} must be provided and service | |
2386 | locations local to the caller are returned. | |
2387 | ||
2388 | Additional hints can be provided. When specified, | |
2389 | @var{hint_flags} should be a bitwise-or of zero or more | |
2390 | constants among the following: | |
2391 | ||
2392 | @table @code | |
2393 | @item AI_PASSIVE | |
2394 | Socket address is intended for @code{bind}. | |
2395 | ||
2396 | @item AI_CANONNAME | |
2397 | Request for canonical host name, available via | |
2398 | @code{addrinfo:canonname}. This makes sense mainly when | |
2399 | DNS lookups are involved. | |
2400 | ||
2401 | @item AI_NUMERICHOST | |
2402 | Specifies that @var{name} is a numeric host address string | |
2403 | (e.g., @code{"127.0.0.1"}), meaning that name resolution | |
2404 | will not be used. | |
2405 | ||
2406 | @item AI_NUMERICSERV | |
2407 | Likewise, specifies that @var{service} is a numeric port | |
2408 | string (e.g., @code{"80"}). | |
2409 | ||
2410 | @item AI_ADDRCONFIG | |
2411 | Return only addresses configured on the local system It is | |
2412 | highly recommended to provide this flag when the returned | |
2413 | socket addresses are to be used to make connections; | |
2414 | otherwise, some of the returned addresses could be unreachable | |
2415 | or use a protocol that is not supported. | |
2416 | ||
2417 | @item AI_V4MAPPED | |
2418 | When looking up IPv6 addresses, return mapped IPv4 addresses if | |
2419 | there is no IPv6 address available at all. | |
2420 | ||
2421 | @item AI_ALL | |
2422 | If this flag is set along with @code{AI_V4MAPPED} when looking up IPv6 | |
2423 | addresses, return all IPv6 addresses as well as all IPv4 addresses, the latter | |
2424 | mapped to IPv6 format. | |
2425 | @end table | |
2426 | ||
2427 | When given, @var{hint_family} should specify the requested | |
2428 | address family, e.g., @code{AF_INET6}. Similarly, | |
2429 | @var{hint_socktype} should specify the requested socket type | |
2430 | (e.g., @code{SOCK_DGRAM}), and @var{hint_protocol} should | |
2431 | specify the requested protocol (its value is interpretered | |
2432 | as in calls to @code{socket}). | |
2433 | ||
2434 | On error, an exception with key @code{getaddrinfo-error} is | |
2435 | thrown, with an error code (an integer) as its argument: | |
2436 | ||
2437 | @example | |
2438 | (catch 'getaddrinfo-error | |
2439 | (lambda () | |
2440 | (getaddrinfo "www.gnu.org" "gopher")) | |
2441 | (lambda (key errcode) | |
2442 | (cond ((= errcode EAI_SERVICE) | |
2443 | (display "doesn't know about Gopher!\n")) | |
2444 | ((= errcode EAI_NONAME) | |
2445 | (display "www.gnu.org not found\\n")) | |
2446 | (else | |
2447 | (format #t "something wrong: ~a\n" | |
2448 | (gai-strerror errcode)))))) | |
2449 | @end example | |
2450 | ||
2451 | Error codes are: | |
2452 | ||
2453 | @table @code | |
2454 | @item EAI_AGAIN | |
2455 | The name or service could not be resolved at this time. Future | |
2456 | attempts may succeed. | |
2457 | ||
2458 | @item EAI_BADFLAGS | |
2459 | @var{hint_flags} contains an invalid value. | |
2460 | ||
2461 | @item EAI_FAIL | |
2462 | A non-recoverable error occurred when attempting to | |
2463 | resolve the name. | |
2464 | ||
2465 | @item EAI_FAMILY | |
2466 | @var{hint_family} was not recognized. | |
2467 | ||
2468 | @item EAI_NONAME | |
2469 | Either @var{name} does not resolve for the supplied parameters, | |
2470 | or neither @var{name} nor @var{service} were supplied. | |
2471 | ||
2472 | @item EAI_SERVICE | |
2473 | @var{service} was not recognized for the specified socket type. | |
2474 | ||
2475 | @item EAI_SOCKTYPE | |
2476 | @var{hint_socktype} was not recognized. | |
2477 | ||
2478 | @item EAI_SYSTEM | |
2479 | A system error occurred; the error code can be found in | |
2480 | @code{errno}. | |
2481 | @end table | |
2482 | ||
2483 | Users are encouraged to read the | |
2484 | @url{http://www.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html, | |
2485 | "POSIX specification} for more details. | |
2486 | @end deffn | |
2487 | ||
2488 | The following procedures take an @code{addrinfo} object as returned by | |
2489 | @code{getaddrinfo}: | |
2490 | ||
2491 | @deffn {Scheme Procedure} addrinfo:flags ai | |
2492 | Return flags for @var{ai} as a bitwise or of @code{AI_} values (see above). | |
2493 | @end deffn | |
2494 | ||
2495 | @deffn {Scheme Procedure} addrinfo:fam ai | |
2496 | Return the address family of @var{ai} (a @code{AF_} value). | |
2497 | @end deffn | |
2498 | ||
2499 | @deffn {Scheme Procedure} addrinfo:socktype ai | |
2500 | Return the socket type for @var{ai} (a @code{SOCK_} value). | |
2501 | @end deffn | |
2502 | ||
2503 | @deffn {Scheme Procedure} addrinfo:protocol ai | |
2504 | Return the protocol of @var{ai}. | |
2505 | @end deffn | |
2506 | ||
2507 | @deffn {Scheme Procedure} addrinfo:addr ai | |
2508 | Return the socket address associated with @var{ai} as a @code{sockaddr} | |
2509 | object (@pxref{Network Socket Address}). | |
2510 | @end deffn | |
2511 | ||
2512 | @deffn {Scheme Procedure} addrinfo:canonname ai | |
2513 | Return a string for the canonical name associated with @var{ai} if | |
2514 | the @code{AI_CANONNAME} flag was supplied. | |
2515 | @end deffn | |
2516 | ||
3229f68b | 2517 | @subsubheading The Host Database |
f3dfb8ac KR |
2518 | @cindex @file{/etc/hosts} |
2519 | @cindex network database | |
a0e07ba4 NJ |
2520 | |
2521 | A @dfn{host object} is a structure that represents what is known about a | |
2522 | network host, and is the usual way of representing a system's network | |
2523 | identity inside software. | |
2524 | ||
2525 | The following functions accept a host object and return a selected | |
2526 | component: | |
2527 | ||
8f85c0c6 | 2528 | @deffn {Scheme Procedure} hostent:name host |
7403e409 | 2529 | The ``official'' hostname for @var{host}. |
a0e07ba4 | 2530 | @end deffn |
8f85c0c6 | 2531 | @deffn {Scheme Procedure} hostent:aliases host |
a0e07ba4 NJ |
2532 | A list of aliases for @var{host}. |
2533 | @end deffn | |
8f85c0c6 | 2534 | @deffn {Scheme Procedure} hostent:addrtype host |
99d16776 KR |
2535 | The host address type, one of the @code{AF} constants, such as |
2536 | @code{AF_INET} or @code{AF_INET6}. | |
a0e07ba4 | 2537 | @end deffn |
8f85c0c6 | 2538 | @deffn {Scheme Procedure} hostent:length host |
a0e07ba4 NJ |
2539 | The length of each address for @var{host}, in bytes. |
2540 | @end deffn | |
8f85c0c6 | 2541 | @deffn {Scheme Procedure} hostent:addr-list host |
99d16776 KR |
2542 | The list of network addresses associated with @var{host}. For |
2543 | @code{AF_INET} these are integer IPv4 address (@pxref{Network Address | |
2544 | Conversion}). | |
a0e07ba4 NJ |
2545 | @end deffn |
2546 | ||
55ae00ea LC |
2547 | The following procedures can be used to search the host database. However, |
2548 | @code{getaddrinfo} should be preferred over them since it's more generic and | |
2549 | thread-safe. | |
a0e07ba4 | 2550 | |
8f85c0c6 NJ |
2551 | @deffn {Scheme Procedure} gethost [host] |
2552 | @deffnx {Scheme Procedure} gethostbyname hostname | |
2553 | @deffnx {Scheme Procedure} gethostbyaddr address | |
2554 | @deffnx {C Function} scm_gethost (host) | |
a0e07ba4 NJ |
2555 | Look up a host by name or address, returning a host object. The |
2556 | @code{gethost} procedure will accept either a string name or an integer | |
2557 | address; if given no arguments, it behaves like @code{gethostent} (see | |
2558 | below). If a name or address is supplied but the address can not be | |
2559 | found, an error will be thrown to one of the keys: | |
2560 | @code{host-not-found}, @code{try-again}, @code{no-recovery} or | |
2561 | @code{no-data}, corresponding to the equivalent @code{h_error} values. | |
2562 | Unusual conditions may result in errors thrown to the | |
2563 | @code{system-error} or @code{misc_error} keys. | |
bcf009c3 NJ |
2564 | |
2565 | @lisp | |
2566 | (gethost "www.gnu.org") | |
2567 | @result{} #("www.gnu.org" () 2 4 (3353880842)) | |
2568 | ||
2569 | (gethostbyname "www.emacs.org") | |
2570 | @result{} #("emacs.org" ("www.emacs.org") 2 4 (1073448978)) | |
2571 | @end lisp | |
a0e07ba4 NJ |
2572 | @end deffn |
2573 | ||
2574 | The following procedures may be used to step through the host | |
2575 | database from beginning to end. | |
2576 | ||
8f85c0c6 | 2577 | @deffn {Scheme Procedure} sethostent [stayopen] |
a0e07ba4 NJ |
2578 | Initialize an internal stream from which host objects may be read. This |
2579 | procedure must be called before any calls to @code{gethostent}, and may | |
2580 | also be called afterward to reset the host entry stream. If | |
2581 | @var{stayopen} is supplied and is not @code{#f}, the database is not | |
2582 | closed by subsequent @code{gethostbyname} or @code{gethostbyaddr} calls, | |
2583 | possibly giving an efficiency gain. | |
2584 | @end deffn | |
2585 | ||
8f85c0c6 | 2586 | @deffn {Scheme Procedure} gethostent |
a0e07ba4 NJ |
2587 | Return the next host object from the host database, or @code{#f} if |
2588 | there are no more hosts to be found (or an error has been encountered). | |
2589 | This procedure may not be used before @code{sethostent} has been called. | |
2590 | @end deffn | |
2591 | ||
8f85c0c6 | 2592 | @deffn {Scheme Procedure} endhostent |
a0e07ba4 NJ |
2593 | Close the stream used by @code{gethostent}. The return value is unspecified. |
2594 | @end deffn | |
2595 | ||
8f85c0c6 NJ |
2596 | @deffn {Scheme Procedure} sethost [stayopen] |
2597 | @deffnx {C Function} scm_sethost (stayopen) | |
a0e07ba4 NJ |
2598 | If @var{stayopen} is omitted, this is equivalent to @code{endhostent}. |
2599 | Otherwise it is equivalent to @code{sethostent stayopen}. | |
2600 | @end deffn | |
3229f68b MV |
2601 | |
2602 | @subsubheading The Network Database | |
f3dfb8ac | 2603 | @cindex network database |
a0e07ba4 NJ |
2604 | |
2605 | The following functions accept an object representing a network | |
2606 | and return a selected component: | |
2607 | ||
8f85c0c6 | 2608 | @deffn {Scheme Procedure} netent:name net |
7403e409 | 2609 | The ``official'' network name. |
a0e07ba4 | 2610 | @end deffn |
8f85c0c6 | 2611 | @deffn {Scheme Procedure} netent:aliases net |
a0e07ba4 NJ |
2612 | A list of aliases for the network. |
2613 | @end deffn | |
8f85c0c6 | 2614 | @deffn {Scheme Procedure} netent:addrtype net |
a0e07ba4 NJ |
2615 | The type of the network number. Currently, this returns only |
2616 | @code{AF_INET}. | |
2617 | @end deffn | |
8f85c0c6 | 2618 | @deffn {Scheme Procedure} netent:net net |
a0e07ba4 NJ |
2619 | The network number. |
2620 | @end deffn | |
2621 | ||
2622 | The following procedures are used to search the network database: | |
2623 | ||
8f85c0c6 NJ |
2624 | @deffn {Scheme Procedure} getnet [net] |
2625 | @deffnx {Scheme Procedure} getnetbyname net-name | |
2626 | @deffnx {Scheme Procedure} getnetbyaddr net-number | |
2627 | @deffnx {C Function} scm_getnet (net) | |
a0e07ba4 NJ |
2628 | Look up a network by name or net number in the network database. The |
2629 | @var{net-name} argument must be a string, and the @var{net-number} | |
2630 | argument must be an integer. @code{getnet} will accept either type of | |
2631 | argument, behaving like @code{getnetent} (see below) if no arguments are | |
2632 | given. | |
2633 | @end deffn | |
2634 | ||
2635 | The following procedures may be used to step through the network | |
2636 | database from beginning to end. | |
2637 | ||
8f85c0c6 | 2638 | @deffn {Scheme Procedure} setnetent [stayopen] |
a0e07ba4 NJ |
2639 | Initialize an internal stream from which network objects may be read. This |
2640 | procedure must be called before any calls to @code{getnetent}, and may | |
2641 | also be called afterward to reset the net entry stream. If | |
2642 | @var{stayopen} is supplied and is not @code{#f}, the database is not | |
2643 | closed by subsequent @code{getnetbyname} or @code{getnetbyaddr} calls, | |
2644 | possibly giving an efficiency gain. | |
2645 | @end deffn | |
2646 | ||
8f85c0c6 | 2647 | @deffn {Scheme Procedure} getnetent |
a0e07ba4 NJ |
2648 | Return the next entry from the network database. |
2649 | @end deffn | |
2650 | ||
8f85c0c6 | 2651 | @deffn {Scheme Procedure} endnetent |
a0e07ba4 NJ |
2652 | Close the stream used by @code{getnetent}. The return value is unspecified. |
2653 | @end deffn | |
2654 | ||
8f85c0c6 NJ |
2655 | @deffn {Scheme Procedure} setnet [stayopen] |
2656 | @deffnx {C Function} scm_setnet (stayopen) | |
a0e07ba4 NJ |
2657 | If @var{stayopen} is omitted, this is equivalent to @code{endnetent}. |
2658 | Otherwise it is equivalent to @code{setnetent stayopen}. | |
2659 | @end deffn | |
2660 | ||
3229f68b | 2661 | @subsubheading The Protocol Database |
f3dfb8ac KR |
2662 | @cindex @file{/etc/protocols} |
2663 | @cindex protocols | |
2664 | @cindex network protocols | |
a0e07ba4 NJ |
2665 | |
2666 | The following functions accept an object representing a protocol | |
2667 | and return a selected component: | |
2668 | ||
8f85c0c6 | 2669 | @deffn {Scheme Procedure} protoent:name protocol |
7403e409 | 2670 | The ``official'' protocol name. |
a0e07ba4 | 2671 | @end deffn |
8f85c0c6 | 2672 | @deffn {Scheme Procedure} protoent:aliases protocol |
a0e07ba4 NJ |
2673 | A list of aliases for the protocol. |
2674 | @end deffn | |
8f85c0c6 | 2675 | @deffn {Scheme Procedure} protoent:proto protocol |
a0e07ba4 NJ |
2676 | The protocol number. |
2677 | @end deffn | |
2678 | ||
2679 | The following procedures are used to search the protocol database: | |
2680 | ||
8f85c0c6 NJ |
2681 | @deffn {Scheme Procedure} getproto [protocol] |
2682 | @deffnx {Scheme Procedure} getprotobyname name | |
2683 | @deffnx {Scheme Procedure} getprotobynumber number | |
2684 | @deffnx {C Function} scm_getproto (protocol) | |
a0e07ba4 NJ |
2685 | Look up a network protocol by name or by number. @code{getprotobyname} |
2686 | takes a string argument, and @code{getprotobynumber} takes an integer | |
2687 | argument. @code{getproto} will accept either type, behaving like | |
2688 | @code{getprotoent} (see below) if no arguments are supplied. | |
2689 | @end deffn | |
2690 | ||
2691 | The following procedures may be used to step through the protocol | |
2692 | database from beginning to end. | |
2693 | ||
8f85c0c6 | 2694 | @deffn {Scheme Procedure} setprotoent [stayopen] |
a0e07ba4 NJ |
2695 | Initialize an internal stream from which protocol objects may be read. This |
2696 | procedure must be called before any calls to @code{getprotoent}, and may | |
2697 | also be called afterward to reset the protocol entry stream. If | |
2698 | @var{stayopen} is supplied and is not @code{#f}, the database is not | |
2699 | closed by subsequent @code{getprotobyname} or @code{getprotobynumber} calls, | |
2700 | possibly giving an efficiency gain. | |
2701 | @end deffn | |
2702 | ||
8f85c0c6 | 2703 | @deffn {Scheme Procedure} getprotoent |
a0e07ba4 NJ |
2704 | Return the next entry from the protocol database. |
2705 | @end deffn | |
2706 | ||
8f85c0c6 | 2707 | @deffn {Scheme Procedure} endprotoent |
a0e07ba4 NJ |
2708 | Close the stream used by @code{getprotoent}. The return value is unspecified. |
2709 | @end deffn | |
2710 | ||
8f85c0c6 NJ |
2711 | @deffn {Scheme Procedure} setproto [stayopen] |
2712 | @deffnx {C Function} scm_setproto (stayopen) | |
a0e07ba4 NJ |
2713 | If @var{stayopen} is omitted, this is equivalent to @code{endprotoent}. |
2714 | Otherwise it is equivalent to @code{setprotoent stayopen}. | |
2715 | @end deffn | |
2716 | ||
3229f68b | 2717 | @subsubheading The Service Database |
f3dfb8ac KR |
2718 | @cindex @file{/etc/services} |
2719 | @cindex services | |
2720 | @cindex network services | |
a0e07ba4 NJ |
2721 | |
2722 | The following functions accept an object representing a service | |
2723 | and return a selected component: | |
2724 | ||
8f85c0c6 | 2725 | @deffn {Scheme Procedure} servent:name serv |
7403e409 | 2726 | The ``official'' name of the network service. |
a0e07ba4 | 2727 | @end deffn |
8f85c0c6 | 2728 | @deffn {Scheme Procedure} servent:aliases serv |
a0e07ba4 NJ |
2729 | A list of aliases for the network service. |
2730 | @end deffn | |
8f85c0c6 | 2731 | @deffn {Scheme Procedure} servent:port serv |
a0e07ba4 NJ |
2732 | The Internet port used by the service. |
2733 | @end deffn | |
8f85c0c6 | 2734 | @deffn {Scheme Procedure} servent:proto serv |
a0e07ba4 NJ |
2735 | The protocol used by the service. A service may be listed many times |
2736 | in the database under different protocol names. | |
2737 | @end deffn | |
2738 | ||
2739 | The following procedures are used to search the service database: | |
2740 | ||
8f85c0c6 NJ |
2741 | @deffn {Scheme Procedure} getserv [name [protocol]] |
2742 | @deffnx {Scheme Procedure} getservbyname name protocol | |
2743 | @deffnx {Scheme Procedure} getservbyport port protocol | |
2744 | @deffnx {C Function} scm_getserv (name, protocol) | |
a0e07ba4 NJ |
2745 | Look up a network service by name or by service number, and return a |
2746 | network service object. The @var{protocol} argument specifies the name | |
2747 | of the desired protocol; if the protocol found in the network service | |
2748 | database does not match this name, a system error is signalled. | |
2749 | ||
2750 | The @code{getserv} procedure will take either a service name or number | |
2751 | as its first argument; if given no arguments, it behaves like | |
2752 | @code{getservent} (see below). | |
bcf009c3 NJ |
2753 | |
2754 | @lisp | |
2755 | (getserv "imap" "tcp") | |
2756 | @result{} #("imap2" ("imap") 143 "tcp") | |
2757 | ||
2758 | (getservbyport 88 "udp") | |
2759 | @result{} #("kerberos" ("kerberos5" "krb5") 88 "udp") | |
2760 | @end lisp | |
a0e07ba4 NJ |
2761 | @end deffn |
2762 | ||
2763 | The following procedures may be used to step through the service | |
2764 | database from beginning to end. | |
2765 | ||
8f85c0c6 | 2766 | @deffn {Scheme Procedure} setservent [stayopen] |
a0e07ba4 NJ |
2767 | Initialize an internal stream from which service objects may be read. This |
2768 | procedure must be called before any calls to @code{getservent}, and may | |
2769 | also be called afterward to reset the service entry stream. If | |
2770 | @var{stayopen} is supplied and is not @code{#f}, the database is not | |
2771 | closed by subsequent @code{getservbyname} or @code{getservbyport} calls, | |
2772 | possibly giving an efficiency gain. | |
2773 | @end deffn | |
2774 | ||
8f85c0c6 | 2775 | @deffn {Scheme Procedure} getservent |
a0e07ba4 NJ |
2776 | Return the next entry from the services database. |
2777 | @end deffn | |
2778 | ||
8f85c0c6 | 2779 | @deffn {Scheme Procedure} endservent |
a0e07ba4 NJ |
2780 | Close the stream used by @code{getservent}. The return value is unspecified. |
2781 | @end deffn | |
2782 | ||
8f85c0c6 NJ |
2783 | @deffn {Scheme Procedure} setserv [stayopen] |
2784 | @deffnx {C Function} scm_setserv (stayopen) | |
a0e07ba4 NJ |
2785 | If @var{stayopen} is omitted, this is equivalent to @code{endservent}. |
2786 | Otherwise it is equivalent to @code{setservent stayopen}. | |
2787 | @end deffn | |
2788 | ||
13ed23db KR |
2789 | |
2790 | @node Network Socket Address | |
2791 | @subsubsection Network Socket Address | |
32ff7370 KR |
2792 | @cindex socket address |
2793 | @cindex network socket address | |
2794 | @tpindex Socket address | |
2795 | ||
2796 | A @dfn{socket address} object identifies a socket endpoint for | |
2797 | communication. In the case of @code{AF_INET} for instance, the socket | |
2798 | address object comprises the host address (or interface on the host) | |
2799 | and a port number which specifies a particular open socket in a | |
2800 | running client or server process. A socket address object can be | |
2801 | created with, | |
2802 | ||
2803 | @deffn {Scheme Procedure} make-socket-address AF_INET ipv4addr port | |
2804 | @deffnx {Scheme Procedure} make-socket-address AF_INET6 ipv6addr port [flowinfo [scopeid]] | |
2805 | @deffnx {Scheme Procedure} make-socket-address AF_UNIX path | |
2806 | @deffnx {C Function} scm_make_socket_address family address arglist | |
13ed23db KR |
2807 | Return a new socket address object. The first argument is the address |
2808 | family, one of the @code{AF} constants, then the arguments vary | |
2809 | according to the family. | |
2810 | ||
2811 | For @code{AF_INET} the arguments are an IPv4 network address number | |
32ff7370 | 2812 | (@pxref{Network Address Conversion}), and a port number. |
13ed23db KR |
2813 | |
2814 | For @code{AF_INET6} the arguments are an IPv6 network address number | |
2815 | and a port number. Optional @var{flowinfo} and @var{scopeid} | |
2816 | arguments may be given (both integers, default 0). | |
2817 | ||
2818 | For @code{AF_UNIX} the argument is a filename (a string). | |
32ff7370 KR |
2819 | |
2820 | The C function @code{scm_make_socket_address} takes the @var{family} | |
2821 | and @var{address} arguments directly, then @var{arglist} is a list of | |
2822 | further arguments, being the port for IPv4, port and optional flowinfo | |
2823 | and scopeid for IPv6, or the empty list @code{SCM_EOL} for Unix | |
2824 | domain. | |
2825 | @end deffn | |
13ed23db KR |
2826 | |
2827 | @noindent | |
2828 | The following functions access the fields of a socket address object, | |
2829 | ||
2830 | @deffn {Scheme Procedure} sockaddr:fam sa | |
2831 | Return the address family from socket address object @var{sa}. This | |
2832 | is one of the @code{AF} constants (eg. @code{AF_INET}). | |
2833 | @end deffn | |
2834 | ||
2835 | @deffn {Scheme Procedure} sockaddr:path sa | |
2836 | For an @code{AF_UNIX} socket address object @var{sa}, return the | |
2837 | filename. | |
2838 | @end deffn | |
2839 | ||
2840 | @deffn {Scheme Procedure} sockaddr:addr sa | |
2841 | For an @code{AF_INET} or @code{AF_INET6} socket address object | |
2842 | @var{sa}, return the network address number. | |
2843 | @end deffn | |
2844 | ||
2845 | @deffn {Scheme Procedure} sockaddr:port sa | |
2846 | For an @code{AF_INET} or @code{AF_INET6} socket address object | |
2847 | @var{sa}, return the port number. | |
2848 | @end deffn | |
2849 | ||
2850 | @deffn {Scheme Procedure} sockaddr:flowinfo sa | |
2851 | For an @code{AF_INET6} socket address object @var{sa}, return the | |
2852 | flowinfo value. | |
2853 | @end deffn | |
2854 | ||
2855 | @deffn {Scheme Procedure} sockaddr:scopeid sa | |
2856 | For an @code{AF_INET6} socket address object @var{sa}, return the | |
2857 | scope ID value. | |
2858 | @end deffn | |
2859 | ||
32ff7370 KR |
2860 | @tpindex @code{struct sockaddr} |
2861 | @tpindex @code{sockaddr} | |
2862 | The functions below convert to and from the C @code{struct sockaddr} | |
2863 | (@pxref{Address Formats,,, libc, The GNU C Library Reference Manual}). | |
2864 | That structure is a generic type, an application can cast to or from | |
2865 | @code{struct sockaddr_in}, @code{struct sockaddr_in6} or @code{struct | |
2866 | sockaddr_un} according to the address family. | |
2867 | ||
2868 | In a @code{struct sockaddr} taken or returned, the byte ordering in | |
2869 | the fields follows the C conventions (@pxref{Byte Order,, Byte Order | |
2870 | Conversion, libc, The GNU C Library Reference Manual}). This means | |
2871 | network byte order for @code{AF_INET} host address | |
2872 | (@code{sin_addr.s_addr}) and port number (@code{sin_port}), and | |
2873 | @code{AF_INET6} port number (@code{sin6_port}). But at the Scheme | |
2874 | level these values are taken or returned in host byte order, so the | |
2875 | port is an ordinary integer, and the host address likewise is an | |
2876 | ordinary integer (as described in @ref{Network Address Conversion}). | |
2877 | ||
2878 | @deftypefn {C Function} {struct sockaddr *} scm_c_make_socket_address (SCM family, SCM address, SCM args, size_t *outsize) | |
2879 | Return a newly-@code{malloc}ed @code{struct sockaddr} created from | |
2880 | arguments like those taken by @code{scm_make_socket_address} above. | |
2881 | ||
2882 | The size (in bytes) of the @code{struct sockaddr} return is stored | |
2883 | into @code{*@var{outsize}}. An application must call @code{free} to | |
2884 | release the returned structure when no longer required. | |
2885 | @end deftypefn | |
2886 | ||
2887 | @deftypefn {C Function} SCM scm_from_sockaddr (const struct sockaddr *address, unsigned address_size) | |
2888 | Return a Scheme socket address object from the C @var{address} | |
2889 | structure. @var{address_size} is the size in bytes of @var{address}. | |
2890 | @end deftypefn | |
2891 | ||
2892 | @deftypefn {C Function} {struct sockaddr *} scm_to_sockaddr (SCM address, size_t *address_size) | |
2893 | Return a newly-@code{malloc}ed @code{struct sockaddr} from a Scheme | |
2894 | level socket address object. | |
2895 | ||
2896 | The size (in bytes) of the @code{struct sockaddr} return is stored | |
2897 | into @code{*@var{outsize}}. An application must call @code{free} to | |
2898 | release the returned structure when no longer required. | |
2899 | @end deftypefn | |
2900 | ||
13ed23db | 2901 | |
a0e07ba4 | 2902 | @node Network Sockets and Communication |
3229f68b | 2903 | @subsubsection Network Sockets and Communication |
f3dfb8ac KR |
2904 | @cindex socket |
2905 | @cindex network socket | |
a0e07ba4 NJ |
2906 | |
2907 | Socket ports can be created using @code{socket} and @code{socketpair}. | |
2908 | The ports are initially unbuffered, to make reading and writing to the | |
2909 | same port more reliable. A buffer can be added to the port using | |
7403e409 | 2910 | @code{setvbuf}; see @ref{Ports and File Descriptors}. |
a0e07ba4 | 2911 | |
9e996fb1 KR |
2912 | Most systems have limits on how many files and sockets can be open, so |
2913 | it's strongly recommended that socket ports be closed explicitly when | |
2914 | no longer required (@pxref{Ports}). | |
2915 | ||
99d16776 KR |
2916 | Some of the underlying C functions take values in network byte order, |
2917 | but the convention in Guile is that at the Scheme level everything is | |
2918 | ordinary host byte order and conversions are made automatically where | |
2919 | necessary. | |
a0e07ba4 | 2920 | |
8f85c0c6 NJ |
2921 | @deffn {Scheme Procedure} socket family style proto |
2922 | @deffnx {C Function} scm_socket (family, style, proto) | |
a0e07ba4 | 2923 | Return a new socket port of the type specified by @var{family}, |
3dba2dd9 KR |
2924 | @var{style} and @var{proto}. All three parameters are integers. The |
2925 | possible values for @var{family} are as follows, where supported by | |
2926 | the system, | |
2927 | ||
2928 | @defvar PF_UNIX | |
2929 | @defvarx PF_INET | |
2930 | @defvarx PF_INET6 | |
2931 | @end defvar | |
2932 | ||
2933 | The possible values for @var{style} are as follows, again where | |
2934 | supported by the system, | |
2935 | ||
2936 | @defvar SOCK_STREAM | |
2937 | @defvarx SOCK_DGRAM | |
2938 | @defvarx SOCK_RAW | |
0bd094c2 KR |
2939 | @defvarx SOCK_RDM |
2940 | @defvarx SOCK_SEQPACKET | |
3dba2dd9 | 2941 | @end defvar |
a0e07ba4 NJ |
2942 | |
2943 | @var{proto} can be obtained from a protocol name using | |
3dba2dd9 KR |
2944 | @code{getprotobyname} (@pxref{Network Databases}). A value of zero |
2945 | means the default protocol, which is usually right. | |
a0e07ba4 | 2946 | |
3dba2dd9 KR |
2947 | A socket cannot by used for communication until it has been connected |
2948 | somewhere, usually with either @code{connect} or @code{accept} below. | |
a0e07ba4 NJ |
2949 | @end deffn |
2950 | ||
8f85c0c6 NJ |
2951 | @deffn {Scheme Procedure} socketpair family style proto |
2952 | @deffnx {C Function} scm_socketpair (family, style, proto) | |
497cbe20 KR |
2953 | Return a pair, the @code{car} and @code{cdr} of which are two unnamed |
2954 | socket ports connected to each other. The connection is full-duplex, | |
2955 | so data can be transferred in either direction between the two. | |
2956 | ||
2957 | @var{family}, @var{style} and @var{proto} are as per @code{socket} | |
2958 | above. But many systems only support socket pairs in the | |
2959 | @code{PF_UNIX} family. Zero is likely to be the only meaningful value | |
2960 | for @var{proto}. | |
a0e07ba4 NJ |
2961 | @end deffn |
2962 | ||
8f85c0c6 | 2963 | @deffn {Scheme Procedure} getsockopt sock level optname |
8b6b6ce5 | 2964 | @deffnx {Scheme Procedure} setsockopt sock level optname value |
8f85c0c6 | 2965 | @deffnx {C Function} scm_getsockopt (sock, level, optname) |
8b6b6ce5 KR |
2966 | @deffnx {C Function} scm_setsockopt (sock, level, optname, value) |
2967 | Get or set an option on socket port @var{sock}. @code{getsockopt} | |
2968 | returns the current value. @code{setsockopt} sets a value and the | |
2969 | return is unspecified. | |
2970 | ||
2971 | @var{level} is an integer specifying a protocol layer, either | |
2972 | @code{SOL_SOCKET} for socket level options, or a protocol number from | |
2973 | the @code{IPPROTO} constants or @code{getprotoent} (@pxref{Network | |
2974 | Databases}). | |
2975 | ||
2976 | @defvar SOL_SOCKET | |
2977 | @defvarx IPPROTO_IP | |
2978 | @defvarx IPPROTO_TCP | |
2979 | @defvarx IPPROTO_UDP | |
2980 | @end defvar | |
a0e07ba4 | 2981 | |
8b6b6ce5 KR |
2982 | @var{optname} is an integer specifying an option within the protocol |
2983 | layer. | |
2984 | ||
2985 | For @code{SOL_SOCKET} level the following @var{optname}s are defined | |
2986 | (when provided by the system). For their meaning see | |
2987 | @ref{Socket-Level Options,,, libc, The GNU C Library Reference | |
2988 | Manual}, or @command{man 7 socket}. | |
2989 | ||
2990 | @defvar SO_DEBUG | |
2991 | @defvarx SO_REUSEADDR | |
2992 | @defvarx SO_STYLE | |
2993 | @defvarx SO_TYPE | |
2994 | @defvarx SO_ERROR | |
2995 | @defvarx SO_DONTROUTE | |
2996 | @defvarx SO_BROADCAST | |
2997 | @defvarx SO_SNDBUF | |
2998 | @defvarx SO_RCVBUF | |
2999 | @defvarx SO_KEEPALIVE | |
3000 | @defvarx SO_OOBINLINE | |
3001 | @defvarx SO_NO_CHECK | |
3002 | @defvarx SO_PRIORITY | |
3003 | The @var{value} taken or returned is an integer. | |
3004 | @end defvar | |
a0e07ba4 | 3005 | |
8b6b6ce5 KR |
3006 | @defvar SO_LINGER |
3007 | The @var{value} taken or returned is a pair of integers | |
3008 | @code{(@var{ENABLE} . @var{TIMEOUT})}. On old systems without timeout | |
3009 | support (ie.@: without @code{struct linger}), only @var{ENABLE} has an | |
3010 | effect but the value in Guile is always a pair. | |
3011 | @end defvar | |
a0e07ba4 | 3012 | |
8b6b6ce5 KR |
3013 | @c Note that we refer only to ``man ip'' here. On GNU/Linux it's |
3014 | @c ``man 7 ip'' but on NetBSD it's ``man 4 ip''. | |
3015 | @c | |
3016 | For IP level (@code{IPPROTO_IP}) the following @var{optname}s are | |
3017 | defined (when provided by the system). See @command{man ip} for what | |
3018 | they mean. | |
3019 | ||
3020 | @defvar IP_ADD_MEMBERSHIP | |
3021 | @defvarx IP_DROP_MEMBERSHIP | |
3022 | These can be used only with @code{setsockopt}, not @code{getsockopt}. | |
3023 | @var{value} is a pair @code{(@var{MULTIADDR} . @var{INTERFACEADDR})} | |
99d16776 | 3024 | of integer IPv4 addresses (@pxref{Network Address Conversion}). |
8b6b6ce5 KR |
3025 | @var{MULTIADDR} is a multicast address to be added to or dropped from |
3026 | the interface @var{INTERFACEADDR}. @var{INTERFACEADDR} can be | |
3027 | @code{INADDR_ANY} to have the system select the interface. | |
3028 | @var{INTERFACEADDR} can also be an interface index number, on systems | |
3029 | supporting that. | |
3030 | @end defvar | |
a0e07ba4 NJ |
3031 | @end deffn |
3032 | ||
8f85c0c6 NJ |
3033 | @deffn {Scheme Procedure} shutdown sock how |
3034 | @deffnx {C Function} scm_shutdown (sock, how) | |
99d16776 | 3035 | Sockets can be closed simply by using @code{close-port}. The |
85a9b4ed | 3036 | @code{shutdown} procedure allows reception or transmission on a |
a0e07ba4 NJ |
3037 | connection to be shut down individually, according to the parameter |
3038 | @var{how}: | |
3039 | ||
3040 | @table @asis | |
3041 | @item 0 | |
99d16776 | 3042 | Stop receiving data for this socket. If further data arrives, reject it. |
a0e07ba4 NJ |
3043 | @item 1 |
3044 | Stop trying to transmit data from this socket. Discard any | |
3045 | data waiting to be sent. Stop looking for acknowledgement of | |
3046 | data already sent; don't retransmit it if it is lost. | |
3047 | @item 2 | |
3048 | Stop both reception and transmission. | |
3049 | @end table | |
3050 | ||
3051 | The return value is unspecified. | |
3052 | @end deffn | |
3053 | ||
13ed23db KR |
3054 | @deffn {Scheme Procedure} connect sock sockaddr |
3055 | @deffnx {Scheme Procedure} connect sock AF_INET ipv4addr port | |
3056 | @deffnx {Scheme Procedure} connect sock AF_INET6 ipv6addr port [flowinfo [scopeid]] | |
3057 | @deffnx {Scheme Procedure} connect sock AF_UNIX path | |
8f85c0c6 | 3058 | @deffnx {C Function} scm_connect (sock, fam, address, args) |
13ed23db KR |
3059 | Initiate a connection on socket port @var{sock} to a given address. |
3060 | The destination is either a socket address object, or arguments the | |
3061 | same as @code{make-socket-address} would take to make such an object | |
3062 | (@pxref{Network Socket Address}). The return value is unspecified. | |
a0e07ba4 | 3063 | |
13ed23db | 3064 | @example |
32bc9257 NJ |
3065 | (connect sock AF_INET INADDR_LOOPBACK 23) |
3066 | (connect sock (make-socket-address AF_INET INADDR_LOOPBACK 23)) | |
13ed23db | 3067 | @end example |
a0e07ba4 NJ |
3068 | @end deffn |
3069 | ||
13ed23db KR |
3070 | @deffn {Scheme Procedure} bind sock sockaddr |
3071 | @deffnx {Scheme Procedure} bind sock AF_INET ipv4addr port | |
3072 | @deffnx {Scheme Procedure} bind sock AF_INET6 ipv6addr port [flowinfo [scopeid]] | |
3073 | @deffnx {Scheme Procedure} bind sock AF_UNIX path | |
8f85c0c6 | 3074 | @deffnx {C Function} scm_bind (sock, fam, address, args) |
13ed23db KR |
3075 | Bind socket port @var{sock} to the given address. The address is |
3076 | either a socket address object, or arguments the same as | |
3077 | @code{make-socket-address} would take to make such an object | |
3078 | (@pxref{Network Socket Address}). The return value is unspecified. | |
a0e07ba4 | 3079 | |
13ed23db KR |
3080 | Generally a socket is only explicitly bound to a particular address |
3081 | when making a server, ie. to listen on a particular port. For an | |
3082 | outgoing connection the system will assign a local address | |
3083 | automatically, if not already bound. | |
a0e07ba4 | 3084 | |
13ed23db KR |
3085 | @example |
3086 | (bind sock AF_INET INADDR_ANY 12345) | |
41185bfe | 3087 | (bind sock (make-socket-address AF_INET INADDR_ANY 12345)) |
13ed23db | 3088 | @end example |
a0e07ba4 NJ |
3089 | @end deffn |
3090 | ||
8f85c0c6 NJ |
3091 | @deffn {Scheme Procedure} listen sock backlog |
3092 | @deffnx {C Function} scm_listen (sock, backlog) | |
a0e07ba4 NJ |
3093 | Enable @var{sock} to accept connection |
3094 | requests. @var{backlog} is an integer specifying | |
3095 | the maximum length of the queue for pending connections. | |
3096 | If the queue fills, new clients will fail to connect until | |
3097 | the server calls @code{accept} to accept a connection from | |
3098 | the queue. | |
3099 | ||
3100 | The return value is unspecified. | |
3101 | @end deffn | |
3102 | ||
8f85c0c6 NJ |
3103 | @deffn {Scheme Procedure} accept sock |
3104 | @deffnx {C Function} scm_accept (sock) | |
13ed23db KR |
3105 | Accept a connection from socket port @var{sock} which has been enabled |
3106 | for listening with @code{listen} above. If there are no incoming | |
1b09b607 KR |
3107 | connections in the queue, wait until one is available (unless |
3108 | @code{O_NONBLOCK} has been set on the socket, @pxref{Ports and File | |
3109 | Descriptors,@code{fcntl}}). | |
a0e07ba4 | 3110 | |
13ed23db KR |
3111 | The return value is a pair. The @code{car} is a new socket port, |
3112 | connected and ready to communicate. The @code{cdr} is a socket | |
3113 | address object (@pxref{Network Socket Address}) which is where the | |
3114 | remote connection is from (like @code{getpeername} below). | |
a0e07ba4 | 3115 | |
13ed23db KR |
3116 | All communication takes place using the new socket returned. The |
3117 | given @var{sock} remains bound and listening, and @code{accept} may be | |
3118 | called on it again to get another incoming connection when desired. | |
2ce02471 | 3119 | @end deffn |
a0e07ba4 | 3120 | |
8f85c0c6 NJ |
3121 | @deffn {Scheme Procedure} getsockname sock |
3122 | @deffnx {C Function} scm_getsockname (sock) | |
13ed23db KR |
3123 | Return a socket address object which is the where @var{sock} is bound |
3124 | locally. @var{sock} may have obtained its local address from | |
3125 | @code{bind} (above), or if a @code{connect} is done with an otherwise | |
3126 | unbound socket (which is usual) then the system will have assigned an | |
3127 | address. | |
3128 | ||
3129 | Note that on many systems the address of a socket in the | |
3130 | @code{AF_UNIX} namespace cannot be read. | |
a0e07ba4 NJ |
3131 | @end deffn |
3132 | ||
8f85c0c6 NJ |
3133 | @deffn {Scheme Procedure} getpeername sock |
3134 | @deffnx {C Function} scm_getpeername (sock) | |
13ed23db KR |
3135 | Return a socket address object which is where @var{sock} is connected |
3136 | to, ie. the remote endpoint. | |
3137 | ||
3138 | Note that on many systems the address of a socket in the | |
3139 | @code{AF_UNIX} namespace cannot be read. | |
a0e07ba4 NJ |
3140 | @end deffn |
3141 | ||
8f85c0c6 NJ |
3142 | @deffn {Scheme Procedure} recv! sock buf [flags] |
3143 | @deffnx {C Function} scm_recv (sock, buf, flags) | |
a0e07ba4 NJ |
3144 | Receive data from a socket port. |
3145 | @var{sock} must already | |
3146 | be bound to the address from which data is to be received. | |
3147 | @var{buf} is a string into which | |
3148 | the data will be written. The size of @var{buf} limits | |
3149 | the amount of | |
3150 | data which can be received: in the case of packet | |
3151 | protocols, if a packet larger than this limit is encountered | |
3152 | then some data | |
3153 | will be irrevocably lost. | |
3154 | ||
2ce02471 NJ |
3155 | @vindex MSG_OOB |
3156 | @vindex MSG_PEEK | |
3157 | @vindex MSG_DONTROUTE | |
7403e409 NJ |
3158 | The optional @var{flags} argument is a value or bitwise OR of |
3159 | @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc. | |
a0e07ba4 NJ |
3160 | |
3161 | The value returned is the number of bytes read from the | |
3162 | socket. | |
3163 | ||
3164 | Note that the data is read directly from the socket file | |
3165 | descriptor: | |
3166 | any unread buffered port data is ignored. | |
3167 | @end deffn | |
3168 | ||
8f85c0c6 NJ |
3169 | @deffn {Scheme Procedure} send sock message [flags] |
3170 | @deffnx {C Function} scm_send (sock, message, flags) | |
2ce02471 NJ |
3171 | @vindex MSG_OOB |
3172 | @vindex MSG_PEEK | |
3173 | @vindex MSG_DONTROUTE | |
a0e07ba4 | 3174 | Transmit the string @var{message} on a socket port @var{sock}. |
7403e409 NJ |
3175 | @var{sock} must already be bound to a destination address. The value |
3176 | returned is the number of bytes transmitted---it's possible for this | |
3177 | to be less than the length of @var{message} if the socket is set to be | |
3178 | non-blocking. The optional @var{flags} argument is a value or bitwise | |
3179 | OR of @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc. | |
a0e07ba4 NJ |
3180 | |
3181 | Note that the data is written directly to the socket | |
3182 | file descriptor: | |
3183 | any unflushed buffered port data is ignored. | |
3184 | @end deffn | |
3185 | ||
8f85c0c6 NJ |
3186 | @deffn {Scheme Procedure} recvfrom! sock str [flags [start [end]]] |
3187 | @deffnx {C Function} scm_recvfrom (sock, str, flags, start, end) | |
40296bab KR |
3188 | Receive data from socket port @var{sock}, returning the originating |
3189 | address as well as the data. This function is usually for datagram | |
3190 | sockets, but can be used on stream-oriented sockets too. | |
3191 | ||
3192 | The data received is stored in the given @var{str}, the whole string | |
3193 | or just the region between the optional @var{start} and @var{end} | |
3194 | positions. The size of @var{str} limits the amount of data which can | |
3195 | be received. For datagram protocols if a packet larger than this is | |
3196 | received then excess bytes are irrevocably lost. | |
3197 | ||
3198 | The return value is a pair. The @code{car} is the number of bytes | |
3199 | read. The @code{cdr} is a socket address object (@pxref{Network | |
3200 | Socket Address}) which is where the data came from, or @code{#f} if | |
3201 | the origin is unknown. | |
a0e07ba4 | 3202 | |
2ce02471 NJ |
3203 | @vindex MSG_OOB |
3204 | @vindex MSG_PEEK | |
3205 | @vindex MSG_DONTROUTE | |
40296bab KR |
3206 | The optional @var{flags} argument is a or bitwise-OR (@code{logior}) |
3207 | of @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc. | |
a0e07ba4 | 3208 | |
40296bab KR |
3209 | Data is read directly from the socket file descriptor, any buffered |
3210 | port data is ignored. | |
a0e07ba4 | 3211 | |
40296bab KR |
3212 | @c This was linux kernel 2.6.15 and glibc 2.3.6, not sure what any |
3213 | @c specs are supposed to say about recvfrom threading. | |
3214 | @c | |
3215 | On a GNU/Linux system @code{recvfrom!} is not multi-threading, all | |
3216 | threads stop while a @code{recvfrom!} call is in progress. An | |
3217 | application may need to use @code{select}, @code{O_NONBLOCK} or | |
3218 | @code{MSG_DONTWAIT} to avoid this. | |
a0e07ba4 NJ |
3219 | @end deffn |
3220 | ||
13ed23db KR |
3221 | @deffn {Scheme Procedure} sendto sock message sockaddr [flags] |
3222 | @deffnx {Scheme Procedure} sendto sock message AF_INET ipv4addr port [flags] | |
3223 | @deffnx {Scheme Procedure} sendto sock message AF_INET6 ipv6addr port [flowinfo [scopeid [flags]]] | |
3224 | @deffnx {Scheme Procedure} sendto sock message AF_UNIX path [flags] | |
8f85c0c6 | 3225 | @deffnx {C Function} scm_sendto (sock, message, fam, address, args_and_flags) |
13ed23db KR |
3226 | Transmit the string @var{message} as a datagram on socket port |
3227 | @var{sock}. The destination is specified either as a socket address | |
3228 | object, or as arguments the same as would be taken by | |
3229 | @code{make-socket-address} to create such an object (@pxref{Network | |
3230 | Socket Address}). | |
3231 | ||
3232 | The destination address may be followed by an optional @var{flags} | |
3233 | argument which is a @code{logior} (@pxref{Bitwise Operations}) of | |
3234 | @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc. | |
a0e07ba4 NJ |
3235 | |
3236 | The value returned is the number of bytes transmitted -- | |
3237 | it's possible for | |
3238 | this to be less than the length of @var{message} if the | |
3239 | socket is | |
3240 | set to be non-blocking. | |
3241 | Note that the data is written directly to the socket | |
3242 | file descriptor: | |
3243 | any unflushed buffered port data is ignored. | |
3244 | @end deffn | |
3245 | ||
3246 | The following functions can be used to convert short and long integers | |
7403e409 | 3247 | between ``host'' and ``network'' order. Although the procedures above do |
a0e07ba4 NJ |
3248 | this automatically for addresses, the conversion will still need to |
3249 | be done when sending or receiving encoded integer data from the network. | |
3250 | ||
8f85c0c6 NJ |
3251 | @deffn {Scheme Procedure} htons value |
3252 | @deffnx {C Function} scm_htons (value) | |
a0e07ba4 NJ |
3253 | Convert a 16 bit quantity from host to network byte ordering. |
3254 | @var{value} is packed into 2 bytes, which are then converted | |
3255 | and returned as a new integer. | |
3256 | @end deffn | |
3257 | ||
8f85c0c6 NJ |
3258 | @deffn {Scheme Procedure} ntohs value |
3259 | @deffnx {C Function} scm_ntohs (value) | |
a0e07ba4 NJ |
3260 | Convert a 16 bit quantity from network to host byte ordering. |
3261 | @var{value} is packed into 2 bytes, which are then converted | |
3262 | and returned as a new integer. | |
3263 | @end deffn | |
3264 | ||
8f85c0c6 NJ |
3265 | @deffn {Scheme Procedure} htonl value |
3266 | @deffnx {C Function} scm_htonl (value) | |
a0e07ba4 NJ |
3267 | Convert a 32 bit quantity from host to network byte ordering. |
3268 | @var{value} is packed into 4 bytes, which are then converted | |
3269 | and returned as a new integer. | |
3270 | @end deffn | |
3271 | ||
8f85c0c6 NJ |
3272 | @deffn {Scheme Procedure} ntohl value |
3273 | @deffnx {C Function} scm_ntohl (value) | |
a0e07ba4 NJ |
3274 | Convert a 32 bit quantity from network to host byte ordering. |
3275 | @var{value} is packed into 4 bytes, which are then converted | |
3276 | and returned as a new integer. | |
3277 | @end deffn | |
3278 | ||
3279 | These procedures are inconvenient to use at present, but consider: | |
3280 | ||
3281 | @example | |
3282 | (define write-network-long | |
3283 | (lambda (value port) | |
3284 | (let ((v (make-uniform-vector 1 1 0))) | |
3285 | (uniform-vector-set! v 0 (htonl value)) | |
3286 | (uniform-vector-write v port)))) | |
3287 | ||
3288 | (define read-network-long | |
3289 | (lambda (port) | |
3290 | (let ((v (make-uniform-vector 1 1 0))) | |
3291 | (uniform-vector-read! v port) | |
3292 | (ntohl (uniform-vector-ref v 0))))) | |
3293 | @end example | |
3294 | ||
bcf009c3 NJ |
3295 | |
3296 | @node Internet Socket Examples | |
3229f68b | 3297 | @subsubsection Network Socket Examples |
f3dfb8ac KR |
3298 | @cindex network examples |
3299 | @cindex socket examples | |
bcf009c3 | 3300 | |
3229f68b | 3301 | The following give examples of how to use network sockets. |
bcf009c3 | 3302 | |
3229f68b | 3303 | @subsubheading Internet Socket Client Example |
bcf009c3 NJ |
3304 | |
3305 | @cindex socket client example | |
3306 | The following example demonstrates an Internet socket client. | |
3307 | It connects to the HTTP daemon running on the local machine and | |
3308 | returns the contents of the root index URL. | |
3309 | ||
3310 | @example | |
a8d0313f | 3311 | (let ((s (socket PF_INET SOCK_STREAM 0))) |
3452e666 | 3312 | (connect s AF_INET (inet-pton AF_INET "127.0.0.1") 80) |
bcf009c3 NJ |
3313 | (display "GET / HTTP/1.0\r\n\r\n" s) |
3314 | ||
3315 | (do ((line (read-line s) (read-line s))) | |
3316 | ((eof-object? line)) | |
3317 | (display line) | |
3318 | (newline))) | |
3319 | @end example | |
3320 | ||
3321 | ||
3229f68b | 3322 | @subsubheading Internet Socket Server Example |
bcf009c3 NJ |
3323 | |
3324 | @cindex socket server example | |
3325 | The following example shows a simple Internet server which listens on | |
3326 | port 2904 for incoming connections and sends a greeting back to the | |
3327 | client. | |
3328 | ||
3329 | @example | |
a8d0313f | 3330 | (let ((s (socket PF_INET SOCK_STREAM 0))) |
bcf009c3 | 3331 | (setsockopt s SOL_SOCKET SO_REUSEADDR 1) |
7403e409 | 3332 | ;; @r{Specific address?} |
3452e666 | 3333 | ;; @r{(bind s AF_INET (inet-pton AF_INET "127.0.0.1") 2904)} |
bcf009c3 NJ |
3334 | (bind s AF_INET INADDR_ANY 2904) |
3335 | (listen s 5) | |
3336 | ||
3337 | (simple-format #t "Listening for clients in pid: ~S" (getpid)) | |
3338 | (newline) | |
3339 | ||
3340 | (while #t | |
99d16776 KR |
3341 | (let* ((client-connection (accept s)) |
3342 | (client-details (cdr client-connection)) | |
3343 | (client (car client-connection))) | |
3344 | (simple-format #t "Got new client connection: ~S" | |
3345 | client-details) | |
3346 | (newline) | |
3347 | (simple-format #t "Client address: ~S" | |
3348 | (gethostbyaddr | |
3349 | (sockaddr:addr client-details))) | |
3350 | (newline) | |
3351 | ;; @r{Send back the greeting to the client port} | |
3352 | (display "Hello client\r\n" client) | |
3353 | (close client)))) | |
bcf009c3 NJ |
3354 | @end example |
3355 | ||
3356 | ||
a0e07ba4 | 3357 | @node System Identification |
3229f68b | 3358 | @subsection System Identification |
f3dfb8ac | 3359 | @cindex system name |
a0e07ba4 NJ |
3360 | |
3361 | This section lists the various procedures Guile provides for accessing | |
3362 | information about the system it runs on. | |
3363 | ||
8f85c0c6 NJ |
3364 | @deffn {Scheme Procedure} uname |
3365 | @deffnx {C Function} scm_uname () | |
a0e07ba4 NJ |
3366 | Return an object with some information about the computer |
3367 | system the program is running on. | |
a0e07ba4 NJ |
3368 | |
3369 | The following procedures accept an object as returned by @code{uname} | |
99d16776 | 3370 | and return a selected component (all of which are strings). |
a0e07ba4 | 3371 | |
2ce02471 | 3372 | @deffn {Scheme Procedure} utsname:sysname un |
a0e07ba4 | 3373 | The name of the operating system. |
2ce02471 NJ |
3374 | @end deffn |
3375 | @deffn {Scheme Procedure} utsname:nodename un | |
a0e07ba4 | 3376 | The network name of the computer. |
2ce02471 NJ |
3377 | @end deffn |
3378 | @deffn {Scheme Procedure} utsname:release un | |
a0e07ba4 | 3379 | The current release level of the operating system implementation. |
2ce02471 NJ |
3380 | @end deffn |
3381 | @deffn {Scheme Procedure} utsname:version un | |
a0e07ba4 | 3382 | The current version level within the release of the operating system. |
2ce02471 NJ |
3383 | @end deffn |
3384 | @deffn {Scheme Procedure} utsname:machine un | |
a0e07ba4 | 3385 | A description of the hardware. |
2ce02471 NJ |
3386 | @end deffn |
3387 | @end deffn | |
a0e07ba4 | 3388 | |
8f85c0c6 NJ |
3389 | @deffn {Scheme Procedure} gethostname |
3390 | @deffnx {C Function} scm_gethostname () | |
f3dfb8ac | 3391 | @cindex host name |
a0e07ba4 NJ |
3392 | Return the host name of the current processor. |
3393 | @end deffn | |
3394 | ||
8f85c0c6 NJ |
3395 | @deffn {Scheme Procedure} sethostname name |
3396 | @deffnx {C Function} scm_sethostname (name) | |
a0e07ba4 NJ |
3397 | Set the host name of the current processor to @var{name}. May |
3398 | only be used by the superuser. The return value is not | |
3399 | specified. | |
3400 | @end deffn | |
3401 | ||
a0e07ba4 | 3402 | @node Locales |
3229f68b | 3403 | @subsection Locales |
f3dfb8ac | 3404 | @cindex locale |
a0e07ba4 | 3405 | |
8f85c0c6 NJ |
3406 | @deffn {Scheme Procedure} setlocale category [locale] |
3407 | @deffnx {C Function} scm_setlocale (category, locale) | |
74f76d62 KR |
3408 | Get or set the current locale, used for various internationalizations. |
3409 | Locales are strings, such as @samp{sv_SE}. | |
3410 | ||
b89c4943 LC |
3411 | If @var{locale} is given then the locale for the given @var{category} |
3412 | is set and the new value returned. If @var{locale} is not given then | |
3413 | the current value is returned. @var{category} should be one of the | |
3414 | following values (@pxref{Locale Categories, Categories of Activities | |
3415 | that Locales Affect,, libc, The GNU C Library Reference Manual}): | |
74f76d62 KR |
3416 | |
3417 | @defvar LC_ALL | |
3418 | @defvarx LC_COLLATE | |
3419 | @defvarx LC_CTYPE | |
3420 | @defvarx LC_MESSAGES | |
3421 | @defvarx LC_MONETARY | |
3422 | @defvarx LC_NUMERIC | |
3423 | @defvarx LC_TIME | |
3424 | @end defvar | |
3425 | ||
f3dfb8ac | 3426 | @cindex @code{LANG} |
74f76d62 KR |
3427 | A common usage is @samp{(setlocale LC_ALL "")}, which initializes all |
3428 | categories based on standard environment variables (@code{LANG} etc). | |
3429 | For full details on categories and locale names @pxref{Locales,, | |
3430 | Locales and Internationalization, libc, The GNU C Library Reference | |
3431 | Manual}. | |
b89c4943 LC |
3432 | |
3433 | Note that @code{setlocale} affects locale settings for the whole | |
a2f00b9b | 3434 | process. @xref{i18n Introduction, locale objects and |
b89c4943 | 3435 | @code{make-locale}}, for a thread-safe alternative. |
a0e07ba4 NJ |
3436 | @end deffn |
3437 | ||
3438 | @node Encryption | |
3229f68b | 3439 | @subsection Encryption |
f3dfb8ac | 3440 | @cindex encryption |
a0e07ba4 NJ |
3441 | |
3442 | Please note that the procedures in this section are not suited for | |
3443 | strong encryption, they are only interfaces to the well-known and | |
3444 | common system library functions of the same name. They are just as good | |
3445 | (or bad) as the underlying functions, so you should refer to your system | |
9a18d8d4 KR |
3446 | documentation before using them (@pxref{crypt,, Encrypting Passwords, |
3447 | libc, The GNU C Library Reference Manual}). | |
a0e07ba4 | 3448 | |
8f85c0c6 NJ |
3449 | @deffn {Scheme Procedure} crypt key salt |
3450 | @deffnx {C Function} scm_crypt (key, salt) | |
9a18d8d4 KR |
3451 | Encrypt @var{key}, with the addition of @var{salt} (both strings), |
3452 | using the @code{crypt} C library call. | |
a0e07ba4 NJ |
3453 | @end deffn |
3454 | ||
5f378d17 TTN |
3455 | Although @code{getpass} is not an encryption procedure per se, it |
3456 | appears here because it is often used in combination with @code{crypt}: | |
a0e07ba4 | 3457 | |
8f85c0c6 NJ |
3458 | @deffn {Scheme Procedure} getpass prompt |
3459 | @deffnx {C Function} scm_getpass (prompt) | |
f3dfb8ac | 3460 | @cindex password |
a0e07ba4 NJ |
3461 | Display @var{prompt} to the standard error output and read |
3462 | a password from @file{/dev/tty}. If this file is not | |
3463 | accessible, it reads from standard input. The password may be | |
3464 | up to 127 characters in length. Additional characters and the | |
3465 | terminating newline character are discarded. While reading | |
3466 | the password, echoing and the generation of signals by special | |
3467 | characters is disabled. | |
3468 | @end deffn | |
5982a8e0 KR |
3469 | |
3470 | ||
3471 | @c Local Variables: | |
3472 | @c TeX-master: "guile.texi" | |
3473 | @c End: |