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 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Libgfortran; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* Implement the non-IOLENGTH variant of the INQUIRY statement */
25 #include "libgfortran.h"
29 static char undefined
[] = "UNDEFINED";
32 /* inquire_via_unit()-- Inquiry via unit number. The unit might not exist. */
35 inquire_via_unit (gfc_unit
* u
)
39 if (ioparm
.exist
!= NULL
)
40 *ioparm
.exist
= (u
!= NULL
);
42 if (ioparm
.opened
!= NULL
)
43 *ioparm
.opened
= (u
!= NULL
);
45 if (ioparm
.number
!= NULL
)
46 *ioparm
.number
= (u
!= NULL
) ? u
->unit_number
: -1;
48 if (ioparm
.named
!= NULL
)
49 *ioparm
.named
= (u
!= NULL
&& u
->flags
.status
!= STATUS_SCRATCH
);
51 if (ioparm
.name
!= NULL
&& u
!= NULL
&& u
->flags
.status
!= STATUS_SCRATCH
)
52 fstrcpy (ioparm
.name
, ioparm
.name_len
, u
->file
, u
->file_len
);
54 if (ioparm
.access
!= NULL
)
59 switch (u
->flags
.access
)
61 case ACCESS_SEQUENTIAL
:
68 internal_error ("inquire_via_unit(): Bad access");
71 cf_strcpy (ioparm
.access
, ioparm
.access_len
, p
);
74 if (ioparm
.sequential
!= NULL
)
76 /* disallow an open direct access file to be accessed
78 if (u
->flags
.access
==ACCESS_DIRECT
)
81 p
= (u
== NULL
) ? inquire_sequential (NULL
, 0) :
82 inquire_sequential (u
->file
, u
->file_len
);
84 cf_strcpy (ioparm
.sequential
, ioparm
.sequential_len
, p
);
87 if (ioparm
.direct
!= NULL
)
89 p
= (u
== NULL
) ? inquire_direct (NULL
, 0) :
90 inquire_direct (u
->file
, u
->file_len
);
92 cf_strcpy (ioparm
.direct
, ioparm
.direct_len
, p
);
95 if (ioparm
.form
!= NULL
)
100 switch (u
->flags
.form
)
105 case FORM_UNFORMATTED
:
109 internal_error ("inquire_via_unit(): Bad form");
112 cf_strcpy (ioparm
.form
, ioparm
.form_len
, p
);
115 if (ioparm
.formatted
!= NULL
)
117 p
= (u
== NULL
) ? inquire_formatted (NULL
, 0) :
118 inquire_formatted (u
->file
, u
->file_len
);
120 cf_strcpy (ioparm
.formatted
, ioparm
.formatted_len
, p
);
123 if (ioparm
.unformatted
!= NULL
)
125 p
= (u
== NULL
) ? inquire_unformatted (NULL
, 0) :
126 inquire_unformatted (u
->file
, u
->file_len
);
128 cf_strcpy (ioparm
.unformatted
, ioparm
.unformatted_len
, p
);
131 if (ioparm
.recl_out
!= NULL
)
132 *ioparm
.recl_out
= (u
!= NULL
) ? u
->recl
: 0;
134 if (ioparm
.nextrec
!= NULL
)
135 *ioparm
.nextrec
= (u
!= NULL
) ? u
->last_record
+ 1 : 0;
137 if (ioparm
.blank
!= NULL
)
142 switch (u
->flags
.blank
)
151 internal_error ("inquire_via_unit(): Bad blank");
154 cf_strcpy (ioparm
.blank
, ioparm
.blank_len
, p
);
157 if (ioparm
.position
!= NULL
)
159 if (u
== NULL
|| u
->flags
.access
== ACCESS_DIRECT
)
163 p
= NULL
; /* TODO: Try to decode what the standard says... */
166 cf_strcpy (ioparm
.blank
, ioparm
.blank_len
, p
);
169 if (ioparm
.action
!= NULL
)
174 switch (u
->flags
.action
)
182 case ACTION_READWRITE
:
186 internal_error ("inquire_via_unit(): Bad action");
189 cf_strcpy (ioparm
.action
, ioparm
.action_len
, p
);
192 if (ioparm
.read
!= NULL
)
194 p
= (u
== NULL
) ? inquire_read (NULL
, 0) :
195 inquire_read (u
->file
, u
->file_len
);
197 cf_strcpy (ioparm
.read
, ioparm
.read_len
, p
);
200 if (ioparm
.write
!= NULL
)
202 p
= (u
== NULL
) ? inquire_write (NULL
, 0) :
203 inquire_write (u
->file
, u
->file_len
);
205 cf_strcpy (ioparm
.write
, ioparm
.write_len
, p
);
208 if (ioparm
.readwrite
!= NULL
)
210 p
= (u
== NULL
) ? inquire_readwrite (NULL
, 0) :
211 inquire_readwrite (u
->file
, u
->file_len
);
213 cf_strcpy (ioparm
.readwrite
, ioparm
.readwrite_len
, p
);
216 if (ioparm
.delim
!= NULL
)
218 if (u
== NULL
|| u
->flags
.form
!= FORM_FORMATTED
)
221 switch (u
->flags
.delim
)
229 case DELIM_APOSTROPHE
:
233 internal_error ("inquire_via_unit(): Bad delim");
236 cf_strcpy (ioparm
.access
, ioparm
.access_len
, p
);
239 if (ioparm
.pad
!= NULL
)
241 if (u
== NULL
|| u
->flags
.form
!= FORM_FORMATTED
)
244 switch (u
->flags
.pad
)
253 internal_error ("inquire_via_unit(): Bad pad");
256 cf_strcpy (ioparm
.pad
, ioparm
.pad_len
, p
);
261 /* inquire_via_filename()-- Inquiry via filename. This subroutine is
262 * only used if the filename is *not* connected to a unit number. */
265 inquire_via_filename (void)
269 if (ioparm
.exist
!= NULL
)
270 *ioparm
.exist
= file_exists ();
272 if (ioparm
.opened
!= NULL
)
275 if (ioparm
.number
!= NULL
)
278 if (ioparm
.named
!= NULL
)
281 if (ioparm
.name
!= NULL
)
282 fstrcpy (ioparm
.name
, ioparm
.name_len
, ioparm
.file
, ioparm
.file_len
);
284 if (ioparm
.access
!= NULL
)
285 cf_strcpy (ioparm
.access
, ioparm
.access_len
, undefined
);
287 if (ioparm
.sequential
!= NULL
)
289 p
= inquire_sequential (ioparm
.file
, ioparm
.file_len
);
290 cf_strcpy (ioparm
.sequential
, ioparm
.sequential_len
, p
);
293 if (ioparm
.direct
!= NULL
)
295 p
= inquire_direct (ioparm
.file
, ioparm
.file_len
);
296 cf_strcpy (ioparm
.direct
, ioparm
.direct_len
, p
);
299 if (ioparm
.form
!= NULL
)
300 cf_strcpy (ioparm
.form
, ioparm
.form_len
, undefined
);
302 if (ioparm
.formatted
!= NULL
)
304 p
= inquire_formatted (ioparm
.file
, ioparm
.file_len
);
305 cf_strcpy (ioparm
.formatted
, ioparm
.formatted_len
, p
);
308 if (ioparm
.unformatted
!= NULL
)
310 p
= inquire_unformatted (ioparm
.file
, ioparm
.file_len
);
311 cf_strcpy (ioparm
.unformatted
, ioparm
.unformatted_len
, p
);
314 if (ioparm
.recl_out
!= NULL
)
315 *ioparm
.recl_out
= 0;
317 if (ioparm
.nextrec
!= NULL
)
320 if (ioparm
.blank
!= NULL
)
321 cf_strcpy (ioparm
.blank
, ioparm
.blank_len
, undefined
);
323 if (ioparm
.position
!= NULL
)
324 cf_strcpy (ioparm
.position
, ioparm
.position_len
, undefined
);
326 if (ioparm
.access
!= NULL
)
327 cf_strcpy (ioparm
.access
, ioparm
.access_len
, undefined
);
329 if (ioparm
.read
!= NULL
)
331 p
= inquire_read (ioparm
.file
, ioparm
.file_len
);
332 cf_strcpy (ioparm
.read
, ioparm
.read_len
, p
);
335 if (ioparm
.write
!= NULL
)
337 p
= inquire_write (ioparm
.file
, ioparm
.file_len
);
338 cf_strcpy (ioparm
.write
, ioparm
.write_len
, p
);
341 if (ioparm
.readwrite
!= NULL
)
343 p
= inquire_read (ioparm
.file
, ioparm
.file_len
);
344 cf_strcpy (ioparm
.readwrite
, ioparm
.readwrite_len
, p
);
347 if (ioparm
.delim
!= NULL
)
348 cf_strcpy (ioparm
.delim
, ioparm
.delim_len
, undefined
);
350 if (ioparm
.pad
!= NULL
)
351 cf_strcpy (ioparm
.pad
, ioparm
.pad_len
, undefined
);
356 /* Library entry point for the INQUIRE statement (non-IOLENGTH
359 extern void st_inquire (void);
360 export_proto(st_inquire
);
369 if (ioparm
.file
== NULL
)
370 inquire_via_unit (find_unit (ioparm
.unit
));
375 inquire_via_filename ();
377 inquire_via_unit (u
);