1 /* Implementation of the FGET, FGETC, FPUT, FPUTC, FLUSH
2 FTELL, TTYNAM and ISATTY intrinsics.
3 Copyright (C) 2005, 2007, 2009 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 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/>. */
34 static const int five
= 5;
35 static const int six
= 6;
37 extern int PREFIX(fgetc
) (const int *, char *, gfc_charlen_type
);
38 export_proto_np(PREFIX(fgetc
));
41 PREFIX(fgetc
) (const int * unit
, char * c
, gfc_charlen_type c_len
)
44 gfc_unit
* u
= find_unit (*unit
);
49 memset (c
, ' ', c_len
);
50 ret
= sread (u
->s
, c
, 1);
63 #define FGETC_SUB(kind) \
64 extern void fgetc_i ## kind ## _sub \
65 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
66 export_proto(fgetc_i ## kind ## _sub); \
67 void fgetc_i ## kind ## _sub \
68 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
70 *st = PREFIX(fgetc) (unit, c, c_len); \
72 PREFIX(fgetc) (unit, c, c_len); }
80 extern int PREFIX(fget
) (char *, gfc_charlen_type
);
81 export_proto_np(PREFIX(fget
));
84 PREFIX(fget
) (char * c
, gfc_charlen_type c_len
)
86 return PREFIX(fgetc
) (&five
, c
, c_len
);
90 #define FGET_SUB(kind) \
91 extern void fget_i ## kind ## _sub \
92 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
93 export_proto(fget_i ## kind ## _sub); \
94 void fget_i ## kind ## _sub \
95 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
97 *st = PREFIX(fgetc) (&five, c, c_len); \
99 PREFIX(fgetc) (&five, c, c_len); }
108 extern int PREFIX(fputc
) (const int *, char *, gfc_charlen_type
);
109 export_proto_np(PREFIX(fputc
));
112 PREFIX(fputc
) (const int * unit
, char * c
,
113 gfc_charlen_type c_len
__attribute__((unused
)))
116 gfc_unit
* u
= find_unit (*unit
);
121 s
= swrite (u
->s
, c
, 1);
129 #define FPUTC_SUB(kind) \
130 extern void fputc_i ## kind ## _sub \
131 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
132 export_proto(fputc_i ## kind ## _sub); \
133 void fputc_i ## kind ## _sub \
134 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
136 *st = PREFIX(fputc) (unit, c, c_len); \
138 PREFIX(fputc) (unit, c, c_len); }
146 extern int PREFIX(fput
) (char *, gfc_charlen_type
);
147 export_proto_np(PREFIX(fput
));
150 PREFIX(fput
) (char * c
, gfc_charlen_type c_len
)
152 return PREFIX(fputc
) (&six
, c
, c_len
);
156 #define FPUT_SUB(kind) \
157 extern void fput_i ## kind ## _sub \
158 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
159 export_proto(fput_i ## kind ## _sub); \
160 void fput_i ## kind ## _sub \
161 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
163 *st = PREFIX(fputc) (&six, c, c_len); \
165 PREFIX(fputc) (&six, c, c_len); }
173 /* SUBROUTINE FLUSH(UNIT)
174 INTEGER, INTENT(IN), OPTIONAL :: UNIT */
176 extern void flush_i4 (GFC_INTEGER_4
*);
177 export_proto(flush_i4
);
180 flush_i4 (GFC_INTEGER_4
*unit
)
184 /* flush all streams */
189 us
= find_unit (*unit
);
199 extern void flush_i8 (GFC_INTEGER_8
*);
200 export_proto(flush_i8
);
203 flush_i8 (GFC_INTEGER_8
*unit
)
207 /* flush all streams */
212 us
= find_unit (*unit
);
221 /* FSEEK intrinsic */
223 extern void fseek_sub (int *, GFC_IO_INT
*, int *, int *);
224 export_proto(fseek_sub
);
227 fseek_sub (int * unit
, GFC_IO_INT
* offset
, int * whence
, int * status
)
229 gfc_unit
* u
= find_unit (*unit
);
232 if (u
!= NULL
&& is_seekable(u
->s
))
234 result
= sseek(u
->s
, *offset
, *whence
);
240 *status
= (result
< 0 ? -1 : 0);
245 /* FTELL intrinsic */
247 extern size_t PREFIX(ftell
) (int *);
248 export_proto_np(PREFIX(ftell
));
251 PREFIX(ftell
) (int * unit
)
253 gfc_unit
* u
= find_unit (*unit
);
256 return ((size_t) -1);
257 ret
= (size_t) stell (u
->s
);
262 #define FTELL_SUB(kind) \
263 extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
264 export_proto(ftell_i ## kind ## _sub); \
266 ftell_i ## kind ## _sub (int * unit, GFC_INTEGER_ ## kind * offset) \
268 gfc_unit * u = find_unit (*unit); \
273 *offset = stell (u->s); \
285 /* LOGICAL FUNCTION ISATTY(UNIT)
286 INTEGER, INTENT(IN) :: UNIT */
288 extern GFC_LOGICAL_4
isatty_l4 (int *);
289 export_proto(isatty_l4
);
292 isatty_l4 (int *unit
)
295 GFC_LOGICAL_4 ret
= 0;
297 u
= find_unit (*unit
);
300 ret
= (GFC_LOGICAL_4
) stream_isatty (u
->s
);
307 extern GFC_LOGICAL_8
isatty_l8 (int *);
308 export_proto(isatty_l8
);
311 isatty_l8 (int *unit
)
314 GFC_LOGICAL_8 ret
= 0;
316 u
= find_unit (*unit
);
319 ret
= (GFC_LOGICAL_8
) stream_isatty (u
->s
);
326 /* SUBROUTINE TTYNAM(UNIT,NAME)
327 INTEGER,SCALAR,INTENT(IN) :: UNIT
328 CHARACTER,SCALAR,INTENT(OUT) :: NAME */
330 extern void ttynam_sub (int *, char *, gfc_charlen_type
);
331 export_proto(ttynam_sub
);
334 ttynam_sub (int *unit
, char * name
, gfc_charlen_type name_len
)
340 memset (name
, ' ', name_len
);
341 u
= find_unit (*unit
);
344 n
= stream_ttyname (u
->s
);
348 while (*n
&& i
< name_len
)
356 extern void ttynam (char **, gfc_charlen_type
*, int);
357 export_proto(ttynam
);
360 ttynam (char ** name
, gfc_charlen_type
* name_len
, int unit
)
364 u
= find_unit (unit
);
367 *name
= stream_ttyname (u
->s
);
370 *name_len
= strlen (*name
);
371 *name
= strdup (*name
);