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.
56 SCM_PROC (s_sys_ftell
, "ftell", 1, 0, 0, scm_sys_ftell
);
59 scm_sys_ftell (SCM port
)
67 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_ftell
);
68 SCM_SYSCALL (pos
= ftell ((FILE *)SCM_STREAM (port
)));
70 SCM_SYSERROR (s_sys_ftell
);
71 if (pos
> 0 && SCM_CRDYP (port
))
73 return SCM_MAKINUM (pos
);
78 SCM_PROC (s_sys_fseek
, "fseek", 3, 0, 0, scm_sys_fseek
);
81 scm_sys_fseek (SCM port
, SCM offset
, SCM whence
)
84 scm_sys_fseek (port
, offset
, whence
)
91 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_fseek
);
92 SCM_ASSERT (SCM_INUMP (offset
), offset
, SCM_ARG2
, s_sys_fseek
);
93 SCM_ASSERT (SCM_INUMP (whence
) && (SCM_INUM (whence
) < 3) && (SCM_INUM (whence
) >= 0),
94 whence
, SCM_ARG3
, s_sys_fseek
);
95 SCM_CLRDY (port
); /* Clear ungetted char */
96 /* Values of whence are interned in scm_init_ioext. */
97 rv
= fseek ((FILE *)SCM_STREAM (port
), SCM_INUM (offset
), SCM_INUM (whence
));
99 SCM_SYSERROR (s_sys_fseek
);
100 return SCM_UNSPECIFIED
;
105 SCM_PROC (s_sys_freopen
, "freopen", 3, 0, 0, scm_sys_freopen
);
108 scm_sys_freopen (SCM filename
, SCM modes
, SCM port
)
111 scm_sys_freopen (filename
, modes
, port
)
118 SCM_ASSERT (SCM_NIMP (filename
) && SCM_STRINGP (filename
), filename
, SCM_ARG1
, s_sys_freopen
);
119 SCM_ASSERT (SCM_NIMP (modes
) && SCM_STRINGP (modes
), modes
, SCM_ARG2
, s_sys_freopen
);
121 SCM_ASSERT (SCM_NIMP (port
) && SCM_FPORTP (port
), port
, SCM_ARG3
, s_sys_freopen
);
122 SCM_SYSCALL (f
= freopen (SCM_CHARS (filename
), SCM_CHARS (modes
), (FILE *)SCM_STREAM (port
)));
127 port
= SCM_MAKINUM (errno
);
128 SCM_CAR (p
) &= ~SCM_OPN
;
129 scm_remove_from_port_table (p
);
133 SCM_CAR (port
) = scm_tc16_fport
| scm_mode_bits (SCM_CHARS (modes
));
134 SCM_SETSTREAM (port
, (SCM
)f
);
135 if (SCM_BUF0
& (SCM_CAR (port
) = scm_tc16_fport
| scm_mode_bits (SCM_CHARS (modes
))))
144 SCM_PROC (s_sys_duplicate_port
, "duplicate-port", 2, 0, 0, scm_sys_duplicate_port
);
147 scm_sys_duplicate_port (SCM oldpt
, SCM modes
)
150 scm_sys_duplicate_port (oldpt
, modes
)
159 SCM_ASSERT (SCM_NIMP (oldpt
) && SCM_OPPORTP (oldpt
), oldpt
, SCM_ARG1
, s_sys_duplicate_port
);
160 SCM_ASSERT (SCM_NIMP (modes
) && SCM_STRINGP (modes
), modes
, SCM_ARG2
, s_sys_duplicate_port
);
163 oldfd
= fileno ((FILE *)SCM_STREAM (oldpt
));
165 SCM_SYSERROR (s_sys_duplicate_port
);
166 SCM_SYSCALL (newfd
= dup (oldfd
));
168 SCM_SYSERROR (s_sys_duplicate_port
);
169 f
= fdopen (newfd
, SCM_CHARS (modes
));
172 SCM_SYSCALL (close (newfd
));
173 SCM_SYSERROR (s_sys_duplicate_port
);
176 struct scm_port_table
* pt
;
177 pt
= scm_add_to_port_table (newpt
);
178 SCM_SETPTAB_ENTRY (newpt
, pt
);
179 if (SCM_BUF0
& (SCM_CAR (newpt
) = scm_tc16_fport
| scm_mode_bits (SCM_CHARS (modes
))))
181 SCM_SETSTREAM (newpt
, (SCM
)f
);
182 SCM_PTAB_ENTRY (newpt
)->file_name
= SCM_PTAB_ENTRY (oldpt
)->file_name
;
190 SCM_PROC (s_sys_redirect_port
, "redirect-port", 2, 0, 0, scm_sys_redirect_port
);
193 scm_sys_redirect_port (SCM into_pt
, SCM from_pt
)
196 scm_sys_redirect_port (into_pt
, from_pt
)
201 int ans
, oldfd
, newfd
;
203 SCM_ASSERT (SCM_NIMP (into_pt
) && SCM_OPPORTP (into_pt
), into_pt
, SCM_ARG1
, s_sys_redirect_port
);
204 SCM_ASSERT (SCM_NIMP (from_pt
) && SCM_OPPORTP (from_pt
), from_pt
, SCM_ARG2
, s_sys_redirect_port
);
205 oldfd
= fileno ((FILE *)SCM_STREAM (into_pt
));
207 SCM_SYSERROR (s_sys_redirect_port
);
208 newfd
= fileno ((FILE *)SCM_STREAM (from_pt
));
210 SCM_SYSERROR (s_sys_redirect_port
);
211 SCM_SYSCALL (ans
= dup2 (oldfd
, newfd
));
213 SCM_SYSERROR (s_sys_redirect_port
);
215 return SCM_UNSPECIFIED
;
218 SCM_PROC (s_sys_fileno
, "fileno", 1, 0, 0, scm_sys_fileno
);
221 scm_sys_fileno (SCM port
)
224 scm_sys_fileno (port
)
229 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_fileno
);
230 fd
= fileno ((FILE *)SCM_STREAM (port
));
232 SCM_SYSERROR (s_sys_fileno
);
233 return SCM_MAKINUM (fd
);
236 SCM_PROC (s_sys_isatty
, "isatty?", 1, 0, 0, scm_sys_isatty_p
);
239 scm_sys_isatty_p (SCM port
)
242 scm_sys_isatty_p (port
)
247 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_isatty
);
248 rv
= fileno ((FILE *)SCM_STREAM (port
));
250 SCM_SYSERROR (s_sys_isatty
);
252 return rv
? SCM_BOOL_T
: SCM_BOOL_F
;
257 SCM_PROC (s_sys_fdopen
, "fdopen", 2, 0, 0, scm_sys_fdopen
);
260 scm_sys_fdopen (SCM fdes
, SCM modes
)
263 scm_sys_fdopen (fdes
, modes
)
270 struct scm_port_table
* pt
;
272 SCM_ASSERT (SCM_INUMP (fdes
), fdes
, SCM_ARG1
, s_sys_fdopen
);
273 SCM_ASSERT (SCM_NIMP (modes
) && SCM_STRINGP (modes
), modes
, SCM_ARG2
, s_sys_fdopen
);
276 f
= fdopen (SCM_INUM (fdes
), SCM_CHARS (modes
));
278 SCM_SYSERROR (s_sys_fdopen
);
279 pt
= scm_add_to_port_table (port
);
280 SCM_SETPTAB_ENTRY (port
, pt
);
281 if (SCM_BUF0
& (SCM_CAR (port
) = scm_tc16_fport
282 | scm_mode_bits (SCM_CHARS (modes
))))
284 SCM_SETSTREAM (port
, (SCM
)f
);
291 /* Move a port's underlying file descriptor to a given value.
292 * Returns #f if fdes is already the given value.
294 * MOVE->FDES is implemented in Scheme and calls this primitive.
296 SCM_PROC (s_sys_primitive_move_to_fdes
, "primitive-move->fdes", 2, 0, 0, scm_sys_primitive_move_to_fdes
);
299 scm_sys_primitive_move_to_fdes (SCM port
, SCM fd
)
302 scm_sys_primitive_move_to_fdes (port
, fd
)
312 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_primitive_move_to_fdes
);
313 SCM_ASSERT (SCM_INUMP (fd
), fd
, SCM_ARG2
, s_sys_primitive_move_to_fdes
);
315 stream
= (FILE *)SCM_STREAM (port
);
316 old_fd
= fileno (stream
);
317 new_fd
= SCM_INUM (fd
);
318 if (old_fd
== new_fd
)
323 scm_evict_ports (new_fd
);
324 rv
= dup2 (old_fd
, new_fd
);
326 SCM_SYSERROR (s_sys_primitive_move_to_fdes
);
327 scm_setfileno (stream
, new_fd
);
328 SCM_SYSCALL (close (old_fd
));
335 scm_setfileno (FILE *fs
, int fd
)
338 scm_setfileno (fs
, fd
)
343 #ifdef SET_FILE_FD_FIELD
344 SET_FILE_FD_FIELD(fs
, fd
);
346 Configure could
not guess the name of the correct field in a
FILE *.
348 This function needs to be ported to your system
.
350 SET_FILE_FD_FIELD should change the descriptor refered to by a stdio
351 stream
, and nothing
else.
353 The way to port
this file is to add cases to configure
.in
. Search
354 that file
for "SET_FILE_FD_FIELD" and follow the examples there
.
358 /* Move ports with the specified file descriptor to new descriptors,
359 * reseting the revealed count to 0.
360 * Should be called with SCM_DEFER_INTS active.
364 scm_evict_ports (int fd
)
373 for (i
= 0; i
< scm_port_table_size
; i
++)
375 if (SCM_FPORTP (scm_port_table
[i
]->port
)
376 && fileno ((FILE *)SCM_STREAM (scm_port_table
[i
]->port
)) == fd
)
378 scm_setfileno ((FILE *)SCM_STREAM (scm_port_table
[i
]->port
), dup (fd
));
379 scm_set_port_revealed_x (scm_port_table
[i
]->port
, SCM_MAKINUM (0));
384 /* Return a list of ports using a given file descriptor. */
385 SCM_PROC(s_fdes_to_ports
, "fdes->ports", 1, 0, 0, scm_fdes_to_ports
);
388 scm_fdes_to_ports (SCM fd
)
391 scm_fdes_to_ports (fd
)
395 SCM result
= SCM_EOL
;
399 SCM_ASSERT (SCM_INUMP (fd
), fd
, SCM_ARG1
, s_fdes_to_ports
);
400 int_fd
= SCM_INUM (fd
);
403 for (i
= 0; i
< scm_port_table_size
; i
++)
405 if (SCM_FPORTP (scm_port_table
[i
]->port
)
406 && fileno ((FILE *)SCM_STREAM (scm_port_table
[i
]->port
)) == int_fd
)
407 result
= scm_cons (scm_port_table
[i
]->port
, result
);
415 scm_init_ioext (void)
421 /* fseek() symbols. */
422 scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET
));
423 scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR
));
424 scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END
));
426 /* access() symbols. */
427 scm_sysintern ("R_OK", SCM_MAKINUM (R_OK
));
428 scm_sysintern ("W_OK", SCM_MAKINUM (W_OK
));
429 scm_sysintern ("X_OK", SCM_MAKINUM (X_OK
));
430 scm_sysintern ("F_OK", SCM_MAKINUM (F_OK
));
432 /* File type/permission bits. */
434 scm_sysintern ("S_IRUSR", SCM_MAKINUM (S_IRUSR
));
437 scm_sysintern ("S_IWUSR", SCM_MAKINUM (S_IWUSR
));
440 scm_sysintern ("S_IXUSR", SCM_MAKINUM (S_IXUSR
));
443 scm_sysintern ("S_IRWXU", SCM_MAKINUM (S_IRWXU
));
447 scm_sysintern ("S_IRGRP", SCM_MAKINUM (S_IRGRP
));
450 scm_sysintern ("S_IWGRP", SCM_MAKINUM (S_IWGRP
));
453 scm_sysintern ("S_IXGRP", SCM_MAKINUM (S_IXGRP
));
456 scm_sysintern ("S_IRWXG", SCM_MAKINUM (S_IRWXG
));
460 scm_sysintern ("S_IROTH", SCM_MAKINUM (S_IROTH
));
463 scm_sysintern ("S_IWOTH", SCM_MAKINUM (S_IWOTH
));
466 scm_sysintern ("S_IXOTH", SCM_MAKINUM (S_IXOTH
));
469 scm_sysintern ("S_IRWXO", SCM_MAKINUM (S_IRWXO
));
473 scm_sysintern ("S_ISUID", SCM_MAKINUM (S_ISUID
));
476 scm_sysintern ("S_ISGID", SCM_MAKINUM (S_ISGID
));
479 scm_sysintern ("S_ISVTX", SCM_MAKINUM (S_ISVTX
));
483 scm_sysintern ("S_IFMT", SCM_MAKINUM (S_IFMT
));
486 scm_sysintern ("S_IFDIR", SCM_MAKINUM (S_IFDIR
));
489 scm_sysintern ("S_IFCHR", SCM_MAKINUM (S_IFCHR
));
492 scm_sysintern ("S_IFBLK", SCM_MAKINUM (S_IFBLK
));
495 scm_sysintern ("S_IFREG", SCM_MAKINUM (S_IFREG
));
498 scm_sysintern ("S_IFLNK", SCM_MAKINUM (S_IFLNK
));
501 scm_sysintern ("S_IFSOCK", SCM_MAKINUM (S_IFSOCK
));
504 scm_sysintern ("S_IFIFO", SCM_MAKINUM (S_IFIFO
));