1 /* Copyright (C) 2002-2003, 2005, 2006, 2007, 2009 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/>. */
28 /* file_pos.c-- Implement the file positioning statements, i.e. BACKSPACE,
29 ENDFILE, and REWIND as well as the FLUSH statement. */
32 /* formatted_backspace(fpp, u)-- Move the file back one line. The
33 current position is after the newline that terminates the previous
34 record, and we have to sift backwards to find the newline before
35 that or the start of the file, whichever comes first. */
37 static const int READ_CHUNK
= 4096;
40 formatted_backspace (st_parameter_filepos
*fpp
, gfc_unit
*u
)
46 base
= stell (u
->s
) - 1;
50 n
= (base
< READ_CHUNK
) ? base
: READ_CHUNK
;
52 if (sseek (u
->s
, base
, SEEK_SET
) < 0)
54 if (sread (u
->s
, p
, n
) != n
)
57 /* We have moved backwards from the current position, it should
58 not be possible to get a short read. Because it is not
59 clear what to do about such thing, we ignore the possibility. */
61 /* There is no memrchr() in the C library, so we have to do it
77 /* base is the new pointer. Seek to it exactly. */
79 if (sseek (u
->s
, base
, SEEK_SET
) < 0)
82 u
->endfile
= NO_ENDFILE
;
87 generate_error (&fpp
->common
, LIBERROR_OS
, NULL
);
91 /* unformatted_backspace(fpp) -- Move the file backwards for an unformatted
92 sequential file. We are guaranteed to be between records on entry and
93 we have to shift to the previous record. Loop over subrecords. */
96 unformatted_backspace (st_parameter_filepos
*fpp
, gfc_unit
*u
)
103 char p
[sizeof (GFC_INTEGER_8
)];
105 if (compile_options
.record_marker
== 0)
106 length
= sizeof (GFC_INTEGER_4
);
108 length
= compile_options
.record_marker
;
112 slen
= - (gfc_offset
) length
;
113 if (sseek (u
->s
, slen
, SEEK_CUR
) < 0)
115 if (sread (u
->s
, p
, length
) != length
)
118 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
119 if (likely (u
->flags
.convert
== GFC_CONVERT_NATIVE
))
123 case sizeof(GFC_INTEGER_4
):
124 memcpy (&m4
, p
, sizeof (m4
));
128 case sizeof(GFC_INTEGER_8
):
129 memcpy (&m8
, p
, sizeof (m8
));
134 runtime_error ("Illegal value for record marker");
142 case sizeof(GFC_INTEGER_4
):
143 reverse_memcpy (&m4
, p
, sizeof (m4
));
147 case sizeof(GFC_INTEGER_8
):
148 reverse_memcpy (&m8
, p
, sizeof (m8
));
153 runtime_error ("Illegal value for record marker");
163 if (sseek (u
->s
, -m
-2 * length
, SEEK_CUR
) < 0)
171 generate_error (&fpp
->common
, LIBERROR_OS
, NULL
);
175 extern void st_backspace (st_parameter_filepos
*);
176 export_proto(st_backspace
);
179 st_backspace (st_parameter_filepos
*fpp
)
183 library_start (&fpp
->common
);
185 u
= find_unit (fpp
->common
.unit
);
188 generate_error (&fpp
->common
, LIBERROR_BAD_UNIT
, NULL
);
192 /* Direct access is prohibited, and so is unformatted stream access. */
195 if (u
->flags
.access
== ACCESS_DIRECT
)
197 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
198 "Cannot BACKSPACE a file opened for DIRECT access");
202 if (u
->flags
.access
== ACCESS_STREAM
&& u
->flags
.form
== FORM_UNFORMATTED
)
204 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
205 "Cannot BACKSPACE an unformatted stream file");
209 /* Make sure format buffer is flushed and reset. */
210 if (u
->flags
.form
== FORM_FORMATTED
)
212 int pos
= fbuf_reset (u
);
214 sseek (u
->s
, pos
, SEEK_CUR
);
218 /* Check for special cases involving the ENDFILE record first. */
220 if (u
->endfile
== AFTER_ENDFILE
)
222 u
->endfile
= AT_ENDFILE
;
223 u
->flags
.position
= POSITION_APPEND
;
228 if (stell (u
->s
) == 0)
230 u
->flags
.position
= POSITION_REWIND
;
231 goto done
; /* Common special case */
234 if (u
->mode
== WRITING
)
236 /* If there are previously written bytes from a write with
237 ADVANCE="no", add a record marker before performing the
240 if (u
->previous_nonadvancing_write
)
241 finish_last_advance_record (u
);
243 u
->previous_nonadvancing_write
= 0;
245 unit_truncate (u
, stell (u
->s
), &fpp
->common
);
249 if (u
->flags
.form
== FORM_FORMATTED
)
250 formatted_backspace (fpp
, u
);
252 unformatted_backspace (fpp
, u
);
254 u
->flags
.position
= POSITION_UNSPECIFIED
;
255 u
->endfile
= NO_ENDFILE
;
256 u
->current_record
= 0;
268 extern void st_endfile (st_parameter_filepos
*);
269 export_proto(st_endfile
);
272 st_endfile (st_parameter_filepos
*fpp
)
276 library_start (&fpp
->common
);
278 u
= find_unit (fpp
->common
.unit
);
281 if (u
->flags
.access
== ACCESS_DIRECT
)
283 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
284 "Cannot perform ENDFILE on a file opened"
285 " for DIRECT access");
289 /* If there are previously written bytes from a write with ADVANCE="no",
290 add a record marker before performing the ENDFILE. */
292 if (u
->previous_nonadvancing_write
)
293 finish_last_advance_record (u
);
295 u
->previous_nonadvancing_write
= 0;
297 if (u
->current_record
)
300 dtp
.common
= fpp
->common
;
301 memset (&dtp
.u
.p
, 0, sizeof (dtp
.u
.p
));
302 dtp
.u
.p
.current_unit
= u
;
303 next_record (&dtp
, 1);
306 unit_truncate (u
, stell (u
->s
), &fpp
->common
);
307 u
->endfile
= AFTER_ENDFILE
;
308 if (0 == stell (u
->s
))
309 u
->flags
.position
= POSITION_REWIND
;
318 extern void st_rewind (st_parameter_filepos
*);
319 export_proto(st_rewind
);
322 st_rewind (st_parameter_filepos
*fpp
)
326 library_start (&fpp
->common
);
328 u
= find_unit (fpp
->common
.unit
);
331 if (u
->flags
.access
== ACCESS_DIRECT
)
332 generate_error (&fpp
->common
, LIBERROR_BAD_OPTION
,
333 "Cannot REWIND a file opened for DIRECT access");
336 /* If there are previously written bytes from a write with ADVANCE="no",
337 add a record marker before performing the ENDFILE. */
339 if (u
->previous_nonadvancing_write
)
340 finish_last_advance_record (u
);
342 u
->previous_nonadvancing_write
= 0;
344 /* Flush the buffers. If we have been writing to the file, the last
345 written record is the last record in the file, so truncate the
346 file now. Reset to read mode so two consecutive rewind
347 statements do not delete the file contents. */
348 if (u
->mode
== WRITING
)
350 /* unit_truncate takes care of flushing. */
351 unit_truncate (u
, stell (u
->s
), &fpp
->common
);
352 /* .. but we still need to reset since we're going to seek. */
357 /* Make sure buffers are reset. */
358 if (u
->flags
.form
== FORM_FORMATTED
)
366 if (sseek (u
->s
, 0, SEEK_SET
) < 0)
367 generate_error (&fpp
->common
, LIBERROR_OS
, NULL
);
369 /* Handle special files like /dev/null differently. */
370 if (!is_special (u
->s
))
372 /* We are rewinding so we are not at the end. */
373 u
->endfile
= NO_ENDFILE
;
377 /* Set this for compatibilty with g77 for /dev/null. */
378 if (file_length (u
->s
) == 0 && stell (u
->s
) == 0)
379 u
->endfile
= AT_ENDFILE
;
380 /* Future refinements on special files can go here. */
383 u
->current_record
= 0;
387 /* Update position for INQUIRE. */
388 u
->flags
.position
= POSITION_REWIND
;
396 extern void st_flush (st_parameter_filepos
*);
397 export_proto(st_flush
);
400 st_flush (st_parameter_filepos
*fpp
)
404 library_start (&fpp
->common
);
406 u
= find_unit (fpp
->common
.unit
);
409 /* Make sure format buffer is flushed. */
410 if (u
->flags
.form
== FORM_FORMATTED
)
411 fbuf_flush (u
, u
->mode
);
417 /* FLUSH on unconnected unit is illegal: F95 std., 9.3.5. */
418 generate_error (&fpp
->common
, LIBERROR_BAD_OPTION
,
419 "Specified UNIT in FLUSH is not connected");