Merge -r 127928:132243 from trunk
[official-gcc.git] / libgfortran / io / inquire.c
blobec462858f67f9029a718b0eda008b71ff69e13d1
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)
49 *iqp->exist = (iqp->common.unit >= 0
50 && iqp->common.unit <= GFC_INTEGER_4_HUGE);
52 if ((cf & IOPARM_INQUIRE_HAS_FILE) == 0)
54 if (!(*iqp->exist))
55 *iqp->common.iostat = LIBERROR_BAD_UNIT;
56 *iqp->exist = *iqp->exist
57 && (*iqp->common.iostat != LIBERROR_BAD_UNIT);
61 if ((cf & IOPARM_INQUIRE_HAS_OPENED) != 0)
62 *iqp->opened = (u != NULL);
64 if ((cf & IOPARM_INQUIRE_HAS_NUMBER) != 0)
65 *iqp->number = (u != NULL) ? u->unit_number : -1;
67 if ((cf & IOPARM_INQUIRE_HAS_NAMED) != 0)
68 *iqp->named = (u != NULL && u->flags.status != STATUS_SCRATCH);
70 if ((cf & IOPARM_INQUIRE_HAS_NAME) != 0
71 && u != NULL && u->flags.status != STATUS_SCRATCH)
72 fstrcpy (iqp->name, iqp->name_len, u->file, u->file_len);
74 if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
76 if (u == NULL)
77 p = undefined;
78 else
79 switch (u->flags.access)
81 case ACCESS_SEQUENTIAL:
82 p = "SEQUENTIAL";
83 break;
84 case ACCESS_DIRECT:
85 p = "DIRECT";
86 break;
87 case ACCESS_STREAM:
88 p = "STREAM";
89 break;
90 default:
91 internal_error (&iqp->common, "inquire_via_unit(): Bad access");
94 cf_strcpy (iqp->access, iqp->access_len, p);
97 if ((cf & IOPARM_INQUIRE_HAS_SEQUENTIAL) != 0)
99 if (u == NULL)
100 p = inquire_sequential (NULL, 0);
101 else
102 switch (u->flags.access)
104 case ACCESS_DIRECT:
105 case ACCESS_STREAM:
106 p = "NO";
107 break;
108 case ACCESS_SEQUENTIAL:
109 p = "YES";
110 break;
111 default:
112 internal_error (&iqp->common, "inquire_via_unit(): Bad access");
115 cf_strcpy (iqp->sequential, iqp->sequential_len, p);
118 if ((cf & IOPARM_INQUIRE_HAS_DIRECT) != 0)
120 if (u == NULL)
121 p = inquire_direct (NULL, 0);
122 else
123 switch (u->flags.access)
125 case ACCESS_SEQUENTIAL:
126 case ACCESS_STREAM:
127 p = "NO";
128 break;
129 case ACCESS_DIRECT:
130 p = "YES";
131 break;
132 default:
133 internal_error (&iqp->common, "inquire_via_unit(): Bad access");
136 cf_strcpy (iqp->direct, iqp->direct_len, p);
139 if ((cf & IOPARM_INQUIRE_HAS_FORM) != 0)
141 if (u == NULL)
142 p = undefined;
143 else
144 switch (u->flags.form)
146 case FORM_FORMATTED:
147 p = "FORMATTED";
148 break;
149 case FORM_UNFORMATTED:
150 p = "UNFORMATTED";
151 break;
152 default:
153 internal_error (&iqp->common, "inquire_via_unit(): Bad form");
156 cf_strcpy (iqp->form, iqp->form_len, p);
159 if ((cf & IOPARM_INQUIRE_HAS_FORMATTED) != 0)
161 if (u == NULL)
162 p = inquire_formatted (NULL, 0);
163 else
164 switch (u->flags.form)
166 case FORM_FORMATTED:
167 p = "YES";
168 break;
169 case FORM_UNFORMATTED:
170 p = "NO";
171 break;
172 default:
173 internal_error (&iqp->common, "inquire_via_unit(): Bad form");
176 cf_strcpy (iqp->formatted, iqp->formatted_len, p);
179 if ((cf & IOPARM_INQUIRE_HAS_UNFORMATTED) != 0)
181 if (u == NULL)
182 p = inquire_unformatted (NULL, 0);
183 else
184 switch (u->flags.form)
186 case FORM_FORMATTED:
187 p = "NO";
188 break;
189 case FORM_UNFORMATTED:
190 p = "YES";
191 break;
192 default:
193 internal_error (&iqp->common, "inquire_via_unit(): Bad form");
196 cf_strcpy (iqp->unformatted, iqp->unformatted_len, p);
199 if ((cf & IOPARM_INQUIRE_HAS_RECL_OUT) != 0)
200 *iqp->recl_out = (u != NULL) ? u->recl : 0;
202 if ((cf & IOPARM_INQUIRE_HAS_STRM_POS_OUT) != 0)
203 *iqp->strm_pos_out = (u != NULL) ? u->strm_pos : 0;
205 if ((cf & IOPARM_INQUIRE_HAS_NEXTREC) != 0)
207 /* This only makes sense in the context of DIRECT access. */
208 if (u != NULL && u->flags.access == ACCESS_DIRECT)
209 *iqp->nextrec = u->last_record + 1;
210 else
211 *iqp->nextrec = 0;
214 if ((cf & IOPARM_INQUIRE_HAS_BLANK) != 0)
216 if (u == NULL)
217 p = undefined;
218 else
219 switch (u->flags.blank)
221 case BLANK_NULL:
222 p = "NULL";
223 break;
224 case BLANK_ZERO:
225 p = "ZERO";
226 break;
227 default:
228 internal_error (&iqp->common, "inquire_via_unit(): Bad blank");
231 cf_strcpy (iqp->blank, iqp->blank_len, p);
234 if ((cf & IOPARM_INQUIRE_HAS_POSITION) != 0)
236 if (u == NULL || u->flags.access == ACCESS_DIRECT)
237 p = undefined;
238 else
239 switch (u->flags.position)
241 case POSITION_REWIND:
242 p = "REWIND";
243 break;
244 case POSITION_APPEND:
245 p = "APPEND";
246 break;
247 case POSITION_ASIS:
248 p = "ASIS";
249 break;
250 default:
251 /* if not direct access, it must be
252 either REWIND, APPEND, or ASIS.
253 ASIS seems to be the best default */
254 p = "ASIS";
255 break;
257 cf_strcpy (iqp->position, iqp->position_len, p);
260 if ((cf & IOPARM_INQUIRE_HAS_ACTION) != 0)
262 if (u == NULL)
263 p = undefined;
264 else
265 switch (u->flags.action)
267 case ACTION_READ:
268 p = "READ";
269 break;
270 case ACTION_WRITE:
271 p = "WRITE";
272 break;
273 case ACTION_READWRITE:
274 p = "READWRITE";
275 break;
276 default:
277 internal_error (&iqp->common, "inquire_via_unit(): Bad action");
280 cf_strcpy (iqp->action, iqp->action_len, p);
283 if ((cf & IOPARM_INQUIRE_HAS_READ) != 0)
285 p = (u == NULL) ? inquire_read (NULL, 0) :
286 inquire_read (u->file, u->file_len);
288 cf_strcpy (iqp->read, iqp->read_len, p);
291 if ((cf & IOPARM_INQUIRE_HAS_WRITE) != 0)
293 p = (u == NULL) ? inquire_write (NULL, 0) :
294 inquire_write (u->file, u->file_len);
296 cf_strcpy (iqp->write, iqp->write_len, p);
299 if ((cf & IOPARM_INQUIRE_HAS_READWRITE) != 0)
301 p = (u == NULL) ? inquire_readwrite (NULL, 0) :
302 inquire_readwrite (u->file, u->file_len);
304 cf_strcpy (iqp->readwrite, iqp->readwrite_len, p);
307 if ((cf & IOPARM_INQUIRE_HAS_DELIM) != 0)
309 if (u == NULL || u->flags.form != FORM_FORMATTED)
310 p = undefined;
311 else
312 switch (u->flags.delim)
314 case DELIM_NONE:
315 p = "NONE";
316 break;
317 case DELIM_QUOTE:
318 p = "QUOTE";
319 break;
320 case DELIM_APOSTROPHE:
321 p = "APOSTROPHE";
322 break;
323 default:
324 internal_error (&iqp->common, "inquire_via_unit(): Bad delim");
327 cf_strcpy (iqp->delim, iqp->delim_len, p);
330 if ((cf & IOPARM_INQUIRE_HAS_PAD) != 0)
332 if (u == NULL || u->flags.form != FORM_FORMATTED)
333 p = undefined;
334 else
335 switch (u->flags.pad)
337 case PAD_NO:
338 p = "NO";
339 break;
340 case PAD_YES:
341 p = "YES";
342 break;
343 default:
344 internal_error (&iqp->common, "inquire_via_unit(): Bad pad");
347 cf_strcpy (iqp->pad, iqp->pad_len, p);
350 if ((cf & IOPARM_INQUIRE_HAS_CONVERT) != 0)
352 if (u == NULL)
353 p = undefined;
354 else
355 switch (u->flags.convert)
357 /* l8_to_l4_offset is 0 for little-endian, 1 for big-endian. */
358 case GFC_CONVERT_NATIVE:
359 p = l8_to_l4_offset ? "BIG_ENDIAN" : "LITTLE_ENDIAN";
360 break;
362 case GFC_CONVERT_SWAP:
363 p = l8_to_l4_offset ? "LITTLE_ENDIAN" : "BIG_ENDIAN";
364 break;
366 default:
367 internal_error (&iqp->common, "inquire_via_unit(): Bad convert");
370 cf_strcpy (iqp->convert, iqp->convert_len, p);
375 /* inquire_via_filename()-- Inquiry via filename. This subroutine is
376 * only used if the filename is *not* connected to a unit number. */
378 static void
379 inquire_via_filename (st_parameter_inquire *iqp)
381 const char *p;
382 GFC_INTEGER_4 cf = iqp->common.flags;
384 if ((cf & IOPARM_INQUIRE_HAS_EXIST) != 0)
385 *iqp->exist = file_exists (iqp->file, iqp->file_len);
387 if ((cf & IOPARM_INQUIRE_HAS_OPENED) != 0)
388 *iqp->opened = 0;
390 if ((cf & IOPARM_INQUIRE_HAS_NUMBER) != 0)
391 *iqp->number = -1;
393 if ((cf & IOPARM_INQUIRE_HAS_NAMED) != 0)
394 *iqp->named = 1;
396 if ((cf & IOPARM_INQUIRE_HAS_NAME) != 0)
397 fstrcpy (iqp->name, iqp->name_len, iqp->file, iqp->file_len);
399 if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
400 cf_strcpy (iqp->access, iqp->access_len, undefined);
402 if ((cf & IOPARM_INQUIRE_HAS_SEQUENTIAL) != 0)
404 p = "UNKNOWN";
405 cf_strcpy (iqp->sequential, iqp->sequential_len, p);
408 if ((cf & IOPARM_INQUIRE_HAS_DIRECT) != 0)
410 p = "UNKNOWN";
411 cf_strcpy (iqp->direct, iqp->direct_len, p);
414 if ((cf & IOPARM_INQUIRE_HAS_FORM) != 0)
415 cf_strcpy (iqp->form, iqp->form_len, undefined);
417 if ((cf & IOPARM_INQUIRE_HAS_FORMATTED) != 0)
419 p = "UNKNOWN";
420 cf_strcpy (iqp->formatted, iqp->formatted_len, p);
423 if ((cf & IOPARM_INQUIRE_HAS_UNFORMATTED) != 0)
425 p = "UNKNOWN";
426 cf_strcpy (iqp->unformatted, iqp->unformatted_len, p);
429 if ((cf & IOPARM_INQUIRE_HAS_RECL_OUT) != 0)
430 *iqp->recl_out = 0;
432 if ((cf & IOPARM_INQUIRE_HAS_NEXTREC) != 0)
433 *iqp->nextrec = 0;
435 if ((cf & IOPARM_INQUIRE_HAS_BLANK) != 0)
436 cf_strcpy (iqp->blank, iqp->blank_len, undefined);
438 if ((cf & IOPARM_INQUIRE_HAS_POSITION) != 0)
439 cf_strcpy (iqp->position, iqp->position_len, undefined);
441 if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
442 cf_strcpy (iqp->access, iqp->access_len, undefined);
444 if ((cf & IOPARM_INQUIRE_HAS_READ) != 0)
446 p = inquire_read (iqp->file, iqp->file_len);
447 cf_strcpy (iqp->read, iqp->read_len, p);
450 if ((cf & IOPARM_INQUIRE_HAS_WRITE) != 0)
452 p = inquire_write (iqp->file, iqp->file_len);
453 cf_strcpy (iqp->write, iqp->write_len, p);
456 if ((cf & IOPARM_INQUIRE_HAS_READWRITE) != 0)
458 p = inquire_read (iqp->file, iqp->file_len);
459 cf_strcpy (iqp->readwrite, iqp->readwrite_len, p);
462 if ((cf & IOPARM_INQUIRE_HAS_DELIM) != 0)
463 cf_strcpy (iqp->delim, iqp->delim_len, undefined);
465 if ((cf & IOPARM_INQUIRE_HAS_PAD) != 0)
466 cf_strcpy (iqp->pad, iqp->pad_len, undefined);
470 /* Library entry point for the INQUIRE statement (non-IOLENGTH
471 form). */
473 extern void st_inquire (st_parameter_inquire *);
474 export_proto(st_inquire);
476 void
477 st_inquire (st_parameter_inquire *iqp)
479 gfc_unit *u;
481 library_start (&iqp->common);
483 if ((iqp->common.flags & IOPARM_INQUIRE_HAS_FILE) == 0)
485 u = find_unit (iqp->common.unit);
486 inquire_via_unit (iqp, u);
488 else
490 u = find_file (iqp->file, iqp->file_len);
491 if (u == NULL)
492 inquire_via_filename (iqp);
493 else
494 inquire_via_unit (iqp, u);
496 if (u != NULL)
497 unlock_unit (u);
499 library_end ();