1 /* Implementation of the FGET, FGETC, FPUT, FPUTC, FLUSH
2 FTELL, TTYNAM and ISATTY intrinsics.
3 Copyright (C) 2005-2018 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/>. */
32 static const int five
= 5;
33 static const int six
= 6;
35 extern int PREFIX(fgetc
) (const int *, char *, gfc_charlen_type
);
36 export_proto_np(PREFIX(fgetc
));
39 PREFIX(fgetc
) (const int *unit
, char *c
, gfc_charlen_type c_len
)
42 gfc_unit
*u
= find_unit (*unit
);
48 if (u
->mode
== WRITING
)
54 memset (c
, ' ', c_len
);
55 ret
= sread (u
->s
, c
, 1);
68 #define FGETC_SUB(kind) \
69 extern void fgetc_i ## kind ## _sub \
70 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
71 export_proto(fgetc_i ## kind ## _sub); \
72 void fgetc_i ## kind ## _sub \
73 (const int *unit, char *c, GFC_INTEGER_ ## kind *st, gfc_charlen_type c_len) \
75 *st = PREFIX(fgetc) (unit, c, c_len); \
77 PREFIX(fgetc) (unit, c, c_len); }
85 extern int PREFIX(fget
) (char *, gfc_charlen_type
);
86 export_proto_np(PREFIX(fget
));
89 PREFIX(fget
) (char *c
, gfc_charlen_type c_len
)
91 return PREFIX(fgetc
) (&five
, c
, c_len
);
95 #define FGET_SUB(kind) \
96 extern void fget_i ## kind ## _sub \
97 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
98 export_proto(fget_i ## kind ## _sub); \
99 void fget_i ## kind ## _sub \
100 (char *c, GFC_INTEGER_ ## kind *st, gfc_charlen_type c_len) \
102 *st = PREFIX(fgetc) (&five, c, c_len); \
104 PREFIX(fgetc) (&five, c, c_len); }
113 extern int PREFIX(fputc
) (const int *, char *, gfc_charlen_type
);
114 export_proto_np(PREFIX(fputc
));
117 PREFIX(fputc
) (const int *unit
, char *c
,
118 gfc_charlen_type c_len
__attribute__((unused
)))
121 gfc_unit
*u
= find_unit (*unit
);
127 if (u
->mode
== READING
)
133 s
= swrite (u
->s
, c
, 1);
141 #define FPUTC_SUB(kind) \
142 extern void fputc_i ## kind ## _sub \
143 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
144 export_proto(fputc_i ## kind ## _sub); \
145 void fputc_i ## kind ## _sub \
146 (const int *unit, char *c, GFC_INTEGER_ ## kind *st, gfc_charlen_type c_len) \
148 *st = PREFIX(fputc) (unit, c, c_len); \
150 PREFIX(fputc) (unit, c, c_len); }
158 extern int PREFIX(fput
) (char *, gfc_charlen_type
);
159 export_proto_np(PREFIX(fput
));
162 PREFIX(fput
) (char *c
, gfc_charlen_type c_len
)
164 return PREFIX(fputc
) (&six
, c
, c_len
);
168 #define FPUT_SUB(kind) \
169 extern void fput_i ## kind ## _sub \
170 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
171 export_proto(fput_i ## kind ## _sub); \
172 void fput_i ## kind ## _sub \
173 (char *c, GFC_INTEGER_ ## kind *st, gfc_charlen_type c_len) \
175 *st = PREFIX(fputc) (&six, c, c_len); \
177 PREFIX(fputc) (&six, c, c_len); }
185 /* SUBROUTINE FLUSH(UNIT)
186 INTEGER, INTENT(IN), OPTIONAL :: UNIT */
188 extern void flush_i4 (GFC_INTEGER_4
*);
189 export_proto(flush_i4
);
192 flush_i4 (GFC_INTEGER_4
*unit
)
196 /* flush all streams */
201 us
= find_unit (*unit
);
211 extern void flush_i8 (GFC_INTEGER_8
*);
212 export_proto(flush_i8
);
215 flush_i8 (GFC_INTEGER_8
*unit
)
219 /* flush all streams */
224 us
= find_unit (*unit
);
233 /* FSEEK intrinsic */
235 extern void fseek_sub (int *, GFC_IO_INT
*, int *, int *);
236 export_proto(fseek_sub
);
239 fseek_sub (int *unit
, GFC_IO_INT
*offset
, int *whence
, int *status
)
241 gfc_unit
*u
= find_unit (*unit
);
246 result
= sseek(u
->s
, *offset
, *whence
);
252 *status
= (result
< 0 ? -1 : 0);
257 /* FTELL intrinsic */
262 gfc_unit
*u
= find_unit (unit
);
265 int pos
= fbuf_reset (u
);
267 sseek (u
->s
, pos
, SEEK_CUR
);
268 gfc_offset ret
= stell (u
->s
);
274 extern GFC_IO_INT
PREFIX(ftell
) (int *);
275 export_proto_np(PREFIX(ftell
));
278 PREFIX(ftell
) (int *unit
)
280 return gf_ftell (*unit
);
284 #define FTELL_SUB(kind) \
285 extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
286 export_proto(ftell_i ## kind ## _sub); \
288 ftell_i ## kind ## _sub (int *unit, GFC_INTEGER_ ## kind *offset) \
290 *offset = gf_ftell (*unit); \
300 /* LOGICAL FUNCTION ISATTY(UNIT)
301 INTEGER, INTENT(IN) :: UNIT */
303 extern GFC_LOGICAL_4
isatty_l4 (int *);
304 export_proto(isatty_l4
);
307 isatty_l4 (int *unit
)
310 GFC_LOGICAL_4 ret
= 0;
312 u
= find_unit (*unit
);
315 ret
= (GFC_LOGICAL_4
) stream_isatty (u
->s
);
322 extern GFC_LOGICAL_8
isatty_l8 (int *);
323 export_proto(isatty_l8
);
326 isatty_l8 (int *unit
)
329 GFC_LOGICAL_8 ret
= 0;
331 u
= find_unit (*unit
);
334 ret
= (GFC_LOGICAL_8
) stream_isatty (u
->s
);
341 /* SUBROUTINE TTYNAM(UNIT,NAME)
342 INTEGER,SCALAR,INTENT(IN) :: UNIT
343 CHARACTER,SCALAR,INTENT(OUT) :: NAME */
345 extern void ttynam_sub (int *, char *, gfc_charlen_type
);
346 export_proto(ttynam_sub
);
349 ttynam_sub (int *unit
, char *name
, gfc_charlen_type name_len
)
355 u
= find_unit (*unit
);
358 err
= stream_ttyname (u
->s
, name
, name_len
);
361 nlen
= strlen (name
);
362 memset (&name
[nlen
], ' ', name_len
- nlen
);
368 memset (name
, ' ', name_len
);
372 extern void ttynam (char **, gfc_charlen_type
*, int);
373 export_proto(ttynam
);
376 ttynam (char **name
, gfc_charlen_type
*name_len
, int unit
)
380 u
= find_unit (unit
);
383 *name
= xmalloc (TTY_NAME_MAX
);
384 int err
= stream_ttyname (u
->s
, *name
, TTY_NAME_MAX
);
387 *name_len
= strlen (*name
);