1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
48 #include "libguile/_scm.h"
49 #include "libguile/ioext.h"
50 #include "libguile/fports.h"
51 #include "libguile/feature.h"
52 #include "libguile/ports.h"
53 #include "libguile/strings.h"
54 #include "libguile/validate.h"
66 SCM_DEFINE (scm_ftell
, "ftell", 1, 0, 0,
68 "Return an integer representing the current position of\n"
69 "@var{fd/port}, measured from the beginning. Equivalent to:\n"
72 "(seek port 0 SEEK_CUR)\n"
74 #define FUNC_NAME s_scm_ftell
76 return scm_seek (fd_port
, SCM_INUM0
, SCM_MAKINUM (SEEK_CUR
));
80 SCM_DEFINE (scm_redirect_port
, "redirect-port", 2, 0, 0,
82 "This procedure takes two ports and duplicates the underlying file\n"
83 "descriptor from @var{old-port} into @var{new-port}. The\n"
84 "current file descriptor in @var{new-port} will be closed.\n"
85 "After the redirection the two ports will share a file position\n"
86 "and file status flags.\n\n"
87 "The return value is unspecified.\n\n"
88 "Unexpected behaviour can result if both ports are subsequently used\n"
89 "and the original and/or duplicate ports are buffered.\n\n"
90 "This procedure does not have any side effects on other ports or\n"
92 #define FUNC_NAME s_scm_redirect_port
94 int ans
, oldfd
, newfd
;
97 old
= SCM_COERCE_OUTPORT (old
);
98 new = SCM_COERCE_OUTPORT (new);
100 SCM_VALIDATE_OPFPORT (1,old
);
101 SCM_VALIDATE_OPFPORT (2,new);
102 oldfd
= SCM_FPORT_FDES (old
);
103 fp
= SCM_FSTREAM (new);
107 scm_t_port
*pt
= SCM_PTAB_ENTRY (new);
108 scm_t_port
*old_pt
= SCM_PTAB_ENTRY (old
);
109 scm_t_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (new)];
111 /* must flush to old fdes. */
112 if (pt
->rw_active
== SCM_PORT_WRITE
)
114 else if (pt
->rw_active
== SCM_PORT_READ
)
116 ans
= dup2 (oldfd
, newfd
);
119 pt
->rw_random
= old_pt
->rw_random
;
120 /* continue using existing buffers, even if inappropriate. */
122 return SCM_UNSPECIFIED
;
126 SCM_DEFINE (scm_dup_to_fdes
, "dup->fdes", 1, 1, 0,
127 (SCM fd_or_port
, SCM fd
),
128 "Return a new integer file descriptor referring to the open file\n"
129 "designated by @var{fd_or_port}, which must be either an open\n"
130 "file port or a file descriptor.")
131 #define FUNC_NAME s_scm_dup_to_fdes
133 int oldfd
, newfd
, rv
;
135 fd_or_port
= SCM_COERCE_OUTPORT (fd_or_port
);
137 if (SCM_INUMP (fd_or_port
))
138 oldfd
= SCM_INUM (fd_or_port
);
141 SCM_VALIDATE_OPFPORT (1,fd_or_port
);
142 oldfd
= SCM_FPORT_FDES (fd_or_port
);
150 fd
= SCM_MAKINUM (newfd
);
154 SCM_VALIDATE_INUM_COPY (2, fd
, newfd
);
157 scm_evict_ports (newfd
); /* see scsh manual. */
158 rv
= dup2 (oldfd
, newfd
);
168 SCM_DEFINE (scm_dup2
, "dup2", 2, 0, 0,
169 (SCM oldfd
, SCM newfd
),
170 "A simple wrapper for the @code{dup2} system call.\n"
171 "Copies the file descriptor @var{oldfd} to descriptor\n"
172 "number @var{newfd}, replacing the previous meaning\n"
173 "of @var{newfd}. Both @var{oldfd} and @var{newfd} must\n"
175 "Unlike for dup->fdes or primitive-move->fdes, no attempt\n"
176 "is made to move away ports which are using @var{newfd}.\n"
177 "The return value is unspecified.")
178 #define FUNC_NAME s_scm_dup2
184 SCM_VALIDATE_INUM_COPY (1, oldfd
, c_oldfd
);
185 SCM_VALIDATE_INUM_COPY (2, newfd
, c_newfd
);
186 rv
= dup2 (c_oldfd
, c_newfd
);
189 return SCM_UNSPECIFIED
;
193 SCM_DEFINE (scm_fileno
, "fileno", 1, 0, 0,
195 "Return the integer file descriptor underlying @var{port}. Does\n"
196 "not change its revealed count.")
197 #define FUNC_NAME s_scm_fileno
199 port
= SCM_COERCE_OUTPORT (port
);
200 SCM_VALIDATE_OPFPORT (1,port
);
201 return SCM_MAKINUM (SCM_FPORT_FDES (port
));
205 /* GJB:FIXME:: why does this not throw
206 an error if the arg is not a port?
207 This proc as is would be better names isattyport?
208 if it is not going to assume that the arg is a port
210 [cmm] I don't see any problem with the above. why should a type
211 predicate assume _anything_ about its argument?
213 SCM_DEFINE (scm_isatty_p
, "isatty?", 1, 0, 0,
215 "Return @code{#t} if @var{port} is using a serial non--file\n"
216 "device, otherwise @code{#f}.")
217 #define FUNC_NAME s_scm_isatty_p
221 port
= SCM_COERCE_OUTPORT (port
);
223 if (!SCM_OPFPORTP (port
))
226 rv
= isatty (SCM_FPORT_FDES (port
));
233 SCM_DEFINE (scm_fdopen
, "fdopen", 2, 0, 0,
234 (SCM fdes
, SCM modes
),
235 "Return a new port based on the file descriptor @var{fdes}.\n"
236 "Modes are given by the string @var{modes}. The revealed count\n"
237 "of the port is initialized to zero. The modes string is the\n"
238 "same as that accepted by @ref{File Ports, open-file}.")
239 #define FUNC_NAME s_scm_fdopen
241 SCM_VALIDATE_INUM (1,fdes
);
242 SCM_VALIDATE_STRING (2, modes
);
244 return scm_fdes_to_port (SCM_INUM (fdes
), SCM_STRING_CHARS (modes
), SCM_BOOL_F
);
250 /* Move a port's underlying file descriptor to a given value.
251 * Returns #f if fdes is already the given value.
253 * MOVE->FDES is implemented in Scheme and calls this primitive.
255 SCM_DEFINE (scm_primitive_move_to_fdes
, "primitive-move->fdes", 2, 0, 0,
257 "Moves the underlying file descriptor for @var{port} to the integer\n"
258 "value @var{fdes} without changing the revealed count of @var{port}.\n"
259 "Any other ports already using this descriptor will be automatically\n"
260 "shifted to new descriptors and their revealed counts reset to zero.\n"
261 "The return value is @code{#f} if the file descriptor already had the\n"
262 "required value or @code{#t} if it was moved.")
263 #define FUNC_NAME s_scm_primitive_move_to_fdes
270 port
= SCM_COERCE_OUTPORT (port
);
272 SCM_VALIDATE_OPFPORT (1,port
);
273 SCM_VALIDATE_INUM (2,fd
);
274 stream
= SCM_FSTREAM (port
);
275 old_fd
= stream
->fdes
;
276 new_fd
= SCM_INUM (fd
);
277 if (old_fd
== new_fd
)
281 scm_evict_ports (new_fd
);
282 rv
= dup2 (old_fd
, new_fd
);
285 stream
->fdes
= new_fd
;
286 SCM_SYSCALL (close (old_fd
));
291 /* Return a list of ports using a given file descriptor. */
292 SCM_DEFINE (scm_fdes_to_ports
, "fdes->ports", 1, 0, 0,
294 "Return a list of existing ports which have @var{fdes} as an\n"
295 "underlying file descriptor, without changing their revealed\n"
297 #define FUNC_NAME s_scm_fdes_to_ports
299 SCM result
= SCM_EOL
;
303 SCM_VALIDATE_INUM_COPY (1,fd
,int_fd
);
305 for (i
= 0; i
< scm_port_table_size
; i
++)
307 if (SCM_OPFPORTP (scm_port_table
[i
]->port
)
308 && ((scm_t_fport
*) scm_port_table
[i
]->stream
)->fdes
== int_fd
)
309 result
= scm_cons (scm_port_table
[i
]->port
, result
);
319 scm_add_feature ("i/o-extensions");
321 #ifndef SCM_MAGIC_SNARFER
322 #include "libguile/ioext.x"