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.
65 #define setbuf(stream, buf) setvbuf(stream, buf, _IONBF, 0)
72 #include <sys/ioctl.h>
83 /* {Ports - file ports}
87 /* should be called with SCM_DEFER_INTS active */
90 scm_setbuf0 (SCM port
)
101 SCM_SYSCALL (setbuf ((FILE *)SCM_STREAM (port
), 0););
106 return SCM_UNSPECIFIED
;
109 /* Return the flags that characterize a port based on the mode
110 * string used to open a file for that port.
112 * See PORT FLAGS in scm.h
116 scm_mode_bits (char *modes
)
119 scm_mode_bits (modes
)
124 | (strchr (modes
, 'r') || strchr (modes
, '+') ? SCM_RDNG
: 0)
125 | ( strchr (modes
, 'w')
126 || strchr (modes
, 'a')
127 || strchr (modes
, '+') ? SCM_WRTNG
: 0)
128 | (strchr (modes
, '0') ? SCM_BUF0
: 0));
133 * Return a new port open on a given file.
135 * The mode string must match the pattern: [rwa+]** which
136 * is interpreted in the usual unix way.
138 * Return the new port.
143 scm_mkfile (char * name
, char * modes
)
146 scm_mkfile (name
, modes
)
155 SCM_SYSCALL (f
= fopen (name
, modes
));
163 struct scm_port_table
* pt
;
164 pt
= scm_add_to_port_table (port
);
165 SCM_SETPTAB_ENTRY (port
, pt
);
166 if (SCM_BUF0
& (SCM_CAR (port
) = scm_tc16_fport
| scm_mode_bits (modes
)))
168 SCM_SETSTREAM (port
, (SCM
)f
);
169 SCM_PTAB_ENTRY (port
)->file_name
= scm_makfrom0str (name
);
175 SCM_PROC(s_open_file
, "open-file", 2, 0, 0, scm_open_file
);
178 scm_open_file (SCM filename
, SCM modes
)
181 scm_open_file (filename
, modes
)
187 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_open_file
);
188 SCM_ASSERT (SCM_NIMP (modes
) && SCM_ROSTRINGP (modes
), modes
, SCM_ARG2
, s_open_file
);
189 if (SCM_SUBSTRP (filename
))
190 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
191 if (SCM_SUBSTRP (modes
))
192 modes
= scm_makfromstr (SCM_ROCHARS (modes
), SCM_ROLENGTH (modes
), 0);
193 port
= scm_mkfile (SCM_ROCHARS (filename
), SCM_ROCHARS (modes
));
195 if (port
== SCM_BOOL_F
) {
196 SCM_SYSERROR (s_open_file
);
197 /* Force the compiler to keep filename and modes alive. */
198 scm_cons (filename
, modes
);
203 /* Return the mode flags from an open port.
204 * Some modes such as "append" are only used when opening
205 * a file and are not returned here.
208 SCM_PROC(s_port_mode
, "port-mode", 1, 0, 0, scm_port_mode
);
211 scm_port_mode (SCM port
)
220 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_port_mode
);
221 if (SCM_CAR (port
) & SCM_RDNG
) {
222 if (SCM_CAR (port
) & SCM_WRTNG
)
223 strcpy (modes
, "r+");
227 else if (SCM_CAR (port
) & SCM_WRTNG
)
229 if (SCM_CAR (port
) & SCM_BUF0
)
231 return scm_makfromstr (modes
, strlen (modes
), 0);
237 prinfport (SCM exp
, SCM port
, int writing
)
240 prinfport (exp
, port
, writing
)
248 if (SCM_CLOSEDP (exp
))
254 name
= SCM_PTAB_ENTRY (exp
)->file_name
;
255 if (SCM_NIMP (name
) && SCM_ROSTRINGP (name
))
256 c
= SCM_ROCHARS (name
);
261 scm_prinport (exp
, port
, c
);
284 pwrite (char *ptr
, scm_sizet size
, nitems
, FILE *port
)
287 pwrite (ptr
, size
, nitems
, port
)
289 scm_sizet size
, nitems
;
293 scm_sizet len
= size
* nitems
;
300 #define ffwrite pwrite
302 #define ffwrite fwrite
306 /* This otherwise pointless code helps some poor
307 * crippled C compilers cope with life.
328 return fputc (c
, fp
);
336 return fputs (s
, fp
);
340 local_ffwrite (ptr
, size
, nitems
, fp
)
346 return ffwrite (ptr
, size
, nitems
, fp
);
350 scm_ptobfuns scm_fptob
=
366 scm_ptobfuns scm_pipob
=
369 0, /* replaced by pclose in scm_init_ioext() */
370 0, /* replaced by prinpipe in scm_init_ioext() */
378 }; /* replaced by pclose in scm_init_ioext() */
383 scm_init_fports (void)