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)
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 */
34 #include "libgfortran.h"
38 static char undefined
[] = "UNDEFINED";
41 /* inquire_via_unit()-- Inquiry via unit number. The unit might not exist. */
44 inquire_via_unit (gfc_unit
* u
)
48 if (ioparm
.exist
!= NULL
)
56 if (ioparm
.opened
!= NULL
)
57 *ioparm
.opened
= (u
!= NULL
);
59 if (ioparm
.number
!= NULL
)
60 *ioparm
.number
= (u
!= NULL
) ? u
->unit_number
: -1;
62 if (ioparm
.named
!= NULL
)
63 *ioparm
.named
= (u
!= NULL
&& u
->flags
.status
!= STATUS_SCRATCH
);
65 if (ioparm
.name
!= NULL
&& u
!= NULL
&& u
->flags
.status
!= STATUS_SCRATCH
)
66 fstrcpy (ioparm
.name
, ioparm
.name_len
, u
->file
, u
->file_len
);
68 if (ioparm
.access
!= NULL
)
73 switch (u
->flags
.access
)
75 case ACCESS_SEQUENTIAL
:
82 internal_error ("inquire_via_unit(): Bad access");
85 cf_strcpy (ioparm
.access
, ioparm
.access_len
, p
);
88 if (ioparm
.sequential
!= NULL
)
91 p
= inquire_sequential (NULL
, 0);
94 /* disallow an open direct access file to be accessed sequentially */
95 if (u
->flags
.access
== ACCESS_DIRECT
)
98 p
= inquire_sequential (u
->file
, u
->file_len
);
101 cf_strcpy (ioparm
.sequential
, ioparm
.sequential_len
, p
);
104 if (ioparm
.direct
!= NULL
)
106 p
= (u
== NULL
) ? inquire_direct (NULL
, 0) :
107 inquire_direct (u
->file
, u
->file_len
);
109 cf_strcpy (ioparm
.direct
, ioparm
.direct_len
, p
);
112 if (ioparm
.form
!= NULL
)
117 switch (u
->flags
.form
)
122 case FORM_UNFORMATTED
:
126 internal_error ("inquire_via_unit(): Bad form");
129 cf_strcpy (ioparm
.form
, ioparm
.form_len
, p
);
132 if (ioparm
.formatted
!= NULL
)
134 p
= (u
== NULL
) ? inquire_formatted (NULL
, 0) :
135 inquire_formatted (u
->file
, u
->file_len
);
137 cf_strcpy (ioparm
.formatted
, ioparm
.formatted_len
, p
);
140 if (ioparm
.unformatted
!= NULL
)
142 p
= (u
== NULL
) ? inquire_unformatted (NULL
, 0) :
143 inquire_unformatted (u
->file
, u
->file_len
);
145 cf_strcpy (ioparm
.unformatted
, ioparm
.unformatted_len
, p
);
148 if (ioparm
.recl_out
!= NULL
)
149 *ioparm
.recl_out
= (u
!= NULL
) ? u
->recl
: 0;
151 if (ioparm
.nextrec
!= NULL
)
152 *ioparm
.nextrec
= (u
!= NULL
) ? u
->last_record
+ 1 : 0;
154 if (ioparm
.blank
!= NULL
)
159 switch (u
->flags
.blank
)
168 internal_error ("inquire_via_unit(): Bad blank");
171 cf_strcpy (ioparm
.blank
, ioparm
.blank_len
, p
);
174 if (ioparm
.position
!= NULL
)
176 if (u
== NULL
|| u
->flags
.access
== ACCESS_DIRECT
)
179 switch (u
->flags
.position
)
181 case POSITION_REWIND
:
184 case POSITION_APPEND
:
191 /* if not direct access, it must be
192 either REWIND, APPEND, or ASIS.
193 ASIS seems to be the best default */
197 cf_strcpy (ioparm
.position
, ioparm
.position_len
, p
);
200 if (ioparm
.action
!= NULL
)
205 switch (u
->flags
.action
)
213 case ACTION_READWRITE
:
217 internal_error ("inquire_via_unit(): Bad action");
220 cf_strcpy (ioparm
.action
, ioparm
.action_len
, p
);
223 if (ioparm
.read
!= NULL
)
225 p
= (u
== NULL
) ? inquire_read (NULL
, 0) :
226 inquire_read (u
->file
, u
->file_len
);
228 cf_strcpy (ioparm
.read
, ioparm
.read_len
, p
);
231 if (ioparm
.write
!= NULL
)
233 p
= (u
== NULL
) ? inquire_write (NULL
, 0) :
234 inquire_write (u
->file
, u
->file_len
);
236 cf_strcpy (ioparm
.write
, ioparm
.write_len
, p
);
239 if (ioparm
.readwrite
!= NULL
)
241 p
= (u
== NULL
) ? inquire_readwrite (NULL
, 0) :
242 inquire_readwrite (u
->file
, u
->file_len
);
244 cf_strcpy (ioparm
.readwrite
, ioparm
.readwrite_len
, p
);
247 if (ioparm
.delim
!= NULL
)
249 if (u
== NULL
|| u
->flags
.form
!= FORM_FORMATTED
)
252 switch (u
->flags
.delim
)
260 case DELIM_APOSTROPHE
:
264 internal_error ("inquire_via_unit(): Bad delim");
267 cf_strcpy (ioparm
.delim
, ioparm
.delim_len
, p
);
270 if (ioparm
.pad
!= NULL
)
272 if (u
== NULL
|| u
->flags
.form
!= FORM_FORMATTED
)
275 switch (u
->flags
.pad
)
284 internal_error ("inquire_via_unit(): Bad pad");
287 cf_strcpy (ioparm
.pad
, ioparm
.pad_len
, p
);
292 /* inquire_via_filename()-- Inquiry via filename. This subroutine is
293 * only used if the filename is *not* connected to a unit number. */
296 inquire_via_filename (void)
300 if (ioparm
.exist
!= NULL
)
301 *ioparm
.exist
= file_exists ();
303 if (ioparm
.opened
!= NULL
)
306 if (ioparm
.number
!= NULL
)
309 if (ioparm
.named
!= NULL
)
312 if (ioparm
.name
!= NULL
)
313 fstrcpy (ioparm
.name
, ioparm
.name_len
, ioparm
.file
, ioparm
.file_len
);
315 if (ioparm
.access
!= NULL
)
316 cf_strcpy (ioparm
.access
, ioparm
.access_len
, undefined
);
318 if (ioparm
.sequential
!= NULL
)
320 p
= inquire_sequential (ioparm
.file
, ioparm
.file_len
);
321 cf_strcpy (ioparm
.sequential
, ioparm
.sequential_len
, p
);
324 if (ioparm
.direct
!= NULL
)
326 p
= inquire_direct (ioparm
.file
, ioparm
.file_len
);
327 cf_strcpy (ioparm
.direct
, ioparm
.direct_len
, p
);
330 if (ioparm
.form
!= NULL
)
331 cf_strcpy (ioparm
.form
, ioparm
.form_len
, undefined
);
333 if (ioparm
.formatted
!= NULL
)
335 p
= inquire_formatted (ioparm
.file
, ioparm
.file_len
);
336 cf_strcpy (ioparm
.formatted
, ioparm
.formatted_len
, p
);
339 if (ioparm
.unformatted
!= NULL
)
341 p
= inquire_unformatted (ioparm
.file
, ioparm
.file_len
);
342 cf_strcpy (ioparm
.unformatted
, ioparm
.unformatted_len
, p
);
345 if (ioparm
.recl_out
!= NULL
)
346 *ioparm
.recl_out
= 0;
348 if (ioparm
.nextrec
!= NULL
)
351 if (ioparm
.blank
!= NULL
)
352 cf_strcpy (ioparm
.blank
, ioparm
.blank_len
, undefined
);
354 if (ioparm
.position
!= NULL
)
355 cf_strcpy (ioparm
.position
, ioparm
.position_len
, undefined
);
357 if (ioparm
.access
!= NULL
)
358 cf_strcpy (ioparm
.access
, ioparm
.access_len
, undefined
);
360 if (ioparm
.read
!= NULL
)
362 p
= inquire_read (ioparm
.file
, ioparm
.file_len
);
363 cf_strcpy (ioparm
.read
, ioparm
.read_len
, p
);
366 if (ioparm
.write
!= NULL
)
368 p
= inquire_write (ioparm
.file
, ioparm
.file_len
);
369 cf_strcpy (ioparm
.write
, ioparm
.write_len
, p
);
372 if (ioparm
.readwrite
!= NULL
)
374 p
= inquire_read (ioparm
.file
, ioparm
.file_len
);
375 cf_strcpy (ioparm
.readwrite
, ioparm
.readwrite_len
, p
);
378 if (ioparm
.delim
!= NULL
)
379 cf_strcpy (ioparm
.delim
, ioparm
.delim_len
, undefined
);
381 if (ioparm
.pad
!= NULL
)
382 cf_strcpy (ioparm
.pad
, ioparm
.pad_len
, undefined
);
387 /* Library entry point for the INQUIRE statement (non-IOLENGTH
390 extern void st_inquire (void);
391 export_proto(st_inquire
);
400 if (ioparm
.file
== NULL
)
401 inquire_via_unit (find_unit (ioparm
.unit
));
406 inquire_via_filename ();
408 inquire_via_unit (u
);