928aeeff5e709c1c2d3a042c2b83f17355e5b048
1 /* Copyright (C) 1995,1996 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.
68 #define setbuf(stream, buf) setvbuf(stream, buf, _IONBF, 0)
75 #include <sys/ioctl.h>
86 /* {Ports - file ports}
90 /* should be called with SCM_DEFER_INTS active */
100 SCM_SYSCALL (setbuf ((FILE *)SCM_STREAM (port
), 0););
105 return SCM_UNSPECIFIED
;
108 /* Return the flags that characterize a port based on the mode
109 * string used to open a file for that port.
111 * See PORT FLAGS in scm.h
115 scm_mode_bits (modes
)
119 | (strchr (modes
, 'r') || strchr (modes
, '+') ? SCM_RDNG
: 0)
120 | ( strchr (modes
, 'w')
121 || strchr (modes
, 'a')
122 || strchr (modes
, '+') ? SCM_WRTNG
: 0)
123 | (strchr (modes
, '0') ? SCM_BUF0
: 0));
128 * Return a new port open on a given file.
130 * The mode string must match the pattern: [rwa+]** which
131 * is interpreted in the usual unix way.
133 * Return the new port.
135 SCM_PROC(s_open_file
, "open-file", 2, 0, 0, scm_open_file
);
138 scm_open_file (filename
, modes
)
147 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_open_file
);
148 SCM_ASSERT (SCM_NIMP (modes
) && SCM_ROSTRINGP (modes
), modes
, SCM_ARG2
, s_open_file
);
149 if (SCM_SUBSTRP (filename
))
150 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
151 if (SCM_SUBSTRP (modes
))
152 modes
= scm_makfromstr (SCM_ROCHARS (modes
), SCM_ROLENGTH (modes
), 0);
154 file
= SCM_ROCHARS (filename
);
155 mode
= SCM_ROCHARS (modes
);
159 SCM_SYSCALL (f
= fopen (file
, mode
));
162 scm_syserror_msg (s_open_file
, "%s: %S",
163 scm_listify (scm_makfrom0str (strerror (errno
)),
169 struct scm_port_table
* pt
;
171 pt
= scm_add_to_port_table (port
);
172 SCM_SETPTAB_ENTRY (port
, pt
);
173 SCM_SETCAR (port
, scm_tc16_fport
| scm_mode_bits (mode
));
174 SCM_SETSTREAM (port
, (SCM
) f
);
175 if (SCM_BUF0
& SCM_CAR (port
))
177 SCM_PTAB_ENTRY (port
)->file_name
= filename
;
184 /* Build a Scheme port from an open stdio port, FILE.
185 MODE indicates whether FILE is open for reading or writing; it uses
186 the same notation as open-file's second argument.
187 If NAME is non-zero, use it as the port's filename.
189 scm_stdio_to_port sets the revealed count for FILE's file
190 descriptor to 1, so that FILE won't be closed when the port object
193 scm_stdio_to_port (file
, mode
, name
)
198 long mode_bits
= scm_mode_bits (mode
);
200 struct scm_port_table
* pt
;
205 pt
= scm_add_to_port_table (port
);
206 SCM_SETPTAB_ENTRY (port
, pt
);
207 SCM_SETCAR (port
, (scm_tc16_fport
| mode_bits
));
208 SCM_SETSTREAM (port
, (SCM
) file
);
209 if (SCM_BUF0
& SCM_CAR (port
))
211 SCM_PTAB_ENTRY (port
)->file_name
= scm_makfrom0str (name
);
214 scm_set_port_revealed_x (port
, SCM_MAKINUM (1));
219 /* Return the mode flags from an open port.
220 * Some modes such as "append" are only used when opening
221 * a file and are not returned here. */
223 SCM_PROC(s_port_mode
, "port-mode", 1, 0, 0, scm_port_mode
);
231 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_port_mode
);
232 if (SCM_CAR (port
) & SCM_RDNG
) {
233 if (SCM_CAR (port
) & SCM_WRTNG
)
234 strcpy (modes
, "r+");
238 else if (SCM_CAR (port
) & SCM_WRTNG
)
240 if (SCM_CAR (port
) & SCM_BUF0
)
242 return scm_makfromstr (modes
, strlen (modes
), 0);
247 static int prinfport
SCM_P ((SCM exp
, SCM port
, scm_print_state
*pstate
));
250 prinfport (exp
, port
, pstate
)
253 scm_print_state
*pstate
;
257 if (SCM_CLOSEDP (exp
))
263 name
= SCM_PTAB_ENTRY (exp
)->file_name
;
264 if (SCM_NIMP (name
) && SCM_ROSTRINGP (name
))
265 c
= SCM_ROCHARS (name
);
270 scm_prinport (exp
, port
, c
);
276 static int scm_fgetc
SCM_P ((FILE * s
));
290 static scm_sizet pwrite
SCM_P ((char *ptr
, scm_sizet size
, nitems
, FILE *port
));
293 pwrite (ptr
, size
, nitems
, port
)
295 scm_sizet size
, nitems
;
298 scm_sizet len
= size
* nitems
;
305 #define ffwrite pwrite
307 #define ffwrite fwrite
311 /* This otherwise pointless code helps some poor
312 * crippled C compilers cope with life.
315 static int local_fclose
SCM_P ((FILE *fp
));
324 static int local_fflush
SCM_P ((FILE *fp
));
333 static int local_fputc
SCM_P ((int c
, FILE *fp
));
340 return fputc (c
, fp
);
343 static int local_fputs
SCM_P ((char *s
, FILE *fp
));
350 return fputs (s
, fp
);
353 static scm_sizet local_ffwrite
SCM_P ((void *ptr
, int size
, int nitems
, FILE *fp
));
356 local_ffwrite (ptr
, size
, nitems
, fp
)
362 return ffwrite (ptr
, size
, nitems
, fp
);
366 scm_ptobfuns scm_fptob
=
369 (int (*) SCM_P ((SCM
))) local_fclose
,
372 (int (*) SCM_P ((int, SCM
))) local_fputc
,
373 (int (*) SCM_P ((char *, SCM
))) local_fputs
,
374 (scm_sizet (*) SCM_P ((char *, scm_sizet
, scm_sizet
, SCM
))) local_ffwrite
,
375 (int (*) SCM_P ((SCM
))) local_fflush
,
376 (int (*) SCM_P ((SCM
))) scm_fgetc
,
377 (int (*) SCM_P ((SCM
))) local_fclose
382 scm_ptobfuns scm_pipob
=
385 0, /* replaced by pclose in scm_init_ioext() */
386 0, /* replaced by prinpipe in scm_init_ioext() */
388 (int (*) SCM_P ((int, SCM
))) local_fputc
,
389 (int (*) SCM_P ((char *, SCM
))) local_fputs
,
390 (scm_sizet (*) SCM_P ((char *, scm_sizet
, scm_sizet
, SCM
))) local_ffwrite
,
391 (int (*) SCM_P ((SCM
))) local_fflush
,
392 (int (*) SCM_P ((SCM
))) scm_fgetc
,
394 }; /* replaced by pclose in scm_init_ioext() */