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/>. */
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");
144 case sizeof(GFC_INTEGER_4
):
145 reverse_memcpy (&m4
, p
, sizeof (m4
));
149 case sizeof(GFC_INTEGER_8
):
150 reverse_memcpy (&m8
, p
, sizeof (m8
));
155 runtime_error ("Illegal value for record marker");
165 if (sseek (u
->s
, -m
-2 * length
, SEEK_CUR
) < 0)
173 generate_error (&fpp
->common
, LIBERROR_OS
, NULL
);
177 extern void st_backspace (st_parameter_filepos
*);
178 export_proto(st_backspace
);
181 st_backspace (st_parameter_filepos
*fpp
)
185 library_start (&fpp
->common
);
187 u
= find_unit (fpp
->common
.unit
);
190 generate_error (&fpp
->common
, LIBERROR_BAD_UNIT
, NULL
);
194 /* Direct access is prohibited, and so is unformatted stream access. */
197 if (u
->flags
.access
== ACCESS_DIRECT
)
199 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
200 "Cannot BACKSPACE a file opened for DIRECT access");
204 if (u
->flags
.access
== ACCESS_STREAM
&& u
->flags
.form
== FORM_UNFORMATTED
)
206 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
207 "Cannot BACKSPACE an unformatted stream file");
211 /* Make sure format buffer is flushed and reset. */
212 if (u
->flags
.form
== FORM_FORMATTED
)
214 int pos
= fbuf_reset (u
);
216 sseek (u
->s
, pos
, SEEK_CUR
);
220 /* Check for special cases involving the ENDFILE record first. */
222 if (u
->endfile
== AFTER_ENDFILE
)
224 u
->endfile
= AT_ENDFILE
;
225 u
->flags
.position
= POSITION_APPEND
;
230 if (stell (u
->s
) == 0)
232 u
->flags
.position
= POSITION_REWIND
;
233 goto done
; /* Common special case */
236 if (u
->mode
== WRITING
)
238 /* If there are previously written bytes from a write with
239 ADVANCE="no", add a record marker before performing the
242 if (u
->previous_nonadvancing_write
)
243 finish_last_advance_record (u
);
245 u
->previous_nonadvancing_write
= 0;
247 unit_truncate (u
, stell (u
->s
), &fpp
->common
);
251 if (u
->flags
.form
== FORM_FORMATTED
)
252 formatted_backspace (fpp
, u
);
254 unformatted_backspace (fpp
, u
);
256 u
->flags
.position
= POSITION_UNSPECIFIED
;
257 u
->endfile
= NO_ENDFILE
;
258 u
->current_record
= 0;
270 extern void st_endfile (st_parameter_filepos
*);
271 export_proto(st_endfile
);
274 st_endfile (st_parameter_filepos
*fpp
)
278 library_start (&fpp
->common
);
280 u
= find_unit (fpp
->common
.unit
);
283 if (u
->flags
.access
== ACCESS_DIRECT
)
285 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
286 "Cannot perform ENDFILE on a file opened "
287 "for DIRECT access");
291 if (u
->flags
.access
== ACCESS_SEQUENTIAL
292 && u
->endfile
== AFTER_ENDFILE
)
294 generate_error (&fpp
->common
, LIBERROR_OPTION_CONFLICT
,
295 "Cannot perform ENDFILE on a file already "
296 "positioned after the EOF marker");
300 /* If there are previously written bytes from a write with ADVANCE="no",
301 add a record marker before performing the ENDFILE. */
303 if (u
->previous_nonadvancing_write
)
304 finish_last_advance_record (u
);
306 u
->previous_nonadvancing_write
= 0;
308 if (u
->current_record
)
311 dtp
.common
= fpp
->common
;
312 memset (&dtp
.u
.p
, 0, sizeof (dtp
.u
.p
));
313 dtp
.u
.p
.current_unit
= u
;
314 next_record (&dtp
, 1);
317 unit_truncate (u
, stell (u
->s
), &fpp
->common
);
318 u
->endfile
= AFTER_ENDFILE
;
319 if (0 == stell (u
->s
))
320 u
->flags
.position
= POSITION_REWIND
;
324 if (fpp
->common
.unit
< 0)
326 generate_error (&fpp
->common
, LIBERROR_BAD_OPTION
,
327 "Bad unit number in statement");
331 u
= find_or_create_unit (fpp
->common
.unit
);
334 /* Open the unit with some default flags. */
335 st_parameter_open opp
;
338 memset (&u_flags
, '\0', sizeof (u_flags
));
339 u_flags
.access
= ACCESS_SEQUENTIAL
;
340 u_flags
.action
= ACTION_READWRITE
;
342 /* Is it unformatted? */
343 if (!(fpp
->common
.flags
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
344 | IOPARM_DT_IONML_SET
)))
345 u_flags
.form
= FORM_UNFORMATTED
;
347 u_flags
.form
= FORM_UNSPECIFIED
;
349 u_flags
.delim
= DELIM_UNSPECIFIED
;
350 u_flags
.blank
= BLANK_UNSPECIFIED
;
351 u_flags
.pad
= PAD_UNSPECIFIED
;
352 u_flags
.decimal
= DECIMAL_UNSPECIFIED
;
353 u_flags
.encoding
= ENCODING_UNSPECIFIED
;
354 u_flags
.async
= ASYNC_UNSPECIFIED
;
355 u_flags
.round
= ROUND_UNSPECIFIED
;
356 u_flags
.sign
= SIGN_UNSPECIFIED
;
357 u_flags
.status
= STATUS_UNKNOWN
;
358 u_flags
.convert
= GFC_CONVERT_NATIVE
;
360 opp
.common
= fpp
->common
;
361 opp
.common
.flags
&= IOPARM_COMMON_MASK
;
362 u
= new_unit (&opp
, u
, &u_flags
);
365 u
->endfile
= AFTER_ENDFILE
;
376 extern void st_rewind (st_parameter_filepos
*);
377 export_proto(st_rewind
);
380 st_rewind (st_parameter_filepos
*fpp
)
384 library_start (&fpp
->common
);
386 u
= find_unit (fpp
->common
.unit
);
389 if (u
->flags
.access
== ACCESS_DIRECT
)
390 generate_error (&fpp
->common
, LIBERROR_BAD_OPTION
,
391 "Cannot REWIND a file opened for DIRECT access");
394 /* If there are previously written bytes from a write with ADVANCE="no",
395 add a record marker before performing the ENDFILE. */
397 if (u
->previous_nonadvancing_write
)
398 finish_last_advance_record (u
);
400 u
->previous_nonadvancing_write
= 0;
406 if (sseek (u
->s
, 0, SEEK_SET
) < 0)
407 generate_error (&fpp
->common
, LIBERROR_OS
, NULL
);
409 /* Handle special files like /dev/null differently. */
410 if (!is_special (u
->s
))
412 /* We are rewinding so we are not at the end. */
413 u
->endfile
= NO_ENDFILE
;
417 /* Set this for compatibilty with g77 for /dev/null. */
418 if (file_length (u
->s
) == 0 && stell (u
->s
) == 0)
419 u
->endfile
= AT_ENDFILE
;
420 /* Future refinements on special files can go here. */
423 u
->current_record
= 0;
427 /* Update position for INQUIRE. */
428 u
->flags
.position
= POSITION_REWIND
;
436 extern void st_flush (st_parameter_filepos
*);
437 export_proto(st_flush
);
440 st_flush (st_parameter_filepos
*fpp
)
444 library_start (&fpp
->common
);
446 u
= find_unit (fpp
->common
.unit
);
449 /* Make sure format buffer is flushed. */
450 if (u
->flags
.form
== FORM_FORMATTED
)
451 fbuf_flush (u
, u
->mode
);
457 /* FLUSH on unconnected unit is illegal: F95 std., 9.3.5. */
458 generate_error (&fpp
->common
, LIBERROR_BAD_OPTION
,
459 "Specified UNIT in FLUSH is not connected");