1 /* Implementation of the FGET, FGETC, FPUT, FPUTC, FLUSH
2 FTELL, TTYNAM and ISATTY intrinsics.
3 Copyright (C) 2005-2016 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/>. */
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
));
40 PREFIX(fgetc
) (const int * unit
, char * c
, gfc_charlen_type c_len
)
43 gfc_unit
* u
= find_unit (*unit
);
49 if (u
->mode
== WRITING
)
55 memset (c
, ' ', c_len
);
56 ret
= sread (u
->s
, c
, 1);
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) \
76 *st = PREFIX(fgetc) (unit, c, c_len); \
78 PREFIX(fgetc) (unit, c, c_len); }
86 extern int PREFIX(fget
) (char *, gfc_charlen_type
);
87 export_proto_np(PREFIX(fget
));
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) \
103 *st = PREFIX(fgetc) (&five, c, c_len); \
105 PREFIX(fgetc) (&five, c, c_len); }
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
)))
122 gfc_unit
* u
= find_unit (*unit
);
128 if (u
->mode
== READING
)
134 s
= swrite (u
->s
, c
, 1);
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) \
149 *st = PREFIX(fputc) (unit, c, c_len); \
151 PREFIX(fputc) (unit, c, c_len); }
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) \
176 *st = PREFIX(fputc) (&six, c, c_len); \
178 PREFIX(fputc) (&six, c, c_len); }
186 /* SUBROUTINE FLUSH(UNIT)
187 INTEGER, INTENT(IN), OPTIONAL :: UNIT */
189 extern void flush_i4 (GFC_INTEGER_4
*);
190 export_proto(flush_i4
);
193 flush_i4 (GFC_INTEGER_4
*unit
)
197 /* flush all streams */
202 us
= find_unit (*unit
);
212 extern void flush_i8 (GFC_INTEGER_8
*);
213 export_proto(flush_i8
);
216 flush_i8 (GFC_INTEGER_8
*unit
)
220 /* flush all streams */
225 us
= find_unit (*unit
);
234 /* FSEEK intrinsic */
236 extern void fseek_sub (int *, GFC_IO_INT
*, int *, int *);
237 export_proto(fseek_sub
);
240 fseek_sub (int * unit
, GFC_IO_INT
* offset
, int * whence
, int * status
)
242 gfc_unit
* u
= find_unit (*unit
);
247 result
= sseek(u
->s
, *offset
, *whence
);
253 *status
= (result
< 0 ? -1 : 0);
258 /* FTELL intrinsic */
263 gfc_unit
* u
= find_unit (unit
);
266 int pos
= fbuf_reset (u
);
268 sseek (u
->s
, pos
, SEEK_CUR
);
269 gfc_offset ret
= stell (u
->s
);
275 extern GFC_IO_INT
PREFIX(ftell
) (int *);
276 export_proto_np(PREFIX(ftell
));
279 PREFIX(ftell
) (int * unit
)
281 return gf_ftell (*unit
);
285 #define FTELL_SUB(kind) \
286 extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
287 export_proto(ftell_i ## kind ## _sub); \
289 ftell_i ## kind ## _sub (int * unit, GFC_INTEGER_ ## kind * offset) \
291 *offset = gf_ftell (*unit); \
301 /* LOGICAL FUNCTION ISATTY(UNIT)
302 INTEGER, INTENT(IN) :: UNIT */
304 extern GFC_LOGICAL_4
isatty_l4 (int *);
305 export_proto(isatty_l4
);
308 isatty_l4 (int *unit
)
311 GFC_LOGICAL_4 ret
= 0;
313 u
= find_unit (*unit
);
316 ret
= (GFC_LOGICAL_4
) stream_isatty (u
->s
);
323 extern GFC_LOGICAL_8
isatty_l8 (int *);
324 export_proto(isatty_l8
);
327 isatty_l8 (int *unit
)
330 GFC_LOGICAL_8 ret
= 0;
332 u
= find_unit (*unit
);
335 ret
= (GFC_LOGICAL_8
) stream_isatty (u
->s
);
342 /* SUBROUTINE TTYNAM(UNIT,NAME)
343 INTEGER,SCALAR,INTENT(IN) :: UNIT
344 CHARACTER,SCALAR,INTENT(OUT) :: NAME */
346 extern void ttynam_sub (int *, char *, gfc_charlen_type
);
347 export_proto(ttynam_sub
);
350 ttynam_sub (int *unit
, char * name
, gfc_charlen_type name_len
)
356 u
= find_unit (*unit
);
359 err
= stream_ttyname (u
->s
, name
, name_len
);
362 nlen
= strlen (name
);
363 memset (&name
[nlen
], ' ', name_len
- nlen
);
369 memset (name
, ' ', name_len
);
373 extern void ttynam (char **, gfc_charlen_type
*, int);
374 export_proto(ttynam
);
377 ttynam (char ** name
, gfc_charlen_type
* name_len
, int unit
)
381 u
= find_unit (unit
);
384 *name
= xmalloc (TTY_NAME_MAX
);
385 int err
= stream_ttyname (u
->s
, *name
, TTY_NAME_MAX
);
388 *name_len
= strlen (*name
);