New I/O specifiers CARRIAGECONTROL, READONLY, SHARE with -fdec.
[gcc.git] / libgfortran / io / transfer.c
1 /* Copyright (C) 2002-2016 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist transfer functions contributed by Paul Thomas
4 F2003 I/O support contributed by Jerry DeLisle
5
6 This file is part of the GNU Fortran runtime library (libgfortran).
7
8 Libgfortran is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27
28 /* transfer.c -- Top level handling of data transfer statements. */
29
30 #include "io.h"
31 #include "fbuf.h"
32 #include "format.h"
33 #include "unix.h"
34 #include <string.h>
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <errno.h>
38
39
40 /* Calling conventions: Data transfer statements are unlike other
41 library calls in that they extend over several calls.
42
43 The first call is always a call to st_read() or st_write(). These
44 subroutines return no status unless a namelist read or write is
45 being done, in which case there is the usual status. No further
46 calls are necessary in this case.
47
48 For other sorts of data transfer, there are zero or more data
49 transfer statement that depend on the format of the data transfer
50 statement. For READ (and for backwards compatibily: for WRITE), one has
51
52 transfer_integer
53 transfer_logical
54 transfer_character
55 transfer_character_wide
56 transfer_real
57 transfer_complex
58 transfer_real128
59 transfer_complex128
60
61 and for WRITE
62
63 transfer_integer_write
64 transfer_logical_write
65 transfer_character_write
66 transfer_character_wide_write
67 transfer_real_write
68 transfer_complex_write
69 transfer_real128_write
70 transfer_complex128_write
71
72 These subroutines do not return status. The *128 functions
73 are in the file transfer128.c.
74
75 The last call is a call to st_[read|write]_done(). While
76 something can easily go wrong with the initial st_read() or
77 st_write(), an error inhibits any data from actually being
78 transferred. */
79
80 extern void transfer_integer (st_parameter_dt *, void *, int);
81 export_proto(transfer_integer);
82
83 extern void transfer_integer_write (st_parameter_dt *, void *, int);
84 export_proto(transfer_integer_write);
85
86 extern void transfer_real (st_parameter_dt *, void *, int);
87 export_proto(transfer_real);
88
89 extern void transfer_real_write (st_parameter_dt *, void *, int);
90 export_proto(transfer_real_write);
91
92 extern void transfer_logical (st_parameter_dt *, void *, int);
93 export_proto(transfer_logical);
94
95 extern void transfer_logical_write (st_parameter_dt *, void *, int);
96 export_proto(transfer_logical_write);
97
98 extern void transfer_character (st_parameter_dt *, void *, int);
99 export_proto(transfer_character);
100
101 extern void transfer_character_write (st_parameter_dt *, void *, int);
102 export_proto(transfer_character_write);
103
104 extern void transfer_character_wide (st_parameter_dt *, void *, int, int);
105 export_proto(transfer_character_wide);
106
107 extern void transfer_character_wide_write (st_parameter_dt *,
108 void *, int, int);
109 export_proto(transfer_character_wide_write);
110
111 extern void transfer_complex (st_parameter_dt *, void *, int);
112 export_proto(transfer_complex);
113
114 extern void transfer_complex_write (st_parameter_dt *, void *, int);
115 export_proto(transfer_complex_write);
116
117 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
118 gfc_charlen_type);
119 export_proto(transfer_array);
120
121 extern void transfer_array_write (st_parameter_dt *, gfc_array_char *, int,
122 gfc_charlen_type);
123 export_proto(transfer_array_write);
124
125 /* User defined derived type input/output. */
126 extern void
127 transfer_derived (st_parameter_dt *dtp, void *dtio_source, void *dtio_proc);
128 export_proto(transfer_derived);
129
130 extern void
131 transfer_derived_write (st_parameter_dt *dtp, void *dtio_source, void *dtio_proc);
132 export_proto(transfer_derived_write);
133
134 static void us_read (st_parameter_dt *, int);
135 static void us_write (st_parameter_dt *, int);
136 static void next_record_r_unf (st_parameter_dt *, int);
137 static void next_record_w_unf (st_parameter_dt *, int);
138
139 static const st_option advance_opt[] = {
140 {"yes", ADVANCE_YES},
141 {"no", ADVANCE_NO},
142 {NULL, 0}
143 };
144
145
146 static const st_option decimal_opt[] = {
147 {"point", DECIMAL_POINT},
148 {"comma", DECIMAL_COMMA},
149 {NULL, 0}
150 };
151
152 static const st_option round_opt[] = {
153 {"up", ROUND_UP},
154 {"down", ROUND_DOWN},
155 {"zero", ROUND_ZERO},
156 {"nearest", ROUND_NEAREST},
157 {"compatible", ROUND_COMPATIBLE},
158 {"processor_defined", ROUND_PROCDEFINED},
159 {NULL, 0}
160 };
161
162
163 static const st_option sign_opt[] = {
164 {"plus", SIGN_SP},
165 {"suppress", SIGN_SS},
166 {"processor_defined", SIGN_S},
167 {NULL, 0}
168 };
169
170 static const st_option blank_opt[] = {
171 {"null", BLANK_NULL},
172 {"zero", BLANK_ZERO},
173 {NULL, 0}
174 };
175
176 static const st_option delim_opt[] = {
177 {"apostrophe", DELIM_APOSTROPHE},
178 {"quote", DELIM_QUOTE},
179 {"none", DELIM_NONE},
180 {NULL, 0}
181 };
182
183 static const st_option pad_opt[] = {
184 {"yes", PAD_YES},
185 {"no", PAD_NO},
186 {NULL, 0}
187 };
188
189 typedef enum
190 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
191 FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
192 }
193 file_mode;
194
195
196 static file_mode
197 current_mode (st_parameter_dt *dtp)
198 {
199 file_mode m;
200
201 m = FORM_UNSPECIFIED;
202
203 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
204 {
205 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
206 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
207 }
208 else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
209 {
210 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
211 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
212 }
213 else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
214 {
215 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
216 FORMATTED_STREAM : UNFORMATTED_STREAM;
217 }
218
219 return m;
220 }
221
222
223 /* Mid level data transfer statements. */
224
225 /* Read sequential file - internal unit */
226
227 static char *
228 read_sf_internal (st_parameter_dt *dtp, int * length)
229 {
230 static char *empty_string[0];
231 char *base;
232 int lorig;
233
234 /* Zero size array gives internal unit len of 0. Nothing to read. */
235 if (dtp->internal_unit_len == 0
236 && dtp->u.p.current_unit->pad_status == PAD_NO)
237 hit_eof (dtp);
238
239 /* If we have seen an eor previously, return a length of 0. The
240 caller is responsible for correctly padding the input field. */
241 if (dtp->u.p.sf_seen_eor)
242 {
243 *length = 0;
244 /* Just return something that isn't a NULL pointer, otherwise the
245 caller thinks an error occurred. */
246 return (char*) empty_string;
247 }
248
249 lorig = *length;
250 if (is_char4_unit(dtp))
251 {
252 int i;
253 gfc_char4_t *p = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s,
254 length);
255 base = fbuf_alloc (dtp->u.p.current_unit, lorig);
256 for (i = 0; i < *length; i++, p++)
257 base[i] = *p > 255 ? '?' : (unsigned char) *p;
258 }
259 else
260 base = mem_alloc_r (dtp->u.p.current_unit->s, length);
261
262 if (unlikely (lorig > *length))
263 {
264 hit_eof (dtp);
265 return NULL;
266 }
267
268 dtp->u.p.current_unit->bytes_left -= *length;
269
270 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
271 dtp->u.p.current_unit->has_size)
272 dtp->u.p.current_unit->size_used += (GFC_IO_INT) *length;
273
274 return base;
275
276 }
277
278 /* When reading sequential formatted records we have a problem. We
279 don't know how long the line is until we read the trailing newline,
280 and we don't want to read too much. If we read too much, we might
281 have to do a physical seek backwards depending on how much data is
282 present, and devices like terminals aren't seekable and would cause
283 an I/O error.
284
285 Given this, the solution is to read a byte at a time, stopping if
286 we hit the newline. For small allocations, we use a static buffer.
287 For larger allocations, we are forced to allocate memory on the
288 heap. Hopefully this won't happen very often. */
289
290 /* Read sequential file - external unit */
291
292 static char *
293 read_sf (st_parameter_dt *dtp, int * length)
294 {
295 static char *empty_string[0];
296 int q, q2;
297 int n, lorig, seen_comma;
298
299 /* If we have seen an eor previously, return a length of 0. The
300 caller is responsible for correctly padding the input field. */
301 if (dtp->u.p.sf_seen_eor)
302 {
303 *length = 0;
304 /* Just return something that isn't a NULL pointer, otherwise the
305 caller thinks an error occurred. */
306 return (char*) empty_string;
307 }
308
309 n = seen_comma = 0;
310
311 /* Read data into format buffer and scan through it. */
312 lorig = *length;
313
314 while (n < *length)
315 {
316 q = fbuf_getc (dtp->u.p.current_unit);
317 if (q == EOF)
318 break;
319 else if (dtp->u.p.current_unit->flags.cc != CC_NONE
320 && (q == '\n' || q == '\r'))
321 {
322 /* Unexpected end of line. Set the position. */
323 dtp->u.p.sf_seen_eor = 1;
324
325 /* If we see an EOR during non-advancing I/O, we need to skip
326 the rest of the I/O statement. Set the corresponding flag. */
327 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
328 dtp->u.p.eor_condition = 1;
329
330 /* If we encounter a CR, it might be a CRLF. */
331 if (q == '\r') /* Probably a CRLF */
332 {
333 /* See if there is an LF. */
334 q2 = fbuf_getc (dtp->u.p.current_unit);
335 if (q2 == '\n')
336 dtp->u.p.sf_seen_eor = 2;
337 else if (q2 != EOF) /* Oops, seek back. */
338 fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR);
339 }
340
341 /* Without padding, terminate the I/O statement without assigning
342 the value. With padding, the value still needs to be assigned,
343 so we can just continue with a short read. */
344 if (dtp->u.p.current_unit->pad_status == PAD_NO)
345 {
346 generate_error (&dtp->common, LIBERROR_EOR, NULL);
347 return NULL;
348 }
349
350 *length = n;
351 goto done;
352 }
353 /* Short circuit the read if a comma is found during numeric input.
354 The flag is set to zero during character reads so that commas in
355 strings are not ignored */
356 else if (q == ',')
357 if (dtp->u.p.sf_read_comma == 1)
358 {
359 seen_comma = 1;
360 notify_std (&dtp->common, GFC_STD_GNU,
361 "Comma in formatted numeric read.");
362 break;
363 }
364 n++;
365 }
366
367 *length = n;
368
369 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
370 some other stuff. Set the relevant flags. */
371 if (lorig > *length && !dtp->u.p.sf_seen_eor && !seen_comma)
372 {
373 if (n > 0)
374 {
375 if (dtp->u.p.advance_status == ADVANCE_NO)
376 {
377 if (dtp->u.p.current_unit->pad_status == PAD_NO)
378 {
379 hit_eof (dtp);
380 return NULL;
381 }
382 else
383 dtp->u.p.eor_condition = 1;
384 }
385 else
386 dtp->u.p.at_eof = 1;
387 }
388 else if (dtp->u.p.advance_status == ADVANCE_NO
389 || dtp->u.p.current_unit->pad_status == PAD_NO
390 || dtp->u.p.current_unit->bytes_left
391 == dtp->u.p.current_unit->recl)
392 {
393 hit_eof (dtp);
394 return NULL;
395 }
396 }
397
398 done:
399
400 dtp->u.p.current_unit->bytes_left -= n;
401
402 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
403 dtp->u.p.current_unit->has_size)
404 dtp->u.p.current_unit->size_used += (GFC_IO_INT) n;
405
406 /* We can't call fbuf_getptr before the loop doing fbuf_getc, because
407 fbuf_getc might reallocate the buffer. So return current pointer
408 minus all the advances, which is n plus up to two characters
409 of newline or comma. */
410 return fbuf_getptr (dtp->u.p.current_unit)
411 - n - dtp->u.p.sf_seen_eor - seen_comma;
412 }
413
414
415 /* Function for reading the next couple of bytes from the current
416 file, advancing the current position. We return NULL on end of record or
417 end of file. This function is only for formatted I/O, unformatted uses
418 read_block_direct.
419
420 If the read is short, then it is because the current record does not
421 have enough data to satisfy the read request and the file was
422 opened with PAD=YES. The caller must assume tailing spaces for
423 short reads. */
424
425 void *
426 read_block_form (st_parameter_dt *dtp, int * nbytes)
427 {
428 char *source;
429 int norig;
430
431 if (!is_stream_io (dtp))
432 {
433 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
434 {
435 /* For preconnected units with default record length, set bytes left
436 to unit record length and proceed, otherwise error. */
437 if (dtp->u.p.current_unit->unit_number == options.stdin_unit
438 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
439 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
440 else
441 {
442 if (unlikely (dtp->u.p.current_unit->pad_status == PAD_NO)
443 && !is_internal_unit (dtp))
444 {
445 /* Not enough data left. */
446 generate_error (&dtp->common, LIBERROR_EOR, NULL);
447 return NULL;
448 }
449 }
450
451 if (unlikely (dtp->u.p.current_unit->bytes_left == 0
452 && !is_internal_unit(dtp)))
453 {
454 hit_eof (dtp);
455 return NULL;
456 }
457
458 *nbytes = dtp->u.p.current_unit->bytes_left;
459 }
460 }
461
462 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
463 (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL ||
464 dtp->u.p.current_unit->flags.access == ACCESS_STREAM))
465 {
466 if (is_internal_unit (dtp))
467 source = read_sf_internal (dtp, nbytes);
468 else
469 source = read_sf (dtp, nbytes);
470
471 dtp->u.p.current_unit->strm_pos +=
472 (gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor);
473 return source;
474 }
475
476 /* If we reach here, we can assume it's direct access. */
477
478 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
479
480 norig = *nbytes;
481 source = fbuf_read (dtp->u.p.current_unit, nbytes);
482 fbuf_seek (dtp->u.p.current_unit, *nbytes, SEEK_CUR);
483
484 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
485 dtp->u.p.current_unit->has_size)
486 dtp->u.p.current_unit->size_used += (GFC_IO_INT) *nbytes;
487
488 if (norig != *nbytes)
489 {
490 /* Short read, this shouldn't happen. */
491 if (dtp->u.p.current_unit->pad_status == PAD_NO)
492 {
493 generate_error (&dtp->common, LIBERROR_EOR, NULL);
494 source = NULL;
495 }
496 }
497
498 dtp->u.p.current_unit->strm_pos += (gfc_offset) *nbytes;
499
500 return source;
501 }
502
503
504 /* Read a block from a character(kind=4) internal unit, to be transferred into
505 a character(kind=4) variable. Note: Portions of this code borrowed from
506 read_sf_internal. */
507 void *
508 read_block_form4 (st_parameter_dt *dtp, int * nbytes)
509 {
510 static gfc_char4_t *empty_string[0];
511 gfc_char4_t *source;
512 int lorig;
513
514 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
515 *nbytes = dtp->u.p.current_unit->bytes_left;
516
517 /* Zero size array gives internal unit len of 0. Nothing to read. */
518 if (dtp->internal_unit_len == 0
519 && dtp->u.p.current_unit->pad_status == PAD_NO)
520 hit_eof (dtp);
521
522 /* If we have seen an eor previously, return a length of 0. The
523 caller is responsible for correctly padding the input field. */
524 if (dtp->u.p.sf_seen_eor)
525 {
526 *nbytes = 0;
527 /* Just return something that isn't a NULL pointer, otherwise the
528 caller thinks an error occurred. */
529 return empty_string;
530 }
531
532 lorig = *nbytes;
533 source = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s, nbytes);
534
535 if (unlikely (lorig > *nbytes))
536 {
537 hit_eof (dtp);
538 return NULL;
539 }
540
541 dtp->u.p.current_unit->bytes_left -= *nbytes;
542
543 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
544 dtp->u.p.current_unit->has_size)
545 dtp->u.p.current_unit->size_used += (GFC_IO_INT) *nbytes;
546
547 return source;
548 }
549
550
551 /* Reads a block directly into application data space. This is for
552 unformatted files. */
553
554 static void
555 read_block_direct (st_parameter_dt *dtp, void *buf, size_t nbytes)
556 {
557 ssize_t to_read_record;
558 ssize_t have_read_record;
559 ssize_t to_read_subrecord;
560 ssize_t have_read_subrecord;
561 int short_record;
562
563 if (is_stream_io (dtp))
564 {
565 have_read_record = sread (dtp->u.p.current_unit->s, buf,
566 nbytes);
567 if (unlikely (have_read_record < 0))
568 {
569 generate_error (&dtp->common, LIBERROR_OS, NULL);
570 return;
571 }
572
573 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_read_record;
574
575 if (unlikely ((ssize_t) nbytes != have_read_record))
576 {
577 /* Short read, e.g. if we hit EOF. For stream files,
578 we have to set the end-of-file condition. */
579 hit_eof (dtp);
580 }
581 return;
582 }
583
584 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
585 {
586 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)
587 {
588 short_record = 1;
589 to_read_record = dtp->u.p.current_unit->bytes_left;
590 nbytes = to_read_record;
591 }
592 else
593 {
594 short_record = 0;
595 to_read_record = nbytes;
596 }
597
598 dtp->u.p.current_unit->bytes_left -= to_read_record;
599
600 to_read_record = sread (dtp->u.p.current_unit->s, buf, to_read_record);
601 if (unlikely (to_read_record < 0))
602 {
603 generate_error (&dtp->common, LIBERROR_OS, NULL);
604 return;
605 }
606
607 if (to_read_record != (ssize_t) nbytes)
608 {
609 /* Short read, e.g. if we hit EOF. Apparently, we read
610 more than was written to the last record. */
611 return;
612 }
613
614 if (unlikely (short_record))
615 {
616 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
617 }
618 return;
619 }
620
621 /* Unformatted sequential. We loop over the subrecords, reading
622 until the request has been fulfilled or the record has run out
623 of continuation subrecords. */
624
625 /* Check whether we exceed the total record length. */
626
627 if (dtp->u.p.current_unit->flags.has_recl
628 && ((gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left))
629 {
630 to_read_record = dtp->u.p.current_unit->bytes_left;
631 short_record = 1;
632 }
633 else
634 {
635 to_read_record = nbytes;
636 short_record = 0;
637 }
638 have_read_record = 0;
639
640 while(1)
641 {
642 if (dtp->u.p.current_unit->bytes_left_subrecord
643 < (gfc_offset) to_read_record)
644 {
645 to_read_subrecord = dtp->u.p.current_unit->bytes_left_subrecord;
646 to_read_record -= to_read_subrecord;
647 }
648 else
649 {
650 to_read_subrecord = to_read_record;
651 to_read_record = 0;
652 }
653
654 dtp->u.p.current_unit->bytes_left_subrecord -= to_read_subrecord;
655
656 have_read_subrecord = sread (dtp->u.p.current_unit->s,
657 buf + have_read_record, to_read_subrecord);
658 if (unlikely (have_read_subrecord < 0))
659 {
660 generate_error (&dtp->common, LIBERROR_OS, NULL);
661 return;
662 }
663
664 have_read_record += have_read_subrecord;
665
666 if (unlikely (to_read_subrecord != have_read_subrecord))
667 {
668 /* Short read, e.g. if we hit EOF. This means the record
669 structure has been corrupted, or the trailing record
670 marker would still be present. */
671
672 generate_error (&dtp->common, LIBERROR_CORRUPT_FILE, NULL);
673 return;
674 }
675
676 if (to_read_record > 0)
677 {
678 if (likely (dtp->u.p.current_unit->continued))
679 {
680 next_record_r_unf (dtp, 0);
681 us_read (dtp, 1);
682 }
683 else
684 {
685 /* Let's make sure the file position is correctly pre-positioned
686 for the next read statement. */
687
688 dtp->u.p.current_unit->current_record = 0;
689 next_record_r_unf (dtp, 0);
690 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
691 return;
692 }
693 }
694 else
695 {
696 /* Normal exit, the read request has been fulfilled. */
697 break;
698 }
699 }
700
701 dtp->u.p.current_unit->bytes_left -= have_read_record;
702 if (unlikely (short_record))
703 {
704 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
705 return;
706 }
707 return;
708 }
709
710
711 /* Function for writing a block of bytes to the current file at the
712 current position, advancing the file pointer. We are given a length
713 and return a pointer to a buffer that the caller must (completely)
714 fill in. Returns NULL on error. */
715
716 void *
717 write_block (st_parameter_dt *dtp, int length)
718 {
719 char *dest;
720
721 if (!is_stream_io (dtp))
722 {
723 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
724 {
725 /* For preconnected units with default record length, set bytes left
726 to unit record length and proceed, otherwise error. */
727 if (likely ((dtp->u.p.current_unit->unit_number
728 == options.stdout_unit
729 || dtp->u.p.current_unit->unit_number
730 == options.stderr_unit)
731 && dtp->u.p.current_unit->recl == DEFAULT_RECL))
732 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
733 else
734 {
735 generate_error (&dtp->common, LIBERROR_EOR, NULL);
736 return NULL;
737 }
738 }
739
740 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
741 }
742
743 if (is_internal_unit (dtp))
744 {
745 if (is_char4_unit(dtp)) /* char4 internel unit. */
746 {
747 gfc_char4_t *dest4;
748 dest4 = mem_alloc_w4 (dtp->u.p.current_unit->s, &length);
749 if (dest4 == NULL)
750 {
751 generate_error (&dtp->common, LIBERROR_END, NULL);
752 return NULL;
753 }
754 return dest4;
755 }
756 else
757 dest = mem_alloc_w (dtp->u.p.current_unit->s, &length);
758
759 if (dest == NULL)
760 {
761 generate_error (&dtp->common, LIBERROR_END, NULL);
762 return NULL;
763 }
764
765 if (unlikely (dtp->u.p.current_unit->endfile == AT_ENDFILE))
766 generate_error (&dtp->common, LIBERROR_END, NULL);
767 }
768 else
769 {
770 dest = fbuf_alloc (dtp->u.p.current_unit, length);
771 if (dest == NULL)
772 {
773 generate_error (&dtp->common, LIBERROR_OS, NULL);
774 return NULL;
775 }
776 }
777
778 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
779 dtp->u.p.current_unit->has_size)
780 dtp->u.p.current_unit->size_used += (GFC_IO_INT) length;
781
782 dtp->u.p.current_unit->strm_pos += (gfc_offset) length;
783
784 return dest;
785 }
786
787
788 /* High level interface to swrite(), taking care of errors. This is only
789 called for unformatted files. There are three cases to consider:
790 Stream I/O, unformatted direct, unformatted sequential. */
791
792 static bool
793 write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
794 {
795
796 ssize_t have_written;
797 ssize_t to_write_subrecord;
798 int short_record;
799
800 /* Stream I/O. */
801
802 if (is_stream_io (dtp))
803 {
804 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
805 if (unlikely (have_written < 0))
806 {
807 generate_error (&dtp->common, LIBERROR_OS, NULL);
808 return false;
809 }
810
811 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
812
813 return true;
814 }
815
816 /* Unformatted direct access. */
817
818 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
819 {
820 if (unlikely (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes))
821 {
822 generate_error (&dtp->common, LIBERROR_DIRECT_EOR, NULL);
823 return false;
824 }
825
826 if (buf == NULL && nbytes == 0)
827 return true;
828
829 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
830 if (unlikely (have_written < 0))
831 {
832 generate_error (&dtp->common, LIBERROR_OS, NULL);
833 return false;
834 }
835
836 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
837 dtp->u.p.current_unit->bytes_left -= (gfc_offset) have_written;
838
839 return true;
840 }
841
842 /* Unformatted sequential. */
843
844 have_written = 0;
845
846 if (dtp->u.p.current_unit->flags.has_recl
847 && (gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left)
848 {
849 nbytes = dtp->u.p.current_unit->bytes_left;
850 short_record = 1;
851 }
852 else
853 {
854 short_record = 0;
855 }
856
857 while (1)
858 {
859
860 to_write_subrecord =
861 (size_t) dtp->u.p.current_unit->bytes_left_subrecord < nbytes ?
862 (size_t) dtp->u.p.current_unit->bytes_left_subrecord : nbytes;
863
864 dtp->u.p.current_unit->bytes_left_subrecord -=
865 (gfc_offset) to_write_subrecord;
866
867 to_write_subrecord = swrite (dtp->u.p.current_unit->s,
868 buf + have_written, to_write_subrecord);
869 if (unlikely (to_write_subrecord < 0))
870 {
871 generate_error (&dtp->common, LIBERROR_OS, NULL);
872 return false;
873 }
874
875 dtp->u.p.current_unit->strm_pos += (gfc_offset) to_write_subrecord;
876 nbytes -= to_write_subrecord;
877 have_written += to_write_subrecord;
878
879 if (nbytes == 0)
880 break;
881
882 next_record_w_unf (dtp, 1);
883 us_write (dtp, 1);
884 }
885 dtp->u.p.current_unit->bytes_left -= have_written;
886 if (unlikely (short_record))
887 {
888 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
889 return false;
890 }
891 return true;
892 }
893
894
895 /* Reverse memcpy - used for byte swapping. */
896
897 static void
898 reverse_memcpy (void *dest, const void *src, size_t n)
899 {
900 char *d, *s;
901 size_t i;
902
903 d = (char *) dest;
904 s = (char *) src + n - 1;
905
906 /* Write with ascending order - this is likely faster
907 on modern architectures because of write combining. */
908 for (i=0; i<n; i++)
909 *(d++) = *(s--);
910 }
911
912
913 /* Utility function for byteswapping an array, using the bswap
914 builtins if possible. dest and src can overlap completely, or then
915 they must point to separate objects; partial overlaps are not
916 allowed. */
917
918 static void
919 bswap_array (void *dest, const void *src, size_t size, size_t nelems)
920 {
921 const char *ps;
922 char *pd;
923
924 switch (size)
925 {
926 case 1:
927 break;
928 case 2:
929 for (size_t i = 0; i < nelems; i++)
930 ((uint16_t*)dest)[i] = __builtin_bswap16 (((uint16_t*)src)[i]);
931 break;
932 case 4:
933 for (size_t i = 0; i < nelems; i++)
934 ((uint32_t*)dest)[i] = __builtin_bswap32 (((uint32_t*)src)[i]);
935 break;
936 case 8:
937 for (size_t i = 0; i < nelems; i++)
938 ((uint64_t*)dest)[i] = __builtin_bswap64 (((uint64_t*)src)[i]);
939 break;
940 case 12:
941 ps = src;
942 pd = dest;
943 for (size_t i = 0; i < nelems; i++)
944 {
945 uint32_t tmp;
946 memcpy (&tmp, ps, 4);
947 *(uint32_t*)pd = __builtin_bswap32 (*(uint32_t*)(ps + 8));
948 *(uint32_t*)(pd + 4) = __builtin_bswap32 (*(uint32_t*)(ps + 4));
949 *(uint32_t*)(pd + 8) = __builtin_bswap32 (tmp);
950 ps += size;
951 pd += size;
952 }
953 break;
954 case 16:
955 ps = src;
956 pd = dest;
957 for (size_t i = 0; i < nelems; i++)
958 {
959 uint64_t tmp;
960 memcpy (&tmp, ps, 8);
961 *(uint64_t*)pd = __builtin_bswap64 (*(uint64_t*)(ps + 8));
962 *(uint64_t*)(pd + 8) = __builtin_bswap64 (tmp);
963 ps += size;
964 pd += size;
965 }
966 break;
967 default:
968 pd = dest;
969 if (dest != src)
970 {
971 ps = src;
972 for (size_t i = 0; i < nelems; i++)
973 {
974 reverse_memcpy (pd, ps, size);
975 ps += size;
976 pd += size;
977 }
978 }
979 else
980 {
981 /* In-place byte swap. */
982 for (size_t i = 0; i < nelems; i++)
983 {
984 char tmp, *low = pd, *high = pd + size - 1;
985 for (size_t j = 0; j < size/2; j++)
986 {
987 tmp = *low;
988 *low = *high;
989 *high = tmp;
990 low++;
991 high--;
992 }
993 pd += size;
994 }
995 }
996 }
997 }
998
999
1000 /* Master function for unformatted reads. */
1001
1002 static void
1003 unformatted_read (st_parameter_dt *dtp, bt type,
1004 void *dest, int kind, size_t size, size_t nelems)
1005 {
1006 if (type == BT_CLASS)
1007 {
1008 int unit = dtp->u.p.current_unit->unit_number;
1009 char tmp_iomsg[IOMSG_LEN] = "";
1010 char *child_iomsg;
1011 gfc_charlen_type child_iomsg_len;
1012 int noiostat;
1013 int *child_iostat = NULL;
1014
1015 /* Set iostat, intent(out). */
1016 noiostat = 0;
1017 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1018 dtp->common.iostat : &noiostat;
1019
1020 /* Set iomsg, intent(inout). */
1021 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1022 {
1023 child_iomsg = dtp->common.iomsg;
1024 child_iomsg_len = dtp->common.iomsg_len;
1025 }
1026 else
1027 {
1028 child_iomsg = tmp_iomsg;
1029 child_iomsg_len = IOMSG_LEN;
1030 }
1031
1032 /* Call the user defined unformatted READ procedure. */
1033 dtp->u.p.current_unit->child_dtio++;
1034 dtp->u.p.ufdtio_ptr (dest, &unit, child_iostat, child_iomsg,
1035 child_iomsg_len);
1036 dtp->u.p.current_unit->child_dtio--;
1037 return;
1038 }
1039
1040 if (type == BT_CHARACTER)
1041 size *= GFC_SIZE_OF_CHAR_KIND(kind);
1042 read_block_direct (dtp, dest, size * nelems);
1043
1044 if (unlikely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_SWAP)
1045 && kind != 1)
1046 {
1047 /* Handle wide chracters. */
1048 if (type == BT_CHARACTER)
1049 {
1050 nelems *= size;
1051 size = kind;
1052 }
1053
1054 /* Break up complex into its constituent reals. */
1055 else if (type == BT_COMPLEX)
1056 {
1057 nelems *= 2;
1058 size /= 2;
1059 }
1060 bswap_array (dest, dest, size, nelems);
1061 }
1062 }
1063
1064
1065 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
1066 bytes on 64 bit machines. The unused bytes are not initialized and never
1067 used, which can show an error with memory checking analyzers like
1068 valgrind. We us BT_CLASS to denote a User Defined I/O call. */
1069
1070 static void
1071 unformatted_write (st_parameter_dt *dtp, bt type,
1072 void *source, int kind, size_t size, size_t nelems)
1073 {
1074 if (type == BT_CLASS)
1075 {
1076 int unit = dtp->u.p.current_unit->unit_number;
1077 char tmp_iomsg[IOMSG_LEN] = "";
1078 char *child_iomsg;
1079 gfc_charlen_type child_iomsg_len;
1080 int noiostat;
1081 int *child_iostat = NULL;
1082
1083 /* Set iostat, intent(out). */
1084 noiostat = 0;
1085 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1086 dtp->common.iostat : &noiostat;
1087
1088 /* Set iomsg, intent(inout). */
1089 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1090 {
1091 child_iomsg = dtp->common.iomsg;
1092 child_iomsg_len = dtp->common.iomsg_len;
1093 }
1094 else
1095 {
1096 child_iomsg = tmp_iomsg;
1097 child_iomsg_len = IOMSG_LEN;
1098 }
1099
1100 /* Call the user defined unformatted WRITE procedure. */
1101 dtp->u.p.current_unit->child_dtio++;
1102 dtp->u.p.ufdtio_ptr (source, &unit, child_iostat, child_iomsg,
1103 child_iomsg_len);
1104 dtp->u.p.current_unit->child_dtio--;
1105 return;
1106 }
1107
1108 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
1109 || kind == 1)
1110 {
1111 size_t stride = type == BT_CHARACTER ?
1112 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1113
1114 write_buf (dtp, source, stride * nelems);
1115 }
1116 else
1117 {
1118 #define BSWAP_BUFSZ 512
1119 char buffer[BSWAP_BUFSZ];
1120 char *p;
1121 size_t nrem;
1122
1123 p = source;
1124
1125 /* Handle wide chracters. */
1126 if (type == BT_CHARACTER && kind != 1)
1127 {
1128 nelems *= size;
1129 size = kind;
1130 }
1131
1132 /* Break up complex into its constituent reals. */
1133 if (type == BT_COMPLEX)
1134 {
1135 nelems *= 2;
1136 size /= 2;
1137 }
1138
1139 /* By now, all complex variables have been split into their
1140 constituent reals. */
1141
1142 nrem = nelems;
1143 do
1144 {
1145 size_t nc;
1146 if (size * nrem > BSWAP_BUFSZ)
1147 nc = BSWAP_BUFSZ / size;
1148 else
1149 nc = nrem;
1150
1151 bswap_array (buffer, p, size, nc);
1152 write_buf (dtp, buffer, size * nc);
1153 p += size * nc;
1154 nrem -= nc;
1155 }
1156 while (nrem > 0);
1157 }
1158 }
1159
1160
1161 /* Return a pointer to the name of a type. */
1162
1163 const char *
1164 type_name (bt type)
1165 {
1166 const char *p;
1167
1168 switch (type)
1169 {
1170 case BT_INTEGER:
1171 p = "INTEGER";
1172 break;
1173 case BT_LOGICAL:
1174 p = "LOGICAL";
1175 break;
1176 case BT_CHARACTER:
1177 p = "CHARACTER";
1178 break;
1179 case BT_REAL:
1180 p = "REAL";
1181 break;
1182 case BT_COMPLEX:
1183 p = "COMPLEX";
1184 break;
1185 case BT_CLASS:
1186 p = "CLASS or DERIVED";
1187 break;
1188 default:
1189 internal_error (NULL, "type_name(): Bad type");
1190 }
1191
1192 return p;
1193 }
1194
1195
1196 /* Write a constant string to the output.
1197 This is complicated because the string can have doubled delimiters
1198 in it. The length in the format node is the true length. */
1199
1200 static void
1201 write_constant_string (st_parameter_dt *dtp, const fnode *f)
1202 {
1203 char c, delimiter, *p, *q;
1204 int length;
1205
1206 length = f->u.string.length;
1207 if (length == 0)
1208 return;
1209
1210 p = write_block (dtp, length);
1211 if (p == NULL)
1212 return;
1213
1214 q = f->u.string.p;
1215 delimiter = q[-1];
1216
1217 for (; length > 0; length--)
1218 {
1219 c = *p++ = *q++;
1220 if (c == delimiter && c != 'H' && c != 'h')
1221 q++; /* Skip the doubled delimiter. */
1222 }
1223 }
1224
1225
1226 /* Given actual and expected types in a formatted data transfer, make
1227 sure they agree. If not, an error message is generated. Returns
1228 nonzero if something went wrong. */
1229
1230 static int
1231 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
1232 {
1233 #define BUFLEN 100
1234 char buffer[BUFLEN];
1235
1236 if (actual == expected)
1237 return 0;
1238
1239 /* Adjust item_count before emitting error message. */
1240 snprintf (buffer, BUFLEN,
1241 "Expected %s for item %d in formatted transfer, got %s",
1242 type_name (expected), dtp->u.p.item_count - 1, type_name (actual));
1243
1244 format_error (dtp, f, buffer);
1245 return 1;
1246 }
1247
1248
1249 static int
1250 require_numeric_type (st_parameter_dt *dtp, bt actual, const fnode *f)
1251 {
1252 #define BUFLEN 100
1253 char buffer[BUFLEN];
1254
1255 if (actual == BT_INTEGER || actual == BT_REAL || actual == BT_COMPLEX)
1256 return 0;
1257
1258 /* Adjust item_count before emitting error message. */
1259 snprintf (buffer, BUFLEN,
1260 "Expected numeric type for item %d in formatted transfer, got %s",
1261 dtp->u.p.item_count - 1, type_name (actual));
1262
1263 format_error (dtp, f, buffer);
1264 return 1;
1265 }
1266
1267
1268 /* This function is in the main loop for a formatted data transfer
1269 statement. It would be natural to implement this as a coroutine
1270 with the user program, but C makes that awkward. We loop,
1271 processing format elements. When we actually have to transfer
1272 data instead of just setting flags, we return control to the user
1273 program which calls a function that supplies the address and type
1274 of the next element, then comes back here to process it. */
1275
1276 static void
1277 formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1278 size_t size)
1279 {
1280 int pos, bytes_used;
1281 const fnode *f;
1282 format_token t;
1283 int n;
1284 int consume_data_flag;
1285
1286 /* Change a complex data item into a pair of reals. */
1287
1288 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1289 if (type == BT_COMPLEX)
1290 {
1291 type = BT_REAL;
1292 size /= 2;
1293 }
1294
1295 /* If there's an EOR condition, we simulate finalizing the transfer
1296 by doing nothing. */
1297 if (dtp->u.p.eor_condition)
1298 return;
1299
1300 /* Set this flag so that commas in reads cause the read to complete before
1301 the entire field has been read. The next read field will start right after
1302 the comma in the stream. (Set to 0 for character reads). */
1303 dtp->u.p.sf_read_comma =
1304 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1305
1306 for (;;)
1307 {
1308 /* If reversion has occurred and there is another real data item,
1309 then we have to move to the next record. */
1310 if (dtp->u.p.reversion_flag && n > 0)
1311 {
1312 dtp->u.p.reversion_flag = 0;
1313 next_record (dtp, 0);
1314 }
1315
1316 consume_data_flag = 1;
1317 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1318 break;
1319
1320 f = next_format (dtp);
1321 if (f == NULL)
1322 {
1323 /* No data descriptors left. */
1324 if (unlikely (n > 0))
1325 generate_error (&dtp->common, LIBERROR_FORMAT,
1326 "Insufficient data descriptors in format after reversion");
1327 return;
1328 }
1329
1330 t = f->format;
1331
1332 bytes_used = (int)(dtp->u.p.current_unit->recl
1333 - dtp->u.p.current_unit->bytes_left);
1334
1335 if (is_stream_io(dtp))
1336 bytes_used = 0;
1337
1338 switch (t)
1339 {
1340 case FMT_I:
1341 if (n == 0)
1342 goto need_read_data;
1343 if (require_type (dtp, BT_INTEGER, type, f))
1344 return;
1345 read_decimal (dtp, f, p, kind);
1346 break;
1347
1348 case FMT_B:
1349 if (n == 0)
1350 goto need_read_data;
1351 if (!(compile_options.allow_std & GFC_STD_GNU)
1352 && require_numeric_type (dtp, type, f))
1353 return;
1354 if (!(compile_options.allow_std & GFC_STD_F2008)
1355 && require_type (dtp, BT_INTEGER, type, f))
1356 return;
1357 read_radix (dtp, f, p, kind, 2);
1358 break;
1359
1360 case FMT_O:
1361 if (n == 0)
1362 goto need_read_data;
1363 if (!(compile_options.allow_std & GFC_STD_GNU)
1364 && require_numeric_type (dtp, type, f))
1365 return;
1366 if (!(compile_options.allow_std & GFC_STD_F2008)
1367 && require_type (dtp, BT_INTEGER, type, f))
1368 return;
1369 read_radix (dtp, f, p, kind, 8);
1370 break;
1371
1372 case FMT_Z:
1373 if (n == 0)
1374 goto need_read_data;
1375 if (!(compile_options.allow_std & GFC_STD_GNU)
1376 && require_numeric_type (dtp, type, f))
1377 return;
1378 if (!(compile_options.allow_std & GFC_STD_F2008)
1379 && require_type (dtp, BT_INTEGER, type, f))
1380 return;
1381 read_radix (dtp, f, p, kind, 16);
1382 break;
1383
1384 case FMT_A:
1385 if (n == 0)
1386 goto need_read_data;
1387
1388 /* It is possible to have FMT_A with something not BT_CHARACTER such
1389 as when writing out hollerith strings, so check both type
1390 and kind before calling wide character routines. */
1391 if (type == BT_CHARACTER && kind == 4)
1392 read_a_char4 (dtp, f, p, size);
1393 else
1394 read_a (dtp, f, p, size);
1395 break;
1396
1397 case FMT_L:
1398 if (n == 0)
1399 goto need_read_data;
1400 read_l (dtp, f, p, kind);
1401 break;
1402
1403 case FMT_D:
1404 if (n == 0)
1405 goto need_read_data;
1406 if (require_type (dtp, BT_REAL, type, f))
1407 return;
1408 read_f (dtp, f, p, kind);
1409 break;
1410
1411 case FMT_DT:
1412 if (n == 0)
1413 goto need_read_data;
1414 if (require_type (dtp, BT_CLASS, type, f))
1415 return;
1416 int unit = dtp->u.p.current_unit->unit_number;
1417 char dt[] = "DT";
1418 char tmp_iomsg[IOMSG_LEN] = "";
1419 char *child_iomsg;
1420 gfc_charlen_type child_iomsg_len;
1421 int noiostat;
1422 int *child_iostat = NULL;
1423 char *iotype = f->u.udf.string;
1424 gfc_charlen_type iotype_len = f->u.udf.string_len;
1425
1426 /* Build the iotype string. */
1427 if (iotype_len == 0)
1428 {
1429 iotype_len = 2;
1430 iotype = dt;
1431 }
1432 else
1433 {
1434 iotype_len += 2;
1435 iotype = xmalloc (iotype_len);
1436 iotype[0] = dt[0];
1437 iotype[1] = dt[1];
1438 memcpy (iotype + 2, f->u.udf.string, f->u.udf.string_len);
1439 }
1440
1441 /* Set iostat, intent(out). */
1442 noiostat = 0;
1443 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1444 dtp->common.iostat : &noiostat;
1445
1446 /* Set iomsg, intent(inout). */
1447 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1448 {
1449 child_iomsg = dtp->common.iomsg;
1450 child_iomsg_len = dtp->common.iomsg_len;
1451 }
1452 else
1453 {
1454 child_iomsg = tmp_iomsg;
1455 child_iomsg_len = IOMSG_LEN;
1456 }
1457
1458 /* Call the user defined formatted READ procedure. */
1459 dtp->u.p.current_unit->child_dtio++;
1460 dtp->u.p.fdtio_ptr (p, &unit, iotype, f->u.udf.vlist,
1461 child_iostat, child_iomsg,
1462 iotype_len, child_iomsg_len);
1463 dtp->u.p.current_unit->child_dtio--;
1464
1465 if (f->u.udf.string_len != 0)
1466 free (iotype);
1467 /* Note: vlist is freed in free_format_data. */
1468 break;
1469
1470 case FMT_E:
1471 if (n == 0)
1472 goto need_read_data;
1473 if (require_type (dtp, BT_REAL, type, f))
1474 return;
1475 read_f (dtp, f, p, kind);
1476 break;
1477
1478 case FMT_EN:
1479 if (n == 0)
1480 goto need_read_data;
1481 if (require_type (dtp, BT_REAL, type, f))
1482 return;
1483 read_f (dtp, f, p, kind);
1484 break;
1485
1486 case FMT_ES:
1487 if (n == 0)
1488 goto need_read_data;
1489 if (require_type (dtp, BT_REAL, type, f))
1490 return;
1491 read_f (dtp, f, p, kind);
1492 break;
1493
1494 case FMT_F:
1495 if (n == 0)
1496 goto need_read_data;
1497 if (require_type (dtp, BT_REAL, type, f))
1498 return;
1499 read_f (dtp, f, p, kind);
1500 break;
1501
1502 case FMT_G:
1503 if (n == 0)
1504 goto need_read_data;
1505 switch (type)
1506 {
1507 case BT_INTEGER:
1508 read_decimal (dtp, f, p, kind);
1509 break;
1510 case BT_LOGICAL:
1511 read_l (dtp, f, p, kind);
1512 break;
1513 case BT_CHARACTER:
1514 if (kind == 4)
1515 read_a_char4 (dtp, f, p, size);
1516 else
1517 read_a (dtp, f, p, size);
1518 break;
1519 case BT_REAL:
1520 read_f (dtp, f, p, kind);
1521 break;
1522 default:
1523 internal_error (&dtp->common, "formatted_transfer(): Bad type");
1524 }
1525 break;
1526
1527 case FMT_STRING:
1528 consume_data_flag = 0;
1529 format_error (dtp, f, "Constant string in input format");
1530 return;
1531
1532 /* Format codes that don't transfer data. */
1533 case FMT_X:
1534 case FMT_TR:
1535 consume_data_flag = 0;
1536 dtp->u.p.skips += f->u.n;
1537 pos = bytes_used + dtp->u.p.skips - 1;
1538 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1539 read_x (dtp, f->u.n);
1540 break;
1541
1542 case FMT_TL:
1543 case FMT_T:
1544 consume_data_flag = 0;
1545
1546 if (f->format == FMT_TL)
1547 {
1548 /* Handle the special case when no bytes have been used yet.
1549 Cannot go below zero. */
1550 if (bytes_used == 0)
1551 {
1552 dtp->u.p.pending_spaces -= f->u.n;
1553 dtp->u.p.skips -= f->u.n;
1554 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1555 }
1556
1557 pos = bytes_used - f->u.n;
1558 }
1559 else /* FMT_T */
1560 pos = f->u.n - 1;
1561
1562 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1563 left tab limit. We do not check if the position has gone
1564 beyond the end of record because a subsequent tab could
1565 bring us back again. */
1566 pos = pos < 0 ? 0 : pos;
1567
1568 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1569 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1570 + pos - dtp->u.p.max_pos;
1571 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1572 ? 0 : dtp->u.p.pending_spaces;
1573 if (dtp->u.p.skips == 0)
1574 break;
1575
1576 /* Adjust everything for end-of-record condition */
1577 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
1578 {
1579 dtp->u.p.current_unit->bytes_left -= dtp->u.p.sf_seen_eor;
1580 dtp->u.p.skips -= dtp->u.p.sf_seen_eor;
1581 bytes_used = pos;
1582 dtp->u.p.sf_seen_eor = 0;
1583 }
1584 if (dtp->u.p.skips < 0)
1585 {
1586 if (is_internal_unit (dtp))
1587 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1588 else
1589 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1590 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1591 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1592 }
1593 else
1594 read_x (dtp, dtp->u.p.skips);
1595 break;
1596
1597 case FMT_S:
1598 consume_data_flag = 0;
1599 dtp->u.p.sign_status = SIGN_S;
1600 break;
1601
1602 case FMT_SS:
1603 consume_data_flag = 0;
1604 dtp->u.p.sign_status = SIGN_SS;
1605 break;
1606
1607 case FMT_SP:
1608 consume_data_flag = 0;
1609 dtp->u.p.sign_status = SIGN_SP;
1610 break;
1611
1612 case FMT_BN:
1613 consume_data_flag = 0 ;
1614 dtp->u.p.blank_status = BLANK_NULL;
1615 break;
1616
1617 case FMT_BZ:
1618 consume_data_flag = 0;
1619 dtp->u.p.blank_status = BLANK_ZERO;
1620 break;
1621
1622 case FMT_DC:
1623 consume_data_flag = 0;
1624 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1625 break;
1626
1627 case FMT_DP:
1628 consume_data_flag = 0;
1629 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1630 break;
1631
1632 case FMT_RC:
1633 consume_data_flag = 0;
1634 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
1635 break;
1636
1637 case FMT_RD:
1638 consume_data_flag = 0;
1639 dtp->u.p.current_unit->round_status = ROUND_DOWN;
1640 break;
1641
1642 case FMT_RN:
1643 consume_data_flag = 0;
1644 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
1645 break;
1646
1647 case FMT_RP:
1648 consume_data_flag = 0;
1649 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
1650 break;
1651
1652 case FMT_RU:
1653 consume_data_flag = 0;
1654 dtp->u.p.current_unit->round_status = ROUND_UP;
1655 break;
1656
1657 case FMT_RZ:
1658 consume_data_flag = 0;
1659 dtp->u.p.current_unit->round_status = ROUND_ZERO;
1660 break;
1661
1662 case FMT_P:
1663 consume_data_flag = 0;
1664 dtp->u.p.scale_factor = f->u.k;
1665 break;
1666
1667 case FMT_DOLLAR:
1668 consume_data_flag = 0;
1669 dtp->u.p.seen_dollar = 1;
1670 break;
1671
1672 case FMT_SLASH:
1673 consume_data_flag = 0;
1674 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1675 next_record (dtp, 0);
1676 break;
1677
1678 case FMT_COLON:
1679 /* A colon descriptor causes us to exit this loop (in
1680 particular preventing another / descriptor from being
1681 processed) unless there is another data item to be
1682 transferred. */
1683 consume_data_flag = 0;
1684 if (n == 0)
1685 return;
1686 break;
1687
1688 default:
1689 internal_error (&dtp->common, "Bad format node");
1690 }
1691
1692 /* Adjust the item count and data pointer. */
1693
1694 if ((consume_data_flag > 0) && (n > 0))
1695 {
1696 n--;
1697 p = ((char *) p) + size;
1698 }
1699
1700 dtp->u.p.skips = 0;
1701
1702 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1703 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1704 }
1705
1706 return;
1707
1708 /* Come here when we need a data descriptor but don't have one. We
1709 push the current format node back onto the input, then return and
1710 let the user program call us back with the data. */
1711 need_read_data:
1712 unget_format (dtp, f);
1713 }
1714
1715
1716 static void
1717 formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kind,
1718 size_t size)
1719 {
1720 int pos, bytes_used;
1721 const fnode *f;
1722 format_token t;
1723 int n;
1724 int consume_data_flag;
1725
1726 /* Change a complex data item into a pair of reals. */
1727
1728 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1729 if (type == BT_COMPLEX)
1730 {
1731 type = BT_REAL;
1732 size /= 2;
1733 }
1734
1735 /* If there's an EOR condition, we simulate finalizing the transfer
1736 by doing nothing. */
1737 if (dtp->u.p.eor_condition)
1738 return;
1739
1740 /* Set this flag so that commas in reads cause the read to complete before
1741 the entire field has been read. The next read field will start right after
1742 the comma in the stream. (Set to 0 for character reads). */
1743 dtp->u.p.sf_read_comma =
1744 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1745
1746 for (;;)
1747 {
1748 /* If reversion has occurred and there is another real data item,
1749 then we have to move to the next record. */
1750 if (dtp->u.p.reversion_flag && n > 0)
1751 {
1752 dtp->u.p.reversion_flag = 0;
1753 next_record (dtp, 0);
1754 }
1755
1756 consume_data_flag = 1;
1757 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1758 break;
1759
1760 f = next_format (dtp);
1761 if (f == NULL)
1762 {
1763 /* No data descriptors left. */
1764 if (unlikely (n > 0))
1765 generate_error (&dtp->common, LIBERROR_FORMAT,
1766 "Insufficient data descriptors in format after reversion");
1767 return;
1768 }
1769
1770 /* Now discharge T, TR and X movements to the right. This is delayed
1771 until a data producing format to suppress trailing spaces. */
1772
1773 t = f->format;
1774 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
1775 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
1776 || t == FMT_Z || t == FMT_F || t == FMT_E
1777 || t == FMT_EN || t == FMT_ES || t == FMT_G
1778 || t == FMT_L || t == FMT_A || t == FMT_D
1779 || t == FMT_DT))
1780 || t == FMT_STRING))
1781 {
1782 if (dtp->u.p.skips > 0)
1783 {
1784 int tmp;
1785 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1786 tmp = (int)(dtp->u.p.current_unit->recl
1787 - dtp->u.p.current_unit->bytes_left);
1788 dtp->u.p.max_pos =
1789 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
1790 dtp->u.p.skips = 0;
1791 }
1792 if (dtp->u.p.skips < 0)
1793 {
1794 if (is_internal_unit (dtp))
1795 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1796 else
1797 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1798 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1799 }
1800 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1801 }
1802
1803 bytes_used = (int)(dtp->u.p.current_unit->recl
1804 - dtp->u.p.current_unit->bytes_left);
1805
1806 if (is_stream_io(dtp))
1807 bytes_used = 0;
1808
1809 switch (t)
1810 {
1811 case FMT_I:
1812 if (n == 0)
1813 goto need_data;
1814 if (require_type (dtp, BT_INTEGER, type, f))
1815 return;
1816 write_i (dtp, f, p, kind);
1817 break;
1818
1819 case FMT_B:
1820 if (n == 0)
1821 goto need_data;
1822 if (!(compile_options.allow_std & GFC_STD_GNU)
1823 && require_numeric_type (dtp, type, f))
1824 return;
1825 if (!(compile_options.allow_std & GFC_STD_F2008)
1826 && require_type (dtp, BT_INTEGER, type, f))
1827 return;
1828 write_b (dtp, f, p, kind);
1829 break;
1830
1831 case FMT_O:
1832 if (n == 0)
1833 goto need_data;
1834 if (!(compile_options.allow_std & GFC_STD_GNU)
1835 && require_numeric_type (dtp, type, f))
1836 return;
1837 if (!(compile_options.allow_std & GFC_STD_F2008)
1838 && require_type (dtp, BT_INTEGER, type, f))
1839 return;
1840 write_o (dtp, f, p, kind);
1841 break;
1842
1843 case FMT_Z:
1844 if (n == 0)
1845 goto need_data;
1846 if (!(compile_options.allow_std & GFC_STD_GNU)
1847 && require_numeric_type (dtp, type, f))
1848 return;
1849 if (!(compile_options.allow_std & GFC_STD_F2008)
1850 && require_type (dtp, BT_INTEGER, type, f))
1851 return;
1852 write_z (dtp, f, p, kind);
1853 break;
1854
1855 case FMT_A:
1856 if (n == 0)
1857 goto need_data;
1858
1859 /* It is possible to have FMT_A with something not BT_CHARACTER such
1860 as when writing out hollerith strings, so check both type
1861 and kind before calling wide character routines. */
1862 if (type == BT_CHARACTER && kind == 4)
1863 write_a_char4 (dtp, f, p, size);
1864 else
1865 write_a (dtp, f, p, size);
1866 break;
1867
1868 case FMT_L:
1869 if (n == 0)
1870 goto need_data;
1871 write_l (dtp, f, p, kind);
1872 break;
1873
1874 case FMT_D:
1875 if (n == 0)
1876 goto need_data;
1877 if (require_type (dtp, BT_REAL, type, f))
1878 return;
1879 write_d (dtp, f, p, kind);
1880 break;
1881
1882 case FMT_DT:
1883 if (n == 0)
1884 goto need_data;
1885 int unit = dtp->u.p.current_unit->unit_number;
1886 char dt[] = "DT";
1887 char tmp_iomsg[IOMSG_LEN] = "";
1888 char *child_iomsg;
1889 gfc_charlen_type child_iomsg_len;
1890 int noiostat;
1891 int *child_iostat = NULL;
1892 char *iotype = f->u.udf.string;
1893 gfc_charlen_type iotype_len = f->u.udf.string_len;
1894
1895 /* Build the iotype string. */
1896 if (iotype_len == 0)
1897 {
1898 iotype_len = 2;
1899 iotype = dt;
1900 }
1901 else
1902 {
1903 iotype_len += 2;
1904 iotype = xmalloc (iotype_len);
1905 iotype[0] = dt[0];
1906 iotype[1] = dt[1];
1907 memcpy (iotype + 2, f->u.udf.string, f->u.udf.string_len);
1908 }
1909
1910 /* Set iostat, intent(out). */
1911 noiostat = 0;
1912 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1913 dtp->common.iostat : &noiostat;
1914
1915 /* Set iomsg, intent(inout). */
1916 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1917 {
1918 child_iomsg = dtp->common.iomsg;
1919 child_iomsg_len = dtp->common.iomsg_len;
1920 }
1921 else
1922 {
1923 child_iomsg = tmp_iomsg;
1924 child_iomsg_len = IOMSG_LEN;
1925 }
1926
1927 /* Call the user defined formatted WRITE procedure. */
1928 dtp->u.p.current_unit->child_dtio++;
1929 dtp->u.p.fdtio_ptr (p, &unit, iotype, f->u.udf.vlist,
1930 child_iostat, child_iomsg,
1931 iotype_len, child_iomsg_len);
1932 dtp->u.p.current_unit->child_dtio--;
1933
1934 if (f->u.udf.string_len != 0)
1935 free (iotype);
1936 /* Note: vlist is freed in free_format_data. */
1937 break;
1938
1939 case FMT_E:
1940 if (n == 0)
1941 goto need_data;
1942 if (require_type (dtp, BT_REAL, type, f))
1943 return;
1944 write_e (dtp, f, p, kind);
1945 break;
1946
1947 case FMT_EN:
1948 if (n == 0)
1949 goto need_data;
1950 if (require_type (dtp, BT_REAL, type, f))
1951 return;
1952 write_en (dtp, f, p, kind);
1953 break;
1954
1955 case FMT_ES:
1956 if (n == 0)
1957 goto need_data;
1958 if (require_type (dtp, BT_REAL, type, f))
1959 return;
1960 write_es (dtp, f, p, kind);
1961 break;
1962
1963 case FMT_F:
1964 if (n == 0)
1965 goto need_data;
1966 if (require_type (dtp, BT_REAL, type, f))
1967 return;
1968 write_f (dtp, f, p, kind);
1969 break;
1970
1971 case FMT_G:
1972 if (n == 0)
1973 goto need_data;
1974 switch (type)
1975 {
1976 case BT_INTEGER:
1977 write_i (dtp, f, p, kind);
1978 break;
1979 case BT_LOGICAL:
1980 write_l (dtp, f, p, kind);
1981 break;
1982 case BT_CHARACTER:
1983 if (kind == 4)
1984 write_a_char4 (dtp, f, p, size);
1985 else
1986 write_a (dtp, f, p, size);
1987 break;
1988 case BT_REAL:
1989 if (f->u.real.w == 0)
1990 write_real_g0 (dtp, p, kind, f->u.real.d);
1991 else
1992 write_d (dtp, f, p, kind);
1993 break;
1994 default:
1995 internal_error (&dtp->common,
1996 "formatted_transfer(): Bad type");
1997 }
1998 break;
1999
2000 case FMT_STRING:
2001 consume_data_flag = 0;
2002 write_constant_string (dtp, f);
2003 break;
2004
2005 /* Format codes that don't transfer data. */
2006 case FMT_X:
2007 case FMT_TR:
2008 consume_data_flag = 0;
2009
2010 dtp->u.p.skips += f->u.n;
2011 pos = bytes_used + dtp->u.p.skips - 1;
2012 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
2013 /* Writes occur just before the switch on f->format, above, so
2014 that trailing blanks are suppressed, unless we are doing a
2015 non-advancing write in which case we want to output the blanks
2016 now. */
2017 if (dtp->u.p.advance_status == ADVANCE_NO)
2018 {
2019 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
2020 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2021 }
2022 break;
2023
2024 case FMT_TL:
2025 case FMT_T:
2026 consume_data_flag = 0;
2027
2028 if (f->format == FMT_TL)
2029 {
2030
2031 /* Handle the special case when no bytes have been used yet.
2032 Cannot go below zero. */
2033 if (bytes_used == 0)
2034 {
2035 dtp->u.p.pending_spaces -= f->u.n;
2036 dtp->u.p.skips -= f->u.n;
2037 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
2038 }
2039
2040 pos = bytes_used - f->u.n;
2041 }
2042 else /* FMT_T */
2043 pos = f->u.n - dtp->u.p.pending_spaces - 1;
2044
2045 /* Standard 10.6.1.1: excessive left tabbing is reset to the
2046 left tab limit. We do not check if the position has gone
2047 beyond the end of record because a subsequent tab could
2048 bring us back again. */
2049 pos = pos < 0 ? 0 : pos;
2050
2051 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
2052 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
2053 + pos - dtp->u.p.max_pos;
2054 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
2055 ? 0 : dtp->u.p.pending_spaces;
2056 break;
2057
2058 case FMT_S:
2059 consume_data_flag = 0;
2060 dtp->u.p.sign_status = SIGN_S;
2061 break;
2062
2063 case FMT_SS:
2064 consume_data_flag = 0;
2065 dtp->u.p.sign_status = SIGN_SS;
2066 break;
2067
2068 case FMT_SP:
2069 consume_data_flag = 0;
2070 dtp->u.p.sign_status = SIGN_SP;
2071 break;
2072
2073 case FMT_BN:
2074 consume_data_flag = 0 ;
2075 dtp->u.p.blank_status = BLANK_NULL;
2076 break;
2077
2078 case FMT_BZ:
2079 consume_data_flag = 0;
2080 dtp->u.p.blank_status = BLANK_ZERO;
2081 break;
2082
2083 case FMT_DC:
2084 consume_data_flag = 0;
2085 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
2086 break;
2087
2088 case FMT_DP:
2089 consume_data_flag = 0;
2090 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
2091 break;
2092
2093 case FMT_RC:
2094 consume_data_flag = 0;
2095 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
2096 break;
2097
2098 case FMT_RD:
2099 consume_data_flag = 0;
2100 dtp->u.p.current_unit->round_status = ROUND_DOWN;
2101 break;
2102
2103 case FMT_RN:
2104 consume_data_flag = 0;
2105 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
2106 break;
2107
2108 case FMT_RP:
2109 consume_data_flag = 0;
2110 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
2111 break;
2112
2113 case FMT_RU:
2114 consume_data_flag = 0;
2115 dtp->u.p.current_unit->round_status = ROUND_UP;
2116 break;
2117
2118 case FMT_RZ:
2119 consume_data_flag = 0;
2120 dtp->u.p.current_unit->round_status = ROUND_ZERO;
2121 break;
2122
2123 case FMT_P:
2124 consume_data_flag = 0;
2125 dtp->u.p.scale_factor = f->u.k;
2126 break;
2127
2128 case FMT_DOLLAR:
2129 consume_data_flag = 0;
2130 dtp->u.p.seen_dollar = 1;
2131 break;
2132
2133 case FMT_SLASH:
2134 consume_data_flag = 0;
2135 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2136 next_record (dtp, 0);
2137 break;
2138
2139 case FMT_COLON:
2140 /* A colon descriptor causes us to exit this loop (in
2141 particular preventing another / descriptor from being
2142 processed) unless there is another data item to be
2143 transferred. */
2144 consume_data_flag = 0;
2145 if (n == 0)
2146 return;
2147 break;
2148
2149 default:
2150 internal_error (&dtp->common, "Bad format node");
2151 }
2152
2153 /* Adjust the item count and data pointer. */
2154
2155 if ((consume_data_flag > 0) && (n > 0))
2156 {
2157 n--;
2158 p = ((char *) p) + size;
2159 }
2160
2161 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
2162 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
2163 }
2164
2165 return;
2166
2167 /* Come here when we need a data descriptor but don't have one. We
2168 push the current format node back onto the input, then return and
2169 let the user program call us back with the data. */
2170 need_data:
2171 unget_format (dtp, f);
2172 }
2173
2174 /* This function is first called from data_init_transfer to initiate the loop
2175 over each item in the format, transferring data as required. Subsequent
2176 calls to this function occur for each data item foound in the READ/WRITE
2177 statement. The item_count is incremented for each call. Since the first
2178 call is from data_transfer_init, the item_count is always one greater than
2179 the actual count number of the item being transferred. */
2180
2181 static void
2182 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
2183 size_t size, size_t nelems)
2184 {
2185 size_t elem;
2186 char *tmp;
2187
2188 tmp = (char *) p;
2189 size_t stride = type == BT_CHARACTER ?
2190 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
2191 if (dtp->u.p.mode == READING)
2192 {
2193 /* Big loop over all the elements. */
2194 for (elem = 0; elem < nelems; elem++)
2195 {
2196 dtp->u.p.item_count++;
2197 formatted_transfer_scalar_read (dtp, type, tmp + stride*elem, kind, size);
2198 }
2199 }
2200 else
2201 {
2202 /* Big loop over all the elements. */
2203 for (elem = 0; elem < nelems; elem++)
2204 {
2205 dtp->u.p.item_count++;
2206 formatted_transfer_scalar_write (dtp, type, tmp + stride*elem, kind, size);
2207 }
2208 }
2209 }
2210
2211
2212 /* Data transfer entry points. The type of the data entity is
2213 implicit in the subroutine call. This prevents us from having to
2214 share a common enum with the compiler. */
2215
2216 void
2217 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
2218 {
2219 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2220 return;
2221 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
2222 }
2223
2224 void
2225 transfer_integer_write (st_parameter_dt *dtp, void *p, int kind)
2226 {
2227 transfer_integer (dtp, p, kind);
2228 }
2229
2230 void
2231 transfer_real (st_parameter_dt *dtp, void *p, int kind)
2232 {
2233 size_t size;
2234 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2235 return;
2236 size = size_from_real_kind (kind);
2237 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
2238 }
2239
2240 void
2241 transfer_real_write (st_parameter_dt *dtp, void *p, int kind)
2242 {
2243 transfer_real (dtp, p, kind);
2244 }
2245
2246 void
2247 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
2248 {
2249 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2250 return;
2251 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
2252 }
2253
2254 void
2255 transfer_logical_write (st_parameter_dt *dtp, void *p, int kind)
2256 {
2257 transfer_logical (dtp, p, kind);
2258 }
2259
2260 void
2261 transfer_character (st_parameter_dt *dtp, void *p, int len)
2262 {
2263 static char *empty_string[0];
2264
2265 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2266 return;
2267
2268 /* Strings of zero length can have p == NULL, which confuses the
2269 transfer routines into thinking we need more data elements. To avoid
2270 this, we give them a nice pointer. */
2271 if (len == 0 && p == NULL)
2272 p = empty_string;
2273
2274 /* Set kind here to 1. */
2275 dtp->u.p.transfer (dtp, BT_CHARACTER, p, 1, len, 1);
2276 }
2277
2278 void
2279 transfer_character_write (st_parameter_dt *dtp, void *p, int len)
2280 {
2281 transfer_character (dtp, p, len);
2282 }
2283
2284 void
2285 transfer_character_wide (st_parameter_dt *dtp, void *p, int len, int kind)
2286 {
2287 static char *empty_string[0];
2288
2289 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2290 return;
2291
2292 /* Strings of zero length can have p == NULL, which confuses the
2293 transfer routines into thinking we need more data elements. To avoid
2294 this, we give them a nice pointer. */
2295 if (len == 0 && p == NULL)
2296 p = empty_string;
2297
2298 /* Here we pass the actual kind value. */
2299 dtp->u.p.transfer (dtp, BT_CHARACTER, p, kind, len, 1);
2300 }
2301
2302 void
2303 transfer_character_wide_write (st_parameter_dt *dtp, void *p, int len, int kind)
2304 {
2305 transfer_character_wide (dtp, p, len, kind);
2306 }
2307
2308 void
2309 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
2310 {
2311 size_t size;
2312 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2313 return;
2314 size = size_from_complex_kind (kind);
2315 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
2316 }
2317
2318 void
2319 transfer_complex_write (st_parameter_dt *dtp, void *p, int kind)
2320 {
2321 transfer_complex (dtp, p, kind);
2322 }
2323
2324 void
2325 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2326 gfc_charlen_type charlen)
2327 {
2328 index_type count[GFC_MAX_DIMENSIONS];
2329 index_type extent[GFC_MAX_DIMENSIONS];
2330 index_type stride[GFC_MAX_DIMENSIONS];
2331 index_type stride0, rank, size, n;
2332 size_t tsize;
2333 char *data;
2334 bt iotype;
2335
2336 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2337 return;
2338
2339 iotype = (bt) GFC_DESCRIPTOR_TYPE (desc);
2340 size = iotype == BT_CHARACTER ? charlen : GFC_DESCRIPTOR_SIZE (desc);
2341
2342 rank = GFC_DESCRIPTOR_RANK (desc);
2343 for (n = 0; n < rank; n++)
2344 {
2345 count[n] = 0;
2346 stride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(desc,n);
2347 extent[n] = GFC_DESCRIPTOR_EXTENT(desc,n);
2348
2349 /* If the extent of even one dimension is zero, then the entire
2350 array section contains zero elements, so we return after writing
2351 a zero array record. */
2352 if (extent[n] <= 0)
2353 {
2354 data = NULL;
2355 tsize = 0;
2356 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2357 return;
2358 }
2359 }
2360
2361 stride0 = stride[0];
2362
2363 /* If the innermost dimension has a stride of 1, we can do the transfer
2364 in contiguous chunks. */
2365 if (stride0 == size)
2366 tsize = extent[0];
2367 else
2368 tsize = 1;
2369
2370 data = GFC_DESCRIPTOR_DATA (desc);
2371
2372 while (data)
2373 {
2374 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2375 data += stride0 * tsize;
2376 count[0] += tsize;
2377 n = 0;
2378 while (count[n] == extent[n])
2379 {
2380 count[n] = 0;
2381 data -= stride[n] * extent[n];
2382 n++;
2383 if (n == rank)
2384 {
2385 data = NULL;
2386 break;
2387 }
2388 else
2389 {
2390 count[n]++;
2391 data += stride[n];
2392 }
2393 }
2394 }
2395 }
2396
2397 void
2398 transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2399 gfc_charlen_type charlen)
2400 {
2401 transfer_array (dtp, desc, kind, charlen);
2402 }
2403
2404
2405 /* User defined input/output iomsg. */
2406
2407 #define IOMSG_LEN 256
2408
2409 void
2410 transfer_derived (st_parameter_dt *parent, void *dtio_source, void *dtio_proc)
2411 {
2412 if (parent->u.p.current_unit)
2413 {
2414 if (parent->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2415 parent->u.p.ufdtio_ptr = (unformatted_dtio) dtio_proc;
2416 else
2417 parent->u.p.fdtio_ptr = (formatted_dtio) dtio_proc;
2418 }
2419 parent->u.p.transfer (parent, BT_CLASS, dtio_source, 0, 0, 1);
2420 }
2421
2422
2423 /* Preposition a sequential unformatted file while reading. */
2424
2425 static void
2426 us_read (st_parameter_dt *dtp, int continued)
2427 {
2428 ssize_t n, nr;
2429 GFC_INTEGER_4 i4;
2430 GFC_INTEGER_8 i8;
2431 gfc_offset i;
2432
2433 if (compile_options.record_marker == 0)
2434 n = sizeof (GFC_INTEGER_4);
2435 else
2436 n = compile_options.record_marker;
2437
2438 nr = sread (dtp->u.p.current_unit->s, &i, n);
2439 if (unlikely (nr < 0))
2440 {
2441 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2442 return;
2443 }
2444 else if (nr == 0)
2445 {
2446 hit_eof (dtp);
2447 return; /* end of file */
2448 }
2449 else if (unlikely (n != nr))
2450 {
2451 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2452 return;
2453 }
2454
2455 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2456 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
2457 {
2458 switch (nr)
2459 {
2460 case sizeof(GFC_INTEGER_4):
2461 memcpy (&i4, &i, sizeof (i4));
2462 i = i4;
2463 break;
2464
2465 case sizeof(GFC_INTEGER_8):
2466 memcpy (&i8, &i, sizeof (i8));
2467 i = i8;
2468 break;
2469
2470 default:
2471 runtime_error ("Illegal value for record marker");
2472 break;
2473 }
2474 }
2475 else
2476 {
2477 uint32_t u32;
2478 uint64_t u64;
2479 switch (nr)
2480 {
2481 case sizeof(GFC_INTEGER_4):
2482 memcpy (&u32, &i, sizeof (u32));
2483 u32 = __builtin_bswap32 (u32);
2484 memcpy (&i4, &u32, sizeof (i4));
2485 i = i4;
2486 break;
2487
2488 case sizeof(GFC_INTEGER_8):
2489 memcpy (&u64, &i, sizeof (u64));
2490 u64 = __builtin_bswap64 (u64);
2491 memcpy (&i8, &u64, sizeof (i8));
2492 i = i8;
2493 break;
2494
2495 default:
2496 runtime_error ("Illegal value for record marker");
2497 break;
2498 }
2499 }
2500
2501 if (i >= 0)
2502 {
2503 dtp->u.p.current_unit->bytes_left_subrecord = i;
2504 dtp->u.p.current_unit->continued = 0;
2505 }
2506 else
2507 {
2508 dtp->u.p.current_unit->bytes_left_subrecord = -i;
2509 dtp->u.p.current_unit->continued = 1;
2510 }
2511
2512 if (! continued)
2513 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2514 }
2515
2516
2517 /* Preposition a sequential unformatted file while writing. This
2518 amount to writing a bogus length that will be filled in later. */
2519
2520 static void
2521 us_write (st_parameter_dt *dtp, int continued)
2522 {
2523 ssize_t nbytes;
2524 gfc_offset dummy;
2525
2526 dummy = 0;
2527
2528 if (compile_options.record_marker == 0)
2529 nbytes = sizeof (GFC_INTEGER_4);
2530 else
2531 nbytes = compile_options.record_marker ;
2532
2533 if (swrite (dtp->u.p.current_unit->s, &dummy, nbytes) != nbytes)
2534 generate_error (&dtp->common, LIBERROR_OS, NULL);
2535
2536 /* For sequential unformatted, if RECL= was not specified in the OPEN
2537 we write until we have more bytes than can fit in the subrecord
2538 markers, then we write a new subrecord. */
2539
2540 dtp->u.p.current_unit->bytes_left_subrecord =
2541 dtp->u.p.current_unit->recl_subrecord;
2542 dtp->u.p.current_unit->continued = continued;
2543 }
2544
2545
2546 /* Position to the next record prior to transfer. We are assumed to
2547 be before the next record. We also calculate the bytes in the next
2548 record. */
2549
2550 static void
2551 pre_position (st_parameter_dt *dtp)
2552 {
2553 if (dtp->u.p.current_unit->current_record)
2554 return; /* Already positioned. */
2555
2556 switch (current_mode (dtp))
2557 {
2558 case FORMATTED_STREAM:
2559 case UNFORMATTED_STREAM:
2560 /* There are no records with stream I/O. If the position was specified
2561 data_transfer_init has already positioned the file. If no position
2562 was specified, we continue from where we last left off. I.e.
2563 there is nothing to do here. */
2564 break;
2565
2566 case UNFORMATTED_SEQUENTIAL:
2567 if (dtp->u.p.mode == READING)
2568 us_read (dtp, 0);
2569 else
2570 us_write (dtp, 0);
2571
2572 break;
2573
2574 case FORMATTED_SEQUENTIAL:
2575 case FORMATTED_DIRECT:
2576 case UNFORMATTED_DIRECT:
2577 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2578 break;
2579 }
2580
2581 dtp->u.p.current_unit->current_record = 1;
2582 }
2583
2584
2585 /* Initialize things for a data transfer. This code is common for
2586 both reading and writing. */
2587
2588 static void
2589 data_transfer_init (st_parameter_dt *dtp, int read_flag)
2590 {
2591 unit_flags u_flags; /* Used for creating a unit if needed. */
2592 GFC_INTEGER_4 cf = dtp->common.flags;
2593 namelist_info *ionml;
2594
2595 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
2596
2597 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2598
2599 dtp->u.p.ionml = ionml;
2600 dtp->u.p.mode = read_flag ? READING : WRITING;
2601
2602 dtp->u.p.cc.len = 0;
2603
2604 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2605 return;
2606
2607 dtp->u.p.current_unit = get_unit (dtp, 1);
2608
2609 if (dtp->u.p.current_unit == NULL)
2610 {
2611 /* This means we tried to access an external unit < 0 without
2612 having opened it first with NEWUNIT=. */
2613 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2614 "Unit number is negative and unit was not already "
2615 "opened with OPEN(NEWUNIT=...)");
2616 return;
2617 }
2618 else if (dtp->u.p.current_unit->s == NULL)
2619 { /* Open the unit with some default flags. */
2620 st_parameter_open opp;
2621 unit_convert conv;
2622
2623 memset (&u_flags, '\0', sizeof (u_flags));
2624 u_flags.access = ACCESS_SEQUENTIAL;
2625 u_flags.action = ACTION_READWRITE;
2626
2627 /* Is it unformatted? */
2628 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
2629 | IOPARM_DT_IONML_SET)))
2630 u_flags.form = FORM_UNFORMATTED;
2631 else
2632 u_flags.form = FORM_UNSPECIFIED;
2633
2634 u_flags.delim = DELIM_UNSPECIFIED;
2635 u_flags.blank = BLANK_UNSPECIFIED;
2636 u_flags.pad = PAD_UNSPECIFIED;
2637 u_flags.decimal = DECIMAL_UNSPECIFIED;
2638 u_flags.encoding = ENCODING_UNSPECIFIED;
2639 u_flags.async = ASYNC_UNSPECIFIED;
2640 u_flags.round = ROUND_UNSPECIFIED;
2641 u_flags.sign = SIGN_UNSPECIFIED;
2642 u_flags.share = SHARE_UNSPECIFIED;
2643 u_flags.cc = CC_UNSPECIFIED;
2644 u_flags.readonly = 0;
2645
2646 u_flags.status = STATUS_UNKNOWN;
2647
2648 conv = get_unformatted_convert (dtp->common.unit);
2649
2650 if (conv == GFC_CONVERT_NONE)
2651 conv = compile_options.convert;
2652
2653 /* We use big_endian, which is 0 on little-endian machines
2654 and 1 on big-endian machines. */
2655 switch (conv)
2656 {
2657 case GFC_CONVERT_NATIVE:
2658 case GFC_CONVERT_SWAP:
2659 break;
2660
2661 case GFC_CONVERT_BIG:
2662 conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
2663 break;
2664
2665 case GFC_CONVERT_LITTLE:
2666 conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
2667 break;
2668
2669 default:
2670 internal_error (&opp.common, "Illegal value for CONVERT");
2671 break;
2672 }
2673
2674 u_flags.convert = conv;
2675
2676 opp.common = dtp->common;
2677 opp.common.flags &= IOPARM_COMMON_MASK;
2678 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
2679 dtp->common.flags &= ~IOPARM_COMMON_MASK;
2680 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
2681 if (dtp->u.p.current_unit == NULL)
2682 return;
2683 }
2684
2685 if (dtp->u.p.current_unit->child_dtio == 0)
2686 {
2687 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2688 {
2689 dtp->u.p.current_unit->has_size = true;
2690 /* Initialize the count. */
2691 dtp->u.p.current_unit->size_used = 0;
2692 }
2693 else
2694 dtp->u.p.current_unit->has_size = false;
2695 }
2696
2697 /* Check the action. */
2698
2699 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
2700 {
2701 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2702 "Cannot read from file opened for WRITE");
2703 return;
2704 }
2705
2706 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
2707 {
2708 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2709 "Cannot write to file opened for READ");
2710 return;
2711 }
2712
2713 dtp->u.p.first_item = 1;
2714
2715 /* Check the format. */
2716
2717 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2718 parse_format (dtp);
2719
2720 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
2721 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2722 != 0)
2723 {
2724 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2725 "Format present for UNFORMATTED data transfer");
2726 return;
2727 }
2728
2729 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
2730 {
2731 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2732 {
2733 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2734 "A format cannot be specified with a namelist");
2735 return;
2736 }
2737 }
2738 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
2739 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
2740 {
2741 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2742 "Missing format for FORMATTED data transfer");
2743 return;
2744 }
2745
2746 if (is_internal_unit (dtp)
2747 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2748 {
2749 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2750 "Internal file cannot be accessed by UNFORMATTED "
2751 "data transfer");
2752 return;
2753 }
2754
2755 /* Check the record or position number. */
2756
2757 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
2758 && (cf & IOPARM_DT_HAS_REC) == 0)
2759 {
2760 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2761 "Direct access data transfer requires record number");
2762 return;
2763 }
2764
2765 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2766 {
2767 if ((cf & IOPARM_DT_HAS_REC) != 0)
2768 {
2769 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2770 "Record number not allowed for sequential access "
2771 "data transfer");
2772 return;
2773 }
2774
2775 if (compile_options.warn_std &&
2776 dtp->u.p.current_unit->endfile == AFTER_ENDFILE)
2777 {
2778 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2779 "Sequential READ or WRITE not allowed after "
2780 "EOF marker, possibly use REWIND or BACKSPACE");
2781 return;
2782 }
2783 }
2784 /* Process the ADVANCE option. */
2785
2786 dtp->u.p.advance_status
2787 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
2788 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
2789 "Bad ADVANCE parameter in data transfer statement");
2790
2791 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
2792 {
2793 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2794 {
2795 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2796 "ADVANCE specification conflicts with sequential "
2797 "access");
2798 return;
2799 }
2800
2801 if (is_internal_unit (dtp))
2802 {
2803 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2804 "ADVANCE specification conflicts with internal file");
2805 return;
2806 }
2807
2808 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2809 != IOPARM_DT_HAS_FORMAT)
2810 {
2811 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2812 "ADVANCE specification requires an explicit format");
2813 return;
2814 }
2815 }
2816
2817 if (read_flag)
2818 {
2819 dtp->u.p.current_unit->previous_nonadvancing_write = 0;
2820
2821 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
2822 {
2823 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2824 "EOR specification requires an ADVANCE specification "
2825 "of NO");
2826 return;
2827 }
2828
2829 if ((cf & IOPARM_DT_HAS_SIZE) != 0
2830 && dtp->u.p.advance_status != ADVANCE_NO)
2831 {
2832 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2833 "SIZE specification requires an ADVANCE "
2834 "specification of NO");
2835 return;
2836 }
2837 }
2838 else
2839 { /* Write constraints. */
2840 if ((cf & IOPARM_END) != 0)
2841 {
2842 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2843 "END specification cannot appear in a write "
2844 "statement");
2845 return;
2846 }
2847
2848 if ((cf & IOPARM_EOR) != 0)
2849 {
2850 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2851 "EOR specification cannot appear in a write "
2852 "statement");
2853 return;
2854 }
2855
2856 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2857 {
2858 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2859 "SIZE specification cannot appear in a write "
2860 "statement");
2861 return;
2862 }
2863 }
2864
2865 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
2866 dtp->u.p.advance_status = ADVANCE_YES;
2867
2868 /* Check the decimal mode. */
2869 dtp->u.p.current_unit->decimal_status
2870 = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
2871 find_option (&dtp->common, dtp->decimal, dtp->decimal_len,
2872 decimal_opt, "Bad DECIMAL parameter in data transfer "
2873 "statement");
2874
2875 if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED)
2876 dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal;
2877
2878 /* Check the round mode. */
2879 dtp->u.p.current_unit->round_status
2880 = !(cf & IOPARM_DT_HAS_ROUND) ? ROUND_UNSPECIFIED :
2881 find_option (&dtp->common, dtp->round, dtp->round_len,
2882 round_opt, "Bad ROUND parameter in data transfer "
2883 "statement");
2884
2885 if (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED)
2886 dtp->u.p.current_unit->round_status = dtp->u.p.current_unit->flags.round;
2887
2888 /* Check the sign mode. */
2889 dtp->u.p.sign_status
2890 = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
2891 find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
2892 "Bad SIGN parameter in data transfer statement");
2893
2894 if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
2895 dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
2896
2897 /* Check the blank mode. */
2898 dtp->u.p.blank_status
2899 = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
2900 find_option (&dtp->common, dtp->blank, dtp->blank_len,
2901 blank_opt,
2902 "Bad BLANK parameter in data transfer statement");
2903
2904 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
2905 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
2906
2907 /* Check the delim mode. */
2908 dtp->u.p.current_unit->delim_status
2909 = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
2910 find_option (&dtp->common, dtp->delim, dtp->delim_len,
2911 delim_opt, "Bad DELIM parameter in data transfer statement");
2912
2913 if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED)
2914 {
2915 if (ionml && dtp->u.p.current_unit->flags.delim == DELIM_UNSPECIFIED)
2916 dtp->u.p.current_unit->delim_status = DELIM_QUOTE;
2917 else
2918 dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim;
2919 }
2920
2921 /* Check the pad mode. */
2922 dtp->u.p.current_unit->pad_status
2923 = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
2924 find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
2925 "Bad PAD parameter in data transfer statement");
2926
2927 if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED)
2928 dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad;
2929
2930 /* Check to see if we might be reading what we wrote before */
2931
2932 if (dtp->u.p.mode != dtp->u.p.current_unit->mode
2933 && !is_internal_unit (dtp))
2934 {
2935 int pos = fbuf_reset (dtp->u.p.current_unit);
2936 if (pos != 0)
2937 sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR);
2938 sflush(dtp->u.p.current_unit->s);
2939 }
2940
2941 /* Check the POS= specifier: that it is in range and that it is used with a
2942 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2943
2944 if (((cf & IOPARM_DT_HAS_POS) != 0))
2945 {
2946 if (is_stream_io (dtp))
2947 {
2948
2949 if (dtp->pos <= 0)
2950 {
2951 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2952 "POS=specifier must be positive");
2953 return;
2954 }
2955
2956 if (dtp->pos >= dtp->u.p.current_unit->maxrec)
2957 {
2958 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2959 "POS=specifier too large");
2960 return;
2961 }
2962
2963 dtp->rec = dtp->pos;
2964
2965 if (dtp->u.p.mode == READING)
2966 {
2967 /* Reset the endfile flag; if we hit EOF during reading
2968 we'll set the flag and generate an error at that point
2969 rather than worrying about it here. */
2970 dtp->u.p.current_unit->endfile = NO_ENDFILE;
2971 }
2972
2973 if (dtp->pos != dtp->u.p.current_unit->strm_pos)
2974 {
2975 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2976 if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, SEEK_SET) < 0)
2977 {
2978 generate_error (&dtp->common, LIBERROR_OS, NULL);
2979 return;
2980 }
2981 dtp->u.p.current_unit->strm_pos = dtp->pos;
2982 }
2983 }
2984 else
2985 {
2986 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2987 "POS=specifier not allowed, "
2988 "Try OPEN with ACCESS='stream'");
2989 return;
2990 }
2991 }
2992
2993
2994 /* Sanity checks on the record number. */
2995 if ((cf & IOPARM_DT_HAS_REC) != 0)
2996 {
2997 if (dtp->rec <= 0)
2998 {
2999 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3000 "Record number must be positive");
3001 return;
3002 }
3003
3004 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
3005 {
3006 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3007 "Record number too large");
3008 return;
3009 }
3010
3011 /* Make sure format buffer is reset. */
3012 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
3013 fbuf_reset (dtp->u.p.current_unit);
3014
3015
3016 /* Check whether the record exists to be read. Only
3017 a partial record needs to exist. */
3018
3019 if (dtp->u.p.mode == READING && (dtp->rec - 1)
3020 * dtp->u.p.current_unit->recl >= ssize (dtp->u.p.current_unit->s))
3021 {
3022 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3023 "Non-existing record number");
3024 return;
3025 }
3026
3027 /* Position the file. */
3028 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
3029 * dtp->u.p.current_unit->recl, SEEK_SET) < 0)
3030 {
3031 generate_error (&dtp->common, LIBERROR_OS, NULL);
3032 return;
3033 }
3034
3035 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
3036 {
3037 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3038 "Record number not allowed for stream access "
3039 "data transfer");
3040 return;
3041 }
3042 }
3043
3044 /* Bugware for badly written mixed C-Fortran I/O. */
3045 if (!is_internal_unit (dtp))
3046 flush_if_preconnected(dtp->u.p.current_unit->s);
3047
3048 dtp->u.p.current_unit->mode = dtp->u.p.mode;
3049
3050 /* Set the maximum position reached from the previous I/O operation. This
3051 could be greater than zero from a previous non-advancing write. */
3052 dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
3053
3054 pre_position (dtp);
3055
3056
3057 /* Set up the subroutine that will handle the transfers. */
3058
3059 if (read_flag)
3060 {
3061 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
3062 dtp->u.p.transfer = unformatted_read;
3063 else
3064 {
3065 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3066 {
3067 if (dtp->u.p.current_unit->child_dtio == 0)
3068 dtp->u.p.current_unit->last_char = EOF - 1;
3069 dtp->u.p.transfer = list_formatted_read;
3070 }
3071 else
3072 dtp->u.p.transfer = formatted_transfer;
3073 }
3074 }
3075 else
3076 {
3077 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
3078 dtp->u.p.transfer = unformatted_write;
3079 else
3080 {
3081 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3082 dtp->u.p.transfer = list_formatted_write;
3083 else
3084 dtp->u.p.transfer = formatted_transfer;
3085 }
3086 }
3087
3088 /* Make sure that we don't do a read after a nonadvancing write. */
3089
3090 if (read_flag)
3091 {
3092 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
3093 {
3094 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3095 "Cannot READ after a nonadvancing WRITE");
3096 return;
3097 }
3098 }
3099 else
3100 {
3101 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
3102 dtp->u.p.current_unit->read_bad = 1;
3103 }
3104
3105 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
3106 {
3107 #ifdef HAVE_USELOCALE
3108 dtp->u.p.old_locale = uselocale (c_locale);
3109 #else
3110 __gthread_mutex_lock (&old_locale_lock);
3111 if (!old_locale_ctr++)
3112 {
3113 old_locale = setlocale (LC_NUMERIC, NULL);
3114 setlocale (LC_NUMERIC, "C");
3115 }
3116 __gthread_mutex_unlock (&old_locale_lock);
3117 #endif
3118 /* Start the data transfer if we are doing a formatted transfer. */
3119 if ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0
3120 && dtp->u.p.ionml == NULL)
3121 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
3122 }
3123 }
3124
3125
3126 /* Initialize an array_loop_spec given the array descriptor. The function
3127 returns the index of the last element of the array, and also returns
3128 starting record, where the first I/O goes to (necessary in case of
3129 negative strides). */
3130
3131 gfc_offset
3132 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
3133 gfc_offset *start_record)
3134 {
3135 int rank = GFC_DESCRIPTOR_RANK(desc);
3136 int i;
3137 gfc_offset index;
3138 int empty;
3139
3140 empty = 0;
3141 index = 1;
3142 *start_record = 0;
3143
3144 for (i=0; i<rank; i++)
3145 {
3146 ls[i].idx = GFC_DESCRIPTOR_LBOUND(desc,i);
3147 ls[i].start = GFC_DESCRIPTOR_LBOUND(desc,i);
3148 ls[i].end = GFC_DESCRIPTOR_UBOUND(desc,i);
3149 ls[i].step = GFC_DESCRIPTOR_STRIDE(desc,i);
3150 empty = empty || (GFC_DESCRIPTOR_UBOUND(desc,i)
3151 < GFC_DESCRIPTOR_LBOUND(desc,i));
3152
3153 if (GFC_DESCRIPTOR_STRIDE(desc,i) > 0)
3154 {
3155 index += (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3156 * GFC_DESCRIPTOR_STRIDE(desc,i);
3157 }
3158 else
3159 {
3160 index -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3161 * GFC_DESCRIPTOR_STRIDE(desc,i);
3162 *start_record -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3163 * GFC_DESCRIPTOR_STRIDE(desc,i);
3164 }
3165 }
3166
3167 if (empty)
3168 return 0;
3169 else
3170 return index;
3171 }
3172
3173 /* Determine the index to the next record in an internal unit array by
3174 by incrementing through the array_loop_spec. */
3175
3176 gfc_offset
3177 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
3178 {
3179 int i, carry;
3180 gfc_offset index;
3181
3182 carry = 1;
3183 index = 0;
3184
3185 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
3186 {
3187 if (carry)
3188 {
3189 ls[i].idx++;
3190 if (ls[i].idx > ls[i].end)
3191 {
3192 ls[i].idx = ls[i].start;
3193 carry = 1;
3194 }
3195 else
3196 carry = 0;
3197 }
3198 index = index + (ls[i].idx - ls[i].start) * ls[i].step;
3199 }
3200
3201 *finished = carry;
3202
3203 return index;
3204 }
3205
3206
3207
3208 /* Skip to the end of the current record, taking care of an optional
3209 record marker of size bytes. If the file is not seekable, we
3210 read chunks of size MAX_READ until we get to the right
3211 position. */
3212
3213 static void
3214 skip_record (st_parameter_dt *dtp, ssize_t bytes)
3215 {
3216 ssize_t rlength, readb;
3217 #define MAX_READ 4096
3218 char p[MAX_READ];
3219
3220 dtp->u.p.current_unit->bytes_left_subrecord += bytes;
3221 if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
3222 return;
3223
3224 /* Direct access files do not generate END conditions,
3225 only I/O errors. */
3226 if (sseek (dtp->u.p.current_unit->s,
3227 dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0)
3228 {
3229 /* Seeking failed, fall back to seeking by reading data. */
3230 while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
3231 {
3232 rlength =
3233 (MAX_READ < dtp->u.p.current_unit->bytes_left_subrecord) ?
3234 MAX_READ : dtp->u.p.current_unit->bytes_left_subrecord;
3235
3236 readb = sread (dtp->u.p.current_unit->s, p, rlength);
3237 if (readb < 0)
3238 {
3239 generate_error (&dtp->common, LIBERROR_OS, NULL);
3240 return;
3241 }
3242
3243 dtp->u.p.current_unit->bytes_left_subrecord -= readb;
3244 }
3245 return;
3246 }
3247 dtp->u.p.current_unit->bytes_left_subrecord = 0;
3248 }
3249
3250
3251 /* Advance to the next record reading unformatted files, taking
3252 care of subrecords. If complete_record is nonzero, we loop
3253 until all subrecords are cleared. */
3254
3255 static void
3256 next_record_r_unf (st_parameter_dt *dtp, int complete_record)
3257 {
3258 size_t bytes;
3259
3260 bytes = compile_options.record_marker == 0 ?
3261 sizeof (GFC_INTEGER_4) : compile_options.record_marker;
3262
3263 while(1)
3264 {
3265
3266 /* Skip over tail */
3267
3268 skip_record (dtp, bytes);
3269
3270 if ( ! (complete_record && dtp->u.p.current_unit->continued))
3271 return;
3272
3273 us_read (dtp, 1);
3274 }
3275 }
3276
3277
3278 static gfc_offset
3279 min_off (gfc_offset a, gfc_offset b)
3280 {
3281 return (a < b ? a : b);
3282 }
3283
3284
3285 /* Space to the next record for read mode. */
3286
3287 static void
3288 next_record_r (st_parameter_dt *dtp, int done)
3289 {
3290 gfc_offset record;
3291 int bytes_left;
3292 char p;
3293 int cc;
3294
3295 switch (current_mode (dtp))
3296 {
3297 /* No records in unformatted STREAM I/O. */
3298 case UNFORMATTED_STREAM:
3299 return;
3300
3301 case UNFORMATTED_SEQUENTIAL:
3302 next_record_r_unf (dtp, 1);
3303 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3304 break;
3305
3306 case FORMATTED_DIRECT:
3307 case UNFORMATTED_DIRECT:
3308 skip_record (dtp, dtp->u.p.current_unit->bytes_left);
3309 break;
3310
3311 case FORMATTED_STREAM:
3312 case FORMATTED_SEQUENTIAL:
3313 /* read_sf has already terminated input because of an '\n', or
3314 we have hit EOF. */
3315 if (dtp->u.p.sf_seen_eor)
3316 {
3317 dtp->u.p.sf_seen_eor = 0;
3318 break;
3319 }
3320
3321 if (is_internal_unit (dtp))
3322 {
3323 if (is_array_io (dtp))
3324 {
3325 int finished;
3326
3327 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3328 &finished);
3329 if (!done && finished)
3330 hit_eof (dtp);
3331
3332 /* Now seek to this record. */
3333 record = record * dtp->u.p.current_unit->recl;
3334 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3335 {
3336 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3337 break;
3338 }
3339 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3340 }
3341 else
3342 {
3343 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
3344 bytes_left = min_off (bytes_left,
3345 ssize (dtp->u.p.current_unit->s)
3346 - stell (dtp->u.p.current_unit->s));
3347 if (sseek (dtp->u.p.current_unit->s,
3348 bytes_left, SEEK_CUR) < 0)
3349 {
3350 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3351 break;
3352 }
3353 dtp->u.p.current_unit->bytes_left
3354 = dtp->u.p.current_unit->recl;
3355 }
3356 break;
3357 }
3358 else if (dtp->u.p.current_unit->flags.cc != CC_NONE)
3359 {
3360 do
3361 {
3362 errno = 0;
3363 cc = fbuf_getc (dtp->u.p.current_unit);
3364 if (cc == EOF)
3365 {
3366 if (errno != 0)
3367 generate_error (&dtp->common, LIBERROR_OS, NULL);
3368 else
3369 {
3370 if (is_stream_io (dtp)
3371 || dtp->u.p.current_unit->pad_status == PAD_NO
3372 || dtp->u.p.current_unit->bytes_left
3373 == dtp->u.p.current_unit->recl)
3374 hit_eof (dtp);
3375 }
3376 break;
3377 }
3378
3379 if (is_stream_io (dtp))
3380 dtp->u.p.current_unit->strm_pos++;
3381
3382 p = (char) cc;
3383 }
3384 while (p != '\n');
3385 }
3386 break;
3387 }
3388 }
3389
3390
3391 /* Small utility function to write a record marker, taking care of
3392 byte swapping and of choosing the correct size. */
3393
3394 static int
3395 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
3396 {
3397 size_t len;
3398 GFC_INTEGER_4 buf4;
3399 GFC_INTEGER_8 buf8;
3400
3401 if (compile_options.record_marker == 0)
3402 len = sizeof (GFC_INTEGER_4);
3403 else
3404 len = compile_options.record_marker;
3405
3406 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
3407 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
3408 {
3409 switch (len)
3410 {
3411 case sizeof (GFC_INTEGER_4):
3412 buf4 = buf;
3413 return swrite (dtp->u.p.current_unit->s, &buf4, len);
3414 break;
3415
3416 case sizeof (GFC_INTEGER_8):
3417 buf8 = buf;
3418 return swrite (dtp->u.p.current_unit->s, &buf8, len);
3419 break;
3420
3421 default:
3422 runtime_error ("Illegal value for record marker");
3423 break;
3424 }
3425 }
3426 else
3427 {
3428 uint32_t u32;
3429 uint64_t u64;
3430 switch (len)
3431 {
3432 case sizeof (GFC_INTEGER_4):
3433 buf4 = buf;
3434 memcpy (&u32, &buf4, sizeof (u32));
3435 u32 = __builtin_bswap32 (u32);
3436 return swrite (dtp->u.p.current_unit->s, &u32, len);
3437 break;
3438
3439 case sizeof (GFC_INTEGER_8):
3440 buf8 = buf;
3441 memcpy (&u64, &buf8, sizeof (u64));
3442 u64 = __builtin_bswap64 (u64);
3443 return swrite (dtp->u.p.current_unit->s, &u64, len);
3444 break;
3445
3446 default:
3447 runtime_error ("Illegal value for record marker");
3448 break;
3449 }
3450 }
3451
3452 }
3453
3454 /* Position to the next (sub)record in write mode for
3455 unformatted sequential files. */
3456
3457 static void
3458 next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
3459 {
3460 gfc_offset m, m_write, record_marker;
3461
3462 /* Bytes written. */
3463 m = dtp->u.p.current_unit->recl_subrecord
3464 - dtp->u.p.current_unit->bytes_left_subrecord;
3465
3466 if (compile_options.record_marker == 0)
3467 record_marker = sizeof (GFC_INTEGER_4);
3468 else
3469 record_marker = compile_options.record_marker;
3470
3471 /* Seek to the head and overwrite the bogus length with the real
3472 length. */
3473
3474 if (unlikely (sseek (dtp->u.p.current_unit->s, - m - record_marker,
3475 SEEK_CUR) < 0))
3476 goto io_error;
3477
3478 if (next_subrecord)
3479 m_write = -m;
3480 else
3481 m_write = m;
3482
3483 if (unlikely (write_us_marker (dtp, m_write) < 0))
3484 goto io_error;
3485
3486 /* Seek past the end of the current record. */
3487
3488 if (unlikely (sseek (dtp->u.p.current_unit->s, m, SEEK_CUR) < 0))
3489 goto io_error;
3490
3491 /* Write the length tail. If we finish a record containing
3492 subrecords, we write out the negative length. */
3493
3494 if (dtp->u.p.current_unit->continued)
3495 m_write = -m;
3496 else
3497 m_write = m;
3498
3499 if (unlikely (write_us_marker (dtp, m_write) < 0))
3500 goto io_error;
3501
3502 return;
3503
3504 io_error:
3505 generate_error (&dtp->common, LIBERROR_OS, NULL);
3506 return;
3507
3508 }
3509
3510
3511 /* Utility function like memset() but operating on streams. Return
3512 value is same as for POSIX write(). */
3513
3514 static ssize_t
3515 sset (stream * s, int c, ssize_t nbyte)
3516 {
3517 #define WRITE_CHUNK 256
3518 char p[WRITE_CHUNK];
3519 ssize_t bytes_left, trans;
3520
3521 if (nbyte < WRITE_CHUNK)
3522 memset (p, c, nbyte);
3523 else
3524 memset (p, c, WRITE_CHUNK);
3525
3526 bytes_left = nbyte;
3527 while (bytes_left > 0)
3528 {
3529 trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK;
3530 trans = swrite (s, p, trans);
3531 if (trans <= 0)
3532 return trans;
3533 bytes_left -= trans;
3534 }
3535
3536 return nbyte - bytes_left;
3537 }
3538
3539
3540 /* Finish up a record according to the legacy carriagecontrol type, based
3541 on the first character in the record. */
3542
3543 static void
3544 next_record_cc (st_parameter_dt *dtp)
3545 {
3546 /* Only valid with CARRIAGECONTROL=FORTRAN. */
3547 if (dtp->u.p.current_unit->flags.cc != CC_FORTRAN)
3548 return;
3549
3550 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3551 if (dtp->u.p.cc.len > 0)
3552 {
3553 char * p = fbuf_alloc (dtp->u.p.current_unit, dtp->u.p.cc.len);
3554 if (!p)
3555 generate_error (&dtp->common, LIBERROR_OS, NULL);
3556
3557 /* Output CR for the first character with default CC setting. */
3558 *(p++) = dtp->u.p.cc.u.end;
3559 if (dtp->u.p.cc.len > 1)
3560 *p = dtp->u.p.cc.u.end;
3561 }
3562 }
3563
3564 /* Position to the next record in write mode. */
3565
3566 static void
3567 next_record_w (st_parameter_dt *dtp, int done)
3568 {
3569 gfc_offset m, record, max_pos;
3570 int length;
3571
3572 /* Zero counters for X- and T-editing. */
3573 max_pos = dtp->u.p.max_pos;
3574 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
3575
3576 switch (current_mode (dtp))
3577 {
3578 /* No records in unformatted STREAM I/O. */
3579 case UNFORMATTED_STREAM:
3580 return;
3581
3582 case FORMATTED_DIRECT:
3583 if (dtp->u.p.current_unit->bytes_left == 0)
3584 break;
3585
3586 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3587 fbuf_flush (dtp->u.p.current_unit, WRITING);
3588 if (sset (dtp->u.p.current_unit->s, ' ',
3589 dtp->u.p.current_unit->bytes_left)
3590 != dtp->u.p.current_unit->bytes_left)
3591 goto io_error;
3592
3593 break;
3594
3595 case UNFORMATTED_DIRECT:
3596 if (dtp->u.p.current_unit->bytes_left > 0)
3597 {
3598 length = (int) dtp->u.p.current_unit->bytes_left;
3599 if (sset (dtp->u.p.current_unit->s, 0, length) != length)
3600 goto io_error;
3601 }
3602 break;
3603
3604 case UNFORMATTED_SEQUENTIAL:
3605 next_record_w_unf (dtp, 0);
3606 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3607 break;
3608
3609 case FORMATTED_STREAM:
3610 case FORMATTED_SEQUENTIAL:
3611
3612 if (is_internal_unit (dtp))
3613 {
3614 char *p;
3615 if (is_array_io (dtp))
3616 {
3617 int finished;
3618
3619 length = (int) dtp->u.p.current_unit->bytes_left;
3620
3621 /* If the farthest position reached is greater than current
3622 position, adjust the position and set length to pad out
3623 whats left. Otherwise just pad whats left.
3624 (for character array unit) */
3625 m = dtp->u.p.current_unit->recl
3626 - dtp->u.p.current_unit->bytes_left;
3627 if (max_pos > m)
3628 {
3629 length = (int) (max_pos - m);
3630 if (sseek (dtp->u.p.current_unit->s,
3631 length, SEEK_CUR) < 0)
3632 {
3633 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3634 return;
3635 }
3636 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3637 }
3638
3639 p = write_block (dtp, length);
3640 if (p == NULL)
3641 return;
3642
3643 if (unlikely (is_char4_unit (dtp)))
3644 {
3645 gfc_char4_t *p4 = (gfc_char4_t *) p;
3646 memset4 (p4, ' ', length);
3647 }
3648 else
3649 memset (p, ' ', length);
3650
3651 /* Now that the current record has been padded out,
3652 determine where the next record in the array is. */
3653 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3654 &finished);
3655 if (finished)
3656 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3657
3658 /* Now seek to this record */
3659 record = record * dtp->u.p.current_unit->recl;
3660
3661 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3662 {
3663 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3664 return;
3665 }
3666
3667 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3668 }
3669 else
3670 {
3671 length = 1;
3672
3673 /* If this is the last call to next_record move to the farthest
3674 position reached and set length to pad out the remainder
3675 of the record. (for character scaler unit) */
3676 if (done)
3677 {
3678 m = dtp->u.p.current_unit->recl
3679 - dtp->u.p.current_unit->bytes_left;
3680 if (max_pos > m)
3681 {
3682 length = (int) (max_pos - m);
3683 if (sseek (dtp->u.p.current_unit->s,
3684 length, SEEK_CUR) < 0)
3685 {
3686 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3687 return;
3688 }
3689 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3690 }
3691 else
3692 length = (int) dtp->u.p.current_unit->bytes_left;
3693 }
3694 if (length > 0)
3695 {
3696 p = write_block (dtp, length);
3697 if (p == NULL)
3698 return;
3699
3700 if (unlikely (is_char4_unit (dtp)))
3701 {
3702 gfc_char4_t *p4 = (gfc_char4_t *) p;
3703 memset4 (p4, (gfc_char4_t) ' ', length);
3704 }
3705 else
3706 memset (p, ' ', length);
3707 }
3708 }
3709 }
3710 /* Handle legacy CARRIAGECONTROL line endings. */
3711 else if (dtp->u.p.current_unit->flags.cc == CC_FORTRAN)
3712 next_record_cc (dtp);
3713 else
3714 {
3715 /* Skip newlines for CC=CC_NONE. */
3716 const int len = (dtp->u.p.current_unit->flags.cc == CC_NONE)
3717 ? 0
3718 #ifdef HAVE_CRLF
3719 : 2;
3720 #else
3721 : 1;
3722 #endif
3723 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3724 if (dtp->u.p.current_unit->flags.cc != CC_NONE)
3725 {
3726 char * p = fbuf_alloc (dtp->u.p.current_unit, len);
3727 if (!p)
3728 goto io_error;
3729 #ifdef HAVE_CRLF
3730 *(p++) = '\r';
3731 #endif
3732 *p = '\n';
3733 }
3734 if (is_stream_io (dtp))
3735 {
3736 dtp->u.p.current_unit->strm_pos += len;
3737 if (dtp->u.p.current_unit->strm_pos
3738 < ssize (dtp->u.p.current_unit->s))
3739 unit_truncate (dtp->u.p.current_unit,
3740 dtp->u.p.current_unit->strm_pos - 1,
3741 &dtp->common);
3742 }
3743 }
3744
3745 break;
3746
3747 io_error:
3748 generate_error (&dtp->common, LIBERROR_OS, NULL);
3749 break;
3750 }
3751 }
3752
3753 /* Position to the next record, which means moving to the end of the
3754 current record. This can happen under several different
3755 conditions. If the done flag is not set, we get ready to process
3756 the next record. */
3757
3758 void
3759 next_record (st_parameter_dt *dtp, int done)
3760 {
3761 gfc_offset fp; /* File position. */
3762
3763 dtp->u.p.current_unit->read_bad = 0;
3764
3765 if (dtp->u.p.mode == READING)
3766 next_record_r (dtp, done);
3767 else
3768 next_record_w (dtp, done);
3769
3770 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3771
3772 if (!is_stream_io (dtp))
3773 {
3774 /* Since we have changed the position, set it to unspecified so
3775 that INQUIRE(POSITION=) knows it needs to look into it. */
3776 if (done)
3777 dtp->u.p.current_unit->flags.position = POSITION_UNSPECIFIED;
3778
3779 dtp->u.p.current_unit->current_record = 0;
3780 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
3781 {
3782 fp = stell (dtp->u.p.current_unit->s);
3783 /* Calculate next record, rounding up partial records. */
3784 dtp->u.p.current_unit->last_record =
3785 (fp + dtp->u.p.current_unit->recl) /
3786 dtp->u.p.current_unit->recl - 1;
3787 }
3788 else
3789 dtp->u.p.current_unit->last_record++;
3790 }
3791
3792 if (!done)
3793 pre_position (dtp);
3794
3795 smarkeor (dtp->u.p.current_unit->s);
3796 }
3797
3798
3799 /* Finalize the current data transfer. For a nonadvancing transfer,
3800 this means advancing to the next record. For internal units close the
3801 stream associated with the unit. */
3802
3803 static void
3804 finalize_transfer (st_parameter_dt *dtp)
3805 {
3806 GFC_INTEGER_4 cf = dtp->common.flags;
3807
3808 if ((dtp->u.p.ionml != NULL)
3809 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
3810 {
3811 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
3812 namelist_read (dtp);
3813 else
3814 namelist_write (dtp);
3815 }
3816
3817 if (dtp->u.p.current_unit && (dtp->u.p.current_unit->child_dtio > 0))
3818 {
3819 if (cf & IOPARM_DT_HAS_FORMAT)
3820 {
3821 free (dtp->u.p.fmt);
3822 free (dtp->format);
3823 }
3824 return;
3825 }
3826
3827 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
3828 *dtp->size = dtp->u.p.current_unit->size_used;
3829
3830 if (dtp->u.p.eor_condition)
3831 {
3832 generate_error (&dtp->common, LIBERROR_EOR, NULL);
3833 goto done;
3834 }
3835
3836 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
3837 {
3838 if (dtp->u.p.current_unit && current_mode (dtp) == UNFORMATTED_SEQUENTIAL)
3839 dtp->u.p.current_unit->current_record = 0;
3840 goto done;
3841 }
3842
3843 dtp->u.p.transfer = NULL;
3844 if (dtp->u.p.current_unit == NULL)
3845 goto done;
3846
3847 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
3848 {
3849 finish_list_read (dtp);
3850 goto done;
3851 }
3852
3853 if (dtp->u.p.mode == WRITING)
3854 dtp->u.p.current_unit->previous_nonadvancing_write
3855 = dtp->u.p.advance_status == ADVANCE_NO;
3856
3857 if (is_stream_io (dtp))
3858 {
3859 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3860 && dtp->u.p.advance_status != ADVANCE_NO)
3861 next_record (dtp, 1);
3862
3863 goto done;
3864 }
3865
3866 dtp->u.p.current_unit->current_record = 0;
3867
3868 if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
3869 {
3870 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3871 dtp->u.p.seen_dollar = 0;
3872 goto done;
3873 }
3874
3875 /* For non-advancing I/O, save the current maximum position for use in the
3876 next I/O operation if needed. */
3877 if (dtp->u.p.advance_status == ADVANCE_NO)
3878 {
3879 if (dtp->u.p.skips > 0)
3880 {
3881 int tmp;
3882 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
3883 tmp = (int)(dtp->u.p.current_unit->recl
3884 - dtp->u.p.current_unit->bytes_left);
3885 dtp->u.p.max_pos =
3886 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
3887 dtp->u.p.skips = 0;
3888 }
3889 int bytes_written = (int) (dtp->u.p.current_unit->recl
3890 - dtp->u.p.current_unit->bytes_left);
3891 dtp->u.p.current_unit->saved_pos =
3892 dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
3893 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3894 goto done;
3895 }
3896 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3897 && dtp->u.p.mode == WRITING && !is_internal_unit (dtp))
3898 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3899
3900 dtp->u.p.current_unit->saved_pos = 0;
3901
3902 next_record (dtp, 1);
3903
3904 done:
3905 #ifdef HAVE_USELOCALE
3906 if (dtp->u.p.old_locale != (locale_t) 0)
3907 {
3908 uselocale (dtp->u.p.old_locale);
3909 dtp->u.p.old_locale = (locale_t) 0;
3910 }
3911 #else
3912 __gthread_mutex_lock (&old_locale_lock);
3913 if (!--old_locale_ctr)
3914 {
3915 setlocale (LC_NUMERIC, old_locale);
3916 old_locale = NULL;
3917 }
3918 __gthread_mutex_unlock (&old_locale_lock);
3919 #endif
3920 }
3921
3922 /* Transfer function for IOLENGTH. It doesn't actually do any
3923 data transfer, it just updates the length counter. */
3924
3925 static void
3926 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
3927 void *dest __attribute__ ((unused)),
3928 int kind __attribute__((unused)),
3929 size_t size, size_t nelems)
3930 {
3931 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3932 *dtp->iolength += (GFC_IO_INT) (size * nelems);
3933 }
3934
3935
3936 /* Initialize the IOLENGTH data transfer. This function is in essence
3937 a very much simplified version of data_transfer_init(), because it
3938 doesn't have to deal with units at all. */
3939
3940 static void
3941 iolength_transfer_init (st_parameter_dt *dtp)
3942 {
3943 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3944 *dtp->iolength = 0;
3945
3946 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
3947
3948 /* Set up the subroutine that will handle the transfers. */
3949
3950 dtp->u.p.transfer = iolength_transfer;
3951 }
3952
3953
3954 /* Library entry point for the IOLENGTH form of the INQUIRE
3955 statement. The IOLENGTH form requires no I/O to be performed, but
3956 it must still be a runtime library call so that we can determine
3957 the iolength for dynamic arrays and such. */
3958
3959 extern void st_iolength (st_parameter_dt *);
3960 export_proto(st_iolength);
3961
3962 void
3963 st_iolength (st_parameter_dt *dtp)
3964 {
3965 library_start (&dtp->common);
3966 iolength_transfer_init (dtp);
3967 }
3968
3969 extern void st_iolength_done (st_parameter_dt *);
3970 export_proto(st_iolength_done);
3971
3972 void
3973 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
3974 {
3975 free_ionml (dtp);
3976 library_end ();
3977 }
3978
3979
3980 /* The READ statement. */
3981
3982 extern void st_read (st_parameter_dt *);
3983 export_proto(st_read);
3984
3985 void
3986 st_read (st_parameter_dt *dtp)
3987 {
3988 library_start (&dtp->common);
3989
3990 data_transfer_init (dtp, 1);
3991 }
3992
3993 extern void st_read_done (st_parameter_dt *);
3994 export_proto(st_read_done);
3995
3996 void
3997 st_read_done (st_parameter_dt *dtp)
3998 {
3999 finalize_transfer (dtp);
4000
4001 free_ionml (dtp);
4002
4003 /* If this is a parent READ statement we do not need to retain the
4004 internal unit structure for child use. Free it and stash the unit
4005 number for reuse. */
4006 if (dtp->u.p.current_unit != NULL
4007 && dtp->u.p.current_unit->child_dtio == 0)
4008 {
4009 if (is_internal_unit (dtp) &&
4010 (dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0)
4011 {
4012 free (dtp->u.p.current_unit->filename);
4013 dtp->u.p.current_unit->filename = NULL;
4014 free (dtp->u.p.current_unit->s);
4015 dtp->u.p.current_unit->s = NULL;
4016 if (dtp->u.p.current_unit->ls)
4017 free (dtp->u.p.current_unit->ls);
4018 dtp->u.p.current_unit->ls = NULL;
4019 stash_internal_unit (dtp);
4020 }
4021 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
4022 {
4023 free_format_data (dtp->u.p.fmt);
4024 free_format (dtp);
4025 }
4026 unlock_unit (dtp->u.p.current_unit);
4027 }
4028
4029 library_end ();
4030 }
4031
4032 extern void st_write (st_parameter_dt *);
4033 export_proto(st_write);
4034
4035 void
4036 st_write (st_parameter_dt *dtp)
4037 {
4038 library_start (&dtp->common);
4039 data_transfer_init (dtp, 0);
4040 }
4041
4042 extern void st_write_done (st_parameter_dt *);
4043 export_proto(st_write_done);
4044
4045 void
4046 st_write_done (st_parameter_dt *dtp)
4047 {
4048 finalize_transfer (dtp);
4049
4050 if (dtp->u.p.current_unit != NULL
4051 && dtp->u.p.current_unit->child_dtio == 0)
4052 {
4053 /* Deal with endfile conditions associated with sequential files. */
4054 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
4055 switch (dtp->u.p.current_unit->endfile)
4056 {
4057 case AT_ENDFILE: /* Remain at the endfile record. */
4058 break;
4059
4060 case AFTER_ENDFILE:
4061 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
4062 break;
4063
4064 case NO_ENDFILE:
4065 /* Get rid of whatever is after this record. */
4066 if (!is_internal_unit (dtp))
4067 unit_truncate (dtp->u.p.current_unit,
4068 stell (dtp->u.p.current_unit->s),
4069 &dtp->common);
4070 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4071 break;
4072 }
4073
4074 free_ionml (dtp);
4075
4076 /* If this is a parent WRITE statement we do not need to retain the
4077 internal unit structure for child use. Free it and stash the
4078 unit number for reuse. */
4079 if (is_internal_unit (dtp) &&
4080 (dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0)
4081 {
4082 free (dtp->u.p.current_unit->filename);
4083 dtp->u.p.current_unit->filename = NULL;
4084 free (dtp->u.p.current_unit->s);
4085 dtp->u.p.current_unit->s = NULL;
4086 if (dtp->u.p.current_unit->ls)
4087 free (dtp->u.p.current_unit->ls);
4088 dtp->u.p.current_unit->ls = NULL;
4089 stash_internal_unit (dtp);
4090 }
4091 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
4092 {
4093 free_format_data (dtp->u.p.fmt);
4094 free_format (dtp);
4095 }
4096 unlock_unit (dtp->u.p.current_unit);
4097 }
4098 library_end ();
4099 }
4100
4101
4102 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
4103 void
4104 st_wait (st_parameter_wait *wtp __attribute__((unused)))
4105 {
4106 }
4107
4108
4109 /* Receives the scalar information for namelist objects and stores it
4110 in a linked list of namelist_info types. */
4111
4112 static void
4113 set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4114 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4115 GFC_INTEGER_4 dtype, void *dtio_sub, void *vtable)
4116 {
4117 namelist_info *t1 = NULL;
4118 namelist_info *nml;
4119 size_t var_name_len = strlen (var_name);
4120
4121 nml = (namelist_info*) xmalloc (sizeof (namelist_info));
4122
4123 nml->mem_pos = var_addr;
4124 nml->dtio_sub = dtio_sub;
4125 nml->vtable = vtable;
4126
4127 nml->var_name = (char*) xmalloc (var_name_len + 1);
4128 memcpy (nml->var_name, var_name, var_name_len);
4129 nml->var_name[var_name_len] = '\0';
4130
4131 nml->len = (int) len;
4132 nml->string_length = (index_type) string_length;
4133
4134 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
4135 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
4136 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
4137
4138 if (nml->var_rank > 0)
4139 {
4140 nml->dim = (descriptor_dimension*)
4141 xmallocarray (nml->var_rank, sizeof (descriptor_dimension));
4142 nml->ls = (array_loop_spec*)
4143 xmallocarray (nml->var_rank, sizeof (array_loop_spec));
4144 }
4145 else
4146 {
4147 nml->dim = NULL;
4148 nml->ls = NULL;
4149 }
4150
4151 nml->next = NULL;
4152
4153 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
4154 {
4155 dtp->common.flags |= IOPARM_DT_IONML_SET;
4156 dtp->u.p.ionml = nml;
4157 }
4158 else
4159 {
4160 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
4161 t1->next = nml;
4162 }
4163 }
4164
4165 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
4166 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
4167 export_proto(st_set_nml_var);
4168
4169 void
4170 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4171 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4172 GFC_INTEGER_4 dtype)
4173 {
4174 set_nml_var (dtp, var_addr, var_name, len, string_length,
4175 dtype, NULL, NULL);
4176 }
4177
4178
4179 /* Essentially the same as previous but carrying the dtio procedure
4180 and the vtable as additional arguments. */
4181 extern void st_set_nml_dtio_var (st_parameter_dt *dtp, void *, char *,
4182 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4,
4183 void *, void *);
4184 export_proto(st_set_nml_dtio_var);
4185
4186
4187 void
4188 st_set_nml_dtio_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4189 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4190 GFC_INTEGER_4 dtype, void *dtio_sub, void *vtable)
4191 {
4192 set_nml_var (dtp, var_addr, var_name, len, string_length,
4193 dtype, dtio_sub, vtable);
4194 }
4195
4196 /* Store the dimensional information for the namelist object. */
4197 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
4198 index_type, index_type,
4199 index_type);
4200 export_proto(st_set_nml_var_dim);
4201
4202 void
4203 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
4204 index_type stride, index_type lbound,
4205 index_type ubound)
4206 {
4207 namelist_info * nml;
4208 int n;
4209
4210 n = (int)n_dim;
4211
4212 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
4213
4214 GFC_DIMENSION_SET(nml->dim[n],lbound,ubound,stride);
4215 }
4216
4217
4218 /* Once upon a time, a poor innocent Fortran program was reading a
4219 file, when suddenly it hit the end-of-file (EOF). Unfortunately
4220 the OS doesn't tell whether we're at the EOF or whether we already
4221 went past it. Luckily our hero, libgfortran, keeps track of this.
4222 Call this function when you detect an EOF condition. See Section
4223 9.10.2 in F2003. */
4224
4225 void
4226 hit_eof (st_parameter_dt * dtp)
4227 {
4228 dtp->u.p.current_unit->flags.position = POSITION_APPEND;
4229
4230 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
4231 switch (dtp->u.p.current_unit->endfile)
4232 {
4233 case NO_ENDFILE:
4234 case AT_ENDFILE:
4235 generate_error (&dtp->common, LIBERROR_END, NULL);
4236 if (!is_internal_unit (dtp) && !dtp->u.p.namelist_mode)
4237 {
4238 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
4239 dtp->u.p.current_unit->current_record = 0;
4240 }
4241 else
4242 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4243 break;
4244
4245 case AFTER_ENDFILE:
4246 generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
4247 dtp->u.p.current_unit->current_record = 0;
4248 break;
4249 }
4250 else
4251 {
4252 /* Non-sequential files don't have an ENDFILE record, so we
4253 can't be at AFTER_ENDFILE. */
4254 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4255 generate_error (&dtp->common, LIBERROR_END, NULL);
4256 dtp->u.p.current_unit->current_record = 0;
4257 }
4258 }