1 /* Copyright (C) 2002, 2003, 2005, 2007 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)
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
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, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
31 /* Implement the non-IOLENGTH variant of the INQUIRY statement */
36 static const char undefined
[] = "UNDEFINED";
39 /* inquire_via_unit()-- Inquiry via unit number. The unit might not exist. */
42 inquire_via_unit (st_parameter_inquire
*iqp
, gfc_unit
* u
)
45 GFC_INTEGER_4 cf
= iqp
->common
.flags
;
47 if ((cf
& IOPARM_INQUIRE_HAS_EXIST
) != 0)
48 *iqp
->exist
= iqp
->common
.unit
>= 0;
50 if ((cf
& IOPARM_INQUIRE_HAS_OPENED
) != 0)
51 *iqp
->opened
= (u
!= NULL
);
53 if ((cf
& IOPARM_INQUIRE_HAS_NUMBER
) != 0)
54 *iqp
->number
= (u
!= NULL
) ? u
->unit_number
: -1;
56 if ((cf
& IOPARM_INQUIRE_HAS_NAMED
) != 0)
57 *iqp
->named
= (u
!= NULL
&& u
->flags
.status
!= STATUS_SCRATCH
);
59 if ((cf
& IOPARM_INQUIRE_HAS_NAME
) != 0
60 && u
!= NULL
&& u
->flags
.status
!= STATUS_SCRATCH
)
61 fstrcpy (iqp
->name
, iqp
->name_len
, u
->file
, u
->file_len
);
63 if ((cf
& IOPARM_INQUIRE_HAS_ACCESS
) != 0)
68 switch (u
->flags
.access
)
70 case ACCESS_SEQUENTIAL
:
80 internal_error (&iqp
->common
, "inquire_via_unit(): Bad access");
83 cf_strcpy (iqp
->access
, iqp
->access_len
, p
);
86 if ((cf
& IOPARM_INQUIRE_HAS_SEQUENTIAL
) != 0)
89 p
= inquire_sequential (NULL
, 0);
92 /* disallow an open direct access file to be accessed sequentially */
93 if (u
->flags
.access
== ACCESS_DIRECT
)
96 p
= inquire_sequential (u
->file
, u
->file_len
);
99 cf_strcpy (iqp
->sequential
, iqp
->sequential_len
, p
);
102 if ((cf
& IOPARM_INQUIRE_HAS_DIRECT
) != 0)
104 p
= (u
== NULL
) ? inquire_direct (NULL
, 0) :
105 inquire_direct (u
->file
, u
->file_len
);
107 cf_strcpy (iqp
->direct
, iqp
->direct_len
, p
);
110 if ((cf
& IOPARM_INQUIRE_HAS_FORM
) != 0)
115 switch (u
->flags
.form
)
120 case FORM_UNFORMATTED
:
124 internal_error (&iqp
->common
, "inquire_via_unit(): Bad form");
127 cf_strcpy (iqp
->form
, iqp
->form_len
, p
);
130 if ((cf
& IOPARM_INQUIRE_HAS_FORMATTED
) != 0)
132 p
= (u
== NULL
) ? inquire_formatted (NULL
, 0) :
133 inquire_formatted (u
->file
, u
->file_len
);
135 cf_strcpy (iqp
->formatted
, iqp
->formatted_len
, p
);
138 if ((cf
& IOPARM_INQUIRE_HAS_UNFORMATTED
) != 0)
140 p
= (u
== NULL
) ? inquire_unformatted (NULL
, 0) :
141 inquire_unformatted (u
->file
, u
->file_len
);
143 cf_strcpy (iqp
->unformatted
, iqp
->unformatted_len
, p
);
146 if ((cf
& IOPARM_INQUIRE_HAS_RECL_OUT
) != 0)
147 *iqp
->recl_out
= (u
!= NULL
) ? u
->recl
: 0;
149 if ((cf
& IOPARM_INQUIRE_HAS_STRM_POS_OUT
) != 0)
150 *iqp
->strm_pos_out
= (u
!= NULL
) ? u
->strm_pos
: 0;
152 if ((cf
& IOPARM_INQUIRE_HAS_NEXTREC
) != 0)
154 /* This only makes sense in the context of DIRECT access. */
155 if (u
!= NULL
&& u
->flags
.access
== ACCESS_DIRECT
)
156 *iqp
->nextrec
= u
->last_record
+ 1;
161 if ((cf
& IOPARM_INQUIRE_HAS_BLANK
) != 0)
166 switch (u
->flags
.blank
)
175 internal_error (&iqp
->common
, "inquire_via_unit(): Bad blank");
178 cf_strcpy (iqp
->blank
, iqp
->blank_len
, p
);
181 if ((cf
& IOPARM_INQUIRE_HAS_POSITION
) != 0)
183 if (u
== NULL
|| u
->flags
.access
== ACCESS_DIRECT
)
186 switch (u
->flags
.position
)
188 case POSITION_REWIND
:
191 case POSITION_APPEND
:
198 /* if not direct access, it must be
199 either REWIND, APPEND, or ASIS.
200 ASIS seems to be the best default */
204 cf_strcpy (iqp
->position
, iqp
->position_len
, p
);
207 if ((cf
& IOPARM_INQUIRE_HAS_ACTION
) != 0)
212 switch (u
->flags
.action
)
220 case ACTION_READWRITE
:
224 internal_error (&iqp
->common
, "inquire_via_unit(): Bad action");
227 cf_strcpy (iqp
->action
, iqp
->action_len
, p
);
230 if ((cf
& IOPARM_INQUIRE_HAS_READ
) != 0)
232 p
= (u
== NULL
) ? inquire_read (NULL
, 0) :
233 inquire_read (u
->file
, u
->file_len
);
235 cf_strcpy (iqp
->read
, iqp
->read_len
, p
);
238 if ((cf
& IOPARM_INQUIRE_HAS_WRITE
) != 0)
240 p
= (u
== NULL
) ? inquire_write (NULL
, 0) :
241 inquire_write (u
->file
, u
->file_len
);
243 cf_strcpy (iqp
->write
, iqp
->write_len
, p
);
246 if ((cf
& IOPARM_INQUIRE_HAS_READWRITE
) != 0)
248 p
= (u
== NULL
) ? inquire_readwrite (NULL
, 0) :
249 inquire_readwrite (u
->file
, u
->file_len
);
251 cf_strcpy (iqp
->readwrite
, iqp
->readwrite_len
, p
);
254 if ((cf
& IOPARM_INQUIRE_HAS_DELIM
) != 0)
256 if (u
== NULL
|| u
->flags
.form
!= FORM_FORMATTED
)
259 switch (u
->flags
.delim
)
267 case DELIM_APOSTROPHE
:
271 internal_error (&iqp
->common
, "inquire_via_unit(): Bad delim");
274 cf_strcpy (iqp
->delim
, iqp
->delim_len
, p
);
277 if ((cf
& IOPARM_INQUIRE_HAS_PAD
) != 0)
279 if (u
== NULL
|| u
->flags
.form
!= FORM_FORMATTED
)
282 switch (u
->flags
.pad
)
291 internal_error (&iqp
->common
, "inquire_via_unit(): Bad pad");
294 cf_strcpy (iqp
->pad
, iqp
->pad_len
, p
);
297 if ((cf
& IOPARM_INQUIRE_HAS_CONVERT
) != 0)
302 switch (u
->flags
.convert
)
304 /* l8_to_l4_offset is 0 for little-endian, 1 for big-endian. */
305 case GFC_CONVERT_NATIVE
:
306 p
= l8_to_l4_offset
? "BIG_ENDIAN" : "LITTLE_ENDIAN";
309 case GFC_CONVERT_SWAP
:
310 p
= l8_to_l4_offset
? "LITTLE_ENDIAN" : "BIG_ENDIAN";
314 internal_error (&iqp
->common
, "inquire_via_unit(): Bad convert");
317 cf_strcpy (iqp
->convert
, iqp
->convert_len
, p
);
322 /* inquire_via_filename()-- Inquiry via filename. This subroutine is
323 * only used if the filename is *not* connected to a unit number. */
326 inquire_via_filename (st_parameter_inquire
*iqp
)
329 GFC_INTEGER_4 cf
= iqp
->common
.flags
;
331 if ((cf
& IOPARM_INQUIRE_HAS_EXIST
) != 0)
332 *iqp
->exist
= file_exists (iqp
->file
, iqp
->file_len
);
334 if ((cf
& IOPARM_INQUIRE_HAS_OPENED
) != 0)
337 if ((cf
& IOPARM_INQUIRE_HAS_NUMBER
) != 0)
340 if ((cf
& IOPARM_INQUIRE_HAS_NAMED
) != 0)
343 if ((cf
& IOPARM_INQUIRE_HAS_NAME
) != 0)
344 fstrcpy (iqp
->name
, iqp
->name_len
, iqp
->file
, iqp
->file_len
);
346 if ((cf
& IOPARM_INQUIRE_HAS_ACCESS
) != 0)
347 cf_strcpy (iqp
->access
, iqp
->access_len
, undefined
);
349 if ((cf
& IOPARM_INQUIRE_HAS_SEQUENTIAL
) != 0)
351 p
= inquire_sequential (iqp
->file
, iqp
->file_len
);
352 cf_strcpy (iqp
->sequential
, iqp
->sequential_len
, p
);
355 if ((cf
& IOPARM_INQUIRE_HAS_DIRECT
) != 0)
357 p
= inquire_direct (iqp
->file
, iqp
->file_len
);
358 cf_strcpy (iqp
->direct
, iqp
->direct_len
, p
);
361 if ((cf
& IOPARM_INQUIRE_HAS_FORM
) != 0)
362 cf_strcpy (iqp
->form
, iqp
->form_len
, undefined
);
364 if ((cf
& IOPARM_INQUIRE_HAS_FORMATTED
) != 0)
366 p
= inquire_formatted (iqp
->file
, iqp
->file_len
);
367 cf_strcpy (iqp
->formatted
, iqp
->formatted_len
, p
);
370 if ((cf
& IOPARM_INQUIRE_HAS_UNFORMATTED
) != 0)
372 p
= inquire_unformatted (iqp
->file
, iqp
->file_len
);
373 cf_strcpy (iqp
->unformatted
, iqp
->unformatted_len
, p
);
376 if ((cf
& IOPARM_INQUIRE_HAS_RECL_OUT
) != 0)
379 if ((cf
& IOPARM_INQUIRE_HAS_NEXTREC
) != 0)
382 if ((cf
& IOPARM_INQUIRE_HAS_BLANK
) != 0)
383 cf_strcpy (iqp
->blank
, iqp
->blank_len
, undefined
);
385 if ((cf
& IOPARM_INQUIRE_HAS_POSITION
) != 0)
386 cf_strcpy (iqp
->position
, iqp
->position_len
, undefined
);
388 if ((cf
& IOPARM_INQUIRE_HAS_ACCESS
) != 0)
389 cf_strcpy (iqp
->access
, iqp
->access_len
, undefined
);
391 if ((cf
& IOPARM_INQUIRE_HAS_READ
) != 0)
393 p
= inquire_read (iqp
->file
, iqp
->file_len
);
394 cf_strcpy (iqp
->read
, iqp
->read_len
, p
);
397 if ((cf
& IOPARM_INQUIRE_HAS_WRITE
) != 0)
399 p
= inquire_write (iqp
->file
, iqp
->file_len
);
400 cf_strcpy (iqp
->write
, iqp
->write_len
, p
);
403 if ((cf
& IOPARM_INQUIRE_HAS_READWRITE
) != 0)
405 p
= inquire_read (iqp
->file
, iqp
->file_len
);
406 cf_strcpy (iqp
->readwrite
, iqp
->readwrite_len
, p
);
409 if ((cf
& IOPARM_INQUIRE_HAS_DELIM
) != 0)
410 cf_strcpy (iqp
->delim
, iqp
->delim_len
, undefined
);
412 if ((cf
& IOPARM_INQUIRE_HAS_PAD
) != 0)
413 cf_strcpy (iqp
->pad
, iqp
->pad_len
, undefined
);
417 /* Library entry point for the INQUIRE statement (non-IOLENGTH
420 extern void st_inquire (st_parameter_inquire
*);
421 export_proto(st_inquire
);
424 st_inquire (st_parameter_inquire
*iqp
)
428 library_start (&iqp
->common
);
430 if ((iqp
->common
.flags
& IOPARM_INQUIRE_HAS_FILE
) == 0)
432 u
= find_unit (iqp
->common
.unit
);
433 inquire_via_unit (iqp
, u
);
437 u
= find_file (iqp
->file
, iqp
->file_len
);
439 inquire_via_filename (iqp
);
441 inquire_via_unit (iqp
, u
);