1 /* Copyright (C) 1995 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, 675 Mass Ave, Cambridge, MA 02139, USA.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
59 SCM_PROC (s_sys_ftell
, "ftell", 1, 0, 0, scm_sys_ftell
);
66 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_ftell
);
67 SCM_SYSCALL (pos
= ftell ((FILE *)SCM_STREAM (port
)));
69 scm_syserror (s_sys_ftell
);
70 if (pos
> 0 && SCM_CRDYP (port
))
72 return SCM_MAKINUM (pos
);
77 SCM_PROC (s_sys_fseek
, "fseek", 3, 0, 0, scm_sys_fseek
);
80 scm_sys_fseek (port
, offset
, whence
)
86 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_fseek
);
87 SCM_ASSERT (SCM_INUMP (offset
), offset
, SCM_ARG2
, s_sys_fseek
);
88 SCM_ASSERT (SCM_INUMP (whence
) && (SCM_INUM (whence
) < 3) && (SCM_INUM (whence
) >= 0),
89 whence
, SCM_ARG3
, s_sys_fseek
);
90 SCM_CLRDY (port
); /* Clear ungetted char */
91 /* Values of whence are interned in scm_init_ioext. */
92 rv
= fseek ((FILE *)SCM_STREAM (port
), SCM_INUM (offset
), SCM_INUM (whence
));
94 scm_syserror (s_sys_fseek
);
95 return SCM_UNSPECIFIED
;
100 SCM_PROC (s_sys_freopen
, "freopen", 3, 0, 0, scm_sys_freopen
);
103 scm_sys_freopen (filename
, modes
, port
)
109 SCM_ASSERT (SCM_NIMP (filename
) && SCM_STRINGP (filename
), filename
, SCM_ARG1
, s_sys_freopen
);
110 SCM_ASSERT (SCM_NIMP (modes
) && SCM_STRINGP (modes
), modes
, SCM_ARG2
, s_sys_freopen
);
112 SCM_ASSERT (SCM_NIMP (port
) && SCM_FPORTP (port
), port
, SCM_ARG3
, s_sys_freopen
);
113 SCM_SYSCALL (f
= freopen (SCM_CHARS (filename
), SCM_CHARS (modes
), (FILE *)SCM_STREAM (port
)));
118 port
= SCM_MAKINUM (errno
);
119 SCM_CAR (p
) &= ~SCM_OPN
;
120 scm_remove_from_port_table (p
);
124 SCM_CAR (port
) = scm_tc16_fport
| scm_mode_bits (SCM_CHARS (modes
));
125 SCM_SETSTREAM (port
, (SCM
)f
);
126 if (SCM_BUF0
& (SCM_CAR (port
) = scm_tc16_fport
| scm_mode_bits (SCM_CHARS (modes
))))
135 SCM_PROC (s_sys_duplicate_port
, "duplicate-port", 2, 0, 0, scm_sys_duplicate_port
);
138 scm_sys_duplicate_port (oldpt
, modes
)
146 SCM_ASSERT (SCM_NIMP (oldpt
) && SCM_OPPORTP (oldpt
), oldpt
, SCM_ARG1
, s_sys_duplicate_port
);
147 SCM_ASSERT (SCM_NIMP (modes
) && SCM_STRINGP (modes
), modes
, SCM_ARG2
, s_sys_duplicate_port
);
150 oldfd
= fileno ((FILE *)SCM_STREAM (oldpt
));
152 scm_syserror (s_sys_duplicate_port
);
153 SCM_SYSCALL (newfd
= dup (oldfd
));
155 scm_syserror (s_sys_duplicate_port
);
156 f
= fdopen (newfd
, SCM_CHARS (modes
));
159 SCM_SYSCALL (close (newfd
));
160 scm_syserror (s_sys_duplicate_port
);
163 struct scm_port_table
* pt
;
164 pt
= scm_add_to_port_table (newpt
);
165 SCM_SETPTAB_ENTRY (newpt
, pt
);
166 if (SCM_BUF0
& (SCM_CAR (newpt
) = scm_tc16_fport
| scm_mode_bits (SCM_CHARS (modes
))))
168 SCM_SETSTREAM (newpt
, (SCM
)f
);
169 SCM_PTAB_ENTRY (newpt
)->file_name
= SCM_PTAB_ENTRY (oldpt
)->file_name
;
177 SCM_PROC (s_sys_redirect_port
, "redirect-port", 2, 0, 0, scm_sys_redirect_port
);
180 scm_sys_redirect_port (into_pt
, from_pt
)
184 int ans
, oldfd
, newfd
;
186 SCM_ASSERT (SCM_NIMP (into_pt
) && SCM_OPPORTP (into_pt
), into_pt
, SCM_ARG1
, s_sys_redirect_port
);
187 SCM_ASSERT (SCM_NIMP (from_pt
) && SCM_OPPORTP (from_pt
), from_pt
, SCM_ARG2
, s_sys_redirect_port
);
188 oldfd
= fileno ((FILE *)SCM_STREAM (into_pt
));
190 scm_syserror (s_sys_redirect_port
);
191 newfd
= fileno ((FILE *)SCM_STREAM (from_pt
));
193 scm_syserror (s_sys_redirect_port
);
194 SCM_SYSCALL (ans
= dup2 (oldfd
, newfd
));
196 scm_syserror (s_sys_redirect_port
);
198 return SCM_UNSPECIFIED
;
201 SCM_PROC (s_sys_fileno
, "fileno", 1, 0, 0, scm_sys_fileno
);
204 scm_sys_fileno (port
)
208 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_fileno
);
209 fd
= fileno ((FILE *)SCM_STREAM (port
));
211 scm_syserror (s_sys_fileno
);
212 return SCM_MAKINUM (fd
);
215 SCM_PROC (s_sys_isatty
, "isatty?", 1, 0, 0, scm_sys_isatty_p
);
218 scm_sys_isatty_p (port
)
222 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_isatty
);
223 rv
= fileno ((FILE *)SCM_STREAM (port
));
225 scm_syserror (s_sys_isatty
);
227 return rv
? SCM_BOOL_T
: SCM_BOOL_F
;
232 SCM_PROC (s_sys_fdopen
, "fdopen", 2, 0, 0, scm_sys_fdopen
);
235 scm_sys_fdopen (fdes
, modes
)
241 struct scm_port_table
* pt
;
243 SCM_ASSERT (SCM_INUMP (fdes
), fdes
, SCM_ARG1
, s_sys_fdopen
);
244 SCM_ASSERT (SCM_NIMP (modes
) && SCM_STRINGP (modes
), modes
, SCM_ARG2
, s_sys_fdopen
);
247 f
= fdopen (SCM_INUM (fdes
), SCM_CHARS (modes
));
249 scm_syserror (s_sys_fdopen
);
250 pt
= scm_add_to_port_table (port
);
251 SCM_SETPTAB_ENTRY (port
, pt
);
252 if (SCM_BUF0
& (SCM_CAR (port
) = scm_tc16_fport
253 | scm_mode_bits (SCM_CHARS (modes
))))
255 SCM_SETSTREAM (port
, (SCM
)f
);
262 /* Move a port's underlying file descriptor to a given value.
263 * Returns #f if fdes is already the given value.
265 * MOVE->FDES is implemented in Scheme and calls this primitive.
267 SCM_PROC (s_sys_primitive_move_to_fdes
, "primitive-move->fdes", 2, 0, 0, scm_sys_primitive_move_to_fdes
);
270 scm_sys_primitive_move_to_fdes (port
, fd
)
279 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_primitive_move_to_fdes
);
280 SCM_ASSERT (SCM_INUMP (fd
), fd
, SCM_ARG2
, s_sys_primitive_move_to_fdes
);
282 stream
= (FILE *)SCM_STREAM (port
);
283 old_fd
= fileno (stream
);
284 new_fd
= SCM_INUM (fd
);
285 if (old_fd
== new_fd
)
290 scm_evict_ports (new_fd
);
291 rv
= dup2 (old_fd
, new_fd
);
293 scm_syserror (s_sys_primitive_move_to_fdes
);
294 scm_setfileno (stream
, new_fd
);
295 SCM_SYSCALL (close (old_fd
));
302 scm_setfileno (fs
, fd
)
306 #ifdef SET_FILE_FD_FIELD
307 SET_FILE_FD_FIELD(fs
, fd
);
309 Configure could
not guess the name of the correct field in a
FILE *.
311 This function needs to be ported to your system
.
313 SET_FILE_FD_FIELD should change the descriptor refered to by a stdio
314 stream
, and nothing
else.
316 The way to port
this file is to add cases to configure
.in
. Search
317 that file
for "SET_FILE_FD_FIELD" and follow the examples there
.
321 /* Move ports with the specified file descriptor to new descriptors,
322 * reseting the revealed count to 0.
323 * Should be called with SCM_DEFER_INTS active.
332 for (i
= 0; i
< scm_port_table_size
; i
++)
334 if (SCM_FPORTP (scm_port_table
[i
]->port
)
335 && fileno ((FILE *)SCM_STREAM (scm_port_table
[i
]->port
)) == fd
)
337 scm_setfileno ((FILE *)SCM_STREAM (scm_port_table
[i
]->port
), dup (fd
));
338 scm_set_port_revealed_x (scm_port_table
[i
]->port
, SCM_MAKINUM (0));
343 /* Return a list of ports using a given file descriptor. */
344 SCM_PROC(s_fdes_to_ports
, "fdes->ports", 1, 0, 0, scm_fdes_to_ports
);
347 scm_fdes_to_ports (fd
)
350 SCM result
= SCM_EOL
;
354 SCM_ASSERT (SCM_INUMP (fd
), fd
, SCM_ARG1
, s_fdes_to_ports
);
355 int_fd
= SCM_INUM (fd
);
358 for (i
= 0; i
< scm_port_table_size
; i
++)
360 if (SCM_FPORTP (scm_port_table
[i
]->port
)
361 && fileno ((FILE *)SCM_STREAM (scm_port_table
[i
]->port
)) == int_fd
)
362 result
= scm_cons (scm_port_table
[i
]->port
, result
);
372 /* fseek() symbols. */
373 scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET
));
374 scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR
));
375 scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END
));
377 /* File type/permission bits. */
379 scm_sysintern ("S_IRUSR", SCM_MAKINUM (S_IRUSR
));
382 scm_sysintern ("S_IWUSR", SCM_MAKINUM (S_IWUSR
));
385 scm_sysintern ("S_IXUSR", SCM_MAKINUM (S_IXUSR
));
388 scm_sysintern ("S_IRWXU", SCM_MAKINUM (S_IRWXU
));
392 scm_sysintern ("S_IRGRP", SCM_MAKINUM (S_IRGRP
));
395 scm_sysintern ("S_IWGRP", SCM_MAKINUM (S_IWGRP
));
398 scm_sysintern ("S_IXGRP", SCM_MAKINUM (S_IXGRP
));
401 scm_sysintern ("S_IRWXG", SCM_MAKINUM (S_IRWXG
));
405 scm_sysintern ("S_IROTH", SCM_MAKINUM (S_IROTH
));
408 scm_sysintern ("S_IWOTH", SCM_MAKINUM (S_IWOTH
));
411 scm_sysintern ("S_IXOTH", SCM_MAKINUM (S_IXOTH
));
414 scm_sysintern ("S_IRWXO", SCM_MAKINUM (S_IRWXO
));
418 scm_sysintern ("S_ISUID", SCM_MAKINUM (S_ISUID
));
421 scm_sysintern ("S_ISGID", SCM_MAKINUM (S_ISGID
));
424 scm_sysintern ("S_ISVTX", SCM_MAKINUM (S_ISVTX
));
428 scm_sysintern ("S_IFMT", SCM_MAKINUM (S_IFMT
));
431 scm_sysintern ("S_IFDIR", SCM_MAKINUM (S_IFDIR
));
434 scm_sysintern ("S_IFCHR", SCM_MAKINUM (S_IFCHR
));
437 scm_sysintern ("S_IFBLK", SCM_MAKINUM (S_IFBLK
));
440 scm_sysintern ("S_IFREG", SCM_MAKINUM (S_IFREG
));
443 scm_sysintern ("S_IFLNK", SCM_MAKINUM (S_IFLNK
));
446 scm_sysintern ("S_IFSOCK", SCM_MAKINUM (S_IFSOCK
));
449 scm_sysintern ("S_IFIFO", SCM_MAKINUM (S_IFIFO
));