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/>. */
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 /* 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
));
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
));
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); \
307 ftell_i ## kind ## _sub (int * unit, GFC_INTEGER_ ## kind * offset) \
309 *offset = gf_ftell (*unit); \
319 /* LOGICAL FUNCTION ISATTY(UNIT)
320 INTEGER, INTENT(IN) :: UNIT */
322 extern GFC_LOGICAL_4
isatty_l4 (int *);
323 export_proto(isatty_l4
);
326 isatty_l4 (int *unit
)
329 GFC_LOGICAL_4 ret
= 0;
331 u
= find_unit (*unit
);
334 ret
= (GFC_LOGICAL_4
) stream_isatty (u
->s
);
341 extern GFC_LOGICAL_8
isatty_l8 (int *);
342 export_proto(isatty_l8
);
345 isatty_l8 (int *unit
)
348 GFC_LOGICAL_8 ret
= 0;
350 u
= find_unit (*unit
);
353 ret
= (GFC_LOGICAL_8
) stream_isatty (u
->s
);
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
);
368 ttynam_sub (int *unit
, char * name
, gfc_charlen_type name_len
)
374 u
= find_unit (*unit
);
377 err
= stream_ttyname (u
->s
, name
, name_len
);
380 nlen
= strlen (name
);
381 memset (&name
[nlen
], ' ', name_len
- nlen
);
387 memset (name
, ' ', name_len
);
391 extern void ttynam (char **, gfc_charlen_type
*, int);
392 export_proto(ttynam
);
395 ttynam (char ** name
, gfc_charlen_type
* name_len
, int unit
)
399 u
= find_unit (unit
);
402 *name
= xmalloc (TTY_NAME_MAX
);
403 int err
= stream_ttyname (u
->s
, *name
, TTY_NAME_MAX
);
406 *name_len
= strlen (*name
);