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.
60 #define ttyname(x) "CON:"
67 #define setbuf(stream, buf) setvbuf(stream, buf, _IONBF, 0)
74 #include <sys/ioctl.h>
85 /* {Ports - file ports}
89 /* should be called with SCM_DEFER_INTS active */
92 scm_setbuf0 (SCM port
)
103 SCM_SYSCALL (setbuf ((FILE *)SCM_STREAM (port
), 0););
108 return SCM_UNSPECIFIED
;
111 /* Return the flags that characterize a port based on the mode
112 * string used to open a file for that port.
114 * See PORT FLAGS in scm.h
118 scm_mode_bits (char *modes
)
121 scm_mode_bits (modes
)
126 | (strchr (modes
, 'r') || strchr (modes
, '+') ? SCM_RDNG
: 0)
127 | ( strchr (modes
, 'w')
128 || strchr (modes
, 'a')
129 || strchr (modes
, '+') ? SCM_WRTNG
: 0)
130 | (strchr (modes
, '0') ? SCM_BUF0
: 0));
135 * Return a new port open on a given file.
137 * The mode string must match the pattern: [rwa+]** which
138 * is interpreted in the usual unix way.
140 * Return the new port.
145 scm_mkfile (char * name
, char * modes
)
148 scm_mkfile (name
, modes
)
157 SCM_SYSCALL (f
= fopen (name
, modes
));
165 struct scm_port_table
* pt
;
166 pt
= scm_add_to_port_table (port
);
167 SCM_SETPTAB_ENTRY (port
, pt
);
168 if (SCM_BUF0
& (SCM_CAR (port
) = scm_tc16_fport
| scm_mode_bits (modes
)))
170 SCM_SETSTREAM (port
, (SCM
)f
);
171 SCM_PTAB_ENTRY (port
)->file_name
= scm_makfrom0str (name
);
177 SCM_PROC(s_open_file
, "open-file", 2, 0, 0, scm_open_file
);
180 scm_open_file (SCM filename
, SCM modes
)
183 scm_open_file (filename
, modes
)
189 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_open_file
);
190 SCM_ASSERT (SCM_NIMP (modes
) && SCM_ROSTRINGP (modes
), modes
, SCM_ARG2
, s_open_file
);
191 if (SCM_SUBSTRP (filename
))
192 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
193 if (SCM_SUBSTRP (modes
))
194 modes
= scm_makfromstr (SCM_ROCHARS (modes
), SCM_ROLENGTH (modes
), 0);
195 port
= scm_mkfile (SCM_ROCHARS (filename
), SCM_ROCHARS (modes
));
196 /* Force the compiler to keep filename and modes alive:
198 if (port
== SCM_BOOL_F
)
199 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)