portability fixes for header inclusion etc.
[bpt/guile.git] / libguile / fports.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 Free Software Foundation, Inc.
2 *
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)
6 * any later version.
7 *
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.
12 *
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.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
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.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
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.
36 *
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.
40 */
41\f
42
43#include <stdio.h>
44#include "_scm.h"
95b88819
GH
45
46#ifdef HAVE_STRING_H
47#include <string.h>
48#endif
0f2d19dd
JB
49#ifdef HAVE_UNISTD_H
50#include <unistd.h>
51#else
0f2d19dd
JB
52scm_sizet fwrite ();
53#endif
0f2d19dd
JB
54
55
56#ifdef __IBMC__
57#include <io.h>
58#include <direct.h>
0f2d19dd
JB
59#else
60#ifndef MSDOS
61#ifndef ultrix
62#ifndef vms
63#ifdef _DCC
64#include <ioctl.h>
65#define setbuf(stream, buf) setvbuf(stream, buf, _IONBF, 0)
66#else
67#ifdef MWC
68#include <sys/io.h>
69#else
70#ifndef THINK_C
71#ifndef ARM_ULIB
72#include <sys/ioctl.h>
73#endif
74#endif
75#endif
76#endif
77#endif
78#endif
79#endif
80#endif
81\f
82
83/* {Ports - file ports}
84 *
85 */
86
87/* should be called with SCM_DEFER_INTS active */
88#ifdef __STDC__
89SCM
90scm_setbuf0 (SCM port)
91#else
92SCM
93scm_setbuf0 (port)
94 SCM port;
95#endif
96{
97#ifndef NOSETBUF
98#ifndef MSDOS
99#ifdef FIONREAD
100#ifndef ultrix
101 SCM_SYSCALL (setbuf ((FILE *)SCM_STREAM (port), 0););
102#endif
103#endif
104#endif
105#endif
106 return SCM_UNSPECIFIED;
107}
108
109/* Return the flags that characterize a port based on the mode
110 * string used to open a file for that port.
111 *
112 * See PORT FLAGS in scm.h
113 */
114#ifdef __STDC__
115long
116scm_mode_bits (char *modes)
117#else
118long
119scm_mode_bits (modes)
120 char *modes;
121#endif
122{
123 return (SCM_OPN
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));
129}
130
131
132/* scm_open_file
133 * Return a new port open on a given file.
134 *
135 * The mode string must match the pattern: [rwa+]** which
136 * is interpreted in the usual unix way.
137 *
138 * Return the new port.
139 */
140
141#ifdef __STDC__
142SCM
143scm_mkfile (char * name, char * modes)
144#else
145SCM
146scm_mkfile (name, modes)
147 char * name;
148 char * modes;
149#endif
150{
151 register SCM port;
152 FILE *f;
153 SCM_NEWCELL (port);
154 SCM_DEFER_INTS;
155 SCM_SYSCALL (f = fopen (name, modes));
156 if (!f)
157 {
158 SCM_ALLOW_INTS;
159 port = SCM_BOOL_F;
160 }
161 else
162 {
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)))
167 scm_setbuf0 (port);
168 SCM_SETSTREAM (port, (SCM)f);
169 SCM_PTAB_ENTRY (port)->file_name = scm_makfrom0str (name);
170 SCM_ALLOW_INTS;
171 }
172 return port;
173}
174
175SCM_PROC(s_open_file, "open-file", 2, 0, 0, scm_open_file);
176#ifdef __STDC__
177SCM
178scm_open_file (SCM filename, SCM modes)
179#else
180SCM
181scm_open_file (filename, modes)
182 SCM filename;
183 SCM modes;
184#endif
185{
186 SCM port;
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));
8b13c6b3
GH
194
195 if (port == SCM_BOOL_F) {
196 SCM_SYSERROR (s_open_file);
197 /* Force the compiler to keep filename and modes alive. */
0f2d19dd 198 scm_cons (filename, modes);
8b13c6b3 199 }
0f2d19dd
JB
200 return port;
201}
202
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.
206 */
207
208SCM_PROC(s_port_mode, "port-mode", 1, 0, 0, scm_port_mode);
209#ifdef __STDC__
210SCM
211scm_port_mode (SCM port)
212#else
213SCM
214scm_port_mode (port)
215 SCM port;
216#endif
217{
218 char modes[3];
219 modes[0] = '\0';
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+");
224 else
225 strcpy (modes, "r");
226 }
227 else if (SCM_CAR (port) & SCM_WRTNG)
228 strcpy (modes, "w");
229 if (SCM_CAR (port) & SCM_BUF0)
230 strcat (modes, "0");
231 return scm_makfromstr (modes, strlen (modes), 0);
232}
233
234
235#ifdef __STDC__
236static int
237prinfport (SCM exp, SCM port, int writing)
238#else
239static int
240prinfport (exp, port, writing)
241 SCM exp;
242 SCM port;
243 int writing;
244#endif
245{
246 SCM name;
247 char * c;
248 if (SCM_CLOSEDP (exp))
249 {
250 c = "file";
251 }
252 else
253 {
254 name = SCM_PTAB_ENTRY (exp)->file_name;
255 if (SCM_NIMP (name) && SCM_ROSTRINGP (name))
256 c = SCM_ROCHARS (name);
257 else
258 c = "file";
259 }
260
261 scm_prinport (exp, port, c);
262 return !0;
263}
264
265
266#ifdef __STDC__
267static int
268scm_fgetc (FILE * s)
269#else
270static int
271scm_fgetc (s)
272 FILE * s;
273#endif
274{
275 if (feof (s))
276 return EOF;
277 else
278 return fgetc (s);
279}
280
281#ifdef vms
282#ifdef __STDC__
283static scm_sizet
284pwrite (char *ptr, scm_sizet size, nitems, FILE *port)
285#else
286static scm_sizet
287pwrite (ptr, size, nitems, port)
288 char *ptr;
289 scm_sizet size, nitems;
290 FILE *port;
291#endif
292{
293 scm_sizet len = size * nitems;
294 scm_sizet i = 0;
295 for (; i < len; i++)
296 putc (ptr[i], port);
297 return len;
298}
299
300#define ffwrite pwrite
301#else
302#define ffwrite fwrite
303#endif
304
305\f
306/* This otherwise pointless code helps some poor
307 * crippled C compilers cope with life.
308 */
309static int
310local_fclose (fp)
311 FILE * fp;
312{
313 return fclose (fp);
314}
315
316static int
317local_fflush (fp)
318 FILE * fp;
319{
320 return fflush (fp);
321}
322
323static int
324local_fputc (c, fp)
325 int c;
326 FILE * fp;
327{
328 return fputc (c, fp);
329}
330
331static int
332local_fputs (s, fp)
333 char * s;
334 FILE * fp;
335{
336 return fputs (s, fp);
337}
338
339static scm_sizet
340local_ffwrite (ptr, size, nitems, fp)
341 void * ptr;
342 int size;
343 int nitems;
344 FILE * fp;
345{
346 return ffwrite (ptr, size, nitems, fp);
347}
348
349\f
350scm_ptobfuns scm_fptob =
351{
352 scm_mark0,
353 local_fclose,
354 prinfport,
355 0,
356 local_fputc,
357 local_fputs,
358 local_ffwrite,
359 local_fflush,
360 scm_fgetc,
361 local_fclose
362};
363
364/* {Pipe ports}
365 */
366scm_ptobfuns scm_pipob =
367{
368 scm_mark0,
369 0, /* replaced by pclose in scm_init_ioext() */
370 0, /* replaced by prinpipe in scm_init_ioext() */
371 0,
372 local_fputc,
373 local_fputs,
374 local_ffwrite,
375 local_fflush,
376 scm_fgetc,
377 0
378}; /* replaced by pclose in scm_init_ioext() */
379
380
381#ifdef __STDC__
382void
383scm_init_fports (void)
384#else
385void
386scm_init_fports ()
387#endif
388{
389#include "fports.x"
390}
391