* cselib.c (clear_table): Rename to cselib_clear_table.
[official-gcc.git] / libgfortran / io / inquire.c
blob1f0fcac6530fa40697fad6e1d64f9a8daf670e47
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)
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, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
31 /* Implement the non-IOLENGTH variant of the INQUIRY statement */
33 #include "config.h"
34 #include "libgfortran.h"
35 #include "io.h"
38 static char undefined[] = "UNDEFINED";
41 /* inquire_via_unit()-- Inquiry via unit number. The unit might not exist. */
43 static void
44 inquire_via_unit (gfc_unit * u)
46 const char *p;
48 if (ioparm.exist != NULL)
50 if (ioparm.unit >= 0)
51 *ioparm.exist = 1;
52 else
53 *ioparm.exist = 0;
56 if (ioparm.opened != NULL)
57 *ioparm.opened = (u != NULL);
59 if (ioparm.number != NULL)
60 *ioparm.number = (u != NULL) ? u->unit_number : -1;
62 if (ioparm.named != NULL)
63 *ioparm.named = (u != NULL && u->flags.status != STATUS_SCRATCH);
65 if (ioparm.name != NULL && u != NULL && u->flags.status != STATUS_SCRATCH)
66 fstrcpy (ioparm.name, ioparm.name_len, u->file, u->file_len);
68 if (ioparm.access != NULL)
70 if (u == NULL)
71 p = undefined;
72 else
73 switch (u->flags.access)
75 case ACCESS_SEQUENTIAL:
76 p = "SEQUENTIAL";
77 break;
78 case ACCESS_DIRECT:
79 p = "DIRECT";
80 break;
81 default:
82 internal_error ("inquire_via_unit(): Bad access");
85 cf_strcpy (ioparm.access, ioparm.access_len, p);
88 if (ioparm.sequential != NULL)
90 /* disallow an open direct access file to be accessed
91 sequentially */
92 if (u->flags.access==ACCESS_DIRECT)
93 p = "NO";
94 else
95 p = (u == NULL) ? inquire_sequential (NULL, 0) :
96 inquire_sequential (u->file, u->file_len);
98 cf_strcpy (ioparm.sequential, ioparm.sequential_len, p);
101 if (ioparm.direct != NULL)
103 p = (u == NULL) ? inquire_direct (NULL, 0) :
104 inquire_direct (u->file, u->file_len);
106 cf_strcpy (ioparm.direct, ioparm.direct_len, p);
109 if (ioparm.form != NULL)
111 if (u == NULL)
112 p = undefined;
113 else
114 switch (u->flags.form)
116 case FORM_FORMATTED:
117 p = "FORMATTED";
118 break;
119 case FORM_UNFORMATTED:
120 p = "UNFORMATTED";
121 break;
122 default:
123 internal_error ("inquire_via_unit(): Bad form");
126 cf_strcpy (ioparm.form, ioparm.form_len, p);
129 if (ioparm.formatted != NULL)
131 p = (u == NULL) ? inquire_formatted (NULL, 0) :
132 inquire_formatted (u->file, u->file_len);
134 cf_strcpy (ioparm.formatted, ioparm.formatted_len, p);
137 if (ioparm.unformatted != NULL)
139 p = (u == NULL) ? inquire_unformatted (NULL, 0) :
140 inquire_unformatted (u->file, u->file_len);
142 cf_strcpy (ioparm.unformatted, ioparm.unformatted_len, p);
145 if (ioparm.recl_out != NULL)
146 *ioparm.recl_out = (u != NULL) ? u->recl : 0;
148 if (ioparm.nextrec != NULL)
149 *ioparm.nextrec = (u != NULL) ? u->last_record + 1 : 0;
151 if (ioparm.blank != NULL)
153 if (u == NULL)
154 p = undefined;
155 else
156 switch (u->flags.blank)
158 case BLANK_NULL:
159 p = "NULL";
160 break;
161 case BLANK_ZERO:
162 p = "ZERO";
163 break;
164 default:
165 internal_error ("inquire_via_unit(): Bad blank");
168 cf_strcpy (ioparm.blank, ioparm.blank_len, p);
171 if (ioparm.position != NULL)
173 if (u == NULL || u->flags.access == ACCESS_DIRECT)
174 p = undefined;
175 else
176 switch (u->flags.position)
178 case POSITION_REWIND:
179 p = "REWIND";
180 break;
181 case POSITION_APPEND:
182 p = "APPEND";
183 break;
184 case POSITION_ASIS:
185 p = "ASIS";
186 break;
187 default:
188 /* if not direct access, it must be
189 either REWIND, APPEND, or ASIS.
190 ASIS seems to be the best default */
191 p = "ASIS";
192 break;
194 cf_strcpy (ioparm.position, ioparm.position_len, p);
197 if (ioparm.action != NULL)
199 if (u == NULL)
200 p = undefined;
201 else
202 switch (u->flags.action)
204 case ACTION_READ:
205 p = "READ";
206 break;
207 case ACTION_WRITE:
208 p = "WRITE";
209 break;
210 case ACTION_READWRITE:
211 p = "READWRITE";
212 break;
213 default:
214 internal_error ("inquire_via_unit(): Bad action");
217 cf_strcpy (ioparm.action, ioparm.action_len, p);
220 if (ioparm.read != NULL)
222 p = (u == NULL) ? inquire_read (NULL, 0) :
223 inquire_read (u->file, u->file_len);
225 cf_strcpy (ioparm.read, ioparm.read_len, p);
228 if (ioparm.write != NULL)
230 p = (u == NULL) ? inquire_write (NULL, 0) :
231 inquire_write (u->file, u->file_len);
233 cf_strcpy (ioparm.write, ioparm.write_len, p);
236 if (ioparm.readwrite != NULL)
238 p = (u == NULL) ? inquire_readwrite (NULL, 0) :
239 inquire_readwrite (u->file, u->file_len);
241 cf_strcpy (ioparm.readwrite, ioparm.readwrite_len, p);
244 if (ioparm.delim != NULL)
246 if (u == NULL || u->flags.form != FORM_FORMATTED)
247 p = undefined;
248 else
249 switch (u->flags.delim)
251 case DELIM_NONE:
252 p = "NONE";
253 break;
254 case DELIM_QUOTE:
255 p = "QUOTE";
256 break;
257 case DELIM_APOSTROPHE:
258 p = "APOSTROPHE";
259 break;
260 default:
261 internal_error ("inquire_via_unit(): Bad delim");
264 cf_strcpy (ioparm.delim, ioparm.delim_len, p);
267 if (ioparm.pad != NULL)
269 if (u == NULL || u->flags.form != FORM_FORMATTED)
270 p = undefined;
271 else
272 switch (u->flags.pad)
274 case PAD_NO:
275 p = "NO";
276 break;
277 case PAD_YES:
278 p = "YES";
279 break;
280 default:
281 internal_error ("inquire_via_unit(): Bad pad");
284 cf_strcpy (ioparm.pad, ioparm.pad_len, p);
289 /* inquire_via_filename()-- Inquiry via filename. This subroutine is
290 * only used if the filename is *not* connected to a unit number. */
292 static void
293 inquire_via_filename (void)
295 const char *p;
297 if (ioparm.exist != NULL)
298 *ioparm.exist = file_exists ();
300 if (ioparm.opened != NULL)
301 *ioparm.opened = 0;
303 if (ioparm.number != NULL)
304 *ioparm.number = -1;
306 if (ioparm.named != NULL)
307 *ioparm.named = 1;
309 if (ioparm.name != NULL)
310 fstrcpy (ioparm.name, ioparm.name_len, ioparm.file, ioparm.file_len);
312 if (ioparm.access != NULL)
313 cf_strcpy (ioparm.access, ioparm.access_len, undefined);
315 if (ioparm.sequential != NULL)
317 p = inquire_sequential (ioparm.file, ioparm.file_len);
318 cf_strcpy (ioparm.sequential, ioparm.sequential_len, p);
321 if (ioparm.direct != NULL)
323 p = inquire_direct (ioparm.file, ioparm.file_len);
324 cf_strcpy (ioparm.direct, ioparm.direct_len, p);
327 if (ioparm.form != NULL)
328 cf_strcpy (ioparm.form, ioparm.form_len, undefined);
330 if (ioparm.formatted != NULL)
332 p = inquire_formatted (ioparm.file, ioparm.file_len);
333 cf_strcpy (ioparm.formatted, ioparm.formatted_len, p);
336 if (ioparm.unformatted != NULL)
338 p = inquire_unformatted (ioparm.file, ioparm.file_len);
339 cf_strcpy (ioparm.unformatted, ioparm.unformatted_len, p);
342 if (ioparm.recl_out != NULL)
343 *ioparm.recl_out = 0;
345 if (ioparm.nextrec != NULL)
346 *ioparm.nextrec = 0;
348 if (ioparm.blank != NULL)
349 cf_strcpy (ioparm.blank, ioparm.blank_len, undefined);
351 if (ioparm.position != NULL)
352 cf_strcpy (ioparm.position, ioparm.position_len, undefined);
354 if (ioparm.access != NULL)
355 cf_strcpy (ioparm.access, ioparm.access_len, undefined);
357 if (ioparm.read != NULL)
359 p = inquire_read (ioparm.file, ioparm.file_len);
360 cf_strcpy (ioparm.read, ioparm.read_len, p);
363 if (ioparm.write != NULL)
365 p = inquire_write (ioparm.file, ioparm.file_len);
366 cf_strcpy (ioparm.write, ioparm.write_len, p);
369 if (ioparm.readwrite != NULL)
371 p = inquire_read (ioparm.file, ioparm.file_len);
372 cf_strcpy (ioparm.readwrite, ioparm.readwrite_len, p);
375 if (ioparm.delim != NULL)
376 cf_strcpy (ioparm.delim, ioparm.delim_len, undefined);
378 if (ioparm.pad != NULL)
379 cf_strcpy (ioparm.pad, ioparm.pad_len, undefined);
384 /* Library entry point for the INQUIRE statement (non-IOLENGTH
385 form). */
387 extern void st_inquire (void);
388 export_proto(st_inquire);
390 void
391 st_inquire (void)
393 gfc_unit *u;
395 library_start ();
397 if (ioparm.file == NULL)
398 inquire_via_unit (find_unit (ioparm.unit));
399 else
401 u = find_file ();
402 if (u == NULL)
403 inquire_via_filename ();
404 else
405 inquire_via_unit (u);
408 library_end ();