Merged with mainline at revision 128810.
[official-gcc.git] / libgfortran / io / inquire.c
blob2c16a3b922d2b130c92f586b4a9a613c72ef9f19
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)
9 any later version.
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
18 executable.)
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 */
33 #include "io.h"
36 static const char undefined[] = "UNDEFINED";
39 /* inquire_via_unit()-- Inquiry via unit number. The unit might not exist. */
41 static void
42 inquire_via_unit (st_parameter_inquire *iqp, gfc_unit * u)
44 const char *p;
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)
65 if (u == NULL)
66 p = undefined;
67 else
68 switch (u->flags.access)
70 case ACCESS_SEQUENTIAL:
71 p = "SEQUENTIAL";
72 break;
73 case ACCESS_DIRECT:
74 p = "DIRECT";
75 break;
76 case ACCESS_STREAM:
77 p = "STREAM";
78 break;
79 default:
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)
88 if (u == NULL)
89 p = inquire_sequential (NULL, 0);
90 else
92 /* disallow an open direct access file to be accessed sequentially */
93 if (u->flags.access == ACCESS_DIRECT)
94 p = "NO";
95 else
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)
112 if (u == NULL)
113 p = undefined;
114 else
115 switch (u->flags.form)
117 case FORM_FORMATTED:
118 p = "FORMATTED";
119 break;
120 case FORM_UNFORMATTED:
121 p = "UNFORMATTED";
122 break;
123 default:
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;
157 else
158 *iqp->nextrec = 0;
161 if ((cf & IOPARM_INQUIRE_HAS_BLANK) != 0)
163 if (u == NULL)
164 p = undefined;
165 else
166 switch (u->flags.blank)
168 case BLANK_NULL:
169 p = "NULL";
170 break;
171 case BLANK_ZERO:
172 p = "ZERO";
173 break;
174 default:
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)
184 p = undefined;
185 else
186 switch (u->flags.position)
188 case POSITION_REWIND:
189 p = "REWIND";
190 break;
191 case POSITION_APPEND:
192 p = "APPEND";
193 break;
194 case POSITION_ASIS:
195 p = "ASIS";
196 break;
197 default:
198 /* if not direct access, it must be
199 either REWIND, APPEND, or ASIS.
200 ASIS seems to be the best default */
201 p = "ASIS";
202 break;
204 cf_strcpy (iqp->position, iqp->position_len, p);
207 if ((cf & IOPARM_INQUIRE_HAS_ACTION) != 0)
209 if (u == NULL)
210 p = undefined;
211 else
212 switch (u->flags.action)
214 case ACTION_READ:
215 p = "READ";
216 break;
217 case ACTION_WRITE:
218 p = "WRITE";
219 break;
220 case ACTION_READWRITE:
221 p = "READWRITE";
222 break;
223 default:
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)
257 p = undefined;
258 else
259 switch (u->flags.delim)
261 case DELIM_NONE:
262 p = "NONE";
263 break;
264 case DELIM_QUOTE:
265 p = "QUOTE";
266 break;
267 case DELIM_APOSTROPHE:
268 p = "APOSTROPHE";
269 break;
270 default:
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)
280 p = undefined;
281 else
282 switch (u->flags.pad)
284 case PAD_NO:
285 p = "NO";
286 break;
287 case PAD_YES:
288 p = "YES";
289 break;
290 default:
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)
299 if (u == NULL)
300 p = undefined;
301 else
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";
307 break;
309 case GFC_CONVERT_SWAP:
310 p = l8_to_l4_offset ? "LITTLE_ENDIAN" : "BIG_ENDIAN";
311 break;
313 default:
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. */
325 static void
326 inquire_via_filename (st_parameter_inquire *iqp)
328 const char *p;
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)
335 *iqp->opened = 0;
337 if ((cf & IOPARM_INQUIRE_HAS_NUMBER) != 0)
338 *iqp->number = -1;
340 if ((cf & IOPARM_INQUIRE_HAS_NAMED) != 0)
341 *iqp->named = 1;
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)
377 *iqp->recl_out = 0;
379 if ((cf & IOPARM_INQUIRE_HAS_NEXTREC) != 0)
380 *iqp->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
418 form). */
420 extern void st_inquire (st_parameter_inquire *);
421 export_proto(st_inquire);
423 void
424 st_inquire (st_parameter_inquire *iqp)
426 gfc_unit *u;
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);
435 else
437 u = find_file (iqp->file, iqp->file_len);
438 if (u == NULL)
439 inquire_via_filename (iqp);
440 else
441 inquire_via_unit (iqp, u);
443 if (u != NULL)
444 unlock_unit (u);
446 library_end ();