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 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. */
39 static const int five
= 5;
40 static const int six
= 6;
42 extern int PREFIX(fgetc
) (const int *, char *, gfc_charlen_type
);
43 export_proto_np(PREFIX(fgetc
));
46 PREFIX(fgetc
) (const int * unit
, char * c
, gfc_charlen_type c_len
)
49 gfc_unit
* u
= find_unit (*unit
);
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
);
126 s
= swrite (u
->s
, c
, 1);
134 #define FPUTC_SUB(kind) \
135 extern void fputc_i ## kind ## _sub \
136 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
137 export_proto(fputc_i ## kind ## _sub); \
138 void fputc_i ## kind ## _sub \
139 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
141 *st = PREFIX(fputc) (unit, c, c_len); \
143 PREFIX(fputc) (unit, c, c_len); }
151 extern int PREFIX(fput
) (char *, gfc_charlen_type
);
152 export_proto_np(PREFIX(fput
));
155 PREFIX(fput
) (char * c
, gfc_charlen_type c_len
)
157 return PREFIX(fputc
) (&six
, c
, c_len
);
161 #define FPUT_SUB(kind) \
162 extern void fput_i ## kind ## _sub \
163 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
164 export_proto(fput_i ## kind ## _sub); \
165 void fput_i ## kind ## _sub \
166 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
168 *st = PREFIX(fputc) (&six, c, c_len); \
170 PREFIX(fputc) (&six, c, c_len); }
178 /* SUBROUTINE FLUSH(UNIT)
179 INTEGER, INTENT(IN), OPTIONAL :: UNIT */
181 extern void flush_i4 (GFC_INTEGER_4
*);
182 export_proto(flush_i4
);
185 flush_i4 (GFC_INTEGER_4
*unit
)
189 /* flush all streams */
194 us
= find_unit (*unit
);
204 extern void flush_i8 (GFC_INTEGER_8
*);
205 export_proto(flush_i8
);
208 flush_i8 (GFC_INTEGER_8
*unit
)
212 /* flush all streams */
217 us
= find_unit (*unit
);
226 /* FSEEK intrinsic */
228 extern void fseek_sub (int *, GFC_IO_INT
*, int *, int *);
229 export_proto(fseek_sub
);
232 fseek_sub (int * unit
, GFC_IO_INT
* offset
, int * whence
, int * status
)
234 gfc_unit
* u
= find_unit (*unit
);
237 if (u
!= NULL
&& is_seekable(u
->s
))
239 result
= sseek(u
->s
, *offset
, *whence
);
245 *status
= (result
< 0 ? -1 : 0);
250 /* FTELL intrinsic */
252 extern size_t PREFIX(ftell
) (int *);
253 export_proto_np(PREFIX(ftell
));
256 PREFIX(ftell
) (int * unit
)
258 gfc_unit
* u
= find_unit (*unit
);
261 return ((size_t) -1);
262 ret
= (size_t) stell (u
->s
);
267 #define FTELL_SUB(kind) \
268 extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
269 export_proto(ftell_i ## kind ## _sub); \
271 ftell_i ## kind ## _sub (int * unit, GFC_INTEGER_ ## kind * offset) \
273 gfc_unit * u = find_unit (*unit); \
278 *offset = stell (u->s); \
290 /* LOGICAL FUNCTION ISATTY(UNIT)
291 INTEGER, INTENT(IN) :: UNIT */
293 extern GFC_LOGICAL_4
isatty_l4 (int *);
294 export_proto(isatty_l4
);
297 isatty_l4 (int *unit
)
300 GFC_LOGICAL_4 ret
= 0;
302 u
= find_unit (*unit
);
305 ret
= (GFC_LOGICAL_4
) stream_isatty (u
->s
);
312 extern GFC_LOGICAL_8
isatty_l8 (int *);
313 export_proto(isatty_l8
);
316 isatty_l8 (int *unit
)
319 GFC_LOGICAL_8 ret
= 0;
321 u
= find_unit (*unit
);
324 ret
= (GFC_LOGICAL_8
) stream_isatty (u
->s
);
331 /* SUBROUTINE TTYNAM(UNIT,NAME)
332 INTEGER,SCALAR,INTENT(IN) :: UNIT
333 CHARACTER,SCALAR,INTENT(OUT) :: NAME */
335 extern void ttynam_sub (int *, char *, gfc_charlen_type
);
336 export_proto(ttynam_sub
);
339 ttynam_sub (int *unit
, char * name
, gfc_charlen_type name_len
)
345 memset (name
, ' ', name_len
);
346 u
= find_unit (*unit
);
349 n
= stream_ttyname (u
->s
);
353 while (*n
&& i
< name_len
)
361 extern void ttynam (char **, gfc_charlen_type
*, int);
362 export_proto(ttynam
);
365 ttynam (char ** name
, gfc_charlen_type
* name_len
, int unit
)
369 u
= find_unit (unit
);
372 *name
= stream_ttyname (u
->s
);
375 *name_len
= strlen (*name
);
376 *name
= strdup (*name
);