* gcc.dg/vect/slp-perm-1.c (main): Make sure loops aren't vectorized.
[official-gcc.git] / libgfortran / io / inquire.c
blobf908cde0ccf692d68c9db8a6d97a09053a19ec28
1 /* Copyright (C) 2002, 2003, 2005, 2007, 2009, 2010
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
27 /* Implement the non-IOLENGTH variant of the INQUIRY statement */
29 #include "io.h"
30 #include "unix.h"
33 static const char undefined[] = "UNDEFINED";
36 /* inquire_via_unit()-- Inquiry via unit number. The unit might not exist. */
38 static void
39 inquire_via_unit (st_parameter_inquire *iqp, gfc_unit * u)
41 const char *p;
42 GFC_INTEGER_4 cf = iqp->common.flags;
44 if ((cf & IOPARM_INQUIRE_HAS_EXIST) != 0)
46 *iqp->exist = (iqp->common.unit >= 0
47 && iqp->common.unit <= GFC_INTEGER_4_HUGE);
49 if ((cf & IOPARM_INQUIRE_HAS_FILE) == 0)
51 if (!(*iqp->exist))
52 *iqp->common.iostat = LIBERROR_BAD_UNIT;
53 *iqp->exist = *iqp->exist
54 && (*iqp->common.iostat != LIBERROR_BAD_UNIT);
58 if ((cf & IOPARM_INQUIRE_HAS_OPENED) != 0)
59 *iqp->opened = (u != NULL);
61 if ((cf & IOPARM_INQUIRE_HAS_NUMBER) != 0)
62 *iqp->number = (u != NULL) ? u->unit_number : -1;
64 if ((cf & IOPARM_INQUIRE_HAS_NAMED) != 0)
65 *iqp->named = (u != NULL && u->flags.status != STATUS_SCRATCH);
67 if ((cf & IOPARM_INQUIRE_HAS_NAME) != 0
68 && u != NULL && u->flags.status != STATUS_SCRATCH)
69 fstrcpy (iqp->name, iqp->name_len, u->file, u->file_len);
71 if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
73 if (u == NULL)
74 p = undefined;
75 else
76 switch (u->flags.access)
78 case ACCESS_SEQUENTIAL:
79 p = "SEQUENTIAL";
80 break;
81 case ACCESS_DIRECT:
82 p = "DIRECT";
83 break;
84 case ACCESS_STREAM:
85 p = "STREAM";
86 break;
87 default:
88 internal_error (&iqp->common, "inquire_via_unit(): Bad access");
91 cf_strcpy (iqp->access, iqp->access_len, p);
94 if ((cf & IOPARM_INQUIRE_HAS_SEQUENTIAL) != 0)
96 if (u == NULL)
97 p = inquire_sequential (NULL, 0);
98 else
99 switch (u->flags.access)
101 case ACCESS_DIRECT:
102 case ACCESS_STREAM:
103 p = "NO";
104 break;
105 case ACCESS_SEQUENTIAL:
106 p = "YES";
107 break;
108 default:
109 internal_error (&iqp->common, "inquire_via_unit(): Bad access");
112 cf_strcpy (iqp->sequential, iqp->sequential_len, p);
115 if ((cf & IOPARM_INQUIRE_HAS_DIRECT) != 0)
117 if (u == NULL)
118 p = inquire_direct (NULL, 0);
119 else
120 switch (u->flags.access)
122 case ACCESS_SEQUENTIAL:
123 case ACCESS_STREAM:
124 p = "NO";
125 break;
126 case ACCESS_DIRECT:
127 p = "YES";
128 break;
129 default:
130 internal_error (&iqp->common, "inquire_via_unit(): Bad access");
133 cf_strcpy (iqp->direct, iqp->direct_len, p);
136 if ((cf & IOPARM_INQUIRE_HAS_FORM) != 0)
138 if (u == NULL)
139 p = undefined;
140 else
141 switch (u->flags.form)
143 case FORM_FORMATTED:
144 p = "FORMATTED";
145 break;
146 case FORM_UNFORMATTED:
147 p = "UNFORMATTED";
148 break;
149 default:
150 internal_error (&iqp->common, "inquire_via_unit(): Bad form");
153 cf_strcpy (iqp->form, iqp->form_len, p);
156 if ((cf & IOPARM_INQUIRE_HAS_FORMATTED) != 0)
158 if (u == NULL)
159 p = inquire_formatted (NULL, 0);
160 else
161 switch (u->flags.form)
163 case FORM_FORMATTED:
164 p = "YES";
165 break;
166 case FORM_UNFORMATTED:
167 p = "NO";
168 break;
169 default:
170 internal_error (&iqp->common, "inquire_via_unit(): Bad form");
173 cf_strcpy (iqp->formatted, iqp->formatted_len, p);
176 if ((cf & IOPARM_INQUIRE_HAS_UNFORMATTED) != 0)
178 if (u == NULL)
179 p = inquire_unformatted (NULL, 0);
180 else
181 switch (u->flags.form)
183 case FORM_FORMATTED:
184 p = "NO";
185 break;
186 case FORM_UNFORMATTED:
187 p = "YES";
188 break;
189 default:
190 internal_error (&iqp->common, "inquire_via_unit(): Bad form");
193 cf_strcpy (iqp->unformatted, iqp->unformatted_len, p);
196 if ((cf & IOPARM_INQUIRE_HAS_RECL_OUT) != 0)
197 *iqp->recl_out = (u != NULL) ? u->recl : 0;
199 if ((cf & IOPARM_INQUIRE_HAS_STRM_POS_OUT) != 0)
200 *iqp->strm_pos_out = (u != NULL) ? u->strm_pos : 0;
202 if ((cf & IOPARM_INQUIRE_HAS_NEXTREC) != 0)
204 /* This only makes sense in the context of DIRECT access. */
205 if (u != NULL && u->flags.access == ACCESS_DIRECT)
206 *iqp->nextrec = u->last_record + 1;
207 else
208 *iqp->nextrec = 0;
211 if ((cf & IOPARM_INQUIRE_HAS_BLANK) != 0)
213 if (u == NULL || u->flags.form != FORM_FORMATTED)
214 p = undefined;
215 else
216 switch (u->flags.blank)
218 case BLANK_NULL:
219 p = "NULL";
220 break;
221 case BLANK_ZERO:
222 p = "ZERO";
223 break;
224 default:
225 internal_error (&iqp->common, "inquire_via_unit(): Bad blank");
228 cf_strcpy (iqp->blank, iqp->blank_len, p);
231 if ((cf & IOPARM_INQUIRE_HAS_PAD) != 0)
233 if (u == NULL || u->flags.form != FORM_FORMATTED)
234 p = undefined;
235 else
236 switch (u->flags.pad)
238 case PAD_YES:
239 p = "YES";
240 break;
241 case PAD_NO:
242 p = "NO";
243 break;
244 default:
245 internal_error (&iqp->common, "inquire_via_unit(): Bad pad");
248 cf_strcpy (iqp->pad, iqp->pad_len, p);
251 if (cf & IOPARM_INQUIRE_HAS_FLAGS2)
253 GFC_INTEGER_4 cf2 = iqp->flags2;
255 if ((cf2 & IOPARM_INQUIRE_HAS_PENDING) != 0)
256 *iqp->pending = 0;
258 if ((cf2 & IOPARM_INQUIRE_HAS_ID) != 0)
259 *iqp->id = 0;
261 if ((cf2 & IOPARM_INQUIRE_HAS_ENCODING) != 0)
263 if (u == NULL || u->flags.form != FORM_FORMATTED)
264 p = undefined;
265 else
266 switch (u->flags.encoding)
268 case ENCODING_DEFAULT:
269 p = "UNKNOWN";
270 break;
271 case ENCODING_UTF8:
272 p = "UTF-8";
273 break;
274 default:
275 internal_error (&iqp->common, "inquire_via_unit(): Bad encoding");
278 cf_strcpy (iqp->encoding, iqp->encoding_len, p);
281 if ((cf2 & IOPARM_INQUIRE_HAS_DECIMAL) != 0)
283 if (u == NULL || u->flags.form != FORM_FORMATTED)
284 p = undefined;
285 else
286 switch (u->flags.decimal)
288 case DECIMAL_POINT:
289 p = "POINT";
290 break;
291 case DECIMAL_COMMA:
292 p = "COMMA";
293 break;
294 default:
295 internal_error (&iqp->common, "inquire_via_unit(): Bad comma");
298 cf_strcpy (iqp->decimal, iqp->decimal_len, p);
301 if ((cf2 & IOPARM_INQUIRE_HAS_ASYNCHRONOUS) != 0)
303 if (u == NULL)
304 p = undefined;
305 else
306 switch (u->flags.async)
308 case ASYNC_YES:
309 p = "YES";
310 break;
311 case ASYNC_NO:
312 p = "NO";
313 break;
314 default:
315 internal_error (&iqp->common, "inquire_via_unit(): Bad async");
318 cf_strcpy (iqp->asynchronous, iqp->asynchronous_len, p);
321 if ((cf2 & IOPARM_INQUIRE_HAS_SIGN) != 0)
323 if (u == NULL)
324 p = undefined;
325 else
326 switch (u->flags.sign)
328 case SIGN_PROCDEFINED:
329 p = "PROCESSOR_DEFINED";
330 break;
331 case SIGN_SUPPRESS:
332 p = "SUPPRESS";
333 break;
334 case SIGN_PLUS:
335 p = "PLUS";
336 break;
337 default:
338 internal_error (&iqp->common, "inquire_via_unit(): Bad sign");
341 cf_strcpy (iqp->sign, iqp->sign_len, p);
344 if ((cf2 & IOPARM_INQUIRE_HAS_ROUND) != 0)
346 if (u == NULL)
347 p = undefined;
348 else
349 switch (u->flags.round)
351 case ROUND_UP:
352 p = "UP";
353 break;
354 case ROUND_DOWN:
355 p = "DOWN";
356 break;
357 case ROUND_ZERO:
358 p = "ZERO";
359 break;
360 case ROUND_NEAREST:
361 p = "NEAREST";
362 break;
363 case ROUND_COMPATIBLE:
364 p = "COMPATIBLE";
365 break;
366 case ROUND_PROCDEFINED:
367 p = "PROCESSOR_DEFINED";
368 break;
369 default:
370 internal_error (&iqp->common, "inquire_via_unit(): Bad round");
373 cf_strcpy (iqp->round, iqp->round_len, p);
376 if ((cf2 & IOPARM_INQUIRE_HAS_SIZE) != 0)
378 if (u == NULL)
379 *iqp->size = -1;
380 else
381 *iqp->size = file_size (u->file, (gfc_charlen_type) u->file_len);
385 if ((cf & IOPARM_INQUIRE_HAS_POSITION) != 0)
387 if (u == NULL || u->flags.access == ACCESS_DIRECT)
388 p = undefined;
389 else
390 switch (u->flags.position)
392 case POSITION_REWIND:
393 p = "REWIND";
394 break;
395 case POSITION_APPEND:
396 p = "APPEND";
397 break;
398 case POSITION_ASIS:
399 p = "ASIS";
400 break;
401 default:
402 /* if not direct access, it must be
403 either REWIND, APPEND, or ASIS.
404 ASIS seems to be the best default */
405 p = "ASIS";
406 break;
408 cf_strcpy (iqp->position, iqp->position_len, p);
411 if ((cf & IOPARM_INQUIRE_HAS_ACTION) != 0)
413 if (u == NULL)
414 p = undefined;
415 else
416 switch (u->flags.action)
418 case ACTION_READ:
419 p = "READ";
420 break;
421 case ACTION_WRITE:
422 p = "WRITE";
423 break;
424 case ACTION_READWRITE:
425 p = "READWRITE";
426 break;
427 default:
428 internal_error (&iqp->common, "inquire_via_unit(): Bad action");
431 cf_strcpy (iqp->action, iqp->action_len, p);
434 if ((cf & IOPARM_INQUIRE_HAS_READ) != 0)
436 p = (u == NULL) ? inquire_read (NULL, 0) :
437 inquire_read (u->file, u->file_len);
439 cf_strcpy (iqp->read, iqp->read_len, p);
442 if ((cf & IOPARM_INQUIRE_HAS_WRITE) != 0)
444 p = (u == NULL) ? inquire_write (NULL, 0) :
445 inquire_write (u->file, u->file_len);
447 cf_strcpy (iqp->write, iqp->write_len, p);
450 if ((cf & IOPARM_INQUIRE_HAS_READWRITE) != 0)
452 p = (u == NULL) ? inquire_readwrite (NULL, 0) :
453 inquire_readwrite (u->file, u->file_len);
455 cf_strcpy (iqp->readwrite, iqp->readwrite_len, p);
458 if ((cf & IOPARM_INQUIRE_HAS_DELIM) != 0)
460 if (u == NULL || u->flags.form != FORM_FORMATTED)
461 p = undefined;
462 else
463 switch (u->flags.delim)
465 case DELIM_NONE:
466 p = "NONE";
467 break;
468 case DELIM_QUOTE:
469 p = "QUOTE";
470 break;
471 case DELIM_APOSTROPHE:
472 p = "APOSTROPHE";
473 break;
474 default:
475 internal_error (&iqp->common, "inquire_via_unit(): Bad delim");
478 cf_strcpy (iqp->delim, iqp->delim_len, p);
481 if ((cf & IOPARM_INQUIRE_HAS_PAD) != 0)
483 if (u == NULL || u->flags.form != FORM_FORMATTED)
484 p = undefined;
485 else
486 switch (u->flags.pad)
488 case PAD_NO:
489 p = "NO";
490 break;
491 case PAD_YES:
492 p = "YES";
493 break;
494 default:
495 internal_error (&iqp->common, "inquire_via_unit(): Bad pad");
498 cf_strcpy (iqp->pad, iqp->pad_len, p);
501 if ((cf & IOPARM_INQUIRE_HAS_CONVERT) != 0)
503 if (u == NULL)
504 p = undefined;
505 else
506 switch (u->flags.convert)
508 /* big_endian is 0 for little-endian, 1 for big-endian. */
509 case GFC_CONVERT_NATIVE:
510 p = big_endian ? "BIG_ENDIAN" : "LITTLE_ENDIAN";
511 break;
513 case GFC_CONVERT_SWAP:
514 p = big_endian ? "LITTLE_ENDIAN" : "BIG_ENDIAN";
515 break;
517 default:
518 internal_error (&iqp->common, "inquire_via_unit(): Bad convert");
521 cf_strcpy (iqp->convert, iqp->convert_len, p);
526 /* inquire_via_filename()-- Inquiry via filename. This subroutine is
527 * only used if the filename is *not* connected to a unit number. */
529 static void
530 inquire_via_filename (st_parameter_inquire *iqp)
532 const char *p;
533 GFC_INTEGER_4 cf = iqp->common.flags;
535 if ((cf & IOPARM_INQUIRE_HAS_EXIST) != 0)
536 *iqp->exist = file_exists (iqp->file, iqp->file_len);
538 if ((cf & IOPARM_INQUIRE_HAS_OPENED) != 0)
539 *iqp->opened = 0;
541 if ((cf & IOPARM_INQUIRE_HAS_NUMBER) != 0)
542 *iqp->number = -1;
544 if ((cf & IOPARM_INQUIRE_HAS_NAMED) != 0)
545 *iqp->named = 1;
547 if ((cf & IOPARM_INQUIRE_HAS_NAME) != 0)
548 fstrcpy (iqp->name, iqp->name_len, iqp->file, iqp->file_len);
550 if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
551 cf_strcpy (iqp->access, iqp->access_len, undefined);
553 if ((cf & IOPARM_INQUIRE_HAS_SEQUENTIAL) != 0)
555 p = "UNKNOWN";
556 cf_strcpy (iqp->sequential, iqp->sequential_len, p);
559 if ((cf & IOPARM_INQUIRE_HAS_DIRECT) != 0)
561 p = "UNKNOWN";
562 cf_strcpy (iqp->direct, iqp->direct_len, p);
565 if ((cf & IOPARM_INQUIRE_HAS_FORM) != 0)
566 cf_strcpy (iqp->form, iqp->form_len, undefined);
568 if ((cf & IOPARM_INQUIRE_HAS_FORMATTED) != 0)
570 p = "UNKNOWN";
571 cf_strcpy (iqp->formatted, iqp->formatted_len, p);
574 if ((cf & IOPARM_INQUIRE_HAS_UNFORMATTED) != 0)
576 p = "UNKNOWN";
577 cf_strcpy (iqp->unformatted, iqp->unformatted_len, p);
580 if ((cf & IOPARM_INQUIRE_HAS_RECL_OUT) != 0)
581 *iqp->recl_out = 0;
583 if ((cf & IOPARM_INQUIRE_HAS_NEXTREC) != 0)
584 *iqp->nextrec = 0;
586 if ((cf & IOPARM_INQUIRE_HAS_BLANK) != 0)
587 cf_strcpy (iqp->blank, iqp->blank_len, undefined);
589 if ((cf & IOPARM_INQUIRE_HAS_PAD) != 0)
590 cf_strcpy (iqp->pad, iqp->pad_len, undefined);
592 if (cf & IOPARM_INQUIRE_HAS_FLAGS2)
594 GFC_INTEGER_4 cf2 = iqp->flags2;
596 if ((cf2 & IOPARM_INQUIRE_HAS_ENCODING) != 0)
597 cf_strcpy (iqp->encoding, iqp->encoding_len, undefined);
599 if ((cf2 & IOPARM_INQUIRE_HAS_DELIM) != 0)
600 cf_strcpy (iqp->delim, iqp->delim_len, undefined);
602 if ((cf2 & IOPARM_INQUIRE_HAS_DECIMAL) != 0)
603 cf_strcpy (iqp->decimal, iqp->decimal_len, undefined);
605 if ((cf2 & IOPARM_INQUIRE_HAS_DELIM) != 0)
606 cf_strcpy (iqp->delim, iqp->delim_len, undefined);
608 if ((cf2 & IOPARM_INQUIRE_HAS_PAD) != 0)
609 cf_strcpy (iqp->pad, iqp->pad_len, undefined);
611 if ((cf2 & IOPARM_INQUIRE_HAS_ENCODING) != 0)
612 cf_strcpy (iqp->encoding, iqp->encoding_len, undefined);
614 if ((cf2 & IOPARM_INQUIRE_HAS_SIZE) != 0)
615 *iqp->size = file_size (iqp->file, iqp->file_len);
618 if ((cf & IOPARM_INQUIRE_HAS_POSITION) != 0)
619 cf_strcpy (iqp->position, iqp->position_len, undefined);
621 if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
622 cf_strcpy (iqp->access, iqp->access_len, undefined);
624 if ((cf & IOPARM_INQUIRE_HAS_READ) != 0)
626 p = inquire_read (iqp->file, iqp->file_len);
627 cf_strcpy (iqp->read, iqp->read_len, p);
630 if ((cf & IOPARM_INQUIRE_HAS_WRITE) != 0)
632 p = inquire_write (iqp->file, iqp->file_len);
633 cf_strcpy (iqp->write, iqp->write_len, p);
636 if ((cf & IOPARM_INQUIRE_HAS_READWRITE) != 0)
638 p = inquire_read (iqp->file, iqp->file_len);
639 cf_strcpy (iqp->readwrite, iqp->readwrite_len, p);
644 /* Library entry point for the INQUIRE statement (non-IOLENGTH
645 form). */
647 extern void st_inquire (st_parameter_inquire *);
648 export_proto(st_inquire);
650 void
651 st_inquire (st_parameter_inquire *iqp)
653 gfc_unit *u;
655 library_start (&iqp->common);
657 if ((iqp->common.flags & IOPARM_INQUIRE_HAS_FILE) == 0)
659 u = find_unit (iqp->common.unit);
660 inquire_via_unit (iqp, u);
662 else
664 u = find_file (iqp->file, iqp->file_len);
665 if (u == NULL)
666 inquire_via_filename (iqp);
667 else
668 inquire_via_unit (iqp, u);
670 if (u != NULL)
671 unlock_unit (u);
673 library_end ();