OSDN Git Service

ecee101b6792827d292c63b4207f109fec13625f
[pf3gnuchains/gcc-fork.git] / libgfortran / io / file_pos.c
1 /* Copyright (C) 2002-2003, 2005, 2006, 2007 Free Software Foundation, Inc.
2    Contributed by Andy Vaught and Janne Blomqvist
3
4 This file is part of the GNU Fortran runtime library (libgfortran).
5
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.
10
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.)
19
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.
24
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, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA.  */
29
30 #include "io.h"
31 #include <string.h>
32
33 /* file_pos.c-- Implement the file positioning statements, i.e. BACKSPACE,
34    ENDFILE, and REWIND as well as the FLUSH statement.  */
35
36
37 /* formatted_backspace(fpp, u)-- Move the file back one line.  The
38    current position is after the newline that terminates the previous
39    record, and we have to sift backwards to find the newline before
40    that or the start of the file, whichever comes first.  */
41
42 static const int READ_CHUNK = 4096;
43
44 static void
45 formatted_backspace (st_parameter_filepos *fpp, gfc_unit *u)
46 {
47   gfc_offset base;
48   char p[READ_CHUNK];
49   ssize_t n;
50
51   base = stell (u->s) - 1;
52
53   do
54     {
55       n = (base < READ_CHUNK) ? base : READ_CHUNK;
56       base -= n;
57       if (sseek (u->s, base, SEEK_SET) < 0)
58         goto io_error;
59       if (sread (u->s, p, n) != n)
60         goto io_error;
61
62       /* We have moved backwards from the current position, it should
63          not be possible to get a short read.  Because it is not
64          clear what to do about such thing, we ignore the possibility.  */
65
66       /* There is no memrchr() in the C library, so we have to do it
67          ourselves.  */
68
69       while (n > 0)
70         {
71           n--;
72           if (p[n] == '\n')
73             {
74               base += n + 1;
75               goto done;
76             }
77         }
78
79     }
80   while (base != 0);
81
82   /* base is the new pointer.  Seek to it exactly.  */
83  done:
84   if (sseek (u->s, base, SEEK_SET) < 0)
85     goto io_error;
86   u->last_record--;
87   u->endfile = NO_ENDFILE;
88
89   return;
90
91  io_error:
92   generate_error (&fpp->common, LIBERROR_OS, NULL);
93 }
94
95
96 /* unformatted_backspace(fpp) -- Move the file backwards for an unformatted
97    sequential file.  We are guaranteed to be between records on entry and 
98    we have to shift to the previous record.  Loop over subrecords.  */
99
100 static void
101 unformatted_backspace (st_parameter_filepos *fpp, gfc_unit *u)
102 {
103   gfc_offset m, slen;
104   GFC_INTEGER_4 m4;
105   GFC_INTEGER_8 m8;
106   ssize_t length;
107   int continued;
108   char p[sizeof (GFC_INTEGER_8)];
109
110   if (compile_options.record_marker == 0)
111     length = sizeof (GFC_INTEGER_4);
112   else
113     length = compile_options.record_marker;
114
115   do
116     {
117       slen = - (gfc_offset) length;
118       if (sseek (u->s, slen, SEEK_CUR) < 0)
119         goto io_error;
120       if (sread (u->s, p, length) != length)
121         goto io_error;
122
123       /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here.  */
124       if (likely (u->flags.convert == GFC_CONVERT_NATIVE))
125         {
126           switch (length)
127             {
128             case sizeof(GFC_INTEGER_4):
129               memcpy (&m4, p, sizeof (m4));
130               m = m4;
131               break;
132
133             case sizeof(GFC_INTEGER_8):
134               memcpy (&m8, p, sizeof (m8));
135               m = m8;
136               break;
137
138             default:
139               runtime_error ("Illegal value for record marker");
140               break;
141             }
142         }
143       else
144         {
145           switch (length)
146             {
147             case sizeof(GFC_INTEGER_4):
148               reverse_memcpy (&m4, p, sizeof (m4));
149               m = m4;
150               break;
151
152             case sizeof(GFC_INTEGER_8):
153               reverse_memcpy (&m8, p, sizeof (m8));
154               m = m8;
155               break;
156
157             default:
158               runtime_error ("Illegal value for record marker");
159               break;
160             }
161
162         }
163
164       continued = m < 0;
165       if (continued)
166         m = -m;
167
168       if (sseek (u->s, -m -2 * length, SEEK_CUR) < 0)
169         goto io_error;
170     } while (continued);
171
172   u->last_record--;
173   return;
174
175  io_error:
176   generate_error (&fpp->common, LIBERROR_OS, NULL);
177 }
178
179
180 extern void st_backspace (st_parameter_filepos *);
181 export_proto(st_backspace);
182
183 void
184 st_backspace (st_parameter_filepos *fpp)
185 {
186   gfc_unit *u;
187
188   library_start (&fpp->common);
189
190   u = find_unit (fpp->common.unit);
191   if (u == NULL)
192     {
193       generate_error (&fpp->common, LIBERROR_BAD_UNIT, NULL);
194       goto done;
195     }
196
197   /* Direct access is prohibited, and so is unformatted stream access.  */
198
199
200   if (u->flags.access == ACCESS_DIRECT)
201     {
202       generate_error (&fpp->common, LIBERROR_OPTION_CONFLICT,
203                       "Cannot BACKSPACE a file opened for DIRECT access");
204       goto done;
205     }
206
207   if (u->flags.access == ACCESS_STREAM && u->flags.form == FORM_UNFORMATTED)
208     {
209       generate_error (&fpp->common, LIBERROR_OPTION_CONFLICT,
210                       "Cannot BACKSPACE an unformatted stream file");
211       goto done;
212     }
213
214   /* Make sure format buffer is flushed and reset.  */
215   if (u->flags.form == FORM_FORMATTED)
216     {
217       int pos = fbuf_reset (u);
218       if (pos != 0)
219         sseek (u->s, pos, SEEK_CUR);
220     }
221
222   
223   /* Check for special cases involving the ENDFILE record first.  */
224
225   if (u->endfile == AFTER_ENDFILE)
226     {
227       u->endfile = AT_ENDFILE;
228       u->flags.position = POSITION_APPEND;
229       sflush (u->s);
230     }
231   else
232     {
233       if (stell (u->s) == 0)
234         {
235           u->flags.position = POSITION_REWIND;
236           goto done;            /* Common special case */
237         }
238
239       if (u->mode == WRITING)
240         {
241           /* If there are previously written bytes from a write with
242              ADVANCE="no", add a record marker before performing the
243              BACKSPACE.  */
244
245           if (u->previous_nonadvancing_write)
246             finish_last_advance_record (u);
247
248           u->previous_nonadvancing_write = 0;
249
250           unit_truncate (u, stell (u->s), &fpp->common);
251           u->mode = READING;
252         }
253
254       if (u->flags.form == FORM_FORMATTED)
255         formatted_backspace (fpp, u);
256       else
257         unformatted_backspace (fpp, u);
258
259       u->flags.position = POSITION_UNSPECIFIED;
260       u->endfile = NO_ENDFILE;
261       u->current_record = 0;
262       u->bytes_left = 0;
263     }
264
265  done:
266   if (u != NULL)
267     unlock_unit (u);
268
269   library_end ();
270 }
271
272
273 extern void st_endfile (st_parameter_filepos *);
274 export_proto(st_endfile);
275
276 void
277 st_endfile (st_parameter_filepos *fpp)
278 {
279   gfc_unit *u;
280
281   library_start (&fpp->common);
282
283   u = find_unit (fpp->common.unit);
284   if (u != NULL)
285     {
286       if (u->flags.access == ACCESS_DIRECT)
287         {
288           generate_error (&fpp->common, LIBERROR_OPTION_CONFLICT,
289                           "Cannot perform ENDFILE on a file opened"
290                           " for DIRECT access");
291           goto done;
292         }
293
294       /* If there are previously written bytes from a write with ADVANCE="no",
295          add a record marker before performing the ENDFILE.  */
296
297       if (u->previous_nonadvancing_write)
298         finish_last_advance_record (u);
299
300       u->previous_nonadvancing_write = 0;
301
302       if (u->current_record)
303         {
304           st_parameter_dt dtp;
305           dtp.common = fpp->common;
306           memset (&dtp.u.p, 0, sizeof (dtp.u.p));
307           dtp.u.p.current_unit = u;
308           next_record (&dtp, 1);
309         }
310
311       unit_truncate (u, stell (u->s), &fpp->common);
312       u->endfile = AFTER_ENDFILE;
313       if (0 == stell (u->s))
314         u->flags.position = POSITION_REWIND;
315     done:
316       unlock_unit (u);
317     }
318
319   library_end ();
320 }
321
322
323 extern void st_rewind (st_parameter_filepos *);
324 export_proto(st_rewind);
325
326 void
327 st_rewind (st_parameter_filepos *fpp)
328 {
329   gfc_unit *u;
330
331   library_start (&fpp->common);
332
333   u = find_unit (fpp->common.unit);
334   if (u != NULL)
335     {
336       if (u->flags.access == ACCESS_DIRECT)
337         generate_error (&fpp->common, LIBERROR_BAD_OPTION,
338                         "Cannot REWIND a file opened for DIRECT access");
339       else
340         {
341           /* If there are previously written bytes from a write with ADVANCE="no",
342              add a record marker before performing the ENDFILE.  */
343
344           if (u->previous_nonadvancing_write)
345             finish_last_advance_record (u);
346
347           u->previous_nonadvancing_write = 0;
348
349           /* Flush the buffers.  If we have been writing to the file, the last
350                written record is the last record in the file, so truncate the
351                file now.  Reset to read mode so two consecutive rewind
352                statements do not delete the file contents.  */
353           if (u->mode == WRITING)
354             {
355               /* unit_truncate takes care of flushing.  */
356               unit_truncate (u, stell (u->s), &fpp->common);
357               /* .. but we still need to reset since we're going to seek.  */
358               fbuf_reset (u);
359             }
360           else
361             {
362               /* Make sure buffers are reset.  */
363               if (u->flags.form == FORM_FORMATTED)
364                 fbuf_reset (u);
365               sflush (u->s);
366             }              
367
368           u->mode = READING;
369           u->last_record = 0;
370
371           if (sseek (u->s, 0, SEEK_SET) < 0)
372             generate_error (&fpp->common, LIBERROR_OS, NULL);
373
374           /* Handle special files like /dev/null differently.  */
375           if (!is_special (u->s))
376             {
377               /* We are rewinding so we are not at the end.  */
378               u->endfile = NO_ENDFILE;
379             }
380           else
381             {
382               /* Set this for compatibilty with g77 for /dev/null.  */
383               if (file_length (u->s) == 0  && stell (u->s) == 0)
384                 u->endfile = AT_ENDFILE;
385               /* Future refinements on special files can go here.  */
386             }
387
388           u->current_record = 0;
389           u->strm_pos = 1;
390           u->read_bad = 0;
391         }
392       /* Update position for INQUIRE.  */
393       u->flags.position = POSITION_REWIND;
394       unlock_unit (u);
395     }
396
397   library_end ();
398 }
399
400
401 extern void st_flush (st_parameter_filepos *);
402 export_proto(st_flush);
403
404 void
405 st_flush (st_parameter_filepos *fpp)
406 {
407   gfc_unit *u;
408
409   library_start (&fpp->common);
410
411   u = find_unit (fpp->common.unit);
412   if (u != NULL)
413     {
414       /* Make sure format buffer is flushed.  */
415       if (u->flags.form == FORM_FORMATTED)
416         fbuf_flush (u, u->mode);
417
418       sflush (u->s);
419       unlock_unit (u);
420     }
421   else
422     /* FLUSH on unconnected unit is illegal: F95 std., 9.3.5. */ 
423     generate_error (&fpp->common, LIBERROR_BAD_OPTION,
424                         "Specified UNIT in FLUSH is not connected");
425
426   library_end ();
427 }