1 /* Copyright (C) 2002, 2003, 2004, 2005
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
35 #include "libgfortran.h"
39 static const st_option access_opt
[] = {
40 {"sequential", ACCESS_SEQUENTIAL
},
41 {"direct", ACCESS_DIRECT
},
42 {"append", ACCESS_APPEND
},
46 static const st_option action_opt
[] =
48 { "read", ACTION_READ
},
49 { "write", ACTION_WRITE
},
50 { "readwrite", ACTION_READWRITE
},
54 static const st_option blank_opt
[] =
56 { "null", BLANK_NULL
},
57 { "zero", BLANK_ZERO
},
61 static const st_option delim_opt
[] =
63 { "none", DELIM_NONE
},
64 { "apostrophe", DELIM_APOSTROPHE
},
65 { "quote", DELIM_QUOTE
},
69 static const st_option form_opt
[] =
71 { "formatted", FORM_FORMATTED
},
72 { "unformatted", FORM_UNFORMATTED
},
76 static const st_option position_opt
[] =
78 { "asis", POSITION_ASIS
},
79 { "rewind", POSITION_REWIND
},
80 { "append", POSITION_APPEND
},
84 static const st_option status_opt
[] =
86 { "unknown", STATUS_UNKNOWN
},
89 { "replace", STATUS_REPLACE
},
90 { "scratch", STATUS_SCRATCH
},
94 static const st_option pad_opt
[] =
101 static const st_option convert_opt
[] =
103 { "native", CONVERT_NATIVE
},
104 { "swap", CONVERT_SWAP
},
105 { "big_endian", CONVERT_BIG
},
106 { "little_endian", CONVERT_LITTLE
},
110 /* Given a unit, test to see if the file is positioned at the terminal
111 point, and if so, change state from NO_ENDFILE flag to AT_ENDFILE.
112 This prevents us from changing the state from AFTER_ENDFILE to
116 test_endfile (gfc_unit
* u
)
118 if (u
->endfile
== NO_ENDFILE
&& file_length (u
->s
) == file_position (u
->s
))
119 u
->endfile
= AT_ENDFILE
;
123 /* Change the modes of a file, those that are allowed * to be
127 edit_modes (st_parameter_open
*opp
, gfc_unit
* u
, unit_flags
* flags
)
129 /* Complain about attempts to change the unchangeable. */
131 if (flags
->status
!= STATUS_UNSPECIFIED
&&
132 u
->flags
.status
!= flags
->status
)
133 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
134 "Cannot change STATUS parameter in OPEN statement");
136 if (flags
->access
!= ACCESS_UNSPECIFIED
&& u
->flags
.access
!= flags
->access
)
137 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
138 "Cannot change ACCESS parameter in OPEN statement");
140 if (flags
->form
!= FORM_UNSPECIFIED
&& u
->flags
.form
!= flags
->form
)
141 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
142 "Cannot change FORM parameter in OPEN statement");
144 if ((opp
->common
.flags
& IOPARM_OPEN_HAS_RECL_IN
)
145 && opp
->recl_in
!= u
->recl
)
146 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
147 "Cannot change RECL parameter in OPEN statement");
149 if (flags
->action
!= ACTION_UNSPECIFIED
&& u
->flags
.access
!= flags
->access
)
150 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
151 "Cannot change ACTION parameter in OPEN statement");
153 /* Status must be OLD if present. */
155 if (flags
->status
!= STATUS_UNSPECIFIED
&& flags
->status
!= STATUS_OLD
&&
156 flags
->status
!= STATUS_UNKNOWN
)
157 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
158 "OPEN statement must have a STATUS of OLD or UNKNOWN");
160 if (u
->flags
.form
== FORM_UNFORMATTED
)
162 if (flags
->delim
!= DELIM_UNSPECIFIED
)
163 generate_error (&opp
->common
, ERROR_OPTION_CONFLICT
,
164 "DELIM parameter conflicts with UNFORMATTED form in "
167 if (flags
->blank
!= BLANK_UNSPECIFIED
)
168 generate_error (&opp
->common
, ERROR_OPTION_CONFLICT
,
169 "BLANK parameter conflicts with UNFORMATTED form in "
172 if (flags
->pad
!= PAD_UNSPECIFIED
)
173 generate_error (&opp
->common
, ERROR_OPTION_CONFLICT
,
174 "PAD paramter conflicts with UNFORMATTED form in "
178 if ((opp
->common
.flags
& IOPARM_LIBRETURN_MASK
) == IOPARM_LIBRETURN_OK
)
180 /* Change the changeable: */
181 if (flags
->blank
!= BLANK_UNSPECIFIED
)
182 u
->flags
.blank
= flags
->blank
;
183 if (flags
->delim
!= DELIM_UNSPECIFIED
)
184 u
->flags
.delim
= flags
->delim
;
185 if (flags
->pad
!= PAD_UNSPECIFIED
)
186 u
->flags
.pad
= flags
->pad
;
189 /* Reposition the file if necessary. */
191 switch (flags
->position
)
193 case POSITION_UNSPECIFIED
:
197 case POSITION_REWIND
:
198 if (sseek (u
->s
, 0) == FAILURE
)
201 u
->current_record
= 0;
204 test_endfile (u
); /* We might be at the end. */
207 case POSITION_APPEND
:
208 if (sseek (u
->s
, file_length (u
->s
)) == FAILURE
)
211 u
->current_record
= 0;
212 u
->endfile
= AT_ENDFILE
; /* We are at the end. */
216 generate_error (&opp
->common
, ERROR_OS
, NULL
);
224 /* Open an unused unit. */
227 new_unit (st_parameter_open
*opp
, gfc_unit
*u
, unit_flags
* flags
)
231 char tmpname
[5 /* fort. */ + 10 /* digits of unit number */ + 1 /* 0 */];
233 /* Change unspecifieds to defaults. Leave (flags->action ==
234 ACTION_UNSPECIFIED) alone so open_external() can set it based on
235 what type of open actually works. */
237 if (flags
->access
== ACCESS_UNSPECIFIED
)
238 flags
->access
= ACCESS_SEQUENTIAL
;
240 if (flags
->form
== FORM_UNSPECIFIED
)
241 flags
->form
= (flags
->access
== ACCESS_SEQUENTIAL
)
242 ? FORM_FORMATTED
: FORM_UNFORMATTED
;
245 if (flags
->delim
== DELIM_UNSPECIFIED
)
246 flags
->delim
= DELIM_NONE
;
249 if (flags
->form
== FORM_UNFORMATTED
)
251 generate_error (&opp
->common
, ERROR_OPTION_CONFLICT
,
252 "DELIM parameter conflicts with UNFORMATTED form in "
258 if (flags
->blank
== BLANK_UNSPECIFIED
)
259 flags
->blank
= BLANK_NULL
;
262 if (flags
->form
== FORM_UNFORMATTED
)
264 generate_error (&opp
->common
, ERROR_OPTION_CONFLICT
,
265 "BLANK parameter conflicts with UNFORMATTED form in "
271 if (flags
->pad
== PAD_UNSPECIFIED
)
272 flags
->pad
= PAD_YES
;
275 if (flags
->form
== FORM_UNFORMATTED
)
277 generate_error (&opp
->common
, ERROR_OPTION_CONFLICT
,
278 "PAD paramter conflicts with UNFORMATTED form in "
284 if (flags
->position
!= POSITION_ASIS
&& flags
->access
== ACCESS_DIRECT
)
286 generate_error (&opp
->common
, ERROR_OPTION_CONFLICT
,
287 "ACCESS parameter conflicts with SEQUENTIAL access in "
292 if (flags
->position
== POSITION_UNSPECIFIED
)
293 flags
->position
= POSITION_ASIS
;
296 if (flags
->status
== STATUS_UNSPECIFIED
)
297 flags
->status
= STATUS_UNKNOWN
;
301 if (flags
->access
== ACCESS_DIRECT
302 && (opp
->common
.flags
& IOPARM_OPEN_HAS_RECL_IN
) == 0)
304 generate_error (&opp
->common
, ERROR_MISSING_OPTION
,
305 "Missing RECL parameter in OPEN statement");
309 if ((opp
->common
.flags
& IOPARM_OPEN_HAS_RECL_IN
) && opp
->recl_in
<= 0)
311 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
312 "RECL parameter is non-positive in OPEN statement");
316 switch (flags
->status
)
319 if ((opp
->common
.flags
& IOPARM_OPEN_HAS_FILE
) == 0)
325 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
326 "FILE parameter must not be present in OPEN statement");
333 if ((opp
->common
.flags
& IOPARM_OPEN_HAS_FILE
))
337 opp
->file_len
= sprintf(opp
->file
, "fort.%d", opp
->common
.unit
);
341 internal_error (&opp
->common
, "new_unit(): Bad status");
344 /* Make sure the file isn't already open someplace else.
345 Do not error if opening file preconnected to stdin, stdout, stderr. */
348 if ((opp
->common
.flags
& IOPARM_OPEN_HAS_FILE
) != 0)
349 u2
= find_file (opp
->file
, opp
->file_len
);
351 && (options
.stdin_unit
< 0 || u2
->unit_number
!= options
.stdin_unit
)
352 && (options
.stdout_unit
< 0 || u2
->unit_number
!= options
.stdout_unit
)
353 && (options
.stderr_unit
< 0 || u2
->unit_number
!= options
.stderr_unit
))
356 generate_error (&opp
->common
, ERROR_ALREADY_OPEN
, NULL
);
365 s
= open_external (opp
, flags
);
368 generate_error (&opp
->common
, ERROR_OS
, NULL
);
372 if (flags
->status
== STATUS_NEW
|| flags
->status
== STATUS_REPLACE
)
373 flags
->status
= STATUS_OLD
;
375 /* Create the unit structure. */
377 u
->file
= get_mem (opp
->file_len
);
378 if (u
->unit_number
!= opp
->common
.unit
)
379 internal_error (&opp
->common
, "Unit number changed");
383 u
->endfile
= NO_ENDFILE
;
385 u
->current_record
= 0;
390 if (flags
->position
== POSITION_APPEND
)
392 if (sseek (u
->s
, file_length (u
->s
)) == FAILURE
)
393 generate_error (&opp
->common
, ERROR_OS
, NULL
);
394 u
->endfile
= AT_ENDFILE
;
397 /* Unspecified recl ends up with a processor dependent value. */
399 if ((opp
->common
.flags
& IOPARM_OPEN_HAS_RECL_IN
))
400 u
->recl
= opp
->recl_in
;
402 u
->recl
= max_offset
;
404 /* If the file is direct access, calculate the maximum record number
405 via a division now instead of letting the multiplication overflow
408 if (flags
->access
== ACCESS_DIRECT
)
409 u
->maxrec
= max_offset
/ u
->recl
;
411 memmove (u
->file
, opp
->file
, opp
->file_len
);
412 u
->file_len
= opp
->file_len
;
414 /* Curiously, the standard requires that the
415 position specifier be ignored for new files so a newly connected
416 file starts out that the initial point. We still need to figure
417 out if the file is at the end or not. */
421 if (flags
->status
== STATUS_SCRATCH
&& opp
->file
!= NULL
)
422 free_mem (opp
->file
);
427 /* Free memory associated with a temporary filename. */
429 if (flags
->status
== STATUS_SCRATCH
&& opp
->file
!= NULL
)
430 free_mem (opp
->file
);
439 /* Open a unit which is already open. This involves changing the
440 modes or closing what is there now and opening the new file. */
443 already_open (st_parameter_open
*opp
, gfc_unit
* u
, unit_flags
* flags
)
445 if ((opp
->common
.flags
& IOPARM_OPEN_HAS_FILE
) == 0)
447 edit_modes (opp
, u
, flags
);
451 /* If the file is connected to something else, close it and open a
454 if (!compare_file_filename (u
, opp
->file
, opp
->file_len
))
456 #if !HAVE_UNLINK_OPEN_FILE
458 if (u
->file
&& u
->flags
.status
== STATUS_SCRATCH
)
460 path
= (char *) gfc_alloca (u
->file_len
+ 1);
461 unpack_filename (path
, u
->file
, u
->file_len
);
465 if (sclose (u
->s
) == FAILURE
)
468 generate_error (&opp
->common
, ERROR_OS
,
469 "Error closing file in OPEN statement");
479 #if !HAVE_UNLINK_OPEN_FILE
484 u
= new_unit (opp
, u
, flags
);
490 edit_modes (opp
, u
, flags
);
496 extern void st_open (st_parameter_open
*opp
);
497 export_proto(st_open
);
500 st_open (st_parameter_open
*opp
)
504 GFC_INTEGER_4 cf
= opp
->common
.flags
;
507 library_start (&opp
->common
);
509 /* Decode options. */
511 flags
.access
= !(cf
& IOPARM_OPEN_HAS_ACCESS
) ? ACCESS_UNSPECIFIED
:
512 find_option (&opp
->common
, opp
->access
, opp
->access_len
,
513 access_opt
, "Bad ACCESS parameter in OPEN statement");
515 flags
.action
= !(cf
& IOPARM_OPEN_HAS_ACTION
) ? ACTION_UNSPECIFIED
:
516 find_option (&opp
->common
, opp
->action
, opp
->action_len
,
517 action_opt
, "Bad ACTION parameter in OPEN statement");
519 flags
.blank
= !(cf
& IOPARM_OPEN_HAS_BLANK
) ? BLANK_UNSPECIFIED
:
520 find_option (&opp
->common
, opp
->blank
, opp
->blank_len
,
521 blank_opt
, "Bad BLANK parameter in OPEN statement");
523 flags
.delim
= !(cf
& IOPARM_OPEN_HAS_DELIM
) ? DELIM_UNSPECIFIED
:
524 find_option (&opp
->common
, opp
->delim
, opp
->delim_len
,
525 delim_opt
, "Bad DELIM parameter in OPEN statement");
527 flags
.pad
= !(cf
& IOPARM_OPEN_HAS_PAD
) ? PAD_UNSPECIFIED
:
528 find_option (&opp
->common
, opp
->pad
, opp
->pad_len
,
529 pad_opt
, "Bad PAD parameter in OPEN statement");
531 flags
.form
= !(cf
& IOPARM_OPEN_HAS_FORM
) ? FORM_UNSPECIFIED
:
532 find_option (&opp
->common
, opp
->form
, opp
->form_len
,
533 form_opt
, "Bad FORM parameter in OPEN statement");
535 flags
.position
= !(cf
& IOPARM_OPEN_HAS_POSITION
) ? POSITION_UNSPECIFIED
:
536 find_option (&opp
->common
, opp
->position
, opp
->position_len
,
537 position_opt
, "Bad POSITION parameter in OPEN statement");
539 flags
.status
= !(cf
& IOPARM_OPEN_HAS_STATUS
) ? STATUS_UNSPECIFIED
:
540 find_option (&opp
->common
, opp
->status
, opp
->status_len
,
541 status_opt
, "Bad STATUS parameter in OPEN statement");
543 /* First, we check wether the convert flag has been set via environment
544 variable. This overrides the convert tag in the open statement. */
546 conv
= get_unformatted_convert (opp
->common
.unit
);
548 if (conv
== CONVERT_NONE
)
550 /* Nothing has been set by environment variable, check the convert tag. */
551 if (cf
& IOPARM_OPEN_HAS_CONVERT
)
552 conv
= find_option (&opp
->common
, opp
->convert
, opp
->convert_len
,
554 "Bad CONVERT parameter in OPEN statement");
556 conv
= compile_options
.convert
;
559 /* We use l8_to_l4_offset, which is 0 on little-endian machines
560 and 1 on big-endian machines. */
568 conv
= l8_to_l4_offset
? CONVERT_NATIVE
: CONVERT_SWAP
;
572 conv
= l8_to_l4_offset
? CONVERT_SWAP
: CONVERT_NATIVE
;
576 internal_error (&opp
->common
, "Illegal value for CONVERT");
580 flags
.convert
= conv
;
582 if (opp
->common
.unit
< 0)
583 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
584 "Bad unit number in OPEN statement");
586 if (flags
.position
!= POSITION_UNSPECIFIED
587 && flags
.access
== ACCESS_DIRECT
)
588 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
589 "Cannot use POSITION with direct access files");
591 if (flags
.access
== ACCESS_APPEND
)
593 if (flags
.position
!= POSITION_UNSPECIFIED
594 && flags
.position
!= POSITION_APPEND
)
595 generate_error (&opp
->common
, ERROR_BAD_OPTION
,
596 "Conflicting ACCESS and POSITION flags in"
599 notify_std (GFC_STD_GNU
,
600 "Extension: APPEND as a value for ACCESS in OPEN statement");
601 flags
.access
= ACCESS_SEQUENTIAL
;
602 flags
.position
= POSITION_APPEND
;
605 if (flags
.position
== POSITION_UNSPECIFIED
)
606 flags
.position
= POSITION_ASIS
;
608 if ((opp
->common
.flags
& IOPARM_LIBRETURN_MASK
) == IOPARM_LIBRETURN_OK
)
610 u
= find_or_create_unit (opp
->common
.unit
);
614 u
= new_unit (opp
, u
, &flags
);
619 already_open (opp
, u
, &flags
);