1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 * 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
3 * 2014 Free Software Foundation, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 3 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #define _LARGEFILE64_SOURCE /* ask for stat64 etc */
24 #define _GNU_SOURCE /* ask for LONG_LONG_MAX/LONG_LONG_MIN */
42 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
47 #include <sys/types.h>
49 #include <sys/select.h>
51 #include <full-write.h>
53 #include "libguile/_scm.h"
54 #include "libguile/strings.h"
55 #include "libguile/validate.h"
56 #include "libguile/gc.h"
57 #include "libguile/posix.h"
58 #include "libguile/dynwind.h"
59 #include "libguile/hashtab.h"
61 #include "libguile/fports.h"
62 #include "libguile/ports-internal.h"
64 #if SIZEOF_OFF_T == SIZEOF_INT
65 #define OFF_T_MAX INT_MAX
66 #define OFF_T_MIN INT_MIN
67 #elif SIZEOF_OFF_T == SIZEOF_LONG
68 #define OFF_T_MAX LONG_MAX
69 #define OFF_T_MIN LONG_MIN
70 #elif SIZEOF_OFF_T == SIZEOF_LONG_LONG
71 #define OFF_T_MAX LONG_LONG_MAX
72 #define OFF_T_MIN LONG_LONG_MIN
74 #error Oops, unknown OFF_T size
77 scm_t_bits scm_tc16_fport
;
80 /* default buffer size, used if the O/S won't supply a value. */
81 static const size_t default_buffer_size
= 1024;
83 /* Create FPORT buffers with specified sizes (or -1 to use default size
84 or 0 for no buffer.) */
86 scm_fport_buffer_add (SCM port
, long read_size
, long write_size
)
87 #define FUNC_NAME "scm_fport_buffer_add"
89 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
91 if (read_size
== -1 || write_size
== -1)
94 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
96 scm_t_fport
*fp
= SCM_FSTREAM (port
);
98 default_size
= (fstat (fp
->fdes
, &st
) == -1) ? default_buffer_size
101 default_size
= default_buffer_size
;
104 read_size
= default_size
;
105 if (write_size
== -1)
106 write_size
= default_size
;
109 if (SCM_INPUT_PORT_P (port
) && read_size
> 0)
111 pt
->read_buf
= scm_gc_malloc_pointerless (read_size
, "port buffer");
112 pt
->read_pos
= pt
->read_end
= pt
->read_buf
;
113 pt
->read_buf_size
= read_size
;
117 pt
->read_pos
= pt
->read_buf
= pt
->read_end
= &pt
->shortbuf
;
118 pt
->read_buf_size
= 1;
121 if (SCM_OUTPUT_PORT_P (port
) && write_size
> 0)
123 pt
->write_buf
= scm_gc_malloc_pointerless (write_size
, "port buffer");
124 pt
->write_pos
= pt
->write_buf
;
125 pt
->write_buf_size
= write_size
;
129 pt
->write_buf
= pt
->write_pos
= &pt
->shortbuf
;
130 pt
->write_buf_size
= 1;
133 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
134 if (read_size
> 0 || write_size
> 0)
135 SCM_SET_CELL_WORD_0 (port
, SCM_CELL_WORD_0 (port
) & ~SCM_BUF0
);
137 SCM_SET_CELL_WORD_0 (port
, SCM_CELL_WORD_0 (port
) | SCM_BUF0
);
141 SCM_DEFINE (scm_setvbuf
, "setvbuf", 2, 1, 0,
142 (SCM port
, SCM mode
, SCM size
),
143 "Set the buffering mode for @var{port}. @var{mode} can be:\n"
150 "block buffered, using a newly allocated buffer of @var{size} bytes.\n"
151 "If @var{size} is omitted, a default size will be used.\n"
153 "Only certain types of ports are supported, most importantly\n"
155 #define FUNC_NAME s_scm_setvbuf
162 scm_t_port_internal
*pti
;
164 port
= SCM_COERCE_OUTPORT (port
);
166 SCM_VALIDATE_OPENPORT (1, port
);
167 pti
= SCM_PORT_GET_INTERNAL (port
);
169 if (pti
->setvbuf
== NULL
)
170 scm_wrong_type_arg_msg (FUNC_NAME
, 1, port
,
171 "port that supports 'setvbuf'");
173 cmode
= scm_to_int (mode
);
174 if (cmode
!= _IONBF
&& cmode
!= _IOFBF
&& cmode
!= _IOLBF
)
175 scm_out_of_range (FUNC_NAME
, mode
);
179 SCM_SET_CELL_WORD_0 (port
, SCM_CELL_WORD_0 (port
) | SCM_BUFLINE
);
183 SCM_SET_CELL_WORD_0 (port
,
184 SCM_CELL_WORD_0 (port
) & ~(scm_t_bits
) SCM_BUFLINE
);
186 if (SCM_UNBNDP (size
))
195 csize
= scm_to_int (size
);
196 if (csize
< 0 || (cmode
== _IONBF
&& csize
> 0))
197 scm_out_of_range (FUNC_NAME
, size
);
200 pt
= SCM_PTAB_ENTRY (port
);
202 if (SCM_INPUT_PORT_P (port
))
204 /* Drain pending input from PORT. Don't use `scm_drain_input' since
205 it returns a string, whereas we want binary input here. */
206 ndrained
= pt
->read_end
- pt
->read_pos
;
207 if (pt
->read_buf
== pt
->putback_buf
)
208 ndrained
+= pt
->saved_read_end
- pt
->saved_read_pos
;
212 drained
= scm_gc_malloc_pointerless (ndrained
, "file port");
213 scm_take_from_input_buffers (port
, drained
, ndrained
);
219 if (SCM_OUTPUT_PORT_P (port
))
222 if (pt
->read_buf
== pt
->putback_buf
)
224 pt
->read_buf
= pt
->saved_read_buf
;
225 pt
->read_pos
= pt
->saved_read_pos
;
226 pt
->read_end
= pt
->saved_read_end
;
227 pt
->read_buf_size
= pt
->saved_read_buf_size
;
230 pti
->setvbuf (port
, csize
, csize
);
233 /* Put DRAINED back to PORT. */
234 scm_unget_bytes ((unsigned char *) drained
, ndrained
, port
);
236 return SCM_UNSPECIFIED
;
240 /* Move ports with the specified file descriptor to new descriptors,
241 * resetting the revealed count to 0.
244 scm_i_evict_port (void *closure
, SCM port
)
246 int fd
= * (int*) closure
;
248 if (SCM_FPORTP (port
))
253 /* XXX: In some cases, we can encounter a port with no associated ptab
255 p
= SCM_PTAB_ENTRY (port
);
256 fp
= (p
!= NULL
) ? (scm_t_fport
*) p
->stream
: NULL
;
258 if ((fp
!= NULL
) && (fp
->fdes
== fd
))
262 scm_syserror ("scm_evict_ports");
263 scm_set_port_revealed_x (port
, scm_from_int (0));
269 scm_evict_ports (int fd
)
271 scm_c_port_for_each (scm_i_evict_port
, (void *) &fd
);
275 SCM_DEFINE (scm_file_port_p
, "file-port?", 1, 0, 0,
277 "Determine whether @var{obj} is a port that is related to a file.")
278 #define FUNC_NAME s_scm_file_port_p
280 return scm_from_bool (SCM_FPORTP (obj
));
285 static SCM sys_file_port_name_canonicalization
;
286 SCM_SYMBOL (sym_relative
, "relative");
287 SCM_SYMBOL (sym_absolute
, "absolute");
290 fport_canonicalize_filename (SCM filename
)
292 SCM mode
= scm_fluid_ref (sys_file_port_name_canonicalization
);
294 if (!scm_is_string (filename
))
298 else if (scm_is_eq (mode
, sym_relative
))
302 path
= scm_variable_ref (scm_c_module_lookup (scm_the_root_module (),
304 rel
= scm_i_relativize_path (filename
, path
);
306 return scm_is_true (rel
) ? rel
: filename
;
308 else if (scm_is_eq (mode
, sym_absolute
))
312 str
= scm_to_locale_string (filename
);
313 canon
= canonicalize_file_name (str
);
316 return canon
? scm_take_locale_string (canon
) : filename
;
324 /* scm_open_file_with_encoding
325 Return a new port open on a given file.
327 The mode string must match the pattern: [rwa+]** which
328 is interpreted in the usual unix way.
330 Unless binary mode is requested, the character encoding of the new
331 port is determined as follows: First, if GUESS_ENCODING is true,
332 'file-encoding' is used to guess the encoding of the file. If
333 GUESS_ENCODING is false or if 'file-encoding' fails, ENCODING is used
334 unless it is also false. As a last resort, the default port encoding
335 is used. It is an error to pass a non-false GUESS_ENCODING or
336 ENCODING if binary mode is requested.
338 Return the new port. */
340 scm_open_file_with_encoding (SCM filename
, SCM mode
,
341 SCM guess_encoding
, SCM encoding
)
342 #define FUNC_NAME "open-file"
345 int fdes
, flags
= 0, binary
= 0;
346 unsigned int retries
;
347 char *file
, *md
, *ptr
;
349 if (SCM_UNLIKELY (!(scm_is_false (encoding
) || scm_is_string (encoding
))))
350 scm_wrong_type_arg_msg (FUNC_NAME
, 0, encoding
,
351 "encoding to be string or false");
353 scm_dynwind_begin (0);
355 file
= scm_to_locale_string (filename
);
356 scm_dynwind_free (file
);
358 md
= scm_to_locale_string (mode
);
359 scm_dynwind_free (md
);
367 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
370 flags
|= O_WRONLY
| O_CREAT
| O_APPEND
;
373 scm_out_of_range (FUNC_NAME
, mode
);
381 flags
= (flags
& ~(O_RDONLY
| O_WRONLY
)) | O_RDWR
;
385 #if defined (O_BINARY)
389 case '0': /* unbuffered: handled later. */
390 case 'l': /* line buffered: handled during output. */
393 scm_out_of_range (FUNC_NAME
, mode
);
398 for (retries
= 0, fdes
= -1;
399 fdes
< 0 && retries
< 2;
402 SCM_SYSCALL (fdes
= open_or_open64 (file
, flags
, 0666));
407 if (en
== EMFILE
&& retries
== 0)
408 /* Run the GC in case it collects open file ports that are no
409 longer referenced. */
410 scm_i_gc (FUNC_NAME
);
412 SCM_SYSERROR_MSG ("~A: ~S",
413 scm_cons (scm_strerror (scm_from_int (en
)),
414 scm_cons (filename
, SCM_EOL
)), en
);
418 /* Create a port from this file descriptor. The port's encoding is initially
419 %default-port-encoding. */
420 port
= scm_i_fdes_to_port (fdes
, scm_i_mode_bits (mode
),
421 fport_canonicalize_filename (filename
));
425 if (scm_is_true (encoding
))
426 scm_misc_error (FUNC_NAME
,
427 "Encoding specified on a binary port",
428 scm_list_1 (encoding
));
429 if (scm_is_true (guess_encoding
))
430 scm_misc_error (FUNC_NAME
,
431 "Request to guess encoding on a binary port",
434 /* Use the binary-friendly ISO-8859-1 encoding. */
435 scm_i_set_port_encoding_x (port
, NULL
);
441 if (scm_is_true (guess_encoding
))
443 if (SCM_INPUT_PORT_P (port
))
444 enc
= scm_i_scan_for_encoding (port
);
446 scm_misc_error (FUNC_NAME
,
447 "Request to guess encoding on an output-only port",
451 if (!enc
&& scm_is_true (encoding
))
453 char *buf
= scm_to_latin1_string (encoding
);
454 enc
= scm_gc_strdup (buf
, "encoding");
459 scm_i_set_port_encoding_x (port
, enc
);
469 scm_open_file (SCM filename
, SCM mode
)
471 return scm_open_file_with_encoding (filename
, mode
, SCM_BOOL_F
, SCM_BOOL_F
);
474 /* We can't define these using SCM_KEYWORD, because keywords have not
475 yet been initialized when scm_init_fports is called. */
476 static SCM k_guess_encoding
= SCM_UNDEFINED
;
477 static SCM k_encoding
= SCM_UNDEFINED
;
479 SCM_DEFINE (scm_i_open_file
, "open-file", 2, 0, 1,
480 (SCM filename
, SCM mode
, SCM keyword_args
),
481 "Open the file whose name is @var{filename}, and return a port\n"
482 "representing that file. The attributes of the port are\n"
483 "determined by the @var{mode} string. The way in which this is\n"
484 "interpreted is similar to C stdio. The first character must be\n"
485 "one of the following:\n"
488 "Open an existing file for input.\n"
490 "Open a file for output, creating it if it doesn't already exist\n"
491 "or removing its contents if it does.\n"
493 "Open a file for output, creating it if it doesn't already\n"
494 "exist. All writes to the port will go to the end of the file.\n"
495 "The \"append mode\" can be turned off while the port is in use\n"
496 "@pxref{Ports and File Descriptors, fcntl}\n"
498 "The following additional characters can be appended:\n"
501 "Open the underlying file in binary mode, if supported by the system.\n"
502 "Also, open the file using the binary-compatible character encoding\n"
503 "\"ISO-8859-1\", ignoring the default port encoding.\n"
505 "Open the port for both input and output. E.g., @code{r+}: open\n"
506 "an existing file for both input and output.\n"
508 "Create an \"unbuffered\" port. In this case input and output\n"
509 "operations are passed directly to the underlying port\n"
510 "implementation without additional buffering. This is likely to\n"
511 "slow down I/O operations. The buffering mode can be changed\n"
512 "while a port is in use @pxref{Ports and File Descriptors,\n"
515 "Add line-buffering to the port. The port output buffer will be\n"
516 "automatically flushed whenever a newline character is written.\n"
518 "In theory we could create read/write ports which were buffered\n"
519 "in one direction only. However this isn't included in the\n"
520 "current interfaces. If a file cannot be opened with the access\n"
521 "requested, @code{open-file} throws an exception.")
522 #define FUNC_NAME s_scm_i_open_file
524 SCM encoding
= SCM_BOOL_F
;
525 SCM guess_encoding
= SCM_BOOL_F
;
527 scm_c_bind_keyword_arguments (FUNC_NAME
, keyword_args
, 0,
528 k_guess_encoding
, &guess_encoding
,
529 k_encoding
, &encoding
,
532 return scm_open_file_with_encoding (filename
, mode
,
533 guess_encoding
, encoding
);
538 /* Building Guile ports from a file descriptor. */
540 /* Build a Scheme port from an open file descriptor `fdes'.
541 MODE indicates whether FILE is open for reading or writing; it uses
542 the same notation as open-file's second argument.
543 NAME is a string to be used as the port's filename.
546 scm_i_fdes_to_port (int fdes
, long mode_bits
, SCM name
)
547 #define FUNC_NAME "scm_fdes_to_port"
551 scm_t_port_internal
*pti
;
553 /* Test that fdes is valid. */
555 int flags
= fcntl (fdes
, F_GETFL
, 0);
560 && ((flags
!= O_WRONLY
&& (mode_bits
& SCM_WRTNG
))
561 || (flags
!= O_RDONLY
&& (mode_bits
& SCM_RDNG
))))
563 SCM_MISC_ERROR ("requested file mode not available on fdes", SCM_EOL
);
566 /* If we don't have F_GETFL, as on mingw, at least we can test that
567 it is a valid file descriptor. */
569 if (fstat (fdes
, &st
) != 0)
573 scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex
);
575 port
= scm_new_port_table_entry (scm_tc16_fport
);
576 SCM_SET_CELL_TYPE(port
, scm_tc16_fport
| mode_bits
);
577 pt
= SCM_PTAB_ENTRY (port
);
579 /* File ports support 'setvbuf'. */
580 pti
= SCM_PORT_GET_INTERNAL (port
);
581 pti
->setvbuf
= scm_fport_buffer_add
;
585 = (scm_t_fport
*) scm_gc_malloc_pointerless (sizeof (scm_t_fport
),
589 pt
->rw_random
= SCM_FDES_RANDOM_P (fdes
);
590 SCM_SETSTREAM (port
, fp
);
591 if (mode_bits
& SCM_BUF0
)
592 scm_fport_buffer_add (port
, 0, 0);
594 scm_fport_buffer_add (port
, -1, -1);
596 SCM_SET_FILENAME (port
, name
);
597 scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex
);
603 scm_fdes_to_port (int fdes
, char *mode
, SCM name
)
605 return scm_i_fdes_to_port (fdes
, scm_mode_bits (mode
), name
);
608 /* Return a lower bound on the number of bytes available for input. */
610 fport_input_waiting (SCM port
)
612 int fdes
= SCM_FSTREAM (port
)->fdes
;
614 struct pollfd pollfd
= { fdes
, POLLIN
, 0 };
616 if (poll (&pollfd
, 1, 0) < 0)
617 scm_syserror ("fport_input_waiting");
619 return pollfd
.revents
& POLLIN
? 1 : 0;
624 fport_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
626 scm_puts ("#<", port
);
627 scm_print_port_mode (exp
, port
);
628 if (SCM_OPFPORTP (exp
))
631 SCM name
= SCM_FILENAME (exp
);
632 if (scm_is_string (name
) || scm_is_symbol (name
))
633 scm_display (name
, port
);
635 scm_puts (SCM_PTOBNAME (SCM_PTOBNUM (exp
)), port
);
636 scm_putc (' ', port
);
637 fdes
= (SCM_FSTREAM (exp
))->fdes
;
639 #if (defined HAVE_TTYNAME) && (defined HAVE_POSIX)
641 scm_display (scm_ttyname (exp
), port
);
643 #endif /* HAVE_TTYNAME */
644 scm_intprint (fdes
, 10, port
);
648 scm_puts (SCM_PTOBNAME (SCM_PTOBNUM (exp
)), port
);
649 scm_putc (' ', port
);
650 scm_uintprint ((scm_t_bits
) SCM_PTAB_ENTRY (exp
), 16, port
);
652 scm_putc ('>', port
);
656 static void fport_flush (SCM port
);
658 /* fill a port's read-buffer with a single read. returns the first
659 char or EOF if end of file. */
661 fport_fill_input (SCM port
)
664 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
665 scm_t_fport
*fp
= SCM_FSTREAM (port
);
667 SCM_SYSCALL (count
= read (fp
->fdes
, pt
->read_buf
, pt
->read_buf_size
));
669 scm_syserror ("fport_fill_input");
671 return (scm_t_wchar
) EOF
;
674 pt
->read_pos
= pt
->read_buf
;
675 pt
->read_end
= pt
->read_buf
+ count
;
676 return *pt
->read_buf
;
681 fport_seek (SCM port
, scm_t_off offset
, int whence
)
683 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
684 scm_t_fport
*fp
= SCM_FSTREAM (port
);
686 off_t_or_off64_t result
;
688 if (pt
->rw_active
== SCM_PORT_WRITE
)
690 if (offset
!= 0 || whence
!= SEEK_CUR
)
693 result
= rv
= lseek_or_lseek64 (fp
->fdes
, offset
, whence
);
697 /* read current position without disturbing the buffer. */
698 rv
= lseek_or_lseek64 (fp
->fdes
, offset
, whence
);
699 result
= rv
+ (pt
->write_pos
- pt
->write_buf
);
702 else if (pt
->rw_active
== SCM_PORT_READ
)
704 if (offset
!= 0 || whence
!= SEEK_CUR
)
706 /* could expand to avoid a second seek. */
707 scm_end_input (port
);
708 result
= rv
= lseek_or_lseek64 (fp
->fdes
, offset
, whence
);
712 /* read current position without disturbing the buffer
713 (particularly the unread-char buffer). */
714 rv
= lseek_or_lseek64 (fp
->fdes
, offset
, whence
);
715 result
= rv
- (pt
->read_end
- pt
->read_pos
);
717 if (pt
->read_buf
== pt
->putback_buf
)
718 result
-= pt
->saved_read_end
- pt
->saved_read_pos
;
721 else /* SCM_PORT_NEITHER */
723 result
= rv
= lseek_or_lseek64 (fp
->fdes
, offset
, whence
);
727 scm_syserror ("fport_seek");
733 fport_truncate (SCM port
, scm_t_off length
)
735 scm_t_fport
*fp
= SCM_FSTREAM (port
);
737 if (ftruncate (fp
->fdes
, length
) == -1)
738 scm_syserror ("ftruncate");
742 fport_write (SCM port
, const void *data
, size_t size
)
743 #define FUNC_NAME "fport_write"
745 /* this procedure tries to minimize the number of writes/flushes. */
746 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
748 if (pt
->write_buf
== &pt
->shortbuf
749 || (pt
->write_pos
== pt
->write_buf
&& size
>= pt
->write_buf_size
))
751 /* Unbuffered port, or port with empty buffer and data won't fit in
753 if (full_write (SCM_FPORT_FDES (port
), data
, size
) < size
)
760 scm_t_off space
= pt
->write_end
- pt
->write_pos
;
764 /* data fits in buffer. */
765 memcpy (pt
->write_pos
, data
, size
);
766 pt
->write_pos
+= size
;
767 if (pt
->write_pos
== pt
->write_end
)
770 /* we can skip the line-buffering check if nothing's buffered. */
776 memcpy (pt
->write_pos
, data
, space
);
777 pt
->write_pos
= pt
->write_end
;
780 const void *ptr
= ((const char *) data
) + space
;
781 size_t remaining
= size
- space
;
783 if (size
>= pt
->write_buf_size
)
785 if (full_write (SCM_FPORT_FDES (port
), ptr
, remaining
)
792 memcpy (pt
->write_pos
, ptr
, remaining
);
793 pt
->write_pos
+= remaining
;
798 /* handle line buffering. */
799 if ((SCM_CELL_WORD_0 (port
) & SCM_BUFLINE
) && memchr (data
, '\n', size
))
806 fport_flush (SCM port
)
809 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
810 scm_t_fport
*fp
= SCM_FSTREAM (port
);
811 size_t count
= pt
->write_pos
- pt
->write_buf
;
813 written
= full_write (fp
->fdes
, pt
->write_buf
, count
);
815 scm_syserror ("scm_flush");
817 pt
->write_pos
= pt
->write_buf
;
818 pt
->rw_active
= SCM_PORT_NEITHER
;
821 /* clear the read buffer and adjust the file position for unread bytes. */
823 fport_end_input (SCM port
, int offset
)
825 scm_t_fport
*fp
= SCM_FSTREAM (port
);
826 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
828 offset
+= pt
->read_end
- pt
->read_pos
;
832 pt
->read_pos
= pt
->read_end
;
833 /* will throw error if unread-char used at beginning of file
834 then attempting to write. seems correct. */
835 if (lseek (fp
->fdes
, -offset
, SEEK_CUR
) == -1)
836 scm_syserror ("fport_end_input");
838 pt
->rw_active
= SCM_PORT_NEITHER
;
842 fport_close (SCM port
)
844 scm_t_fport
*fp
= SCM_FSTREAM (port
);
845 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
849 SCM_SYSCALL (rv
= close (fp
->fdes
));
850 if (rv
== -1 && errno
!= EBADF
)
852 if (scm_gc_running_p
)
853 /* silently ignore the error. scm_error would abort if we
857 scm_syserror ("fport_close");
859 if (pt
->read_buf
== pt
->putback_buf
)
860 pt
->read_buf
= pt
->saved_read_buf
;
861 if (pt
->read_buf
!= &pt
->shortbuf
)
862 scm_gc_free (pt
->read_buf
, pt
->read_buf_size
, "port buffer");
863 if (pt
->write_buf
!= &pt
->shortbuf
)
864 scm_gc_free (pt
->write_buf
, pt
->write_buf_size
, "port buffer");
865 scm_gc_free (fp
, sizeof (*fp
), "file port");
870 fport_free (SCM port
)
879 scm_t_bits tc
= scm_make_port_type ("file", fport_fill_input
, fport_write
);
881 scm_set_port_free (tc
, fport_free
);
882 scm_set_port_print (tc
, fport_print
);
883 scm_set_port_flush (tc
, fport_flush
);
884 scm_set_port_end_input (tc
, fport_end_input
);
885 scm_set_port_close (tc
, fport_close
);
886 scm_set_port_seek (tc
, fport_seek
);
887 scm_set_port_truncate (tc
, fport_truncate
);
888 scm_set_port_input_waiting (tc
, fport_input_waiting
);
893 /* We can't initialize the keywords from 'scm_init_fports', because
894 keywords haven't yet been initialized at that point. */
896 scm_init_fports_keywords ()
898 k_guess_encoding
= scm_from_latin1_keyword ("guess-encoding");
899 k_encoding
= scm_from_latin1_keyword ("encoding");
905 scm_tc16_fport
= scm_make_fptob ();
907 scm_c_define ("_IOFBF", scm_from_int (_IOFBF
));
908 scm_c_define ("_IOLBF", scm_from_int (_IOLBF
));
909 scm_c_define ("_IONBF", scm_from_int (_IONBF
));
911 sys_file_port_name_canonicalization
= scm_make_fluid ();
912 scm_c_define ("%file-port-name-canonicalization",
913 sys_file_port_name_canonicalization
);
915 #include "libguile/fports.x"