1 /* Implementation of the FGET, FGETC, FPUT, FPUTC, FLUSH
2 FTELL, TTYNAM and ISATTY intrinsics.
3 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
5 This file is part of the GNU Fortran 95 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
32 #include "libgfortran.h"
42 static const int five
= 5;
43 static const int six
= 6;
45 extern int PREFIX(fgetc
) (const int *, char *, gfc_charlen_type
);
46 export_proto_np(PREFIX(fgetc
));
49 PREFIX(fgetc
) (const int * unit
, char * c
, gfc_charlen_type c_len
)
53 gfc_unit
* u
= find_unit (*unit
);
59 memset (c
, ' ', c_len
);
60 ret
= sread (u
->s
, c
, &s
);
73 #define FGETC_SUB(kind) \
74 extern void fgetc_i ## kind ## _sub \
75 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
76 export_proto(fgetc_i ## kind ## _sub); \
77 void fgetc_i ## kind ## _sub \
78 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
80 *st = PREFIX(fgetc) (unit, c, c_len); \
82 PREFIX(fgetc) (unit, c, c_len); }
90 extern int PREFIX(fget
) (char *, gfc_charlen_type
);
91 export_proto_np(PREFIX(fget
));
94 PREFIX(fget
) (char * c
, gfc_charlen_type c_len
)
96 return PREFIX(fgetc
) (&five
, c
, c_len
);
100 #define FGET_SUB(kind) \
101 extern void fget_i ## kind ## _sub \
102 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
103 export_proto(fget_i ## kind ## _sub); \
104 void fget_i ## kind ## _sub \
105 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
107 *st = PREFIX(fgetc) (&five, c, c_len); \
109 PREFIX(fgetc) (&five, c, c_len); }
118 extern int PREFIX(fputc
) (const int *, char *, gfc_charlen_type
);
119 export_proto_np(PREFIX(fputc
));
122 PREFIX(fputc
) (const int * unit
, char * c
,
123 gfc_charlen_type c_len
__attribute__((unused
)))
127 gfc_unit
* u
= find_unit (*unit
);
133 ret
= swrite (u
->s
, c
, &s
);
139 #define FPUTC_SUB(kind) \
140 extern void fputc_i ## kind ## _sub \
141 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
142 export_proto(fputc_i ## kind ## _sub); \
143 void fputc_i ## kind ## _sub \
144 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
146 *st = PREFIX(fputc) (unit, c, c_len); \
148 PREFIX(fputc) (unit, c, c_len); }
156 extern int PREFIX(fput
) (char *, gfc_charlen_type
);
157 export_proto_np(PREFIX(fput
));
160 PREFIX(fput
) (char * c
, gfc_charlen_type c_len
)
162 return PREFIX(fputc
) (&six
, c
, c_len
);
166 #define FPUT_SUB(kind) \
167 extern void fput_i ## kind ## _sub \
168 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
169 export_proto(fput_i ## kind ## _sub); \
170 void fput_i ## kind ## _sub \
171 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
173 *st = PREFIX(fputc) (&six, c, c_len); \
175 PREFIX(fputc) (&six, c, c_len); }
183 /* SUBROUTINE FLUSH(UNIT)
184 INTEGER, INTENT(IN), OPTIONAL :: UNIT */
186 extern void flush_i4 (GFC_INTEGER_4
*);
187 export_proto(flush_i4
);
190 flush_i4 (GFC_INTEGER_4
*unit
)
194 /* flush all streams */
199 us
= find_unit (*unit
);
209 extern void flush_i8 (GFC_INTEGER_8
*);
210 export_proto(flush_i8
);
213 flush_i8 (GFC_INTEGER_8
*unit
)
217 /* flush all streams */
222 us
= find_unit (*unit
);
232 /* FTELL intrinsic */
234 extern size_t PREFIX(ftell
) (int *);
235 export_proto_np(PREFIX(ftell
));
238 PREFIX(ftell
) (int * unit
)
240 gfc_unit
* u
= find_unit (*unit
);
243 return ((size_t) -1);
244 ret
= (size_t) stream_offset (u
->s
);
249 #define FTELL_SUB(kind) \
250 extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
251 export_proto(ftell_i ## kind ## _sub); \
253 ftell_i ## kind ## _sub (int * unit, GFC_INTEGER_ ## kind * offset) \
255 gfc_unit * u = find_unit (*unit); \
260 *offset = stream_offset (u->s); \
272 /* LOGICAL FUNCTION ISATTY(UNIT)
273 INTEGER, INTENT(IN) :: UNIT */
275 extern GFC_LOGICAL_4
isatty_l4 (int *);
276 export_proto(isatty_l4
);
279 isatty_l4 (int *unit
)
282 GFC_LOGICAL_4 ret
= 0;
284 u
= find_unit (*unit
);
287 ret
= (GFC_LOGICAL_4
) stream_isatty (u
->s
);
294 extern GFC_LOGICAL_8
isatty_l8 (int *);
295 export_proto(isatty_l8
);
298 isatty_l8 (int *unit
)
301 GFC_LOGICAL_8 ret
= 0;
303 u
= find_unit (*unit
);
306 ret
= (GFC_LOGICAL_8
) stream_isatty (u
->s
);
313 /* SUBROUTINE TTYNAM(UNIT,NAME)
314 INTEGER,SCALAR,INTENT(IN) :: UNIT
315 CHARACTER,SCALAR,INTENT(OUT) :: NAME */
317 extern void ttynam_sub (int *, char *, gfc_charlen_type
);
318 export_proto(ttynam_sub
);
321 ttynam_sub (int *unit
, char * name
, gfc_charlen_type name_len
)
327 memset (name
, ' ', name_len
);
328 u
= find_unit (*unit
);
331 n
= stream_ttyname (u
->s
);
335 while (*n
&& i
< name_len
)
343 extern void ttynam (char **, gfc_charlen_type
*, int);
344 export_proto(ttynam
);
347 ttynam (char ** name
, gfc_charlen_type
* name_len
, int unit
)
351 u
= find_unit (unit
);
354 *name
= stream_ttyname (u
->s
);
357 *name_len
= strlen (*name
);
358 *name
= strdup (*name
);