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, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, 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
)
90 /* disallow an open direct access file to be accessed
92 if (u
->flags
.access
==ACCESS_DIRECT
)
95 p
= (u
== NULL
) ? inquire_sequential (NULL
, 0) :
96 inquire_sequential (u
->file
, u
->file_len
);
98 cf_strcpy (ioparm
.sequential
, ioparm
.sequential_len
, p
);
101 if (ioparm
.direct
!= NULL
)
103 p
= (u
== NULL
) ? inquire_direct (NULL
, 0) :
104 inquire_direct (u
->file
, u
->file_len
);
106 cf_strcpy (ioparm
.direct
, ioparm
.direct_len
, p
);
109 if (ioparm
.form
!= NULL
)
114 switch (u
->flags
.form
)
119 case FORM_UNFORMATTED
:
123 internal_error ("inquire_via_unit(): Bad form");
126 cf_strcpy (ioparm
.form
, ioparm
.form_len
, p
);
129 if (ioparm
.formatted
!= NULL
)
131 p
= (u
== NULL
) ? inquire_formatted (NULL
, 0) :
132 inquire_formatted (u
->file
, u
->file_len
);
134 cf_strcpy (ioparm
.formatted
, ioparm
.formatted_len
, p
);
137 if (ioparm
.unformatted
!= NULL
)
139 p
= (u
== NULL
) ? inquire_unformatted (NULL
, 0) :
140 inquire_unformatted (u
->file
, u
->file_len
);
142 cf_strcpy (ioparm
.unformatted
, ioparm
.unformatted_len
, p
);
145 if (ioparm
.recl_out
!= NULL
)
146 *ioparm
.recl_out
= (u
!= NULL
) ? u
->recl
: 0;
148 if (ioparm
.nextrec
!= NULL
)
149 *ioparm
.nextrec
= (u
!= NULL
) ? u
->last_record
+ 1 : 0;
151 if (ioparm
.blank
!= NULL
)
156 switch (u
->flags
.blank
)
165 internal_error ("inquire_via_unit(): Bad blank");
168 cf_strcpy (ioparm
.blank
, ioparm
.blank_len
, p
);
171 if (ioparm
.position
!= NULL
)
173 if (u
== NULL
|| u
->flags
.access
== ACCESS_DIRECT
)
176 switch (u
->flags
.position
)
178 case POSITION_REWIND
:
181 case POSITION_APPEND
:
188 /* if not direct access, it must be
189 either REWIND, APPEND, or ASIS.
190 ASIS seems to be the best default */
194 cf_strcpy (ioparm
.position
, ioparm
.position_len
, p
);
197 if (ioparm
.action
!= NULL
)
202 switch (u
->flags
.action
)
210 case ACTION_READWRITE
:
214 internal_error ("inquire_via_unit(): Bad action");
217 cf_strcpy (ioparm
.action
, ioparm
.action_len
, p
);
220 if (ioparm
.read
!= NULL
)
222 p
= (u
== NULL
) ? inquire_read (NULL
, 0) :
223 inquire_read (u
->file
, u
->file_len
);
225 cf_strcpy (ioparm
.read
, ioparm
.read_len
, p
);
228 if (ioparm
.write
!= NULL
)
230 p
= (u
== NULL
) ? inquire_write (NULL
, 0) :
231 inquire_write (u
->file
, u
->file_len
);
233 cf_strcpy (ioparm
.write
, ioparm
.write_len
, p
);
236 if (ioparm
.readwrite
!= NULL
)
238 p
= (u
== NULL
) ? inquire_readwrite (NULL
, 0) :
239 inquire_readwrite (u
->file
, u
->file_len
);
241 cf_strcpy (ioparm
.readwrite
, ioparm
.readwrite_len
, p
);
244 if (ioparm
.delim
!= NULL
)
246 if (u
== NULL
|| u
->flags
.form
!= FORM_FORMATTED
)
249 switch (u
->flags
.delim
)
257 case DELIM_APOSTROPHE
:
261 internal_error ("inquire_via_unit(): Bad delim");
264 cf_strcpy (ioparm
.delim
, ioparm
.delim_len
, p
);
267 if (ioparm
.pad
!= NULL
)
269 if (u
== NULL
|| u
->flags
.form
!= FORM_FORMATTED
)
272 switch (u
->flags
.pad
)
281 internal_error ("inquire_via_unit(): Bad pad");
284 cf_strcpy (ioparm
.pad
, ioparm
.pad_len
, p
);
289 /* inquire_via_filename()-- Inquiry via filename. This subroutine is
290 * only used if the filename is *not* connected to a unit number. */
293 inquire_via_filename (void)
297 if (ioparm
.exist
!= NULL
)
298 *ioparm
.exist
= file_exists ();
300 if (ioparm
.opened
!= NULL
)
303 if (ioparm
.number
!= NULL
)
306 if (ioparm
.named
!= NULL
)
309 if (ioparm
.name
!= NULL
)
310 fstrcpy (ioparm
.name
, ioparm
.name_len
, ioparm
.file
, ioparm
.file_len
);
312 if (ioparm
.access
!= NULL
)
313 cf_strcpy (ioparm
.access
, ioparm
.access_len
, undefined
);
315 if (ioparm
.sequential
!= NULL
)
317 p
= inquire_sequential (ioparm
.file
, ioparm
.file_len
);
318 cf_strcpy (ioparm
.sequential
, ioparm
.sequential_len
, p
);
321 if (ioparm
.direct
!= NULL
)
323 p
= inquire_direct (ioparm
.file
, ioparm
.file_len
);
324 cf_strcpy (ioparm
.direct
, ioparm
.direct_len
, p
);
327 if (ioparm
.form
!= NULL
)
328 cf_strcpy (ioparm
.form
, ioparm
.form_len
, undefined
);
330 if (ioparm
.formatted
!= NULL
)
332 p
= inquire_formatted (ioparm
.file
, ioparm
.file_len
);
333 cf_strcpy (ioparm
.formatted
, ioparm
.formatted_len
, p
);
336 if (ioparm
.unformatted
!= NULL
)
338 p
= inquire_unformatted (ioparm
.file
, ioparm
.file_len
);
339 cf_strcpy (ioparm
.unformatted
, ioparm
.unformatted_len
, p
);
342 if (ioparm
.recl_out
!= NULL
)
343 *ioparm
.recl_out
= 0;
345 if (ioparm
.nextrec
!= NULL
)
348 if (ioparm
.blank
!= NULL
)
349 cf_strcpy (ioparm
.blank
, ioparm
.blank_len
, undefined
);
351 if (ioparm
.position
!= NULL
)
352 cf_strcpy (ioparm
.position
, ioparm
.position_len
, undefined
);
354 if (ioparm
.access
!= NULL
)
355 cf_strcpy (ioparm
.access
, ioparm
.access_len
, undefined
);
357 if (ioparm
.read
!= NULL
)
359 p
= inquire_read (ioparm
.file
, ioparm
.file_len
);
360 cf_strcpy (ioparm
.read
, ioparm
.read_len
, p
);
363 if (ioparm
.write
!= NULL
)
365 p
= inquire_write (ioparm
.file
, ioparm
.file_len
);
366 cf_strcpy (ioparm
.write
, ioparm
.write_len
, p
);
369 if (ioparm
.readwrite
!= NULL
)
371 p
= inquire_read (ioparm
.file
, ioparm
.file_len
);
372 cf_strcpy (ioparm
.readwrite
, ioparm
.readwrite_len
, p
);
375 if (ioparm
.delim
!= NULL
)
376 cf_strcpy (ioparm
.delim
, ioparm
.delim_len
, undefined
);
378 if (ioparm
.pad
!= NULL
)
379 cf_strcpy (ioparm
.pad
, ioparm
.pad_len
, undefined
);
384 /* Library entry point for the INQUIRE statement (non-IOLENGTH
387 extern void st_inquire (void);
388 export_proto(st_inquire
);
397 if (ioparm
.file
== NULL
)
398 inquire_via_unit (find_unit (ioparm
.unit
));
403 inquire_via_filename ();
405 inquire_via_unit (u
);