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