1 /* Copyright (C) 2002-2013 Free Software Foundation, Inc.
2 Contributed by Andy Vaught and Janne Blomqvist
4 This file is part of the GNU Fortran runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
30 /* file_pos.c-- Implement the file positioning statements, i.e. BACKSPACE,
31 ENDFILE, and REWIND as well as the FLUSH statement. */
34 /* formatted_backspace(fpp, u)-- Move the file back one line. The
35 current position is after the newline that terminates the previous
36 record, and we have to sift backwards to find the newline before
37 that or the start of the file, whichever comes first. */
39 static const int READ_CHUNK
= 4096;
42 formatted_backspace (st_parameter_filepos
*fpp
, gfc_unit
*u
)
48 base
= stell (u
->s
) - 1;
52 n
= (base
< READ_CHUNK
) ? base
: READ_CHUNK
;
54 if (sseek (u
->s
, base
, SEEK_SET
) < 0)
56 if (sread (u
->s
, p
, n
) != n
)
59 /* We have moved backwards from the current position, it should
60 not be possible to get a short read. Because it is not
61 clear what to do about such thing, we ignore the possibility. */
63 /* There is no memrchr() in the C library, so we have to do it
79 /* base is the new pointer. Seek to it exactly. */
81 if (sseek (u
->s
, base
, SEEK_SET
) < 0)
84 u
->endfile
= NO_ENDFILE
;
89 generate_error (&fpp
->common
, LIBERROR_OS
, NULL
);
93 /* unformatted_backspace(fpp) -- Move the file backwards for an unformatted
94 sequential file. We are guaranteed to be between records on entry and
95 we have to shift to the previous record. Loop over subrecords. */
98 unformatted_backspace (st_parameter_filepos
*fpp
, gfc_unit
*u
)
105 char p
[sizeof (GFC_INTEGER_8
)];
107 if (compile_options
.record_marker
== 0)
108 length
= sizeof (GFC_INTEGER_4
);
110 length
= compile_options
.record_marker
;
114 slen
= - (gfc_offset
) length
;
115 if (sseek (u
->s
, slen
, SEEK_CUR
) < 0)
117 if (sread (u
->s
, p
, length
) != length
)
120 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
121 if (likely (u
->flags
.convert
== GFC_CONVERT_NATIVE
))
125 case sizeof(GFC_INTEGER_4
):
126 memcpy (&m4
, p
, sizeof (m4
));
130 case sizeof(GFC_INTEGER_8
):
131 memcpy (&m8
, p
, sizeof (m8
));
136 runtime_error ("Illegal value for record marker");
146 case sizeof(GFC_INTEGER_4
):
147 memcpy (&u32
, p
, sizeof (u32
));
148 u32
= __builtin_bswap32 (u32
);
149 memcpy (&m4
, &u32
, sizeof (m4
));
153 case sizeof(GFC_INTEGER_8
):
154 memcpy (&u64
, p
, sizeof (u64
));
155 u64
= __builtin_bswap64 (u64
);
156 memcpy (&m8
, &u64
, sizeof (m8
));
161 runtime_error ("Illegal value for record marker");
171 if (sseek (u
->s
, -m
-2 * length
, SEEK_CUR
) < 0)
179 generate_error (&fpp
->common
, LIBERROR_OS
, NULL
);
183 extern void st_backspace (st_parameter_filepos
*);
184 export_proto(st_backspace
);
187 st_backspace (st_parameter_filepos
*fpp
)
191 library_start (&fpp
->common
);
193 u
= find_unit (fpp
->common
.unit
);
196 generate_error (&fpp
->common
, LIBERROR_BAD_UNIT
, NULL
);
200 /* Direct access is prohibited, and so is unformatted stream access. */
203 if (u
->flags
.access
== ACCESS_DIRECT
)
205 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
206 "Cannot BACKSPACE a file opened for DIRECT access");
210 if (u
->flags
.access
== ACCESS_STREAM
&& u
->flags
.form
== FORM_UNFORMATTED
)
212 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
213 "Cannot BACKSPACE an unformatted stream file");
217 /* Make sure format buffer is flushed and reset. */
218 if (u
->flags
.form
== FORM_FORMATTED
)
220 int pos
= fbuf_reset (u
);
222 sseek (u
->s
, pos
, SEEK_CUR
);
226 /* Check for special cases involving the ENDFILE record first. */
228 if (u
->endfile
== AFTER_ENDFILE
)
230 u
->endfile
= AT_ENDFILE
;
231 u
->flags
.position
= POSITION_APPEND
;
236 if (stell (u
->s
) == 0)
238 u
->flags
.position
= POSITION_REWIND
;
239 goto done
; /* Common special case */
242 if (u
->mode
== WRITING
)
244 /* If there are previously written bytes from a write with
245 ADVANCE="no", add a record marker before performing the
248 if (u
->previous_nonadvancing_write
)
249 finish_last_advance_record (u
);
251 u
->previous_nonadvancing_write
= 0;
253 unit_truncate (u
, stell (u
->s
), &fpp
->common
);
257 if (u
->flags
.form
== FORM_FORMATTED
)
258 formatted_backspace (fpp
, u
);
260 unformatted_backspace (fpp
, u
);
262 u
->flags
.position
= POSITION_UNSPECIFIED
;
263 u
->endfile
= NO_ENDFILE
;
264 u
->current_record
= 0;
276 extern void st_endfile (st_parameter_filepos
*);
277 export_proto(st_endfile
);
280 st_endfile (st_parameter_filepos
*fpp
)
284 library_start (&fpp
->common
);
286 u
= find_unit (fpp
->common
.unit
);
289 if (u
->flags
.access
== ACCESS_DIRECT
)
291 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
292 "Cannot perform ENDFILE on a file opened "
293 "for DIRECT access");
297 if (u
->flags
.access
== ACCESS_SEQUENTIAL
298 && u
->endfile
== AFTER_ENDFILE
)
300 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
301 "Cannot perform ENDFILE on a file already "
302 "positioned after the EOF marker");
306 /* If there are previously written bytes from a write with ADVANCE="no",
307 add a record marker before performing the ENDFILE. */
309 if (u
->previous_nonadvancing_write
)
310 finish_last_advance_record (u
);
312 u
->previous_nonadvancing_write
= 0;
314 if (u
->current_record
)
317 dtp
.common
= fpp
->common
;
318 memset (&dtp
.u
.p
, 0, sizeof (dtp
.u
.p
));
319 dtp
.u
.p
.current_unit
= u
;
320 next_record (&dtp
, 1);
323 unit_truncate (u
, stell (u
->s
), &fpp
->common
);
324 u
->endfile
= AFTER_ENDFILE
;
325 if (0 == stell (u
->s
))
326 u
->flags
.position
= POSITION_REWIND
;
330 if (fpp
->common
.unit
< 0)
332 generate_error (&fpp
->common
, LIBERROR_BAD_OPTION
,
333 "Bad unit number in statement");
337 u
= find_or_create_unit (fpp
->common
.unit
);
340 /* Open the unit with some default flags. */
341 st_parameter_open opp
;
344 memset (&u_flags
, '\0', sizeof (u_flags
));
345 u_flags
.access
= ACCESS_SEQUENTIAL
;
346 u_flags
.action
= ACTION_READWRITE
;
348 /* Is it unformatted? */
349 if (!(fpp
->common
.flags
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
350 | IOPARM_DT_IONML_SET
)))
351 u_flags
.form
= FORM_UNFORMATTED
;
353 u_flags
.form
= FORM_UNSPECIFIED
;
355 u_flags
.delim
= DELIM_UNSPECIFIED
;
356 u_flags
.blank
= BLANK_UNSPECIFIED
;
357 u_flags
.pad
= PAD_UNSPECIFIED
;
358 u_flags
.decimal
= DECIMAL_UNSPECIFIED
;
359 u_flags
.encoding
= ENCODING_UNSPECIFIED
;
360 u_flags
.async
= ASYNC_UNSPECIFIED
;
361 u_flags
.round
= ROUND_UNSPECIFIED
;
362 u_flags
.sign
= SIGN_UNSPECIFIED
;
363 u_flags
.status
= STATUS_UNKNOWN
;
364 u_flags
.convert
= GFC_CONVERT_NATIVE
;
366 opp
.common
= fpp
->common
;
367 opp
.common
.flags
&= IOPARM_COMMON_MASK
;
368 u
= new_unit (&opp
, u
, &u_flags
);
371 u
->endfile
= AFTER_ENDFILE
;
382 extern void st_rewind (st_parameter_filepos
*);
383 export_proto(st_rewind
);
386 st_rewind (st_parameter_filepos
*fpp
)
390 library_start (&fpp
->common
);
392 u
= find_unit (fpp
->common
.unit
);
395 if (u
->flags
.access
== ACCESS_DIRECT
)
396 generate_error (&fpp
->common
, LIBERROR_BAD_OPTION
,
397 "Cannot REWIND a file opened for DIRECT access");
400 /* If there are previously written bytes from a write with ADVANCE="no",
401 add a record marker before performing the ENDFILE. */
403 if (u
->previous_nonadvancing_write
)
404 finish_last_advance_record (u
);
406 u
->previous_nonadvancing_write
= 0;
412 if (sseek (u
->s
, 0, SEEK_SET
) < 0)
413 generate_error (&fpp
->common
, LIBERROR_OS
, NULL
);
415 /* Set this for compatibilty with g77 for /dev/null. */
416 if (ssize (u
->s
) == 0)
417 u
->endfile
= AT_ENDFILE
;
420 /* We are rewinding so we are not at the end. */
421 u
->endfile
= NO_ENDFILE
;
424 u
->current_record
= 0;
428 /* Update position for INQUIRE. */
429 u
->flags
.position
= POSITION_REWIND
;
437 extern void st_flush (st_parameter_filepos
*);
438 export_proto(st_flush
);
441 st_flush (st_parameter_filepos
*fpp
)
445 library_start (&fpp
->common
);
447 u
= find_unit (fpp
->common
.unit
);
450 /* Make sure format buffer is flushed. */
451 if (u
->flags
.form
== FORM_FORMATTED
)
452 fbuf_flush (u
, u
->mode
);
458 /* FLUSH on unconnected unit is illegal: F95 std., 9.3.5. */
459 generate_error (&fpp
->common
, LIBERROR_BAD_OPTION
,
460 "Specified UNIT in FLUSH is not connected");