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