]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/io/inquire.c
inquire.c (inquire_via_unit): If a unit is opened...
[thirdparty/gcc.git] / libgfortran / io / inquire.c
1 /* Copyright (C) 2002, 2003, 2005, 2007 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
5
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.
10
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.)
19
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.
24
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. */
29
30
31 /* Implement the non-IOLENGTH variant of the INQUIRY statement */
32
33 #include "io.h"
34
35
36 static const char undefined[] = "UNDEFINED";
37
38
39 /* inquire_via_unit()-- Inquiry via unit number. The unit might not exist. */
40
41 static void
42 inquire_via_unit (st_parameter_inquire *iqp, gfc_unit * u)
43 {
44 const char *p;
45 GFC_INTEGER_4 cf = iqp->common.flags;
46
47 if ((cf & IOPARM_INQUIRE_HAS_EXIST) != 0)
48 {
49 *iqp->exist = (iqp->common.unit >= 0
50 && iqp->common.unit <= GFC_INTEGER_4_HUGE);
51
52 if ((cf & IOPARM_INQUIRE_HAS_FILE) == 0)
53 {
54 if (!(*iqp->exist))
55 *iqp->common.iostat = LIBERROR_BAD_UNIT;
56 *iqp->exist = *iqp->exist
57 && (*iqp->common.iostat != LIBERROR_BAD_UNIT);
58 }
59 }
60
61 if ((cf & IOPARM_INQUIRE_HAS_OPENED) != 0)
62 *iqp->opened = (u != NULL);
63
64 if ((cf & IOPARM_INQUIRE_HAS_NUMBER) != 0)
65 *iqp->number = (u != NULL) ? u->unit_number : -1;
66
67 if ((cf & IOPARM_INQUIRE_HAS_NAMED) != 0)
68 *iqp->named = (u != NULL && u->flags.status != STATUS_SCRATCH);
69
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);
73
74 if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
75 {
76 if (u == NULL)
77 p = undefined;
78 else
79 switch (u->flags.access)
80 {
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");
92 }
93
94 cf_strcpy (iqp->access, iqp->access_len, p);
95 }
96
97 if ((cf & IOPARM_INQUIRE_HAS_SEQUENTIAL) != 0)
98 {
99 if (u == NULL)
100 p = inquire_sequential (NULL, 0);
101 else
102 switch (u->flags.access)
103 {
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");
113 }
114
115 cf_strcpy (iqp->sequential, iqp->sequential_len, p);
116 }
117
118 if ((cf & IOPARM_INQUIRE_HAS_DIRECT) != 0)
119 {
120 if (u == NULL)
121 p = inquire_direct (NULL, 0);
122 else
123 switch (u->flags.access)
124 {
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");
134 }
135
136 cf_strcpy (iqp->direct, iqp->direct_len, p);
137 }
138
139 if ((cf & IOPARM_INQUIRE_HAS_FORM) != 0)
140 {
141 if (u == NULL)
142 p = undefined;
143 else
144 switch (u->flags.form)
145 {
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");
154 }
155
156 cf_strcpy (iqp->form, iqp->form_len, p);
157 }
158
159 if ((cf & IOPARM_INQUIRE_HAS_FORMATTED) != 0)
160 {
161 if (u == NULL)
162 p = inquire_formatted (NULL, 0);
163 else
164 switch (u->flags.form)
165 {
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");
174 }
175
176 cf_strcpy (iqp->formatted, iqp->formatted_len, p);
177 }
178
179 if ((cf & IOPARM_INQUIRE_HAS_UNFORMATTED) != 0)
180 {
181 if (u == NULL)
182 p = inquire_unformatted (NULL, 0);
183 else
184 switch (u->flags.form)
185 {
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");
194 }
195
196 cf_strcpy (iqp->unformatted, iqp->unformatted_len, p);
197 }
198
199 if ((cf & IOPARM_INQUIRE_HAS_RECL_OUT) != 0)
200 *iqp->recl_out = (u != NULL) ? u->recl : 0;
201
202 if ((cf & IOPARM_INQUIRE_HAS_STRM_POS_OUT) != 0)
203 *iqp->strm_pos_out = (u != NULL) ? u->strm_pos : 0;
204
205 if ((cf & IOPARM_INQUIRE_HAS_NEXTREC) != 0)
206 {
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;
212 }
213
214 if ((cf & IOPARM_INQUIRE_HAS_BLANK) != 0)
215 {
216 if (u == NULL)
217 p = undefined;
218 else
219 switch (u->flags.blank)
220 {
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");
229 }
230
231 cf_strcpy (iqp->blank, iqp->blank_len, p);
232 }
233
234 if ((cf & IOPARM_INQUIRE_HAS_POSITION) != 0)
235 {
236 if (u == NULL || u->flags.access == ACCESS_DIRECT)
237 p = undefined;
238 else
239 switch (u->flags.position)
240 {
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;
256 }
257 cf_strcpy (iqp->position, iqp->position_len, p);
258 }
259
260 if ((cf & IOPARM_INQUIRE_HAS_ACTION) != 0)
261 {
262 if (u == NULL)
263 p = undefined;
264 else
265 switch (u->flags.action)
266 {
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");
278 }
279
280 cf_strcpy (iqp->action, iqp->action_len, p);
281 }
282
283 if ((cf & IOPARM_INQUIRE_HAS_READ) != 0)
284 {
285 p = (u == NULL) ? inquire_read (NULL, 0) :
286 inquire_read (u->file, u->file_len);
287
288 cf_strcpy (iqp->read, iqp->read_len, p);
289 }
290
291 if ((cf & IOPARM_INQUIRE_HAS_WRITE) != 0)
292 {
293 p = (u == NULL) ? inquire_write (NULL, 0) :
294 inquire_write (u->file, u->file_len);
295
296 cf_strcpy (iqp->write, iqp->write_len, p);
297 }
298
299 if ((cf & IOPARM_INQUIRE_HAS_READWRITE) != 0)
300 {
301 p = (u == NULL) ? inquire_readwrite (NULL, 0) :
302 inquire_readwrite (u->file, u->file_len);
303
304 cf_strcpy (iqp->readwrite, iqp->readwrite_len, p);
305 }
306
307 if ((cf & IOPARM_INQUIRE_HAS_DELIM) != 0)
308 {
309 if (u == NULL || u->flags.form != FORM_FORMATTED)
310 p = undefined;
311 else
312 switch (u->flags.delim)
313 {
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");
325 }
326
327 cf_strcpy (iqp->delim, iqp->delim_len, p);
328 }
329
330 if ((cf & IOPARM_INQUIRE_HAS_PAD) != 0)
331 {
332 if (u == NULL || u->flags.form != FORM_FORMATTED)
333 p = undefined;
334 else
335 switch (u->flags.pad)
336 {
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");
345 }
346
347 cf_strcpy (iqp->pad, iqp->pad_len, p);
348 }
349
350 if ((cf & IOPARM_INQUIRE_HAS_CONVERT) != 0)
351 {
352 if (u == NULL)
353 p = undefined;
354 else
355 switch (u->flags.convert)
356 {
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;
361
362 case GFC_CONVERT_SWAP:
363 p = l8_to_l4_offset ? "LITTLE_ENDIAN" : "BIG_ENDIAN";
364 break;
365
366 default:
367 internal_error (&iqp->common, "inquire_via_unit(): Bad convert");
368 }
369
370 cf_strcpy (iqp->convert, iqp->convert_len, p);
371 }
372 }
373
374
375 /* inquire_via_filename()-- Inquiry via filename. This subroutine is
376 * only used if the filename is *not* connected to a unit number. */
377
378 static void
379 inquire_via_filename (st_parameter_inquire *iqp)
380 {
381 const char *p;
382 GFC_INTEGER_4 cf = iqp->common.flags;
383
384 if ((cf & IOPARM_INQUIRE_HAS_EXIST) != 0)
385 *iqp->exist = file_exists (iqp->file, iqp->file_len);
386
387 if ((cf & IOPARM_INQUIRE_HAS_OPENED) != 0)
388 *iqp->opened = 0;
389
390 if ((cf & IOPARM_INQUIRE_HAS_NUMBER) != 0)
391 *iqp->number = -1;
392
393 if ((cf & IOPARM_INQUIRE_HAS_NAMED) != 0)
394 *iqp->named = 1;
395
396 if ((cf & IOPARM_INQUIRE_HAS_NAME) != 0)
397 fstrcpy (iqp->name, iqp->name_len, iqp->file, iqp->file_len);
398
399 if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
400 cf_strcpy (iqp->access, iqp->access_len, undefined);
401
402 if ((cf & IOPARM_INQUIRE_HAS_SEQUENTIAL) != 0)
403 {
404 p = "UNKNOWN";
405 cf_strcpy (iqp->sequential, iqp->sequential_len, p);
406 }
407
408 if ((cf & IOPARM_INQUIRE_HAS_DIRECT) != 0)
409 {
410 p = "UNKNOWN";
411 cf_strcpy (iqp->direct, iqp->direct_len, p);
412 }
413
414 if ((cf & IOPARM_INQUIRE_HAS_FORM) != 0)
415 cf_strcpy (iqp->form, iqp->form_len, undefined);
416
417 if ((cf & IOPARM_INQUIRE_HAS_FORMATTED) != 0)
418 {
419 p = "UNKNOWN";
420 cf_strcpy (iqp->formatted, iqp->formatted_len, p);
421 }
422
423 if ((cf & IOPARM_INQUIRE_HAS_UNFORMATTED) != 0)
424 {
425 p = "UNKNOWN";
426 cf_strcpy (iqp->unformatted, iqp->unformatted_len, p);
427 }
428
429 if ((cf & IOPARM_INQUIRE_HAS_RECL_OUT) != 0)
430 *iqp->recl_out = 0;
431
432 if ((cf & IOPARM_INQUIRE_HAS_NEXTREC) != 0)
433 *iqp->nextrec = 0;
434
435 if ((cf & IOPARM_INQUIRE_HAS_BLANK) != 0)
436 cf_strcpy (iqp->blank, iqp->blank_len, undefined);
437
438 if ((cf & IOPARM_INQUIRE_HAS_POSITION) != 0)
439 cf_strcpy (iqp->position, iqp->position_len, undefined);
440
441 if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
442 cf_strcpy (iqp->access, iqp->access_len, undefined);
443
444 if ((cf & IOPARM_INQUIRE_HAS_READ) != 0)
445 {
446 p = inquire_read (iqp->file, iqp->file_len);
447 cf_strcpy (iqp->read, iqp->read_len, p);
448 }
449
450 if ((cf & IOPARM_INQUIRE_HAS_WRITE) != 0)
451 {
452 p = inquire_write (iqp->file, iqp->file_len);
453 cf_strcpy (iqp->write, iqp->write_len, p);
454 }
455
456 if ((cf & IOPARM_INQUIRE_HAS_READWRITE) != 0)
457 {
458 p = inquire_read (iqp->file, iqp->file_len);
459 cf_strcpy (iqp->readwrite, iqp->readwrite_len, p);
460 }
461
462 if ((cf & IOPARM_INQUIRE_HAS_DELIM) != 0)
463 cf_strcpy (iqp->delim, iqp->delim_len, undefined);
464
465 if ((cf & IOPARM_INQUIRE_HAS_PAD) != 0)
466 cf_strcpy (iqp->pad, iqp->pad_len, undefined);
467 }
468
469
470 /* Library entry point for the INQUIRE statement (non-IOLENGTH
471 form). */
472
473 extern void st_inquire (st_parameter_inquire *);
474 export_proto(st_inquire);
475
476 void
477 st_inquire (st_parameter_inquire *iqp)
478 {
479 gfc_unit *u;
480
481 library_start (&iqp->common);
482
483 if ((iqp->common.flags & IOPARM_INQUIRE_HAS_FILE) == 0)
484 {
485 u = find_unit (iqp->common.unit);
486 inquire_via_unit (iqp, u);
487 }
488 else
489 {
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);
495 }
496 if (u != NULL)
497 unlock_unit (u);
498
499 library_end ();
500 }