tree-vectorizer.c (slpeel_verify_cfg_after_peeling): Define only if checking is enabled.
[official-gcc.git] / libgfortran / io / backspace.c
blobc40e506c876476694e4574ff58079e52792aa678
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 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 You should have received a copy of the GNU General Public License
17 along with Libgfortran; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "libgfortran.h"
23 #include "io.h"
25 /* backspace.c -- Implement the BACKSPACE statement */
27 /* formatted_backspace(void)-- Move the file back one line. The
28 * current position is after the newline that terminates the previous
29 * record, and we have to sift backwards to find the newline before
30 * that or the start of the file, whichever comes first. */
32 #define READ_CHUNK 4096
34 static void
35 formatted_backspace (void)
37 gfc_offset base;
38 char *p;
39 int n;
41 base = file_position (current_unit->s) - 1;
45 n = (base < READ_CHUNK) ? base : READ_CHUNK;
46 base -= n;
48 p = salloc_r_at (current_unit->s, &n, base);
49 if (p == NULL)
50 goto io_error;
52 /* Because we've moved backwords from the current position, it
53 * should not be possible to get a short read. Because it isn't
54 * clear what to do about such thing, we ignore the possibility. */
56 /* There is no memrchr() in the C library, so we have to do it
57 * ourselves. */
59 n--;
60 while (n >= 0)
62 if (p[n] == '\n')
64 base += n + 1;
65 goto done;
68 n--;
72 while (base != 0);
74 /* base is the new pointer. Seek to it exactly */
76 done:
77 if (sseek (current_unit->s, base) == FAILURE)
78 goto io_error;
79 current_unit->last_record--;
81 return;
83 io_error:
84 generate_error (ERROR_OS, NULL);
88 /* unformatted_backspace()-- Move the file backwards for an
89 * unformatted sequential file. We are guaranteed to be between
90 * records on entry and we have to shift to the previous record. */
92 static void
93 unformatted_backspace (void)
95 gfc_offset *p, new;
96 int length;
98 length = sizeof (gfc_offset);
100 p = (gfc_offset *) salloc_r_at (current_unit->s, &length,
101 file_position (current_unit->s) - length);
102 if (p == NULL)
103 goto io_error;
105 new = file_position (current_unit->s) - *p - length;
106 if (sseek (current_unit->s, new) == FAILURE)
107 goto io_error;
109 current_unit->last_record--;
110 return;
112 io_error:
113 generate_error (ERROR_OS, NULL);
117 void
118 st_backspace (void)
120 gfc_unit *u;
122 library_start ();
124 u = find_unit (ioparm.unit);
125 if (u == NULL)
127 generate_error (ERROR_BAD_UNIT, NULL);
128 goto done;
131 current_unit = u;
133 /* Ignore direct access. Non-advancing I/O is only allowed for
134 * formatted sequential I/O and the next direct access transfer
135 * repositions the file anyway. */
137 if (u->flags.access == ACCESS_DIRECT)
138 goto done;
140 /* Check for special cases involving the ENDFILE record first */
142 if (u->endfile == AFTER_ENDFILE)
143 u->endfile = AT_ENDFILE;
144 else
146 if (u->current_record)
147 next_record (1);
149 if (file_position (u->s) == 0)
150 goto done; /* Common special case */
152 if (u->flags.form == FORM_FORMATTED)
153 formatted_backspace ();
154 else
155 unformatted_backspace ();
158 done:
159 library_end ();