Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libgfortran / io / backspace.c
blobf8ab01c348809d837dcfa8b4973b1ff67dff3b0a
1 /* Copyright (C) 2002-2003 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 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 2, or (at your option)
9 any later version.
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
18 executable.)
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with Libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
30 #include "config.h"
31 #include "libgfortran.h"
32 #include "io.h"
34 /* backspace.c -- Implement the BACKSPACE statement */
36 /* formatted_backspace(void)-- Move the file back one line. The
37 * current position is after the newline that terminates the previous
38 * record, and we have to sift backwards to find the newline before
39 * that or the start of the file, whichever comes first. */
41 #define READ_CHUNK 4096
43 static void
44 formatted_backspace (void)
46 gfc_offset base;
47 char *p;
48 int n;
50 base = file_position (current_unit->s) - 1;
54 n = (base < READ_CHUNK) ? base : READ_CHUNK;
55 base -= n;
57 p = salloc_r_at (current_unit->s, &n, base);
58 if (p == NULL)
59 goto io_error;
61 /* Because we've moved backwords from the current position, it
62 * should not be possible to get a short read. Because it isn't
63 * clear what to do about such thing, we ignore the possibility. */
65 /* There is no memrchr() in the C library, so we have to do it
66 * ourselves. */
68 n--;
69 while (n >= 0)
71 if (p[n] == '\n')
73 base += n + 1;
74 goto done;
77 n--;
81 while (base != 0);
83 /* base is the new pointer. Seek to it exactly */
84 done:
85 if (sseek (current_unit->s, base) == FAILURE)
86 goto io_error;
87 current_unit->last_record--;
88 current_unit->endfile = NO_ENDFILE;
90 return;
92 io_error:
93 generate_error (ERROR_OS, NULL);
97 /* unformatted_backspace()-- Move the file backwards for an
98 * unformatted sequential file. We are guaranteed to be between
99 * records on entry and we have to shift to the previous record. */
101 static void
102 unformatted_backspace (void)
104 gfc_offset *p, new;
105 int length;
107 length = sizeof (gfc_offset);
109 p = (gfc_offset *) salloc_r_at (current_unit->s, &length,
110 file_position (current_unit->s) - length);
111 if (p == NULL)
112 goto io_error;
114 new = file_position (current_unit->s) - *p - length;
115 if (sseek (current_unit->s, new) == FAILURE)
116 goto io_error;
118 current_unit->last_record--;
119 return;
121 io_error:
122 generate_error (ERROR_OS, NULL);
126 extern void st_backspace (void);
127 export_proto(st_backspace);
129 void
130 st_backspace (void)
132 gfc_unit *u;
134 library_start ();
136 u = find_unit (ioparm.unit);
137 if (u == NULL)
139 generate_error (ERROR_BAD_UNIT, NULL);
140 goto done;
143 current_unit = u;
145 /* Ignore direct access. Non-advancing I/O is only allowed for
146 * formatted sequential I/O and the next direct access transfer
147 * repositions the file anyway. */
149 if (u->flags.access == ACCESS_DIRECT)
150 goto done;
152 /* Check for special cases involving the ENDFILE record first */
154 if (u->endfile == AFTER_ENDFILE)
155 u->endfile = AT_ENDFILE;
156 else
158 if (u->current_record)
159 next_record (1);
161 if (file_position (u->s) == 0)
162 goto done; /* Common special case */
164 if (u->flags.form == FORM_FORMATTED)
165 formatted_backspace ();
166 else
167 unformatted_backspace ();
170 done:
171 library_end ();