Default missing exponents to 0 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 if (dtp->u.p.pending_spaces == 0)
1583 dtp->u.p.sf_seen_eor = 0;
1584 }
1585 if (dtp->u.p.skips < 0)
1586 {
1587 if (is_internal_unit (dtp))
1588 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1589 else
1590 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1591 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1592 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1593 }
1594 else
1595 read_x (dtp, dtp->u.p.skips);
1596 break;
1597
1598 case FMT_S:
1599 consume_data_flag = 0;
1600 dtp->u.p.sign_status = SIGN_S;
1601 break;
1602
1603 case FMT_SS:
1604 consume_data_flag = 0;
1605 dtp->u.p.sign_status = SIGN_SS;
1606 break;
1607
1608 case FMT_SP:
1609 consume_data_flag = 0;
1610 dtp->u.p.sign_status = SIGN_SP;
1611 break;
1612
1613 case FMT_BN:
1614 consume_data_flag = 0 ;
1615 dtp->u.p.blank_status = BLANK_NULL;
1616 break;
1617
1618 case FMT_BZ:
1619 consume_data_flag = 0;
1620 dtp->u.p.blank_status = BLANK_ZERO;
1621 break;
1622
1623 case FMT_DC:
1624 consume_data_flag = 0;
1625 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1626 break;
1627
1628 case FMT_DP:
1629 consume_data_flag = 0;
1630 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1631 break;
1632
1633 case FMT_RC:
1634 consume_data_flag = 0;
1635 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
1636 break;
1637
1638 case FMT_RD:
1639 consume_data_flag = 0;
1640 dtp->u.p.current_unit->round_status = ROUND_DOWN;
1641 break;
1642
1643 case FMT_RN:
1644 consume_data_flag = 0;
1645 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
1646 break;
1647
1648 case FMT_RP:
1649 consume_data_flag = 0;
1650 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
1651 break;
1652
1653 case FMT_RU:
1654 consume_data_flag = 0;
1655 dtp->u.p.current_unit->round_status = ROUND_UP;
1656 break;
1657
1658 case FMT_RZ:
1659 consume_data_flag = 0;
1660 dtp->u.p.current_unit->round_status = ROUND_ZERO;
1661 break;
1662
1663 case FMT_P:
1664 consume_data_flag = 0;
1665 dtp->u.p.scale_factor = f->u.k;
1666 break;
1667
1668 case FMT_DOLLAR:
1669 consume_data_flag = 0;
1670 dtp->u.p.seen_dollar = 1;
1671 break;
1672
1673 case FMT_SLASH:
1674 consume_data_flag = 0;
1675 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1676 next_record (dtp, 0);
1677 break;
1678
1679 case FMT_COLON:
1680 /* A colon descriptor causes us to exit this loop (in
1681 particular preventing another / descriptor from being
1682 processed) unless there is another data item to be
1683 transferred. */
1684 consume_data_flag = 0;
1685 if (n == 0)
1686 return;
1687 break;
1688
1689 default:
1690 internal_error (&dtp->common, "Bad format node");
1691 }
1692
1693 /* Adjust the item count and data pointer. */
1694
1695 if ((consume_data_flag > 0) && (n > 0))
1696 {
1697 n--;
1698 p = ((char *) p) + size;
1699 }
1700
1701 dtp->u.p.skips = 0;
1702
1703 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1704 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1705 }
1706
1707 return;
1708
1709 /* Come here when we need a data descriptor but don't have one. We
1710 push the current format node back onto the input, then return and
1711 let the user program call us back with the data. */
1712 need_read_data:
1713 unget_format (dtp, f);
1714 }
1715
1716
1717 static void
1718 formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kind,
1719 size_t size)
1720 {
1721 int pos, bytes_used;
1722 const fnode *f;
1723 format_token t;
1724 int n;
1725 int consume_data_flag;
1726
1727 /* Change a complex data item into a pair of reals. */
1728
1729 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1730 if (type == BT_COMPLEX)
1731 {
1732 type = BT_REAL;
1733 size /= 2;
1734 }
1735
1736 /* If there's an EOR condition, we simulate finalizing the transfer
1737 by doing nothing. */
1738 if (dtp->u.p.eor_condition)
1739 return;
1740
1741 /* Set this flag so that commas in reads cause the read to complete before
1742 the entire field has been read. The next read field will start right after
1743 the comma in the stream. (Set to 0 for character reads). */
1744 dtp->u.p.sf_read_comma =
1745 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1746
1747 for (;;)
1748 {
1749 /* If reversion has occurred and there is another real data item,
1750 then we have to move to the next record. */
1751 if (dtp->u.p.reversion_flag && n > 0)
1752 {
1753 dtp->u.p.reversion_flag = 0;
1754 next_record (dtp, 0);
1755 }
1756
1757 consume_data_flag = 1;
1758 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1759 break;
1760
1761 f = next_format (dtp);
1762 if (f == NULL)
1763 {
1764 /* No data descriptors left. */
1765 if (unlikely (n > 0))
1766 generate_error (&dtp->common, LIBERROR_FORMAT,
1767 "Insufficient data descriptors in format after reversion");
1768 return;
1769 }
1770
1771 /* Now discharge T, TR and X movements to the right. This is delayed
1772 until a data producing format to suppress trailing spaces. */
1773
1774 t = f->format;
1775 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
1776 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
1777 || t == FMT_Z || t == FMT_F || t == FMT_E
1778 || t == FMT_EN || t == FMT_ES || t == FMT_G
1779 || t == FMT_L || t == FMT_A || t == FMT_D
1780 || t == FMT_DT))
1781 || t == FMT_STRING))
1782 {
1783 if (dtp->u.p.skips > 0)
1784 {
1785 int tmp;
1786 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1787 tmp = (int)(dtp->u.p.current_unit->recl
1788 - dtp->u.p.current_unit->bytes_left);
1789 dtp->u.p.max_pos =
1790 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
1791 dtp->u.p.skips = 0;
1792 }
1793 if (dtp->u.p.skips < 0)
1794 {
1795 if (is_internal_unit (dtp))
1796 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1797 else
1798 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1799 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1800 }
1801 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1802 }
1803
1804 bytes_used = (int)(dtp->u.p.current_unit->recl
1805 - dtp->u.p.current_unit->bytes_left);
1806
1807 if (is_stream_io(dtp))
1808 bytes_used = 0;
1809
1810 switch (t)
1811 {
1812 case FMT_I:
1813 if (n == 0)
1814 goto need_data;
1815 if (require_type (dtp, BT_INTEGER, type, f))
1816 return;
1817 write_i (dtp, f, p, kind);
1818 break;
1819
1820 case FMT_B:
1821 if (n == 0)
1822 goto need_data;
1823 if (!(compile_options.allow_std & GFC_STD_GNU)
1824 && require_numeric_type (dtp, type, f))
1825 return;
1826 if (!(compile_options.allow_std & GFC_STD_F2008)
1827 && require_type (dtp, BT_INTEGER, type, f))
1828 return;
1829 write_b (dtp, f, p, kind);
1830 break;
1831
1832 case FMT_O:
1833 if (n == 0)
1834 goto need_data;
1835 if (!(compile_options.allow_std & GFC_STD_GNU)
1836 && require_numeric_type (dtp, type, f))
1837 return;
1838 if (!(compile_options.allow_std & GFC_STD_F2008)
1839 && require_type (dtp, BT_INTEGER, type, f))
1840 return;
1841 write_o (dtp, f, p, kind);
1842 break;
1843
1844 case FMT_Z:
1845 if (n == 0)
1846 goto need_data;
1847 if (!(compile_options.allow_std & GFC_STD_GNU)
1848 && require_numeric_type (dtp, type, f))
1849 return;
1850 if (!(compile_options.allow_std & GFC_STD_F2008)
1851 && require_type (dtp, BT_INTEGER, type, f))
1852 return;
1853 write_z (dtp, f, p, kind);
1854 break;
1855
1856 case FMT_A:
1857 if (n == 0)
1858 goto need_data;
1859
1860 /* It is possible to have FMT_A with something not BT_CHARACTER such
1861 as when writing out hollerith strings, so check both type
1862 and kind before calling wide character routines. */
1863 if (type == BT_CHARACTER && kind == 4)
1864 write_a_char4 (dtp, f, p, size);
1865 else
1866 write_a (dtp, f, p, size);
1867 break;
1868
1869 case FMT_L:
1870 if (n == 0)
1871 goto need_data;
1872 write_l (dtp, f, p, kind);
1873 break;
1874
1875 case FMT_D:
1876 if (n == 0)
1877 goto need_data;
1878 if (require_type (dtp, BT_REAL, type, f))
1879 return;
1880 write_d (dtp, f, p, kind);
1881 break;
1882
1883 case FMT_DT:
1884 if (n == 0)
1885 goto need_data;
1886 int unit = dtp->u.p.current_unit->unit_number;
1887 char dt[] = "DT";
1888 char tmp_iomsg[IOMSG_LEN] = "";
1889 char *child_iomsg;
1890 gfc_charlen_type child_iomsg_len;
1891 int noiostat;
1892 int *child_iostat = NULL;
1893 char *iotype = f->u.udf.string;
1894 gfc_charlen_type iotype_len = f->u.udf.string_len;
1895
1896 /* Build the iotype string. */
1897 if (iotype_len == 0)
1898 {
1899 iotype_len = 2;
1900 iotype = dt;
1901 }
1902 else
1903 {
1904 iotype_len += 2;
1905 iotype = xmalloc (iotype_len);
1906 iotype[0] = dt[0];
1907 iotype[1] = dt[1];
1908 memcpy (iotype + 2, f->u.udf.string, f->u.udf.string_len);
1909 }
1910
1911 /* Set iostat, intent(out). */
1912 noiostat = 0;
1913 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1914 dtp->common.iostat : &noiostat;
1915
1916 /* Set iomsg, intent(inout). */
1917 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1918 {
1919 child_iomsg = dtp->common.iomsg;
1920 child_iomsg_len = dtp->common.iomsg_len;
1921 }
1922 else
1923 {
1924 child_iomsg = tmp_iomsg;
1925 child_iomsg_len = IOMSG_LEN;
1926 }
1927
1928 /* Call the user defined formatted WRITE procedure. */
1929 dtp->u.p.current_unit->child_dtio++;
1930 dtp->u.p.fdtio_ptr (p, &unit, iotype, f->u.udf.vlist,
1931 child_iostat, child_iomsg,
1932 iotype_len, child_iomsg_len);
1933 dtp->u.p.current_unit->child_dtio--;
1934
1935 if (f->u.udf.string_len != 0)
1936 free (iotype);
1937 /* Note: vlist is freed in free_format_data. */
1938 break;
1939
1940 case FMT_E:
1941 if (n == 0)
1942 goto need_data;
1943 if (require_type (dtp, BT_REAL, type, f))
1944 return;
1945 write_e (dtp, f, p, kind);
1946 break;
1947
1948 case FMT_EN:
1949 if (n == 0)
1950 goto need_data;
1951 if (require_type (dtp, BT_REAL, type, f))
1952 return;
1953 write_en (dtp, f, p, kind);
1954 break;
1955
1956 case FMT_ES:
1957 if (n == 0)
1958 goto need_data;
1959 if (require_type (dtp, BT_REAL, type, f))
1960 return;
1961 write_es (dtp, f, p, kind);
1962 break;
1963
1964 case FMT_F:
1965 if (n == 0)
1966 goto need_data;
1967 if (require_type (dtp, BT_REAL, type, f))
1968 return;
1969 write_f (dtp, f, p, kind);
1970 break;
1971
1972 case FMT_G:
1973 if (n == 0)
1974 goto need_data;
1975 switch (type)
1976 {
1977 case BT_INTEGER:
1978 write_i (dtp, f, p, kind);
1979 break;
1980 case BT_LOGICAL:
1981 write_l (dtp, f, p, kind);
1982 break;
1983 case BT_CHARACTER:
1984 if (kind == 4)
1985 write_a_char4 (dtp, f, p, size);
1986 else
1987 write_a (dtp, f, p, size);
1988 break;
1989 case BT_REAL:
1990 if (f->u.real.w == 0)
1991 write_real_g0 (dtp, p, kind, f->u.real.d);
1992 else
1993 write_d (dtp, f, p, kind);
1994 break;
1995 default:
1996 internal_error (&dtp->common,
1997 "formatted_transfer(): Bad type");
1998 }
1999 break;
2000
2001 case FMT_STRING:
2002 consume_data_flag = 0;
2003 write_constant_string (dtp, f);
2004 break;
2005
2006 /* Format codes that don't transfer data. */
2007 case FMT_X:
2008 case FMT_TR:
2009 consume_data_flag = 0;
2010
2011 dtp->u.p.skips += f->u.n;
2012 pos = bytes_used + dtp->u.p.skips - 1;
2013 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
2014 /* Writes occur just before the switch on f->format, above, so
2015 that trailing blanks are suppressed, unless we are doing a
2016 non-advancing write in which case we want to output the blanks
2017 now. */
2018 if (dtp->u.p.advance_status == ADVANCE_NO)
2019 {
2020 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
2021 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2022 }
2023 break;
2024
2025 case FMT_TL:
2026 case FMT_T:
2027 consume_data_flag = 0;
2028
2029 if (f->format == FMT_TL)
2030 {
2031
2032 /* Handle the special case when no bytes have been used yet.
2033 Cannot go below zero. */
2034 if (bytes_used == 0)
2035 {
2036 dtp->u.p.pending_spaces -= f->u.n;
2037 dtp->u.p.skips -= f->u.n;
2038 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
2039 }
2040
2041 pos = bytes_used - f->u.n;
2042 }
2043 else /* FMT_T */
2044 pos = f->u.n - dtp->u.p.pending_spaces - 1;
2045
2046 /* Standard 10.6.1.1: excessive left tabbing is reset to the
2047 left tab limit. We do not check if the position has gone
2048 beyond the end of record because a subsequent tab could
2049 bring us back again. */
2050 pos = pos < 0 ? 0 : pos;
2051
2052 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
2053 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
2054 + pos - dtp->u.p.max_pos;
2055 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
2056 ? 0 : dtp->u.p.pending_spaces;
2057 break;
2058
2059 case FMT_S:
2060 consume_data_flag = 0;
2061 dtp->u.p.sign_status = SIGN_S;
2062 break;
2063
2064 case FMT_SS:
2065 consume_data_flag = 0;
2066 dtp->u.p.sign_status = SIGN_SS;
2067 break;
2068
2069 case FMT_SP:
2070 consume_data_flag = 0;
2071 dtp->u.p.sign_status = SIGN_SP;
2072 break;
2073
2074 case FMT_BN:
2075 consume_data_flag = 0 ;
2076 dtp->u.p.blank_status = BLANK_NULL;
2077 break;
2078
2079 case FMT_BZ:
2080 consume_data_flag = 0;
2081 dtp->u.p.blank_status = BLANK_ZERO;
2082 break;
2083
2084 case FMT_DC:
2085 consume_data_flag = 0;
2086 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
2087 break;
2088
2089 case FMT_DP:
2090 consume_data_flag = 0;
2091 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
2092 break;
2093
2094 case FMT_RC:
2095 consume_data_flag = 0;
2096 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
2097 break;
2098
2099 case FMT_RD:
2100 consume_data_flag = 0;
2101 dtp->u.p.current_unit->round_status = ROUND_DOWN;
2102 break;
2103
2104 case FMT_RN:
2105 consume_data_flag = 0;
2106 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
2107 break;
2108
2109 case FMT_RP:
2110 consume_data_flag = 0;
2111 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
2112 break;
2113
2114 case FMT_RU:
2115 consume_data_flag = 0;
2116 dtp->u.p.current_unit->round_status = ROUND_UP;
2117 break;
2118
2119 case FMT_RZ:
2120 consume_data_flag = 0;
2121 dtp->u.p.current_unit->round_status = ROUND_ZERO;
2122 break;
2123
2124 case FMT_P:
2125 consume_data_flag = 0;
2126 dtp->u.p.scale_factor = f->u.k;
2127 break;
2128
2129 case FMT_DOLLAR:
2130 consume_data_flag = 0;
2131 dtp->u.p.seen_dollar = 1;
2132 break;
2133
2134 case FMT_SLASH:
2135 consume_data_flag = 0;
2136 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2137 next_record (dtp, 0);
2138 break;
2139
2140 case FMT_COLON:
2141 /* A colon descriptor causes us to exit this loop (in
2142 particular preventing another / descriptor from being
2143 processed) unless there is another data item to be
2144 transferred. */
2145 consume_data_flag = 0;
2146 if (n == 0)
2147 return;
2148 break;
2149
2150 default:
2151 internal_error (&dtp->common, "Bad format node");
2152 }
2153
2154 /* Adjust the item count and data pointer. */
2155
2156 if ((consume_data_flag > 0) && (n > 0))
2157 {
2158 n--;
2159 p = ((char *) p) + size;
2160 }
2161
2162 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
2163 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
2164 }
2165
2166 return;
2167
2168 /* Come here when we need a data descriptor but don't have one. We
2169 push the current format node back onto the input, then return and
2170 let the user program call us back with the data. */
2171 need_data:
2172 unget_format (dtp, f);
2173 }
2174
2175 /* This function is first called from data_init_transfer to initiate the loop
2176 over each item in the format, transferring data as required. Subsequent
2177 calls to this function occur for each data item foound in the READ/WRITE
2178 statement. The item_count is incremented for each call. Since the first
2179 call is from data_transfer_init, the item_count is always one greater than
2180 the actual count number of the item being transferred. */
2181
2182 static void
2183 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
2184 size_t size, size_t nelems)
2185 {
2186 size_t elem;
2187 char *tmp;
2188
2189 tmp = (char *) p;
2190 size_t stride = type == BT_CHARACTER ?
2191 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
2192 if (dtp->u.p.mode == READING)
2193 {
2194 /* Big loop over all the elements. */
2195 for (elem = 0; elem < nelems; elem++)
2196 {
2197 dtp->u.p.item_count++;
2198 formatted_transfer_scalar_read (dtp, type, tmp + stride*elem, kind, size);
2199 }
2200 }
2201 else
2202 {
2203 /* Big loop over all the elements. */
2204 for (elem = 0; elem < nelems; elem++)
2205 {
2206 dtp->u.p.item_count++;
2207 formatted_transfer_scalar_write (dtp, type, tmp + stride*elem, kind, size);
2208 }
2209 }
2210 }
2211
2212
2213 /* Data transfer entry points. The type of the data entity is
2214 implicit in the subroutine call. This prevents us from having to
2215 share a common enum with the compiler. */
2216
2217 void
2218 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
2219 {
2220 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2221 return;
2222 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
2223 }
2224
2225 void
2226 transfer_integer_write (st_parameter_dt *dtp, void *p, int kind)
2227 {
2228 transfer_integer (dtp, p, kind);
2229 }
2230
2231 void
2232 transfer_real (st_parameter_dt *dtp, void *p, int kind)
2233 {
2234 size_t size;
2235 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2236 return;
2237 size = size_from_real_kind (kind);
2238 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
2239 }
2240
2241 void
2242 transfer_real_write (st_parameter_dt *dtp, void *p, int kind)
2243 {
2244 transfer_real (dtp, p, kind);
2245 }
2246
2247 void
2248 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
2249 {
2250 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2251 return;
2252 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
2253 }
2254
2255 void
2256 transfer_logical_write (st_parameter_dt *dtp, void *p, int kind)
2257 {
2258 transfer_logical (dtp, p, kind);
2259 }
2260
2261 void
2262 transfer_character (st_parameter_dt *dtp, void *p, int len)
2263 {
2264 static char *empty_string[0];
2265
2266 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2267 return;
2268
2269 /* Strings of zero length can have p == NULL, which confuses the
2270 transfer routines into thinking we need more data elements. To avoid
2271 this, we give them a nice pointer. */
2272 if (len == 0 && p == NULL)
2273 p = empty_string;
2274
2275 /* Set kind here to 1. */
2276 dtp->u.p.transfer (dtp, BT_CHARACTER, p, 1, len, 1);
2277 }
2278
2279 void
2280 transfer_character_write (st_parameter_dt *dtp, void *p, int len)
2281 {
2282 transfer_character (dtp, p, len);
2283 }
2284
2285 void
2286 transfer_character_wide (st_parameter_dt *dtp, void *p, int len, int kind)
2287 {
2288 static char *empty_string[0];
2289
2290 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2291 return;
2292
2293 /* Strings of zero length can have p == NULL, which confuses the
2294 transfer routines into thinking we need more data elements. To avoid
2295 this, we give them a nice pointer. */
2296 if (len == 0 && p == NULL)
2297 p = empty_string;
2298
2299 /* Here we pass the actual kind value. */
2300 dtp->u.p.transfer (dtp, BT_CHARACTER, p, kind, len, 1);
2301 }
2302
2303 void
2304 transfer_character_wide_write (st_parameter_dt *dtp, void *p, int len, int kind)
2305 {
2306 transfer_character_wide (dtp, p, len, kind);
2307 }
2308
2309 void
2310 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
2311 {
2312 size_t size;
2313 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2314 return;
2315 size = size_from_complex_kind (kind);
2316 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
2317 }
2318
2319 void
2320 transfer_complex_write (st_parameter_dt *dtp, void *p, int kind)
2321 {
2322 transfer_complex (dtp, p, kind);
2323 }
2324
2325 void
2326 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2327 gfc_charlen_type charlen)
2328 {
2329 index_type count[GFC_MAX_DIMENSIONS];
2330 index_type extent[GFC_MAX_DIMENSIONS];
2331 index_type stride[GFC_MAX_DIMENSIONS];
2332 index_type stride0, rank, size, n;
2333 size_t tsize;
2334 char *data;
2335 bt iotype;
2336
2337 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2338 return;
2339
2340 iotype = (bt) GFC_DESCRIPTOR_TYPE (desc);
2341 size = iotype == BT_CHARACTER ? charlen : GFC_DESCRIPTOR_SIZE (desc);
2342
2343 rank = GFC_DESCRIPTOR_RANK (desc);
2344 for (n = 0; n < rank; n++)
2345 {
2346 count[n] = 0;
2347 stride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(desc,n);
2348 extent[n] = GFC_DESCRIPTOR_EXTENT(desc,n);
2349
2350 /* If the extent of even one dimension is zero, then the entire
2351 array section contains zero elements, so we return after writing
2352 a zero array record. */
2353 if (extent[n] <= 0)
2354 {
2355 data = NULL;
2356 tsize = 0;
2357 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2358 return;
2359 }
2360 }
2361
2362 stride0 = stride[0];
2363
2364 /* If the innermost dimension has a stride of 1, we can do the transfer
2365 in contiguous chunks. */
2366 if (stride0 == size)
2367 tsize = extent[0];
2368 else
2369 tsize = 1;
2370
2371 data = GFC_DESCRIPTOR_DATA (desc);
2372
2373 while (data)
2374 {
2375 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2376 data += stride0 * tsize;
2377 count[0] += tsize;
2378 n = 0;
2379 while (count[n] == extent[n])
2380 {
2381 count[n] = 0;
2382 data -= stride[n] * extent[n];
2383 n++;
2384 if (n == rank)
2385 {
2386 data = NULL;
2387 break;
2388 }
2389 else
2390 {
2391 count[n]++;
2392 data += stride[n];
2393 }
2394 }
2395 }
2396 }
2397
2398 void
2399 transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2400 gfc_charlen_type charlen)
2401 {
2402 transfer_array (dtp, desc, kind, charlen);
2403 }
2404
2405
2406 /* User defined input/output iomsg. */
2407
2408 #define IOMSG_LEN 256
2409
2410 void
2411 transfer_derived (st_parameter_dt *parent, void *dtio_source, void *dtio_proc)
2412 {
2413 if (parent->u.p.current_unit)
2414 {
2415 if (parent->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2416 parent->u.p.ufdtio_ptr = (unformatted_dtio) dtio_proc;
2417 else
2418 parent->u.p.fdtio_ptr = (formatted_dtio) dtio_proc;
2419 }
2420 parent->u.p.transfer (parent, BT_CLASS, dtio_source, 0, 0, 1);
2421 }
2422
2423
2424 /* Preposition a sequential unformatted file while reading. */
2425
2426 static void
2427 us_read (st_parameter_dt *dtp, int continued)
2428 {
2429 ssize_t n, nr;
2430 GFC_INTEGER_4 i4;
2431 GFC_INTEGER_8 i8;
2432 gfc_offset i;
2433
2434 if (compile_options.record_marker == 0)
2435 n = sizeof (GFC_INTEGER_4);
2436 else
2437 n = compile_options.record_marker;
2438
2439 nr = sread (dtp->u.p.current_unit->s, &i, n);
2440 if (unlikely (nr < 0))
2441 {
2442 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2443 return;
2444 }
2445 else if (nr == 0)
2446 {
2447 hit_eof (dtp);
2448 return; /* end of file */
2449 }
2450 else if (unlikely (n != nr))
2451 {
2452 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2453 return;
2454 }
2455
2456 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2457 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
2458 {
2459 switch (nr)
2460 {
2461 case sizeof(GFC_INTEGER_4):
2462 memcpy (&i4, &i, sizeof (i4));
2463 i = i4;
2464 break;
2465
2466 case sizeof(GFC_INTEGER_8):
2467 memcpy (&i8, &i, sizeof (i8));
2468 i = i8;
2469 break;
2470
2471 default:
2472 runtime_error ("Illegal value for record marker");
2473 break;
2474 }
2475 }
2476 else
2477 {
2478 uint32_t u32;
2479 uint64_t u64;
2480 switch (nr)
2481 {
2482 case sizeof(GFC_INTEGER_4):
2483 memcpy (&u32, &i, sizeof (u32));
2484 u32 = __builtin_bswap32 (u32);
2485 memcpy (&i4, &u32, sizeof (i4));
2486 i = i4;
2487 break;
2488
2489 case sizeof(GFC_INTEGER_8):
2490 memcpy (&u64, &i, sizeof (u64));
2491 u64 = __builtin_bswap64 (u64);
2492 memcpy (&i8, &u64, sizeof (i8));
2493 i = i8;
2494 break;
2495
2496 default:
2497 runtime_error ("Illegal value for record marker");
2498 break;
2499 }
2500 }
2501
2502 if (i >= 0)
2503 {
2504 dtp->u.p.current_unit->bytes_left_subrecord = i;
2505 dtp->u.p.current_unit->continued = 0;
2506 }
2507 else
2508 {
2509 dtp->u.p.current_unit->bytes_left_subrecord = -i;
2510 dtp->u.p.current_unit->continued = 1;
2511 }
2512
2513 if (! continued)
2514 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2515 }
2516
2517
2518 /* Preposition a sequential unformatted file while writing. This
2519 amount to writing a bogus length that will be filled in later. */
2520
2521 static void
2522 us_write (st_parameter_dt *dtp, int continued)
2523 {
2524 ssize_t nbytes;
2525 gfc_offset dummy;
2526
2527 dummy = 0;
2528
2529 if (compile_options.record_marker == 0)
2530 nbytes = sizeof (GFC_INTEGER_4);
2531 else
2532 nbytes = compile_options.record_marker ;
2533
2534 if (swrite (dtp->u.p.current_unit->s, &dummy, nbytes) != nbytes)
2535 generate_error (&dtp->common, LIBERROR_OS, NULL);
2536
2537 /* For sequential unformatted, if RECL= was not specified in the OPEN
2538 we write until we have more bytes than can fit in the subrecord
2539 markers, then we write a new subrecord. */
2540
2541 dtp->u.p.current_unit->bytes_left_subrecord =
2542 dtp->u.p.current_unit->recl_subrecord;
2543 dtp->u.p.current_unit->continued = continued;
2544 }
2545
2546
2547 /* Position to the next record prior to transfer. We are assumed to
2548 be before the next record. We also calculate the bytes in the next
2549 record. */
2550
2551 static void
2552 pre_position (st_parameter_dt *dtp)
2553 {
2554 if (dtp->u.p.current_unit->current_record)
2555 return; /* Already positioned. */
2556
2557 switch (current_mode (dtp))
2558 {
2559 case FORMATTED_STREAM:
2560 case UNFORMATTED_STREAM:
2561 /* There are no records with stream I/O. If the position was specified
2562 data_transfer_init has already positioned the file. If no position
2563 was specified, we continue from where we last left off. I.e.
2564 there is nothing to do here. */
2565 break;
2566
2567 case UNFORMATTED_SEQUENTIAL:
2568 if (dtp->u.p.mode == READING)
2569 us_read (dtp, 0);
2570 else
2571 us_write (dtp, 0);
2572
2573 break;
2574
2575 case FORMATTED_SEQUENTIAL:
2576 case FORMATTED_DIRECT:
2577 case UNFORMATTED_DIRECT:
2578 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2579 break;
2580 }
2581
2582 dtp->u.p.current_unit->current_record = 1;
2583 }
2584
2585
2586 /* Initialize things for a data transfer. This code is common for
2587 both reading and writing. */
2588
2589 static void
2590 data_transfer_init (st_parameter_dt *dtp, int read_flag)
2591 {
2592 unit_flags u_flags; /* Used for creating a unit if needed. */
2593 GFC_INTEGER_4 cf = dtp->common.flags;
2594 namelist_info *ionml;
2595
2596 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
2597
2598 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2599
2600 dtp->u.p.ionml = ionml;
2601 dtp->u.p.mode = read_flag ? READING : WRITING;
2602
2603 dtp->u.p.cc.len = 0;
2604
2605 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2606 return;
2607
2608 dtp->u.p.current_unit = get_unit (dtp, 1);
2609
2610 if (dtp->u.p.current_unit == NULL)
2611 {
2612 /* This means we tried to access an external unit < 0 without
2613 having opened it first with NEWUNIT=. */
2614 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2615 "Unit number is negative and unit was not already "
2616 "opened with OPEN(NEWUNIT=...)");
2617 return;
2618 }
2619 else if (dtp->u.p.current_unit->s == NULL)
2620 { /* Open the unit with some default flags. */
2621 st_parameter_open opp;
2622 unit_convert conv;
2623
2624 memset (&u_flags, '\0', sizeof (u_flags));
2625 u_flags.access = ACCESS_SEQUENTIAL;
2626 u_flags.action = ACTION_READWRITE;
2627
2628 /* Is it unformatted? */
2629 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
2630 | IOPARM_DT_IONML_SET)))
2631 u_flags.form = FORM_UNFORMATTED;
2632 else
2633 u_flags.form = FORM_UNSPECIFIED;
2634
2635 u_flags.delim = DELIM_UNSPECIFIED;
2636 u_flags.blank = BLANK_UNSPECIFIED;
2637 u_flags.pad = PAD_UNSPECIFIED;
2638 u_flags.decimal = DECIMAL_UNSPECIFIED;
2639 u_flags.encoding = ENCODING_UNSPECIFIED;
2640 u_flags.async = ASYNC_UNSPECIFIED;
2641 u_flags.round = ROUND_UNSPECIFIED;
2642 u_flags.sign = SIGN_UNSPECIFIED;
2643 u_flags.share = SHARE_UNSPECIFIED;
2644 u_flags.cc = CC_UNSPECIFIED;
2645 u_flags.readonly = 0;
2646
2647 u_flags.status = STATUS_UNKNOWN;
2648
2649 conv = get_unformatted_convert (dtp->common.unit);
2650
2651 if (conv == GFC_CONVERT_NONE)
2652 conv = compile_options.convert;
2653
2654 /* We use big_endian, which is 0 on little-endian machines
2655 and 1 on big-endian machines. */
2656 switch (conv)
2657 {
2658 case GFC_CONVERT_NATIVE:
2659 case GFC_CONVERT_SWAP:
2660 break;
2661
2662 case GFC_CONVERT_BIG:
2663 conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
2664 break;
2665
2666 case GFC_CONVERT_LITTLE:
2667 conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
2668 break;
2669
2670 default:
2671 internal_error (&opp.common, "Illegal value for CONVERT");
2672 break;
2673 }
2674
2675 u_flags.convert = conv;
2676
2677 opp.common = dtp->common;
2678 opp.common.flags &= IOPARM_COMMON_MASK;
2679 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
2680 dtp->common.flags &= ~IOPARM_COMMON_MASK;
2681 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
2682 if (dtp->u.p.current_unit == NULL)
2683 return;
2684 }
2685
2686 if (dtp->u.p.current_unit->child_dtio == 0)
2687 {
2688 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2689 {
2690 dtp->u.p.current_unit->has_size = true;
2691 /* Initialize the count. */
2692 dtp->u.p.current_unit->size_used = 0;
2693 }
2694 else
2695 dtp->u.p.current_unit->has_size = false;
2696 }
2697
2698 /* Check the action. */
2699
2700 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
2701 {
2702 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2703 "Cannot read from file opened for WRITE");
2704 return;
2705 }
2706
2707 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
2708 {
2709 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2710 "Cannot write to file opened for READ");
2711 return;
2712 }
2713
2714 dtp->u.p.first_item = 1;
2715
2716 /* Check the format. */
2717
2718 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2719 parse_format (dtp);
2720
2721 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
2722 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2723 != 0)
2724 {
2725 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2726 "Format present for UNFORMATTED data transfer");
2727 return;
2728 }
2729
2730 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
2731 {
2732 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2733 {
2734 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2735 "A format cannot be specified with a namelist");
2736 return;
2737 }
2738 }
2739 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
2740 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
2741 {
2742 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2743 "Missing format for FORMATTED data transfer");
2744 return;
2745 }
2746
2747 if (is_internal_unit (dtp)
2748 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2749 {
2750 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2751 "Internal file cannot be accessed by UNFORMATTED "
2752 "data transfer");
2753 return;
2754 }
2755
2756 /* Check the record or position number. */
2757
2758 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
2759 && (cf & IOPARM_DT_HAS_REC) == 0)
2760 {
2761 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2762 "Direct access data transfer requires record number");
2763 return;
2764 }
2765
2766 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2767 {
2768 if ((cf & IOPARM_DT_HAS_REC) != 0)
2769 {
2770 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2771 "Record number not allowed for sequential access "
2772 "data transfer");
2773 return;
2774 }
2775
2776 if (compile_options.warn_std &&
2777 dtp->u.p.current_unit->endfile == AFTER_ENDFILE)
2778 {
2779 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2780 "Sequential READ or WRITE not allowed after "
2781 "EOF marker, possibly use REWIND or BACKSPACE");
2782 return;
2783 }
2784 }
2785 /* Process the ADVANCE option. */
2786
2787 dtp->u.p.advance_status
2788 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
2789 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
2790 "Bad ADVANCE parameter in data transfer statement");
2791
2792 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
2793 {
2794 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2795 {
2796 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2797 "ADVANCE specification conflicts with sequential "
2798 "access");
2799 return;
2800 }
2801
2802 if (is_internal_unit (dtp))
2803 {
2804 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2805 "ADVANCE specification conflicts with internal file");
2806 return;
2807 }
2808
2809 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2810 != IOPARM_DT_HAS_FORMAT)
2811 {
2812 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2813 "ADVANCE specification requires an explicit format");
2814 return;
2815 }
2816 }
2817
2818 if (read_flag)
2819 {
2820 dtp->u.p.current_unit->previous_nonadvancing_write = 0;
2821
2822 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
2823 {
2824 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2825 "EOR specification requires an ADVANCE specification "
2826 "of NO");
2827 return;
2828 }
2829
2830 if ((cf & IOPARM_DT_HAS_SIZE) != 0
2831 && dtp->u.p.advance_status != ADVANCE_NO)
2832 {
2833 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2834 "SIZE specification requires an ADVANCE "
2835 "specification of NO");
2836 return;
2837 }
2838 }
2839 else
2840 { /* Write constraints. */
2841 if ((cf & IOPARM_END) != 0)
2842 {
2843 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2844 "END specification cannot appear in a write "
2845 "statement");
2846 return;
2847 }
2848
2849 if ((cf & IOPARM_EOR) != 0)
2850 {
2851 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2852 "EOR specification cannot appear in a write "
2853 "statement");
2854 return;
2855 }
2856
2857 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2858 {
2859 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2860 "SIZE specification cannot appear in a write "
2861 "statement");
2862 return;
2863 }
2864 }
2865
2866 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
2867 dtp->u.p.advance_status = ADVANCE_YES;
2868
2869 /* Check the decimal mode. */
2870 dtp->u.p.current_unit->decimal_status
2871 = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
2872 find_option (&dtp->common, dtp->decimal, dtp->decimal_len,
2873 decimal_opt, "Bad DECIMAL parameter in data transfer "
2874 "statement");
2875
2876 if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED)
2877 dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal;
2878
2879 /* Check the round mode. */
2880 dtp->u.p.current_unit->round_status
2881 = !(cf & IOPARM_DT_HAS_ROUND) ? ROUND_UNSPECIFIED :
2882 find_option (&dtp->common, dtp->round, dtp->round_len,
2883 round_opt, "Bad ROUND parameter in data transfer "
2884 "statement");
2885
2886 if (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED)
2887 dtp->u.p.current_unit->round_status = dtp->u.p.current_unit->flags.round;
2888
2889 /* Check the sign mode. */
2890 dtp->u.p.sign_status
2891 = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
2892 find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
2893 "Bad SIGN parameter in data transfer statement");
2894
2895 if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
2896 dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
2897
2898 /* Check the blank mode. */
2899 dtp->u.p.blank_status
2900 = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
2901 find_option (&dtp->common, dtp->blank, dtp->blank_len,
2902 blank_opt,
2903 "Bad BLANK parameter in data transfer statement");
2904
2905 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
2906 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
2907
2908 /* Check the delim mode. */
2909 dtp->u.p.current_unit->delim_status
2910 = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
2911 find_option (&dtp->common, dtp->delim, dtp->delim_len,
2912 delim_opt, "Bad DELIM parameter in data transfer statement");
2913
2914 if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED)
2915 {
2916 if (ionml && dtp->u.p.current_unit->flags.delim == DELIM_UNSPECIFIED)
2917 dtp->u.p.current_unit->delim_status = DELIM_QUOTE;
2918 else
2919 dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim;
2920 }
2921
2922 /* Check the pad mode. */
2923 dtp->u.p.current_unit->pad_status
2924 = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
2925 find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
2926 "Bad PAD parameter in data transfer statement");
2927
2928 if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED)
2929 dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad;
2930
2931 /* Check to see if we might be reading what we wrote before */
2932
2933 if (dtp->u.p.mode != dtp->u.p.current_unit->mode
2934 && !is_internal_unit (dtp))
2935 {
2936 int pos = fbuf_reset (dtp->u.p.current_unit);
2937 if (pos != 0)
2938 sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR);
2939 sflush(dtp->u.p.current_unit->s);
2940 }
2941
2942 /* Check the POS= specifier: that it is in range and that it is used with a
2943 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2944
2945 if (((cf & IOPARM_DT_HAS_POS) != 0))
2946 {
2947 if (is_stream_io (dtp))
2948 {
2949
2950 if (dtp->pos <= 0)
2951 {
2952 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2953 "POS=specifier must be positive");
2954 return;
2955 }
2956
2957 if (dtp->pos >= dtp->u.p.current_unit->maxrec)
2958 {
2959 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2960 "POS=specifier too large");
2961 return;
2962 }
2963
2964 dtp->rec = dtp->pos;
2965
2966 if (dtp->u.p.mode == READING)
2967 {
2968 /* Reset the endfile flag; if we hit EOF during reading
2969 we'll set the flag and generate an error at that point
2970 rather than worrying about it here. */
2971 dtp->u.p.current_unit->endfile = NO_ENDFILE;
2972 }
2973
2974 if (dtp->pos != dtp->u.p.current_unit->strm_pos)
2975 {
2976 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2977 if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, SEEK_SET) < 0)
2978 {
2979 generate_error (&dtp->common, LIBERROR_OS, NULL);
2980 return;
2981 }
2982 dtp->u.p.current_unit->strm_pos = dtp->pos;
2983 }
2984 }
2985 else
2986 {
2987 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2988 "POS=specifier not allowed, "
2989 "Try OPEN with ACCESS='stream'");
2990 return;
2991 }
2992 }
2993
2994
2995 /* Sanity checks on the record number. */
2996 if ((cf & IOPARM_DT_HAS_REC) != 0)
2997 {
2998 if (dtp->rec <= 0)
2999 {
3000 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3001 "Record number must be positive");
3002 return;
3003 }
3004
3005 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
3006 {
3007 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3008 "Record number too large");
3009 return;
3010 }
3011
3012 /* Make sure format buffer is reset. */
3013 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
3014 fbuf_reset (dtp->u.p.current_unit);
3015
3016
3017 /* Check whether the record exists to be read. Only
3018 a partial record needs to exist. */
3019
3020 if (dtp->u.p.mode == READING && (dtp->rec - 1)
3021 * dtp->u.p.current_unit->recl >= ssize (dtp->u.p.current_unit->s))
3022 {
3023 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3024 "Non-existing record number");
3025 return;
3026 }
3027
3028 /* Position the file. */
3029 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
3030 * dtp->u.p.current_unit->recl, SEEK_SET) < 0)
3031 {
3032 generate_error (&dtp->common, LIBERROR_OS, NULL);
3033 return;
3034 }
3035
3036 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
3037 {
3038 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3039 "Record number not allowed for stream access "
3040 "data transfer");
3041 return;
3042 }
3043 }
3044
3045 /* Bugware for badly written mixed C-Fortran I/O. */
3046 if (!is_internal_unit (dtp))
3047 flush_if_preconnected(dtp->u.p.current_unit->s);
3048
3049 dtp->u.p.current_unit->mode = dtp->u.p.mode;
3050
3051 /* Set the maximum position reached from the previous I/O operation. This
3052 could be greater than zero from a previous non-advancing write. */
3053 dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
3054
3055 pre_position (dtp);
3056
3057
3058 /* Set up the subroutine that will handle the transfers. */
3059
3060 if (read_flag)
3061 {
3062 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
3063 dtp->u.p.transfer = unformatted_read;
3064 else
3065 {
3066 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3067 {
3068 if (dtp->u.p.current_unit->child_dtio == 0)
3069 dtp->u.p.current_unit->last_char = EOF - 1;
3070 dtp->u.p.transfer = list_formatted_read;
3071 }
3072 else
3073 dtp->u.p.transfer = formatted_transfer;
3074 }
3075 }
3076 else
3077 {
3078 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
3079 dtp->u.p.transfer = unformatted_write;
3080 else
3081 {
3082 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3083 dtp->u.p.transfer = list_formatted_write;
3084 else
3085 dtp->u.p.transfer = formatted_transfer;
3086 }
3087 }
3088
3089 /* Make sure that we don't do a read after a nonadvancing write. */
3090
3091 if (read_flag)
3092 {
3093 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
3094 {
3095 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3096 "Cannot READ after a nonadvancing WRITE");
3097 return;
3098 }
3099 }
3100 else
3101 {
3102 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
3103 dtp->u.p.current_unit->read_bad = 1;
3104 }
3105
3106 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
3107 {
3108 #ifdef HAVE_USELOCALE
3109 dtp->u.p.old_locale = uselocale (c_locale);
3110 #else
3111 __gthread_mutex_lock (&old_locale_lock);
3112 if (!old_locale_ctr++)
3113 {
3114 old_locale = setlocale (LC_NUMERIC, NULL);
3115 setlocale (LC_NUMERIC, "C");
3116 }
3117 __gthread_mutex_unlock (&old_locale_lock);
3118 #endif
3119 /* Start the data transfer if we are doing a formatted transfer. */
3120 if ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0
3121 && dtp->u.p.ionml == NULL)
3122 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
3123 }
3124 }
3125
3126
3127 /* Initialize an array_loop_spec given the array descriptor. The function
3128 returns the index of the last element of the array, and also returns
3129 starting record, where the first I/O goes to (necessary in case of
3130 negative strides). */
3131
3132 gfc_offset
3133 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
3134 gfc_offset *start_record)
3135 {
3136 int rank = GFC_DESCRIPTOR_RANK(desc);
3137 int i;
3138 gfc_offset index;
3139 int empty;
3140
3141 empty = 0;
3142 index = 1;
3143 *start_record = 0;
3144
3145 for (i=0; i<rank; i++)
3146 {
3147 ls[i].idx = GFC_DESCRIPTOR_LBOUND(desc,i);
3148 ls[i].start = GFC_DESCRIPTOR_LBOUND(desc,i);
3149 ls[i].end = GFC_DESCRIPTOR_UBOUND(desc,i);
3150 ls[i].step = GFC_DESCRIPTOR_STRIDE(desc,i);
3151 empty = empty || (GFC_DESCRIPTOR_UBOUND(desc,i)
3152 < GFC_DESCRIPTOR_LBOUND(desc,i));
3153
3154 if (GFC_DESCRIPTOR_STRIDE(desc,i) > 0)
3155 {
3156 index += (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3157 * GFC_DESCRIPTOR_STRIDE(desc,i);
3158 }
3159 else
3160 {
3161 index -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3162 * GFC_DESCRIPTOR_STRIDE(desc,i);
3163 *start_record -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3164 * GFC_DESCRIPTOR_STRIDE(desc,i);
3165 }
3166 }
3167
3168 if (empty)
3169 return 0;
3170 else
3171 return index;
3172 }
3173
3174 /* Determine the index to the next record in an internal unit array by
3175 by incrementing through the array_loop_spec. */
3176
3177 gfc_offset
3178 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
3179 {
3180 int i, carry;
3181 gfc_offset index;
3182
3183 carry = 1;
3184 index = 0;
3185
3186 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
3187 {
3188 if (carry)
3189 {
3190 ls[i].idx++;
3191 if (ls[i].idx > ls[i].end)
3192 {
3193 ls[i].idx = ls[i].start;
3194 carry = 1;
3195 }
3196 else
3197 carry = 0;
3198 }
3199 index = index + (ls[i].idx - ls[i].start) * ls[i].step;
3200 }
3201
3202 *finished = carry;
3203
3204 return index;
3205 }
3206
3207
3208
3209 /* Skip to the end of the current record, taking care of an optional
3210 record marker of size bytes. If the file is not seekable, we
3211 read chunks of size MAX_READ until we get to the right
3212 position. */
3213
3214 static void
3215 skip_record (st_parameter_dt *dtp, ssize_t bytes)
3216 {
3217 ssize_t rlength, readb;
3218 #define MAX_READ 4096
3219 char p[MAX_READ];
3220
3221 dtp->u.p.current_unit->bytes_left_subrecord += bytes;
3222 if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
3223 return;
3224
3225 /* Direct access files do not generate END conditions,
3226 only I/O errors. */
3227 if (sseek (dtp->u.p.current_unit->s,
3228 dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0)
3229 {
3230 /* Seeking failed, fall back to seeking by reading data. */
3231 while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
3232 {
3233 rlength =
3234 (MAX_READ < dtp->u.p.current_unit->bytes_left_subrecord) ?
3235 MAX_READ : dtp->u.p.current_unit->bytes_left_subrecord;
3236
3237 readb = sread (dtp->u.p.current_unit->s, p, rlength);
3238 if (readb < 0)
3239 {
3240 generate_error (&dtp->common, LIBERROR_OS, NULL);
3241 return;
3242 }
3243
3244 dtp->u.p.current_unit->bytes_left_subrecord -= readb;
3245 }
3246 return;
3247 }
3248 dtp->u.p.current_unit->bytes_left_subrecord = 0;
3249 }
3250
3251
3252 /* Advance to the next record reading unformatted files, taking
3253 care of subrecords. If complete_record is nonzero, we loop
3254 until all subrecords are cleared. */
3255
3256 static void
3257 next_record_r_unf (st_parameter_dt *dtp, int complete_record)
3258 {
3259 size_t bytes;
3260
3261 bytes = compile_options.record_marker == 0 ?
3262 sizeof (GFC_INTEGER_4) : compile_options.record_marker;
3263
3264 while(1)
3265 {
3266
3267 /* Skip over tail */
3268
3269 skip_record (dtp, bytes);
3270
3271 if ( ! (complete_record && dtp->u.p.current_unit->continued))
3272 return;
3273
3274 us_read (dtp, 1);
3275 }
3276 }
3277
3278
3279 static gfc_offset
3280 min_off (gfc_offset a, gfc_offset b)
3281 {
3282 return (a < b ? a : b);
3283 }
3284
3285
3286 /* Space to the next record for read mode. */
3287
3288 static void
3289 next_record_r (st_parameter_dt *dtp, int done)
3290 {
3291 gfc_offset record;
3292 int bytes_left;
3293 char p;
3294 int cc;
3295
3296 switch (current_mode (dtp))
3297 {
3298 /* No records in unformatted STREAM I/O. */
3299 case UNFORMATTED_STREAM:
3300 return;
3301
3302 case UNFORMATTED_SEQUENTIAL:
3303 next_record_r_unf (dtp, 1);
3304 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3305 break;
3306
3307 case FORMATTED_DIRECT:
3308 case UNFORMATTED_DIRECT:
3309 skip_record (dtp, dtp->u.p.current_unit->bytes_left);
3310 break;
3311
3312 case FORMATTED_STREAM:
3313 case FORMATTED_SEQUENTIAL:
3314 /* read_sf has already terminated input because of an '\n', or
3315 we have hit EOF. */
3316 if (dtp->u.p.sf_seen_eor)
3317 {
3318 dtp->u.p.sf_seen_eor = 0;
3319 break;
3320 }
3321
3322 if (is_internal_unit (dtp))
3323 {
3324 if (is_array_io (dtp))
3325 {
3326 int finished;
3327
3328 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3329 &finished);
3330 if (!done && finished)
3331 hit_eof (dtp);
3332
3333 /* Now seek to this record. */
3334 record = record * dtp->u.p.current_unit->recl;
3335 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3336 {
3337 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3338 break;
3339 }
3340 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3341 }
3342 else
3343 {
3344 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
3345 bytes_left = min_off (bytes_left,
3346 ssize (dtp->u.p.current_unit->s)
3347 - stell (dtp->u.p.current_unit->s));
3348 if (sseek (dtp->u.p.current_unit->s,
3349 bytes_left, SEEK_CUR) < 0)
3350 {
3351 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3352 break;
3353 }
3354 dtp->u.p.current_unit->bytes_left
3355 = dtp->u.p.current_unit->recl;
3356 }
3357 break;
3358 }
3359 else if (dtp->u.p.current_unit->flags.cc != CC_NONE)
3360 {
3361 do
3362 {
3363 errno = 0;
3364 cc = fbuf_getc (dtp->u.p.current_unit);
3365 if (cc == EOF)
3366 {
3367 if (errno != 0)
3368 generate_error (&dtp->common, LIBERROR_OS, NULL);
3369 else
3370 {
3371 if (is_stream_io (dtp)
3372 || dtp->u.p.current_unit->pad_status == PAD_NO
3373 || dtp->u.p.current_unit->bytes_left
3374 == dtp->u.p.current_unit->recl)
3375 hit_eof (dtp);
3376 }
3377 break;
3378 }
3379
3380 if (is_stream_io (dtp))
3381 dtp->u.p.current_unit->strm_pos++;
3382
3383 p = (char) cc;
3384 }
3385 while (p != '\n');
3386 }
3387 break;
3388 }
3389 }
3390
3391
3392 /* Small utility function to write a record marker, taking care of
3393 byte swapping and of choosing the correct size. */
3394
3395 static int
3396 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
3397 {
3398 size_t len;
3399 GFC_INTEGER_4 buf4;
3400 GFC_INTEGER_8 buf8;
3401
3402 if (compile_options.record_marker == 0)
3403 len = sizeof (GFC_INTEGER_4);
3404 else
3405 len = compile_options.record_marker;
3406
3407 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
3408 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
3409 {
3410 switch (len)
3411 {
3412 case sizeof (GFC_INTEGER_4):
3413 buf4 = buf;
3414 return swrite (dtp->u.p.current_unit->s, &buf4, len);
3415 break;
3416
3417 case sizeof (GFC_INTEGER_8):
3418 buf8 = buf;
3419 return swrite (dtp->u.p.current_unit->s, &buf8, len);
3420 break;
3421
3422 default:
3423 runtime_error ("Illegal value for record marker");
3424 break;
3425 }
3426 }
3427 else
3428 {
3429 uint32_t u32;
3430 uint64_t u64;
3431 switch (len)
3432 {
3433 case sizeof (GFC_INTEGER_4):
3434 buf4 = buf;
3435 memcpy (&u32, &buf4, sizeof (u32));
3436 u32 = __builtin_bswap32 (u32);
3437 return swrite (dtp->u.p.current_unit->s, &u32, len);
3438 break;
3439
3440 case sizeof (GFC_INTEGER_8):
3441 buf8 = buf;
3442 memcpy (&u64, &buf8, sizeof (u64));
3443 u64 = __builtin_bswap64 (u64);
3444 return swrite (dtp->u.p.current_unit->s, &u64, len);
3445 break;
3446
3447 default:
3448 runtime_error ("Illegal value for record marker");
3449 break;
3450 }
3451 }
3452
3453 }
3454
3455 /* Position to the next (sub)record in write mode for
3456 unformatted sequential files. */
3457
3458 static void
3459 next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
3460 {
3461 gfc_offset m, m_write, record_marker;
3462
3463 /* Bytes written. */
3464 m = dtp->u.p.current_unit->recl_subrecord
3465 - dtp->u.p.current_unit->bytes_left_subrecord;
3466
3467 if (compile_options.record_marker == 0)
3468 record_marker = sizeof (GFC_INTEGER_4);
3469 else
3470 record_marker = compile_options.record_marker;
3471
3472 /* Seek to the head and overwrite the bogus length with the real
3473 length. */
3474
3475 if (unlikely (sseek (dtp->u.p.current_unit->s, - m - record_marker,
3476 SEEK_CUR) < 0))
3477 goto io_error;
3478
3479 if (next_subrecord)
3480 m_write = -m;
3481 else
3482 m_write = m;
3483
3484 if (unlikely (write_us_marker (dtp, m_write) < 0))
3485 goto io_error;
3486
3487 /* Seek past the end of the current record. */
3488
3489 if (unlikely (sseek (dtp->u.p.current_unit->s, m, SEEK_CUR) < 0))
3490 goto io_error;
3491
3492 /* Write the length tail. If we finish a record containing
3493 subrecords, we write out the negative length. */
3494
3495 if (dtp->u.p.current_unit->continued)
3496 m_write = -m;
3497 else
3498 m_write = m;
3499
3500 if (unlikely (write_us_marker (dtp, m_write) < 0))
3501 goto io_error;
3502
3503 return;
3504
3505 io_error:
3506 generate_error (&dtp->common, LIBERROR_OS, NULL);
3507 return;
3508
3509 }
3510
3511
3512 /* Utility function like memset() but operating on streams. Return
3513 value is same as for POSIX write(). */
3514
3515 static ssize_t
3516 sset (stream * s, int c, ssize_t nbyte)
3517 {
3518 #define WRITE_CHUNK 256
3519 char p[WRITE_CHUNK];
3520 ssize_t bytes_left, trans;
3521
3522 if (nbyte < WRITE_CHUNK)
3523 memset (p, c, nbyte);
3524 else
3525 memset (p, c, WRITE_CHUNK);
3526
3527 bytes_left = nbyte;
3528 while (bytes_left > 0)
3529 {
3530 trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK;
3531 trans = swrite (s, p, trans);
3532 if (trans <= 0)
3533 return trans;
3534 bytes_left -= trans;
3535 }
3536
3537 return nbyte - bytes_left;
3538 }
3539
3540
3541 /* Finish up a record according to the legacy carriagecontrol type, based
3542 on the first character in the record. */
3543
3544 static void
3545 next_record_cc (st_parameter_dt *dtp)
3546 {
3547 /* Only valid with CARRIAGECONTROL=FORTRAN. */
3548 if (dtp->u.p.current_unit->flags.cc != CC_FORTRAN)
3549 return;
3550
3551 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3552 if (dtp->u.p.cc.len > 0)
3553 {
3554 char * p = fbuf_alloc (dtp->u.p.current_unit, dtp->u.p.cc.len);
3555 if (!p)
3556 generate_error (&dtp->common, LIBERROR_OS, NULL);
3557
3558 /* Output CR for the first character with default CC setting. */
3559 *(p++) = dtp->u.p.cc.u.end;
3560 if (dtp->u.p.cc.len > 1)
3561 *p = dtp->u.p.cc.u.end;
3562 }
3563 }
3564
3565 /* Position to the next record in write mode. */
3566
3567 static void
3568 next_record_w (st_parameter_dt *dtp, int done)
3569 {
3570 gfc_offset m, record, max_pos;
3571 int length;
3572
3573 /* Zero counters for X- and T-editing. */
3574 max_pos = dtp->u.p.max_pos;
3575 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
3576
3577 switch (current_mode (dtp))
3578 {
3579 /* No records in unformatted STREAM I/O. */
3580 case UNFORMATTED_STREAM:
3581 return;
3582
3583 case FORMATTED_DIRECT:
3584 if (dtp->u.p.current_unit->bytes_left == 0)
3585 break;
3586
3587 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3588 fbuf_flush (dtp->u.p.current_unit, WRITING);
3589 if (sset (dtp->u.p.current_unit->s, ' ',
3590 dtp->u.p.current_unit->bytes_left)
3591 != dtp->u.p.current_unit->bytes_left)
3592 goto io_error;
3593
3594 break;
3595
3596 case UNFORMATTED_DIRECT:
3597 if (dtp->u.p.current_unit->bytes_left > 0)
3598 {
3599 length = (int) dtp->u.p.current_unit->bytes_left;
3600 if (sset (dtp->u.p.current_unit->s, 0, length) != length)
3601 goto io_error;
3602 }
3603 break;
3604
3605 case UNFORMATTED_SEQUENTIAL:
3606 next_record_w_unf (dtp, 0);
3607 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3608 break;
3609
3610 case FORMATTED_STREAM:
3611 case FORMATTED_SEQUENTIAL:
3612
3613 if (is_internal_unit (dtp))
3614 {
3615 char *p;
3616 if (is_array_io (dtp))
3617 {
3618 int finished;
3619
3620 length = (int) dtp->u.p.current_unit->bytes_left;
3621
3622 /* If the farthest position reached is greater than current
3623 position, adjust the position and set length to pad out
3624 whats left. Otherwise just pad whats left.
3625 (for character array unit) */
3626 m = dtp->u.p.current_unit->recl
3627 - dtp->u.p.current_unit->bytes_left;
3628 if (max_pos > m)
3629 {
3630 length = (int) (max_pos - m);
3631 if (sseek (dtp->u.p.current_unit->s,
3632 length, SEEK_CUR) < 0)
3633 {
3634 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3635 return;
3636 }
3637 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3638 }
3639
3640 p = write_block (dtp, length);
3641 if (p == NULL)
3642 return;
3643
3644 if (unlikely (is_char4_unit (dtp)))
3645 {
3646 gfc_char4_t *p4 = (gfc_char4_t *) p;
3647 memset4 (p4, ' ', length);
3648 }
3649 else
3650 memset (p, ' ', length);
3651
3652 /* Now that the current record has been padded out,
3653 determine where the next record in the array is. */
3654 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3655 &finished);
3656 if (finished)
3657 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3658
3659 /* Now seek to this record */
3660 record = record * dtp->u.p.current_unit->recl;
3661
3662 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3663 {
3664 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3665 return;
3666 }
3667
3668 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3669 }
3670 else
3671 {
3672 length = 1;
3673
3674 /* If this is the last call to next_record move to the farthest
3675 position reached and set length to pad out the remainder
3676 of the record. (for character scaler unit) */
3677 if (done)
3678 {
3679 m = dtp->u.p.current_unit->recl
3680 - dtp->u.p.current_unit->bytes_left;
3681 if (max_pos > m)
3682 {
3683 length = (int) (max_pos - m);
3684 if (sseek (dtp->u.p.current_unit->s,
3685 length, SEEK_CUR) < 0)
3686 {
3687 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3688 return;
3689 }
3690 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3691 }
3692 else
3693 length = (int) dtp->u.p.current_unit->bytes_left;
3694 }
3695 if (length > 0)
3696 {
3697 p = write_block (dtp, length);
3698 if (p == NULL)
3699 return;
3700
3701 if (unlikely (is_char4_unit (dtp)))
3702 {
3703 gfc_char4_t *p4 = (gfc_char4_t *) p;
3704 memset4 (p4, (gfc_char4_t) ' ', length);
3705 }
3706 else
3707 memset (p, ' ', length);
3708 }
3709 }
3710 }
3711 /* Handle legacy CARRIAGECONTROL line endings. */
3712 else if (dtp->u.p.current_unit->flags.cc == CC_FORTRAN)
3713 next_record_cc (dtp);
3714 else
3715 {
3716 /* Skip newlines for CC=CC_NONE. */
3717 const int len = (dtp->u.p.current_unit->flags.cc == CC_NONE)
3718 ? 0
3719 #ifdef HAVE_CRLF
3720 : 2;
3721 #else
3722 : 1;
3723 #endif
3724 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3725 if (dtp->u.p.current_unit->flags.cc != CC_NONE)
3726 {
3727 char * p = fbuf_alloc (dtp->u.p.current_unit, len);
3728 if (!p)
3729 goto io_error;
3730 #ifdef HAVE_CRLF
3731 *(p++) = '\r';
3732 #endif
3733 *p = '\n';
3734 }
3735 if (is_stream_io (dtp))
3736 {
3737 dtp->u.p.current_unit->strm_pos += len;
3738 if (dtp->u.p.current_unit->strm_pos
3739 < ssize (dtp->u.p.current_unit->s))
3740 unit_truncate (dtp->u.p.current_unit,
3741 dtp->u.p.current_unit->strm_pos - 1,
3742 &dtp->common);
3743 }
3744 }
3745
3746 break;
3747
3748 io_error:
3749 generate_error (&dtp->common, LIBERROR_OS, NULL);
3750 break;
3751 }
3752 }
3753
3754 /* Position to the next record, which means moving to the end of the
3755 current record. This can happen under several different
3756 conditions. If the done flag is not set, we get ready to process
3757 the next record. */
3758
3759 void
3760 next_record (st_parameter_dt *dtp, int done)
3761 {
3762 gfc_offset fp; /* File position. */
3763
3764 dtp->u.p.current_unit->read_bad = 0;
3765
3766 if (dtp->u.p.mode == READING)
3767 next_record_r (dtp, done);
3768 else
3769 next_record_w (dtp, done);
3770
3771 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3772
3773 if (!is_stream_io (dtp))
3774 {
3775 /* Since we have changed the position, set it to unspecified so
3776 that INQUIRE(POSITION=) knows it needs to look into it. */
3777 if (done)
3778 dtp->u.p.current_unit->flags.position = POSITION_UNSPECIFIED;
3779
3780 dtp->u.p.current_unit->current_record = 0;
3781 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
3782 {
3783 fp = stell (dtp->u.p.current_unit->s);
3784 /* Calculate next record, rounding up partial records. */
3785 dtp->u.p.current_unit->last_record =
3786 (fp + dtp->u.p.current_unit->recl) /
3787 dtp->u.p.current_unit->recl - 1;
3788 }
3789 else
3790 dtp->u.p.current_unit->last_record++;
3791 }
3792
3793 if (!done)
3794 pre_position (dtp);
3795
3796 smarkeor (dtp->u.p.current_unit->s);
3797 }
3798
3799
3800 /* Finalize the current data transfer. For a nonadvancing transfer,
3801 this means advancing to the next record. For internal units close the
3802 stream associated with the unit. */
3803
3804 static void
3805 finalize_transfer (st_parameter_dt *dtp)
3806 {
3807 GFC_INTEGER_4 cf = dtp->common.flags;
3808
3809 if ((dtp->u.p.ionml != NULL)
3810 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
3811 {
3812 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
3813 namelist_read (dtp);
3814 else
3815 namelist_write (dtp);
3816 }
3817
3818 if (dtp->u.p.current_unit && (dtp->u.p.current_unit->child_dtio > 0))
3819 {
3820 if (cf & IOPARM_DT_HAS_FORMAT)
3821 {
3822 free (dtp->u.p.fmt);
3823 free (dtp->format);
3824 }
3825 return;
3826 }
3827
3828 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
3829 *dtp->size = dtp->u.p.current_unit->size_used;
3830
3831 if (dtp->u.p.eor_condition)
3832 {
3833 generate_error (&dtp->common, LIBERROR_EOR, NULL);
3834 goto done;
3835 }
3836
3837 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
3838 {
3839 if (dtp->u.p.current_unit && current_mode (dtp) == UNFORMATTED_SEQUENTIAL)
3840 dtp->u.p.current_unit->current_record = 0;
3841 goto done;
3842 }
3843
3844 dtp->u.p.transfer = NULL;
3845 if (dtp->u.p.current_unit == NULL)
3846 goto done;
3847
3848 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
3849 {
3850 finish_list_read (dtp);
3851 goto done;
3852 }
3853
3854 if (dtp->u.p.mode == WRITING)
3855 dtp->u.p.current_unit->previous_nonadvancing_write
3856 = dtp->u.p.advance_status == ADVANCE_NO;
3857
3858 if (is_stream_io (dtp))
3859 {
3860 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3861 && dtp->u.p.advance_status != ADVANCE_NO)
3862 next_record (dtp, 1);
3863
3864 goto done;
3865 }
3866
3867 dtp->u.p.current_unit->current_record = 0;
3868
3869 if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
3870 {
3871 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3872 dtp->u.p.seen_dollar = 0;
3873 goto done;
3874 }
3875
3876 /* For non-advancing I/O, save the current maximum position for use in the
3877 next I/O operation if needed. */
3878 if (dtp->u.p.advance_status == ADVANCE_NO)
3879 {
3880 if (dtp->u.p.skips > 0)
3881 {
3882 int tmp;
3883 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
3884 tmp = (int)(dtp->u.p.current_unit->recl
3885 - dtp->u.p.current_unit->bytes_left);
3886 dtp->u.p.max_pos =
3887 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
3888 dtp->u.p.skips = 0;
3889 }
3890 int bytes_written = (int) (dtp->u.p.current_unit->recl
3891 - dtp->u.p.current_unit->bytes_left);
3892 dtp->u.p.current_unit->saved_pos =
3893 dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
3894 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3895 goto done;
3896 }
3897 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3898 && dtp->u.p.mode == WRITING && !is_internal_unit (dtp))
3899 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3900
3901 dtp->u.p.current_unit->saved_pos = 0;
3902
3903 next_record (dtp, 1);
3904
3905 done:
3906 #ifdef HAVE_USELOCALE
3907 if (dtp->u.p.old_locale != (locale_t) 0)
3908 {
3909 uselocale (dtp->u.p.old_locale);
3910 dtp->u.p.old_locale = (locale_t) 0;
3911 }
3912 #else
3913 __gthread_mutex_lock (&old_locale_lock);
3914 if (!--old_locale_ctr)
3915 {
3916 setlocale (LC_NUMERIC, old_locale);
3917 old_locale = NULL;
3918 }
3919 __gthread_mutex_unlock (&old_locale_lock);
3920 #endif
3921 }
3922
3923 /* Transfer function for IOLENGTH. It doesn't actually do any
3924 data transfer, it just updates the length counter. */
3925
3926 static void
3927 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
3928 void *dest __attribute__ ((unused)),
3929 int kind __attribute__((unused)),
3930 size_t size, size_t nelems)
3931 {
3932 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3933 *dtp->iolength += (GFC_IO_INT) (size * nelems);
3934 }
3935
3936
3937 /* Initialize the IOLENGTH data transfer. This function is in essence
3938 a very much simplified version of data_transfer_init(), because it
3939 doesn't have to deal with units at all. */
3940
3941 static void
3942 iolength_transfer_init (st_parameter_dt *dtp)
3943 {
3944 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3945 *dtp->iolength = 0;
3946
3947 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
3948
3949 /* Set up the subroutine that will handle the transfers. */
3950
3951 dtp->u.p.transfer = iolength_transfer;
3952 }
3953
3954
3955 /* Library entry point for the IOLENGTH form of the INQUIRE
3956 statement. The IOLENGTH form requires no I/O to be performed, but
3957 it must still be a runtime library call so that we can determine
3958 the iolength for dynamic arrays and such. */
3959
3960 extern void st_iolength (st_parameter_dt *);
3961 export_proto(st_iolength);
3962
3963 void
3964 st_iolength (st_parameter_dt *dtp)
3965 {
3966 library_start (&dtp->common);
3967 iolength_transfer_init (dtp);
3968 }
3969
3970 extern void st_iolength_done (st_parameter_dt *);
3971 export_proto(st_iolength_done);
3972
3973 void
3974 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
3975 {
3976 free_ionml (dtp);
3977 library_end ();
3978 }
3979
3980
3981 /* The READ statement. */
3982
3983 extern void st_read (st_parameter_dt *);
3984 export_proto(st_read);
3985
3986 void
3987 st_read (st_parameter_dt *dtp)
3988 {
3989 library_start (&dtp->common);
3990
3991 data_transfer_init (dtp, 1);
3992 }
3993
3994 extern void st_read_done (st_parameter_dt *);
3995 export_proto(st_read_done);
3996
3997 void
3998 st_read_done (st_parameter_dt *dtp)
3999 {
4000 finalize_transfer (dtp);
4001
4002 free_ionml (dtp);
4003
4004 /* If this is a parent READ statement we do not need to retain the
4005 internal unit structure for child use. Free it and stash the unit
4006 number for reuse. */
4007 if (dtp->u.p.current_unit != NULL
4008 && dtp->u.p.current_unit->child_dtio == 0)
4009 {
4010 if (is_internal_unit (dtp) &&
4011 (dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0)
4012 {
4013 free (dtp->u.p.current_unit->filename);
4014 dtp->u.p.current_unit->filename = NULL;
4015 free (dtp->u.p.current_unit->s);
4016 dtp->u.p.current_unit->s = NULL;
4017 if (dtp->u.p.current_unit->ls)
4018 free (dtp->u.p.current_unit->ls);
4019 dtp->u.p.current_unit->ls = NULL;
4020 stash_internal_unit (dtp);
4021 }
4022 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
4023 {
4024 free_format_data (dtp->u.p.fmt);
4025 free_format (dtp);
4026 }
4027 unlock_unit (dtp->u.p.current_unit);
4028 }
4029
4030 library_end ();
4031 }
4032
4033 extern void st_write (st_parameter_dt *);
4034 export_proto(st_write);
4035
4036 void
4037 st_write (st_parameter_dt *dtp)
4038 {
4039 library_start (&dtp->common);
4040 data_transfer_init (dtp, 0);
4041 }
4042
4043 extern void st_write_done (st_parameter_dt *);
4044 export_proto(st_write_done);
4045
4046 void
4047 st_write_done (st_parameter_dt *dtp)
4048 {
4049 finalize_transfer (dtp);
4050
4051 if (dtp->u.p.current_unit != NULL
4052 && dtp->u.p.current_unit->child_dtio == 0)
4053 {
4054 /* Deal with endfile conditions associated with sequential files. */
4055 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
4056 switch (dtp->u.p.current_unit->endfile)
4057 {
4058 case AT_ENDFILE: /* Remain at the endfile record. */
4059 break;
4060
4061 case AFTER_ENDFILE:
4062 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
4063 break;
4064
4065 case NO_ENDFILE:
4066 /* Get rid of whatever is after this record. */
4067 if (!is_internal_unit (dtp))
4068 unit_truncate (dtp->u.p.current_unit,
4069 stell (dtp->u.p.current_unit->s),
4070 &dtp->common);
4071 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4072 break;
4073 }
4074
4075 free_ionml (dtp);
4076
4077 /* If this is a parent WRITE statement we do not need to retain the
4078 internal unit structure for child use. Free it and stash the
4079 unit number for reuse. */
4080 if (is_internal_unit (dtp) &&
4081 (dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0)
4082 {
4083 free (dtp->u.p.current_unit->filename);
4084 dtp->u.p.current_unit->filename = NULL;
4085 free (dtp->u.p.current_unit->s);
4086 dtp->u.p.current_unit->s = NULL;
4087 if (dtp->u.p.current_unit->ls)
4088 free (dtp->u.p.current_unit->ls);
4089 dtp->u.p.current_unit->ls = NULL;
4090 stash_internal_unit (dtp);
4091 }
4092 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
4093 {
4094 free_format_data (dtp->u.p.fmt);
4095 free_format (dtp);
4096 }
4097 unlock_unit (dtp->u.p.current_unit);
4098 }
4099 library_end ();
4100 }
4101
4102
4103 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
4104 void
4105 st_wait (st_parameter_wait *wtp __attribute__((unused)))
4106 {
4107 }
4108
4109
4110 /* Receives the scalar information for namelist objects and stores it
4111 in a linked list of namelist_info types. */
4112
4113 static void
4114 set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4115 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4116 GFC_INTEGER_4 dtype, void *dtio_sub, void *vtable)
4117 {
4118 namelist_info *t1 = NULL;
4119 namelist_info *nml;
4120 size_t var_name_len = strlen (var_name);
4121
4122 nml = (namelist_info*) xmalloc (sizeof (namelist_info));
4123
4124 nml->mem_pos = var_addr;
4125 nml->dtio_sub = dtio_sub;
4126 nml->vtable = vtable;
4127
4128 nml->var_name = (char*) xmalloc (var_name_len + 1);
4129 memcpy (nml->var_name, var_name, var_name_len);
4130 nml->var_name[var_name_len] = '\0';
4131
4132 nml->len = (int) len;
4133 nml->string_length = (index_type) string_length;
4134
4135 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
4136 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
4137 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
4138
4139 if (nml->var_rank > 0)
4140 {
4141 nml->dim = (descriptor_dimension*)
4142 xmallocarray (nml->var_rank, sizeof (descriptor_dimension));
4143 nml->ls = (array_loop_spec*)
4144 xmallocarray (nml->var_rank, sizeof (array_loop_spec));
4145 }
4146 else
4147 {
4148 nml->dim = NULL;
4149 nml->ls = NULL;
4150 }
4151
4152 nml->next = NULL;
4153
4154 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
4155 {
4156 dtp->common.flags |= IOPARM_DT_IONML_SET;
4157 dtp->u.p.ionml = nml;
4158 }
4159 else
4160 {
4161 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
4162 t1->next = nml;
4163 }
4164 }
4165
4166 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
4167 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
4168 export_proto(st_set_nml_var);
4169
4170 void
4171 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4172 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4173 GFC_INTEGER_4 dtype)
4174 {
4175 set_nml_var (dtp, var_addr, var_name, len, string_length,
4176 dtype, NULL, NULL);
4177 }
4178
4179
4180 /* Essentially the same as previous but carrying the dtio procedure
4181 and the vtable as additional arguments. */
4182 extern void st_set_nml_dtio_var (st_parameter_dt *dtp, void *, char *,
4183 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4,
4184 void *, void *);
4185 export_proto(st_set_nml_dtio_var);
4186
4187
4188 void
4189 st_set_nml_dtio_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4190 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4191 GFC_INTEGER_4 dtype, void *dtio_sub, void *vtable)
4192 {
4193 set_nml_var (dtp, var_addr, var_name, len, string_length,
4194 dtype, dtio_sub, vtable);
4195 }
4196
4197 /* Store the dimensional information for the namelist object. */
4198 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
4199 index_type, index_type,
4200 index_type);
4201 export_proto(st_set_nml_var_dim);
4202
4203 void
4204 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
4205 index_type stride, index_type lbound,
4206 index_type ubound)
4207 {
4208 namelist_info * nml;
4209 int n;
4210
4211 n = (int)n_dim;
4212
4213 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
4214
4215 GFC_DIMENSION_SET(nml->dim[n],lbound,ubound,stride);
4216 }
4217
4218
4219 /* Once upon a time, a poor innocent Fortran program was reading a
4220 file, when suddenly it hit the end-of-file (EOF). Unfortunately
4221 the OS doesn't tell whether we're at the EOF or whether we already
4222 went past it. Luckily our hero, libgfortran, keeps track of this.
4223 Call this function when you detect an EOF condition. See Section
4224 9.10.2 in F2003. */
4225
4226 void
4227 hit_eof (st_parameter_dt * dtp)
4228 {
4229 dtp->u.p.current_unit->flags.position = POSITION_APPEND;
4230
4231 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
4232 switch (dtp->u.p.current_unit->endfile)
4233 {
4234 case NO_ENDFILE:
4235 case AT_ENDFILE:
4236 generate_error (&dtp->common, LIBERROR_END, NULL);
4237 if (!is_internal_unit (dtp) && !dtp->u.p.namelist_mode)
4238 {
4239 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
4240 dtp->u.p.current_unit->current_record = 0;
4241 }
4242 else
4243 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4244 break;
4245
4246 case AFTER_ENDFILE:
4247 generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
4248 dtp->u.p.current_unit->current_record = 0;
4249 break;
4250 }
4251 else
4252 {
4253 /* Non-sequential files don't have an ENDFILE record, so we
4254 can't be at AFTER_ENDFILE. */
4255 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4256 generate_error (&dtp->common, LIBERROR_END, NULL);
4257 dtp->u.p.current_unit->current_record = 0;
4258 }
4259 }