a few fixups to primitive functions
[bpt/guile.git] / libguile / ioext.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995 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
44#include <stdio.h>
45#include <unistd.h>
46#include "fd.h"
47#include "_scm.h"
48
49\f
50
02b754d3 51SCM_PROC (s_sys_ftell, "ftell", 1, 0, 0, scm_sys_ftell);
0f2d19dd
JB
52#ifdef __STDC__
53SCM
54scm_sys_ftell (SCM port)
55#else
56SCM
57scm_sys_ftell (port)
58 SCM port;
59#endif
60{
61 long pos;
62 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_sys_ftell);
63 SCM_SYSCALL (pos = ftell ((FILE *)SCM_STREAM (port)));
64 if (pos < 0)
02b754d3 65 SCM_SYSERROR (s_sys_ftell);
0f2d19dd
JB
66 if (pos > 0 && SCM_CRDYP (port))
67 pos--;
68 return SCM_MAKINUM (pos);
69}
70
71
72
02b754d3 73SCM_PROC (s_sys_fseek, "fseek", 3, 0, 0, scm_sys_fseek);
0f2d19dd
JB
74#ifdef __STDC__
75SCM
76scm_sys_fseek (SCM port, SCM offset, SCM whence)
77#else
78SCM
79scm_sys_fseek (port, offset, whence)
80 SCM port;
81 SCM offset;
82 SCM whence;
83#endif
84{
85 int rv;
86 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_sys_fseek);
87 SCM_ASSERT (SCM_INUMP (offset), offset, SCM_ARG2, s_sys_fseek);
88 SCM_ASSERT (SCM_INUMP (whence) && (SCM_INUM (whence) < 3) && (SCM_INUM (whence) >= 0),
89 whence, SCM_ARG3, s_sys_fseek);
90 SCM_CLRDY (port); /* Clear ungetted char */
91 /* Values of whence are interned in scm_init_ioext. */
92 rv = fseek ((FILE *)SCM_STREAM (port), SCM_INUM (offset), SCM_INUM (whence));
02b754d3
GH
93 if (rv != 0)
94 SCM_SYSERROR (s_sys_fseek);
95 return SCM_UNSPECIFIED;
0f2d19dd
JB
96}
97
98
99
02b754d3 100SCM_PROC (s_sys_freopen, "freopen", 3, 0, 0, scm_sys_freopen);
0f2d19dd
JB
101#ifdef __STDC__
102SCM
103scm_sys_freopen (SCM filename, SCM modes, SCM port)
104#else
105SCM
106scm_sys_freopen (filename, modes, port)
107 SCM filename;
108 SCM modes;
109 SCM port;
110#endif
111{
112 FILE *f;
113 SCM_ASSERT (SCM_NIMP (filename) && SCM_STRINGP (filename), filename, SCM_ARG1, s_sys_freopen);
114 SCM_ASSERT (SCM_NIMP (modes) && SCM_STRINGP (modes), modes, SCM_ARG2, s_sys_freopen);
115 SCM_DEFER_INTS;
116 SCM_ASSERT (SCM_NIMP (port) && SCM_FPORTP (port), port, SCM_ARG3, s_sys_freopen);
117 SCM_SYSCALL (f = freopen (SCM_CHARS (filename), SCM_CHARS (modes), (FILE *)SCM_STREAM (port)));
118 if (!f)
119 {
120 SCM p;
121 p = port;
122 port = SCM_MAKINUM (errno);
123 SCM_CAR (p) &= ~SCM_OPN;
124 scm_remove_from_port_table (p);
125 }
126 else
127 {
128 SCM_CAR (port) = scm_tc16_fport | scm_mode_bits (SCM_CHARS (modes));
129 SCM_SETSTREAM (port, (SCM)f);
130 if (SCM_BUF0 & (SCM_CAR (port) = scm_tc16_fport | scm_mode_bits (SCM_CHARS (modes))))
131 scm_setbuf0 (port);
132 }
133 SCM_ALLOW_INTS;
134 return port;
135}
136
137
138
02b754d3 139SCM_PROC (s_sys_duplicate_port, "duplicate-port", 2, 0, 0, scm_sys_duplicate_port);
0f2d19dd
JB
140#ifdef __STDC__
141SCM
142scm_sys_duplicate_port (SCM oldpt, SCM modes)
143#else
144SCM
145scm_sys_duplicate_port (oldpt, modes)
146 SCM oldpt;
147 SCM modes;
148#endif
149{
150 int oldfd;
151 int newfd;
152 FILE *f;
153 SCM newpt;
154 SCM_ASSERT (SCM_NIMP (oldpt) && SCM_OPPORTP (oldpt), oldpt, SCM_ARG1, s_sys_duplicate_port);
155 SCM_ASSERT (SCM_NIMP (modes) && SCM_STRINGP (modes), modes, SCM_ARG2, s_sys_duplicate_port);
156 SCM_NEWCELL (newpt);
157 SCM_DEFER_INTS;
158 oldfd = fileno ((FILE *)SCM_STREAM (oldpt));
159 if (oldfd == -1)
02b754d3 160 SCM_SYSERROR (s_sys_duplicate_port);
0f2d19dd
JB
161 SCM_SYSCALL (newfd = dup (oldfd));
162 if (newfd == -1)
02b754d3 163 SCM_SYSERROR (s_sys_duplicate_port);
0f2d19dd
JB
164 f = fdopen (newfd, SCM_CHARS (modes));
165 if (!f)
166 {
167 SCM_SYSCALL (close (newfd));
02b754d3 168 SCM_SYSERROR (s_sys_duplicate_port);
0f2d19dd
JB
169 }
170 {
171 struct scm_port_table * pt;
172 pt = scm_add_to_port_table (newpt);
173 SCM_SETPTAB_ENTRY (newpt, pt);
174 if (SCM_BUF0 & (SCM_CAR (newpt) = scm_tc16_fport | scm_mode_bits (SCM_CHARS (modes))))
175 scm_setbuf0 (newpt);
176 SCM_SETSTREAM (newpt, (SCM)f);
177 SCM_PTAB_ENTRY (newpt)->file_name = SCM_PTAB_ENTRY (oldpt)->file_name;
178 }
179 SCM_ALLOW_INTS;
180 return newpt;
181}
182
183
184
02b754d3 185SCM_PROC (s_sys_redirect_port, "redirect-port", 2, 0, 0, scm_sys_redirect_port);
0f2d19dd
JB
186#ifdef __STDC__
187SCM
188scm_sys_redirect_port (SCM into_pt, SCM from_pt)
189#else
190SCM
191scm_sys_redirect_port (into_pt, from_pt)
192 SCM into_pt;
193 SCM from_pt;
194#endif
195{
196 int ans, oldfd, newfd;
197 SCM_DEFER_INTS;
198 SCM_ASSERT (SCM_NIMP (into_pt) && SCM_OPPORTP (into_pt), into_pt, SCM_ARG1, s_sys_redirect_port);
199 SCM_ASSERT (SCM_NIMP (from_pt) && SCM_OPPORTP (from_pt), from_pt, SCM_ARG2, s_sys_redirect_port);
200 oldfd = fileno ((FILE *)SCM_STREAM (into_pt));
02b754d3
GH
201 if (oldfd == -1)
202 SCM_SYSERROR (s_sys_redirect_port);
0f2d19dd 203 newfd = fileno ((FILE *)SCM_STREAM (from_pt));
02b754d3
GH
204 if (newfd == -1)
205 SCM_SYSERROR (s_sys_redirect_port);
206 SCM_SYSCALL (ans = dup2 (oldfd, newfd));
207 if (ans == -1)
208 SCM_SYSERROR (s_sys_redirect_port);
0f2d19dd 209 SCM_ALLOW_INTS;
02b754d3 210 return SCM_UNSPECIFIED;
0f2d19dd
JB
211}
212
8b13c6b3 213SCM_PROC (s_sys_fileno, "fileno", 1, 0, 0, scm_sys_fileno);
0f2d19dd
JB
214#ifdef __STDC__
215SCM
216scm_sys_fileno (SCM port)
217#else
218SCM
219scm_sys_fileno (port)
220 SCM port;
221#endif
222{
223 int fd;
224 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_sys_fileno);
225 fd = fileno ((FILE *)SCM_STREAM (port));
02b754d3
GH
226 if (fd == -1)
227 SCM_SYSERROR (s_sys_fileno);
228 return SCM_MAKINUM (fd);
0f2d19dd
JB
229}
230
02b754d3 231SCM_PROC (s_sys_isatty, "isatty?", 1, 0, 0, scm_sys_isatty_p);
0f2d19dd
JB
232#ifdef __STDC__
233SCM
234scm_sys_isatty_p (SCM port)
235#else
236SCM
237scm_sys_isatty_p (port)
238 SCM port;
239#endif
240{
241 int rv;
242 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_sys_isatty);
243 rv = fileno ((FILE *)SCM_STREAM (port));
244 if (rv == -1)
02b754d3
GH
245 SCM_SYSERROR (s_sys_isatty);
246 rv = isatty (rv);
247 return rv ? SCM_BOOL_T : SCM_BOOL_F;
0f2d19dd
JB
248}
249
250
251
02b754d3 252SCM_PROC (s_sys_fdopen, "fdopen", 2, 0, 0, scm_sys_fdopen);
0f2d19dd
JB
253#ifdef __STDC__
254SCM
255scm_sys_fdopen (SCM fdes, SCM modes)
256#else
257SCM
258scm_sys_fdopen (fdes, modes)
259 SCM fdes;
260 SCM modes;
261#endif
262{
263 FILE *f;
264 SCM port;
8b13c6b3 265 struct scm_port_table * pt;
0f2d19dd
JB
266
267 SCM_ASSERT (SCM_INUMP (fdes), fdes, SCM_ARG1, s_sys_fdopen);
268 SCM_ASSERT (SCM_NIMP (modes) && SCM_STRINGP (modes), modes, SCM_ARG2, s_sys_fdopen);
8b13c6b3 269 SCM_NEWCELL (port);
0f2d19dd
JB
270 SCM_DEFER_INTS;
271 f = fdopen (SCM_INUM (fdes), SCM_CHARS (modes));
272 if (f == NULL)
02b754d3 273 SCM_SYSERROR (s_sys_fdopen);
8b13c6b3
GH
274 pt = scm_add_to_port_table (port);
275 SCM_SETPTAB_ENTRY (port, pt);
276 if (SCM_BUF0 & (SCM_CAR (port) = scm_tc16_fport
277 | scm_mode_bits (SCM_CHARS (modes))))
278 scm_setbuf0 (port);
279 SCM_SETSTREAM (port, (SCM)f);
0f2d19dd
JB
280 SCM_ALLOW_INTS;
281 return port;
282}
283
284
285
286/* Move a port's underlying file descriptor to a given value.
8b13c6b3
GH
287 * Returns #f if fdes is already the given value.
288 * #t if fdes moved.
0f2d19dd
JB
289 * MOVE->FDES is implemented in Scheme and calls this primitive.
290 */
02b754d3 291SCM_PROC (s_sys_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0, scm_sys_primitive_move_to_fdes);
0f2d19dd
JB
292#ifdef __STDC__
293SCM
294scm_sys_primitive_move_to_fdes (SCM port, SCM fd)
295#else
296SCM
297scm_sys_primitive_move_to_fdes (port, fd)
298 SCM port;
299 SCM fd;
300#endif
301{
302 FILE *stream;
303 int old_fd;
304 int new_fd;
305 int rv;
306
307 SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_sys_primitive_move_to_fdes);
308 SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG2, s_sys_primitive_move_to_fdes);
309 SCM_DEFER_INTS;
310 stream = (FILE *)SCM_STREAM (port);
311 old_fd = fileno (stream);
312 new_fd = SCM_INUM (fd);
313 if (old_fd == new_fd)
314 {
315 SCM_ALLOW_INTS;
8b13c6b3 316 return SCM_BOOL_F;
0f2d19dd
JB
317 }
318 scm_evict_ports (new_fd);
319 rv = dup2 (old_fd, new_fd);
320 if (rv == -1)
02b754d3 321 SCM_SYSERROR (s_sys_primitive_move_to_fdes);
0f2d19dd
JB
322 scm_setfileno (stream, new_fd);
323 SCM_SYSCALL (close (old_fd));
324 SCM_ALLOW_INTS;
8b13c6b3 325 return SCM_BOOL_T;
0f2d19dd
JB
326}
327
0f2d19dd
JB
328#ifdef __STDC__
329void
330scm_setfileno (FILE *fs, int fd)
331#else
332void
333scm_setfileno (fs, fd)
334 FILE *fs;
335 int fd;
336#endif
337{
338#ifdef SET_FILE_FD_FIELD
339 SET_FILE_FD_FIELD(fs, fd);
340#else
341 Configure could not guess the name of the correct field in a FILE *.
342
343 This function needs to be ported to your system.
344
345 SET_FILE_FD_FIELD should change the descriptor refered to by a stdio
346 stream, and nothing else.
347
348 The way to port this file is to add cases to configure.in. Search
349 that file for "SET_FILE_FD_FIELD" and follow the examples there.
350#endif
351}
352
353/* Move ports with the specified file descriptor to new descriptors,
354 * reseting the revealed count to 0.
355 * Should be called with SCM_DEFER_INTS active.
356 */
357#ifdef __STDC__
358void
359scm_evict_ports (int fd)
360#else
361void
362scm_evict_ports (fd)
363 int fd;
364#endif
365{
366 int i;
367
368 for (i = 0; i < scm_port_table_size; i++)
369 {
370 if (SCM_FPORTP (scm_port_table[i]->port)
371 && fileno ((FILE *)SCM_STREAM (scm_port_table[i]->port)) == fd)
372 {
373 scm_setfileno ((FILE *)SCM_STREAM (scm_port_table[i]->port), dup (fd));
374 scm_set_port_revealed_x (scm_port_table[i]->port, SCM_MAKINUM (0));
375 }
376 }
377}
378
379/* Return a list of ports using a given file descriptor. */
380SCM_PROC(s_fdes_to_ports, "fdes->ports", 1, 0, 0, scm_fdes_to_ports);
381#ifdef __STDC__
382SCM
383scm_fdes_to_ports (SCM fd)
384#else
385SCM
386scm_fdes_to_ports (fd)
387 SCM fd;
388#endif
389{
390 SCM result = SCM_EOL;
391 int int_fd;
392 int i;
393
394 SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG1, s_fdes_to_ports);
395 int_fd = SCM_INUM (fd);
396
397 SCM_DEFER_INTS;
398 for (i = 0; i < scm_port_table_size; i++)
399 {
400 if (SCM_FPORTP (scm_port_table[i]->port)
401 && fileno ((FILE *)SCM_STREAM (scm_port_table[i]->port)) == int_fd)
402 result = scm_cons (scm_port_table[i]->port, result);
403 }
404 SCM_ALLOW_INTS;
405 return result;
406}
407
408#ifdef __STDC__
409void
410scm_init_ioext (void)
411#else
412void
413scm_init_ioext ()
414#endif
415{
416 /* fseek() symbols. */
417 scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET));
418 scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR));
419 scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END));
420
421 /* access() symbols. */
422 scm_sysintern ("R_OK", SCM_MAKINUM (R_OK));
423 scm_sysintern ("W_OK", SCM_MAKINUM (W_OK));
424 scm_sysintern ("X_OK", SCM_MAKINUM (X_OK));
425 scm_sysintern ("F_OK", SCM_MAKINUM (F_OK));
426
427 /* File type/permission bits. */
428#ifdef S_IRUSR
429 scm_sysintern ("S_IRUSR", SCM_MAKINUM (S_IRUSR));
430#endif
431#ifdef S_IWUSR
432 scm_sysintern ("S_IWUSR", SCM_MAKINUM (S_IWUSR));
433#endif
434#ifdef S_IXUSR
435 scm_sysintern ("S_IXUSR", SCM_MAKINUM (S_IXUSR));
436#endif
437#ifdef S_IRWXU
438 scm_sysintern ("S_IRWXU", SCM_MAKINUM (S_IRWXU));
439#endif
440
441#ifdef S_IRGRP
442 scm_sysintern ("S_IRGRP", SCM_MAKINUM (S_IRGRP));
443#endif
444#ifdef S_IWGRP
445 scm_sysintern ("S_IWGRP", SCM_MAKINUM (S_IWGRP));
446#endif
447#ifdef S_IXGRP
448 scm_sysintern ("S_IXGRP", SCM_MAKINUM (S_IXGRP));
449#endif
450#ifdef S_IRWXG
451 scm_sysintern ("S_IRWXG", SCM_MAKINUM (S_IRWXG));
452#endif
453
454#ifdef S_IROTH
455 scm_sysintern ("S_IROTH", SCM_MAKINUM (S_IROTH));
456#endif
457#ifdef S_IWOTH
458 scm_sysintern ("S_IWOTH", SCM_MAKINUM (S_IWOTH));
459#endif
460#ifdef S_IXOTH
461 scm_sysintern ("S_IXOTH", SCM_MAKINUM (S_IXOTH));
462#endif
463#ifdef S_IRWXO
464 scm_sysintern ("S_IRWXO", SCM_MAKINUM (S_IRWXO));
465#endif
466
467#ifdef S_ISUID
468 scm_sysintern ("S_ISUID", SCM_MAKINUM (S_ISUID));
469#endif
470#ifdef S_ISGID
471 scm_sysintern ("S_ISGID", SCM_MAKINUM (S_ISGID));
472#endif
473#ifdef S_ISVTX
474 scm_sysintern ("S_ISVTX", SCM_MAKINUM (S_ISVTX));
475#endif
476
477#ifdef S_IFMT
478 scm_sysintern ("S_IFMT", SCM_MAKINUM (S_IFMT));
479#endif
480#ifdef S_IFDIR
481 scm_sysintern ("S_IFDIR", SCM_MAKINUM (S_IFDIR));
482#endif
483#ifdef S_IFCHR
484 scm_sysintern ("S_IFCHR", SCM_MAKINUM (S_IFCHR));
485#endif
486#ifdef S_IFBLK
487 scm_sysintern ("S_IFBLK", SCM_MAKINUM (S_IFBLK));
488#endif
489#ifdef S_IFREG
490 scm_sysintern ("S_IFREG", SCM_MAKINUM (S_IFREG));
491#endif
492#ifdef S_IFLNK
493 scm_sysintern ("S_IFLNK", SCM_MAKINUM (S_IFLNK));
494#endif
495#ifdef S_IFSOCK
496 scm_sysintern ("S_IFSOCK", SCM_MAKINUM (S_IFSOCK));
497#endif
498#ifdef S_IFIFO
499 scm_sysintern ("S_IFIFO", SCM_MAKINUM (S_IFIFO));
500#endif
501#include "ioext.x"
502}
503