Commit | Line | Data |
---|---|---|
840ae05d | 1 | /* Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc. |
0f2d19dd JB |
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 | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
0f2d19dd JB |
41 | \f |
42 | ||
43 | #include <stdio.h> | |
cb63cf9e | 44 | #include <fcntl.h> |
0f2d19dd | 45 | #include "_scm.h" |
20e6290e JB |
46 | |
47 | #include "fports.h" | |
95b88819 GH |
48 | |
49 | #ifdef HAVE_STRING_H | |
50 | #include <string.h> | |
51 | #endif | |
0f2d19dd JB |
52 | #ifdef HAVE_UNISTD_H |
53 | #include <unistd.h> | |
54 | #else | |
0f2d19dd JB |
55 | scm_sizet fwrite (); |
56 | #endif | |
cb63cf9e JB |
57 | #ifdef HAVE_ST_BLKSIZE |
58 | #include <sys/stat.h> | |
59 | #endif | |
0f2d19dd | 60 | |
cb63cf9e | 61 | #include <errno.h> |
e145dd02 | 62 | |
cb63cf9e JB |
63 | #include "iselect.h" |
64 | ||
65 | /* create FPORT buffer with specified sizes (or -1 to use default size or | |
66 | 0 for no buffer. */ | |
67 | static void | |
68 | scm_fport_buffer_add (SCM port, int read_size, int write_size) | |
e145dd02 | 69 | { |
cb63cf9e | 70 | struct scm_fport *fp = SCM_FSTREAM (port); |
840ae05d | 71 | scm_port *pt = SCM_PTAB_ENTRY (port); |
cb63cf9e | 72 | char *s_scm_fport_buffer_add = "scm_fport_buffer_add"; |
e145dd02 | 73 | |
cb63cf9e JB |
74 | if (read_size == -1 || write_size == -1) |
75 | { | |
76 | int default_size; | |
77 | #ifdef HAVE_ST_BLKSIZE | |
78 | struct stat st; | |
79 | ||
80 | if (fstat (fp->fdes, &st) == -1) | |
81 | scm_syserror (s_scm_fport_buffer_add); | |
82 | default_size = st.st_blksize; | |
83 | #else | |
84 | default_size = 1024; | |
85 | #endif | |
86 | if (read_size == -1) | |
87 | read_size = default_size; | |
88 | if (write_size == -1) | |
89 | write_size = default_size; | |
90 | } | |
0f2d19dd | 91 | |
cb63cf9e JB |
92 | if (SCM_INPORTP (port) && read_size > 0) |
93 | { | |
840ae05d JB |
94 | pt->read_buf = malloc (read_size); |
95 | if (pt->read_buf == NULL) | |
96 | scm_memory_error (s_scm_fport_buffer_add); | |
cb63cf9e JB |
97 | pt->read_pos = pt->read_end = pt->read_buf; |
98 | pt->read_buf_size = read_size; | |
99 | } | |
100 | else | |
101 | { | |
840ae05d | 102 | pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf; |
cb63cf9e JB |
103 | pt->read_buf_size = 1; |
104 | } | |
1717856b | 105 | |
cb63cf9e JB |
106 | if (SCM_OUTPORTP (port) && write_size > 0) |
107 | { | |
840ae05d JB |
108 | pt->write_buf = malloc (write_size); |
109 | if (pt->write_buf == NULL) | |
110 | scm_memory_error (s_scm_fport_buffer_add); | |
cb63cf9e JB |
111 | pt->write_pos = pt->write_buf; |
112 | pt->write_buf_size = write_size; | |
113 | } | |
114 | else | |
115 | { | |
116 | pt->write_buf = pt->write_pos = &pt->shortbuf; | |
117 | pt->write_buf_size = 1; | |
118 | } | |
119 | ||
120 | pt->write_end = pt->write_buf + pt->write_buf_size; | |
121 | if (read_size > 0 || write_size > 0) | |
122 | SCM_SETCAR (port, SCM_CAR (port) & ~SCM_BUF0); | |
123 | else | |
124 | SCM_SETCAR (port, (SCM_CAR (port) | SCM_BUF0)); | |
7a6f1ffa GH |
125 | } |
126 | ||
127 | SCM_PROC (s_setvbuf, "setvbuf", 2, 1, 0, scm_setvbuf); | |
128 | SCM | |
129 | scm_setvbuf (SCM port, SCM mode, SCM size) | |
130 | { | |
7a6f1ffa | 131 | int cmode, csize; |
840ae05d | 132 | scm_port *pt; |
7a6f1ffa | 133 | |
78446828 MV |
134 | port = SCM_COERCE_OUTPORT (port); |
135 | ||
cb63cf9e JB |
136 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, |
137 | s_setvbuf); | |
7a6f1ffa | 138 | SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG2, s_setvbuf); |
cb63cf9e JB |
139 | cmode = SCM_INUM (mode); |
140 | if (cmode != _IONBF && cmode != _IOFBF) | |
141 | scm_out_of_range (s_setvbuf, mode); | |
7a6f1ffa | 142 | if (SCM_UNBNDP (size)) |
cb63cf9e JB |
143 | { |
144 | if (cmode == _IOFBF) | |
145 | csize = -1; | |
146 | else | |
147 | csize = 0; | |
148 | } | |
7a6f1ffa GH |
149 | else |
150 | { | |
151 | SCM_ASSERT (SCM_INUMP (size), size, SCM_ARG3, s_setvbuf); | |
152 | csize = SCM_INUM (size); | |
cb63cf9e JB |
153 | if (csize < 0 || (cmode == _IONBF && csize > 0)) |
154 | scm_out_of_range (s_setvbuf, size); | |
7a6f1ffa | 155 | } |
cb63cf9e | 156 | pt = SCM_PTAB_ENTRY (port); |
7a6f1ffa | 157 | |
cb63cf9e JB |
158 | /* silently discards buffered chars. */ |
159 | if (pt->read_buf != &pt->shortbuf) | |
160 | scm_must_free (pt->read_buf); | |
161 | if (pt->write_buf != &pt->shortbuf) | |
162 | scm_must_free (pt->write_buf); | |
7a6f1ffa | 163 | |
cb63cf9e JB |
164 | scm_fport_buffer_add (port, csize, csize); |
165 | return SCM_UNSPECIFIED; | |
0f2d19dd JB |
166 | } |
167 | ||
eadd48de GH |
168 | /* Move ports with the specified file descriptor to new descriptors, |
169 | * reseting the revealed count to 0. | |
0f2d19dd | 170 | */ |
1717856b | 171 | |
eadd48de GH |
172 | void |
173 | scm_evict_ports (fd) | |
174 | int fd; | |
0f2d19dd | 175 | { |
eadd48de | 176 | int i; |
0f2d19dd | 177 | |
eadd48de GH |
178 | for (i = 0; i < scm_port_table_size; i++) |
179 | { | |
cb63cf9e JB |
180 | SCM port = scm_port_table[i]->port; |
181 | ||
182 | if (SCM_FPORTP (port)) | |
eadd48de | 183 | { |
cb63cf9e JB |
184 | struct scm_fport *fp = SCM_FSTREAM (port); |
185 | ||
186 | if (fp->fdes == fd) | |
187 | { | |
188 | fp->fdes = dup (fd); | |
189 | if (fp->fdes == -1) | |
190 | scm_syserror ("scm_evict_ports"); | |
191 | scm_set_port_revealed_x (port, SCM_MAKINUM (0)); | |
192 | } | |
eadd48de GH |
193 | } |
194 | } | |
195 | } | |
0f2d19dd JB |
196 | |
197 | /* scm_open_file | |
198 | * Return a new port open on a given file. | |
199 | * | |
200 | * The mode string must match the pattern: [rwa+]** which | |
201 | * is interpreted in the usual unix way. | |
202 | * | |
203 | * Return the new port. | |
204 | */ | |
19639113 | 205 | SCM_PROC(s_open_file, "open-file", 2, 0, 0, scm_open_file); |
1717856b | 206 | |
0f2d19dd | 207 | SCM |
19639113 GH |
208 | scm_open_file (filename, modes) |
209 | SCM filename; | |
210 | SCM modes; | |
0f2d19dd | 211 | { |
19639113 | 212 | SCM port; |
cb63cf9e JB |
213 | int fdes; |
214 | int flags = 0; | |
19639113 GH |
215 | char *file; |
216 | char *mode; | |
cb63cf9e | 217 | char *ptr; |
19639113 GH |
218 | |
219 | SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename, SCM_ARG1, s_open_file); | |
220 | SCM_ASSERT (SCM_NIMP (modes) && SCM_ROSTRINGP (modes), modes, SCM_ARG2, s_open_file); | |
221 | if (SCM_SUBSTRP (filename)) | |
222 | filename = scm_makfromstr (SCM_ROCHARS (filename), SCM_ROLENGTH (filename), 0); | |
223 | if (SCM_SUBSTRP (modes)) | |
224 | modes = scm_makfromstr (SCM_ROCHARS (modes), SCM_ROLENGTH (modes), 0); | |
225 | ||
226 | file = SCM_ROCHARS (filename); | |
227 | mode = SCM_ROCHARS (modes); | |
228 | ||
cb63cf9e | 229 | switch (*mode) |
0f2d19dd | 230 | { |
cb63cf9e JB |
231 | case 'r': |
232 | flags |= O_RDONLY; | |
233 | break; | |
234 | case 'w': | |
235 | flags |= O_WRONLY | O_CREAT | O_TRUNC; | |
236 | break; | |
237 | case 'a': | |
238 | flags |= O_WRONLY | O_CREAT | O_APPEND; | |
239 | break; | |
240 | default: | |
241 | scm_out_of_range (s_open_file, modes); | |
0f2d19dd | 242 | } |
cb63cf9e JB |
243 | ptr = mode + 1; |
244 | while (*ptr != '\0') | |
e145dd02 | 245 | { |
cb63cf9e JB |
246 | switch (*ptr) |
247 | { | |
248 | case '+': | |
249 | flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR; | |
250 | break; | |
251 | case '0': /* unbuffered: handled later. */ | |
252 | case 'b': /* 'binary' mode: ignored. */ | |
253 | break; | |
254 | default: | |
255 | scm_out_of_range (s_open_file, modes); | |
256 | } | |
257 | ptr++; | |
e145dd02 | 258 | } |
cb63cf9e JB |
259 | SCM_SYSCALL (fdes = open (file, flags, 0666)); |
260 | if (fdes == -1) | |
e145dd02 | 261 | { |
cb63cf9e JB |
262 | int en = errno; |
263 | ||
264 | scm_syserror_msg (s_open_file, "%s: %S", | |
265 | scm_cons (scm_makfrom0str (strerror (en)), | |
266 | scm_cons (filename, SCM_EOL)), | |
267 | en); | |
0f2d19dd | 268 | } |
cb63cf9e | 269 | port = scm_fdes_to_port (fdes, mode, filename); |
0f2d19dd JB |
270 | return port; |
271 | } | |
272 | ||
e145dd02 | 273 | \f |
cb63cf9e | 274 | /* Building Guile ports from a file descriptor. */ |
e145dd02 | 275 | |
cb63cf9e | 276 | /* Build a Scheme port from an open file descriptor `fdes'. |
a089567e JB |
277 | MODE indicates whether FILE is open for reading or writing; it uses |
278 | the same notation as open-file's second argument. | |
e145dd02 | 279 | Use NAME as the port's filename. */ |
cb63cf9e | 280 | |
a089567e | 281 | SCM |
cb63cf9e | 282 | scm_fdes_to_port (int fdes, char *mode, SCM name) |
a089567e JB |
283 | { |
284 | long mode_bits = scm_mode_bits (mode); | |
285 | SCM port; | |
840ae05d | 286 | scm_port *pt; |
a089567e JB |
287 | |
288 | SCM_NEWCELL (port); | |
289 | SCM_DEFER_INTS; | |
cb63cf9e JB |
290 | pt = scm_add_to_port_table (port); |
291 | SCM_SETPTAB_ENTRY (port, pt); | |
292 | SCM_SETCAR (port, (scm_tc16_fport | mode_bits)); | |
293 | ||
a089567e | 294 | { |
cb63cf9e | 295 | struct scm_fport *fp |
840ae05d JB |
296 | = (struct scm_fport *) malloc (sizeof (struct scm_fport)); |
297 | if (fp == NULL) | |
298 | scm_memory_error ("scm_fdes_to_port"); | |
cb63cf9e | 299 | fp->fdes = fdes; |
840ae05d JB |
300 | pt->rw_random = (mode_bits & SCM_RDNG) && (mode_bits & SCM_WRTNG) |
301 | && SCM_FDES_RANDOM_P (fdes); | |
cb63cf9e JB |
302 | SCM_SETSTREAM (port, fp); |
303 | if (mode_bits & SCM_BUF0) | |
304 | scm_fport_buffer_add (port, 0, 0); | |
305 | else | |
306 | scm_fport_buffer_add (port, -1, -1); | |
a089567e | 307 | } |
cb63cf9e | 308 | SCM_PTAB_ENTRY (port)->file_name = name; |
a089567e | 309 | SCM_ALLOW_INTS; |
e145dd02 JB |
310 | return port; |
311 | } | |
312 | ||
313 | ||
affc96b5 | 314 | /* Return a lower bound on the number of bytes available for input. */ |
cb63cf9e | 315 | static int |
affc96b5 | 316 | fport_input_waiting (SCM port) |
e145dd02 | 317 | { |
cb63cf9e JB |
318 | int fdes = SCM_FSTREAM (port)->fdes; |
319 | ||
320 | #ifdef HAVE_SELECT | |
321 | struct timeval timeout; | |
322 | SELECT_TYPE read_set; | |
323 | SELECT_TYPE write_set; | |
324 | SELECT_TYPE except_set; | |
325 | ||
326 | FD_ZERO (&read_set); | |
327 | FD_ZERO (&write_set); | |
328 | FD_ZERO (&except_set); | |
329 | ||
330 | FD_SET (fdes, &read_set); | |
331 | ||
332 | timeout.tv_sec = 0; | |
333 | timeout.tv_usec = 0; | |
334 | ||
335 | if (select (SELECT_SET_SIZE, | |
336 | &read_set, &write_set, &except_set, &timeout) | |
337 | < 0) | |
affc96b5 GH |
338 | scm_syserror ("fport_input_waiting"); |
339 | return FD_ISSET (fdes, &read_set) ? 1 : 0; | |
cb63cf9e JB |
340 | #elif defined (FIONREAD) |
341 | int remir; | |
342 | ioctl(fdes, FIONREAD, &remir); | |
343 | return remir; | |
344 | #else | |
affc96b5 | 345 | scm_misc_error ("fport_input_waiting", |
cb63cf9e JB |
346 | "Not fully implemented on this platform", |
347 | SCM_EOL); | |
348 | #endif | |
a089567e JB |
349 | } |
350 | ||
cb63cf9e | 351 | \f |
1717856b JB |
352 | static int prinfport SCM_P ((SCM exp, SCM port, scm_print_state *pstate)); |
353 | ||
0f2d19dd | 354 | static int |
1717856b | 355 | prinfport (exp, port, pstate) |
0f2d19dd JB |
356 | SCM exp; |
357 | SCM port; | |
1717856b | 358 | scm_print_state *pstate; |
0f2d19dd | 359 | { |
b3ec3c64 MD |
360 | scm_puts ("#<", port); |
361 | scm_print_port_mode (exp, port); | |
362 | if (SCM_OPFPORTP (exp)) | |
0f2d19dd | 363 | { |
b3ec3c64 MD |
364 | int fdes; |
365 | SCM name = SCM_PTAB_ENTRY (exp)->file_name; | |
366 | scm_puts (SCM_NIMP (name) && SCM_ROSTRINGP (name) | |
367 | ? SCM_ROCHARS (name) | |
368 | : SCM_PTOBNAME (SCM_PTOBNUM (exp)), | |
369 | port); | |
370 | scm_putc (' ', port); | |
371 | fdes = (SCM_FSTREAM (exp))->fdes; | |
372 | ||
373 | if (isatty (fdes)) | |
374 | scm_puts (ttyname (fdes), port); | |
375 | else | |
376 | scm_intprint (fdes, 10, port); | |
0f2d19dd JB |
377 | } |
378 | else | |
379 | { | |
b3ec3c64 MD |
380 | scm_puts (SCM_PTOBNAME (SCM_PTOBNUM (exp)), port); |
381 | scm_putc (' ', port); | |
382 | scm_intprint (SCM_CDR (exp), 16, port); | |
0f2d19dd | 383 | } |
b3ec3c64 MD |
384 | scm_putc ('>', port); |
385 | return 1; | |
0f2d19dd JB |
386 | } |
387 | ||
cb63cf9e JB |
388 | #ifdef GUILE_ISELECT |
389 | /* thread-local block for input on fport's fdes. */ | |
390 | static void | |
391 | fport_wait_for_input (SCM port) | |
3cb988bd | 392 | { |
cb63cf9e | 393 | int fdes = SCM_FSTREAM (port)->fdes; |
3cb988bd | 394 | |
affc96b5 | 395 | if (!fport_input_waiting (port)) |
8122b543 | 396 | { |
cb63cf9e JB |
397 | int n; |
398 | SELECT_TYPE readfds; | |
399 | int flags = fcntl (fdes, F_GETFL); | |
400 | ||
401 | if (flags == -1) | |
402 | scm_syserror ("scm_fdes_wait_for_input"); | |
403 | if (!(flags & O_NONBLOCK)) | |
404 | do | |
405 | { | |
406 | FD_ZERO (&readfds); | |
407 | FD_SET (fdes, &readfds); | |
408 | n = scm_internal_select (fdes + 1, &readfds, NULL, NULL, NULL); | |
409 | } | |
410 | while (n == -1 && errno == EINTR); | |
8122b543 | 411 | } |
3cb988bd | 412 | } |
0f2d19dd JB |
413 | #endif |
414 | ||
affc96b5 | 415 | static void fport_flush (SCM port); |
0f2d19dd | 416 | |
cb63cf9e JB |
417 | /* fill a port's read-buffer with a single read. |
418 | returns the first char and moves the read_pos pointer past it. | |
419 | or returns EOF if end of file. */ | |
0f2d19dd | 420 | static int |
affc96b5 | 421 | fport_fill_input (SCM port) |
0f2d19dd | 422 | { |
cb63cf9e | 423 | int count; |
840ae05d | 424 | scm_port *pt = SCM_PTAB_ENTRY (port); |
cb63cf9e JB |
425 | struct scm_fport *fp = SCM_FSTREAM (port); |
426 | ||
cb63cf9e JB |
427 | #ifdef GUILE_ISELECT |
428 | fport_wait_for_input (port); | |
429 | #endif | |
430 | SCM_SYSCALL (count = read (fp->fdes, pt->read_buf, pt->read_buf_size)); | |
431 | if (count == -1) | |
affc96b5 | 432 | scm_syserror ("fport_fill_input"); |
cb63cf9e JB |
433 | if (count == 0) |
434 | return EOF; | |
435 | else | |
436 | { | |
5c070ca7 | 437 | pt->read_pos = pt->read_buf; |
cb63cf9e | 438 | pt->read_end = pt->read_buf + count; |
5c070ca7 | 439 | return *pt->read_buf; |
cb63cf9e | 440 | } |
0f2d19dd JB |
441 | } |
442 | ||
cb63cf9e | 443 | static off_t |
affc96b5 | 444 | fport_seek (SCM port, off_t offset, int whence) |
0f2d19dd | 445 | { |
cb63cf9e | 446 | struct scm_fport *fp = SCM_FSTREAM (port); |
cb8dfa3f JB |
447 | off_t result = lseek (fp->fdes, offset, whence); |
448 | ||
449 | if (result == -1) | |
affc96b5 | 450 | scm_syserror ("fport_seek"); |
cb8dfa3f | 451 | return result; |
0f2d19dd JB |
452 | } |
453 | ||
840ae05d | 454 | static void |
affc96b5 | 455 | fport_truncate (SCM port, off_t length) |
840ae05d JB |
456 | { |
457 | struct scm_fport *fp = SCM_FSTREAM (port); | |
458 | ||
459 | if (ftruncate (fp->fdes, length) == -1) | |
460 | scm_syserror ("ftruncate"); | |
461 | } | |
462 | ||
31703ab8 GH |
463 | static void |
464 | fport_write (SCM port, void *data, size_t size) | |
465 | { | |
466 | scm_port *pt = SCM_PTAB_ENTRY (port); | |
467 | ||
468 | if (pt->write_buf == &pt->shortbuf) | |
469 | { | |
470 | /* "unbuffered" port. */ | |
471 | int fdes = SCM_FSTREAM (port)->fdes; | |
472 | ||
473 | if (write (fdes, data, size) == -1) | |
474 | scm_syserror ("fport_write"); | |
475 | } | |
476 | else | |
477 | { | |
478 | const char *input = (char *) data; | |
479 | while (size > 0) | |
480 | { | |
481 | int space = pt->write_end - pt->write_pos; | |
482 | int write_len = (size > space) ? space : size; | |
483 | ||
162d88ca | 484 | memcpy (pt->write_pos, input, write_len); |
31703ab8 GH |
485 | pt->write_pos += write_len; |
486 | size -= write_len; | |
487 | input += write_len; | |
488 | if (write_len == space) | |
affc96b5 | 489 | fport_flush (port); |
31703ab8 GH |
490 | } |
491 | ||
492 | /* handle line buffering. */ | |
493 | if ((SCM_CAR (port) & SCM_BUFLINE) && memchr (data, '\n', size)) | |
affc96b5 | 494 | fport_flush (port); |
31703ab8 GH |
495 | } |
496 | } | |
497 | ||
498 | /* becomes 1 when process is exiting: normal exception handling won't | |
499 | work by this time. */ | |
cb63cf9e | 500 | extern int terminating; |
0f2d19dd | 501 | |
cb63cf9e | 502 | static void |
affc96b5 | 503 | fport_flush (SCM port) |
0f2d19dd | 504 | { |
840ae05d | 505 | scm_port *pt = SCM_PTAB_ENTRY (port); |
cb63cf9e JB |
506 | struct scm_fport *fp = SCM_FSTREAM (port); |
507 | char *ptr = pt->write_buf; | |
508 | int init_size = pt->write_pos - pt->write_buf; | |
509 | int remaining = init_size; | |
0f2d19dd | 510 | |
cb63cf9e JB |
511 | while (remaining > 0) |
512 | { | |
513 | int count; | |
514 | ||
515 | SCM_SYSCALL (count = write (fp->fdes, ptr, remaining)); | |
516 | if (count < 0) | |
517 | { | |
518 | /* error. assume nothing was written this call, but | |
519 | fix up the buffer for any previous successful writes. */ | |
520 | int done = init_size - remaining; | |
521 | ||
522 | if (done > 0) | |
523 | { | |
524 | int i; | |
525 | ||
526 | for (i = 0; i < remaining; i++) | |
527 | { | |
528 | *(pt->write_buf + i) = *(pt->write_buf + done + i); | |
529 | } | |
530 | pt->write_pos = pt->write_buf + remaining; | |
531 | } | |
532 | if (!terminating) | |
affc96b5 | 533 | scm_syserror ("fport_flush"); |
cb63cf9e JB |
534 | else |
535 | { | |
536 | const char *msg = "Error: could not flush file-descriptor "; | |
537 | char buf[11]; | |
538 | ||
539 | write (2, msg, strlen (msg)); | |
540 | sprintf (buf, "%d\n", fp->fdes); | |
541 | write (2, buf, strlen (buf)); | |
542 | ||
543 | count = remaining; | |
544 | } | |
545 | } | |
546 | ptr += count; | |
547 | remaining -= count; | |
548 | } | |
549 | pt->write_pos = pt->write_buf; | |
840ae05d JB |
550 | pt->rw_active = 0; |
551 | } | |
552 | ||
283a1a0e | 553 | /* clear the read buffer and adjust the file position for unread bytes. */ |
840ae05d | 554 | static void |
affc96b5 | 555 | fport_end_input (SCM port, int offset) |
840ae05d JB |
556 | { |
557 | struct scm_fport *fp = SCM_FSTREAM (port); | |
558 | scm_port *pt = SCM_PTAB_ENTRY (port); | |
283a1a0e GH |
559 | |
560 | offset += pt->read_end - pt->read_pos; | |
840ae05d | 561 | |
840ae05d JB |
562 | if (offset > 0) |
563 | { | |
564 | pt->read_pos = pt->read_end; | |
565 | /* will throw error if unread-char used at beginning of file | |
566 | then attempting to write. seems correct. */ | |
567 | if (lseek (fp->fdes, -offset, SEEK_CUR) == -1) | |
affc96b5 | 568 | scm_syserror ("fport_end_input"); |
840ae05d JB |
569 | } |
570 | pt->rw_active = 0; | |
8f29fbd0 JB |
571 | } |
572 | ||
6a2c4c81 | 573 | static int |
affc96b5 | 574 | fport_close (SCM port) |
6a2c4c81 | 575 | { |
cb63cf9e | 576 | struct scm_fport *fp = SCM_FSTREAM (port); |
840ae05d | 577 | scm_port *pt = SCM_PTAB_ENTRY (port); |
cb63cf9e | 578 | int rv; |
840ae05d | 579 | |
affc96b5 | 580 | fport_flush (port); |
cb63cf9e JB |
581 | SCM_SYSCALL (rv = close (fp->fdes)); |
582 | if (rv == -1 && errno != EBADF) | |
affc96b5 | 583 | scm_syserror ("fport_close"); |
6c951427 GH |
584 | if (pt->read_buf == pt->putback_buf) |
585 | pt->read_buf = pt->saved_read_buf; | |
cb63cf9e | 586 | if (pt->read_buf != &pt->shortbuf) |
840ae05d | 587 | free (pt->read_buf); |
cb63cf9e | 588 | if (pt->write_buf != &pt->shortbuf) |
840ae05d JB |
589 | free (pt->write_buf); |
590 | free ((char *) fp); | |
cb63cf9e | 591 | return rv; |
6a2c4c81 JB |
592 | } |
593 | ||
b3ec3c64 | 594 | static scm_sizet |
affc96b5 | 595 | fport_free (SCM port) |
b3ec3c64 | 596 | { |
affc96b5 | 597 | fport_close (port); |
b3ec3c64 MD |
598 | return 0; |
599 | } | |
600 | ||
601 | void scm_make_fptob (void); /* Called from ports.c */ | |
602 | ||
603 | void | |
604 | scm_make_fptob () | |
605 | { | |
affc96b5 GH |
606 | long tc = scm_make_port_type ("file", fport_fill_input, fport_write); |
607 | scm_set_port_free (tc, fport_free); | |
6c747373 | 608 | scm_set_port_print (tc, prinfport); |
affc96b5 GH |
609 | scm_set_port_flush (tc, fport_flush); |
610 | scm_set_port_end_input (tc, fport_end_input); | |
611 | scm_set_port_close (tc, fport_close); | |
612 | scm_set_port_seek (tc, fport_seek); | |
613 | scm_set_port_truncate (tc, fport_truncate); | |
614 | scm_set_port_input_waiting (tc, fport_input_waiting); | |
b3ec3c64 | 615 | } |
0f2d19dd | 616 | |
0f2d19dd JB |
617 | void |
618 | scm_init_fports () | |
0f2d19dd JB |
619 | { |
620 | #include "fports.x" | |
7a6f1ffa GH |
621 | scm_sysintern ("_IOFBF", SCM_MAKINUM (_IOFBF)); |
622 | scm_sysintern ("_IOLBF", SCM_MAKINUM (_IOLBF)); | |
623 | scm_sysintern ("_IONBF", SCM_MAKINUM (_IONBF)); | |
0f2d19dd | 624 | } |