2013-05-30 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / libgfortran / io / intrinsics.c
blob1573434d6dba89fda48f12c67bfea86936bd19c2
1 /* Implementation of the FGET, FGETC, FPUT, FPUTC, FLUSH
2 FTELL, TTYNAM and ISATTY intrinsics.
3 Copyright (C) 2005-2013 Free Software Foundation, Inc.
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) 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/>. */
26 #include "io.h"
27 #include "fbuf.h"
28 #include "unix.h"
29 #include <stdlib.h>
30 #include <string.h>
33 static const int five = 5;
34 static const int six = 6;
36 extern int PREFIX(fgetc) (const int *, char *, gfc_charlen_type);
37 export_proto_np(PREFIX(fgetc));
39 int
40 PREFIX(fgetc) (const int * unit, char * c, gfc_charlen_type c_len)
42 int ret;
43 gfc_unit * u = find_unit (*unit);
45 if (u == NULL)
46 return -1;
48 fbuf_reset (u);
49 if (u->mode == WRITING)
51 sflush (u->s);
52 u->mode = READING;
55 memset (c, ' ', c_len);
56 ret = sread (u->s, c, 1);
57 unlock_unit (u);
59 if (ret < 0)
60 return ret;
62 if (ret != 1)
63 return -1;
64 else
65 return 0;
69 #define FGETC_SUB(kind) \
70 extern void fgetc_i ## kind ## _sub \
71 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
72 export_proto(fgetc_i ## kind ## _sub); \
73 void fgetc_i ## kind ## _sub \
74 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
75 { if (st != NULL) \
76 *st = PREFIX(fgetc) (unit, c, c_len); \
77 else \
78 PREFIX(fgetc) (unit, c, c_len); }
80 FGETC_SUB(1)
81 FGETC_SUB(2)
82 FGETC_SUB(4)
83 FGETC_SUB(8)
86 extern int PREFIX(fget) (char *, gfc_charlen_type);
87 export_proto_np(PREFIX(fget));
89 int
90 PREFIX(fget) (char * c, gfc_charlen_type c_len)
92 return PREFIX(fgetc) (&five, c, c_len);
96 #define FGET_SUB(kind) \
97 extern void fget_i ## kind ## _sub \
98 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
99 export_proto(fget_i ## kind ## _sub); \
100 void fget_i ## kind ## _sub \
101 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
102 { if (st != NULL) \
103 *st = PREFIX(fgetc) (&five, c, c_len); \
104 else \
105 PREFIX(fgetc) (&five, c, c_len); }
107 FGET_SUB(1)
108 FGET_SUB(2)
109 FGET_SUB(4)
110 FGET_SUB(8)
114 extern int PREFIX(fputc) (const int *, char *, gfc_charlen_type);
115 export_proto_np(PREFIX(fputc));
118 PREFIX(fputc) (const int * unit, char * c,
119 gfc_charlen_type c_len __attribute__((unused)))
121 ssize_t s;
122 gfc_unit * u = find_unit (*unit);
124 if (u == NULL)
125 return -1;
127 fbuf_reset (u);
128 if (u->mode == READING)
130 sflush (u->s);
131 u->mode = WRITING;
134 s = swrite (u->s, c, 1);
135 unlock_unit (u);
136 if (s < 0)
137 return -1;
138 return 0;
142 #define FPUTC_SUB(kind) \
143 extern void fputc_i ## kind ## _sub \
144 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
145 export_proto(fputc_i ## kind ## _sub); \
146 void fputc_i ## kind ## _sub \
147 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
148 { if (st != NULL) \
149 *st = PREFIX(fputc) (unit, c, c_len); \
150 else \
151 PREFIX(fputc) (unit, c, c_len); }
153 FPUTC_SUB(1)
154 FPUTC_SUB(2)
155 FPUTC_SUB(4)
156 FPUTC_SUB(8)
159 extern int PREFIX(fput) (char *, gfc_charlen_type);
160 export_proto_np(PREFIX(fput));
163 PREFIX(fput) (char * c, gfc_charlen_type c_len)
165 return PREFIX(fputc) (&six, c, c_len);
169 #define FPUT_SUB(kind) \
170 extern void fput_i ## kind ## _sub \
171 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
172 export_proto(fput_i ## kind ## _sub); \
173 void fput_i ## kind ## _sub \
174 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
175 { if (st != NULL) \
176 *st = PREFIX(fputc) (&six, c, c_len); \
177 else \
178 PREFIX(fputc) (&six, c, c_len); }
180 FPUT_SUB(1)
181 FPUT_SUB(2)
182 FPUT_SUB(4)
183 FPUT_SUB(8)
186 /* SUBROUTINE FLUSH(UNIT)
187 INTEGER, INTENT(IN), OPTIONAL :: UNIT */
189 extern void flush_i4 (GFC_INTEGER_4 *);
190 export_proto(flush_i4);
192 void
193 flush_i4 (GFC_INTEGER_4 *unit)
195 gfc_unit *us;
197 /* flush all streams */
198 if (unit == NULL)
199 flush_all_units ();
200 else
202 us = find_unit (*unit);
203 if (us != NULL)
205 sflush (us->s);
206 unlock_unit (us);
212 extern void flush_i8 (GFC_INTEGER_8 *);
213 export_proto(flush_i8);
215 void
216 flush_i8 (GFC_INTEGER_8 *unit)
218 gfc_unit *us;
220 /* flush all streams */
221 if (unit == NULL)
222 flush_all_units ();
223 else
225 us = find_unit (*unit);
226 if (us != NULL)
228 sflush (us->s);
229 unlock_unit (us);
234 /* FSEEK intrinsic */
236 extern void fseek_sub (int *, GFC_IO_INT *, int *, int *);
237 export_proto(fseek_sub);
239 void
240 fseek_sub (int * unit, GFC_IO_INT * offset, int * whence, int * status)
242 gfc_unit * u = find_unit (*unit);
243 ssize_t result = -1;
245 if (u != NULL)
247 result = sseek(u->s, *offset, *whence);
249 unlock_unit (u);
252 if (status)
253 *status = (result < 0 ? -1 : 0);
258 /* FTELL intrinsic */
260 static gfc_offset
261 gf_ftell (int unit)
263 gfc_unit * u = find_unit (unit);
264 if (u == NULL)
265 return -1;
266 int pos = fbuf_reset (u);
267 if (pos != 0)
268 sseek (u->s, pos, SEEK_CUR);
269 gfc_offset ret = stell (u->s);
270 unlock_unit (u);
271 return ret;
275 /* Here is the ftell function with an incorrect return type; retained
276 due to ABI compatibility. */
278 extern size_t PREFIX(ftell) (int *);
279 export_proto_np(PREFIX(ftell));
281 size_t
282 PREFIX(ftell) (int * unit)
284 return gf_ftell (*unit);
288 /* Here is the ftell function with the correct return type, ensuring
289 that large files can be supported as long as the target supports
290 large integers; as of 4.8 the FTELL intrinsic function will call
291 this one instead of the old ftell above. */
293 extern GFC_IO_INT PREFIX(ftell2) (int *);
294 export_proto_np(PREFIX(ftell2));
296 GFC_IO_INT
297 PREFIX(ftell2) (int * unit)
299 return gf_ftell (*unit);
303 #define FTELL_SUB(kind) \
304 extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
305 export_proto(ftell_i ## kind ## _sub); \
306 void \
307 ftell_i ## kind ## _sub (int * unit, GFC_INTEGER_ ## kind * offset) \
309 *offset = gf_ftell (*unit); \
312 FTELL_SUB(1)
313 FTELL_SUB(2)
314 FTELL_SUB(4)
315 FTELL_SUB(8)
319 /* LOGICAL FUNCTION ISATTY(UNIT)
320 INTEGER, INTENT(IN) :: UNIT */
322 extern GFC_LOGICAL_4 isatty_l4 (int *);
323 export_proto(isatty_l4);
325 GFC_LOGICAL_4
326 isatty_l4 (int *unit)
328 gfc_unit *u;
329 GFC_LOGICAL_4 ret = 0;
331 u = find_unit (*unit);
332 if (u != NULL)
334 ret = (GFC_LOGICAL_4) stream_isatty (u->s);
335 unlock_unit (u);
337 return ret;
341 extern GFC_LOGICAL_8 isatty_l8 (int *);
342 export_proto(isatty_l8);
344 GFC_LOGICAL_8
345 isatty_l8 (int *unit)
347 gfc_unit *u;
348 GFC_LOGICAL_8 ret = 0;
350 u = find_unit (*unit);
351 if (u != NULL)
353 ret = (GFC_LOGICAL_8) stream_isatty (u->s);
354 unlock_unit (u);
356 return ret;
360 /* SUBROUTINE TTYNAM(UNIT,NAME)
361 INTEGER,SCALAR,INTENT(IN) :: UNIT
362 CHARACTER,SCALAR,INTENT(OUT) :: NAME */
364 extern void ttynam_sub (int *, char *, gfc_charlen_type);
365 export_proto(ttynam_sub);
367 void
368 ttynam_sub (int *unit, char * name, gfc_charlen_type name_len)
370 gfc_unit *u;
371 int nlen;
372 int err = 1;
374 u = find_unit (*unit);
375 if (u != NULL)
377 err = stream_ttyname (u->s, name, name_len);
378 if (err == 0)
380 nlen = strlen (name);
381 memset (&name[nlen], ' ', name_len - nlen);
384 unlock_unit (u);
386 if (err != 0)
387 memset (name, ' ', name_len);
391 extern void ttynam (char **, gfc_charlen_type *, int);
392 export_proto(ttynam);
394 void
395 ttynam (char ** name, gfc_charlen_type * name_len, int unit)
397 gfc_unit *u;
399 u = find_unit (unit);
400 if (u != NULL)
402 *name = xmalloc (TTY_NAME_MAX);
403 int err = stream_ttyname (u->s, *name, TTY_NAME_MAX);
404 if (err == 0)
406 *name_len = strlen (*name);
407 unlock_unit (u);
408 return;
410 free (*name);
411 unlock_unit (u);
414 *name_len = 0;
415 *name = NULL;