1 /* Implementation of the FGET, FGETC, FPUT, FPUTC, FLUSH
2 FTELL, TTYNAM and ISATTY intrinsics.
3 Copyright (C) 2005, 2007, 2009, 2010 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/>. */
36 static const int five
= 5;
37 static const int six
= 6;
39 extern int PREFIX(fgetc
) (const int *, char *, gfc_charlen_type
);
40 export_proto_np(PREFIX(fgetc
));
43 PREFIX(fgetc
) (const int * unit
, char * c
, gfc_charlen_type c_len
)
46 gfc_unit
* u
= find_unit (*unit
);
52 if (u
->mode
== WRITING
)
58 memset (c
, ' ', c_len
);
59 ret
= sread (u
->s
, c
, 1);
72 #define FGETC_SUB(kind) \
73 extern void fgetc_i ## kind ## _sub \
74 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
75 export_proto(fgetc_i ## kind ## _sub); \
76 void fgetc_i ## kind ## _sub \
77 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
79 *st = PREFIX(fgetc) (unit, c, c_len); \
81 PREFIX(fgetc) (unit, c, c_len); }
89 extern int PREFIX(fget
) (char *, gfc_charlen_type
);
90 export_proto_np(PREFIX(fget
));
93 PREFIX(fget
) (char * c
, gfc_charlen_type c_len
)
95 return PREFIX(fgetc
) (&five
, c
, c_len
);
99 #define FGET_SUB(kind) \
100 extern void fget_i ## kind ## _sub \
101 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
102 export_proto(fget_i ## kind ## _sub); \
103 void fget_i ## kind ## _sub \
104 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
106 *st = PREFIX(fgetc) (&five, c, c_len); \
108 PREFIX(fgetc) (&five, c, c_len); }
117 extern int PREFIX(fputc
) (const int *, char *, gfc_charlen_type
);
118 export_proto_np(PREFIX(fputc
));
121 PREFIX(fputc
) (const int * unit
, char * c
,
122 gfc_charlen_type c_len
__attribute__((unused
)))
125 gfc_unit
* u
= find_unit (*unit
);
131 if (u
->mode
== READING
)
137 s
= swrite (u
->s
, c
, 1);
145 #define FPUTC_SUB(kind) \
146 extern void fputc_i ## kind ## _sub \
147 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
148 export_proto(fputc_i ## kind ## _sub); \
149 void fputc_i ## kind ## _sub \
150 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
152 *st = PREFIX(fputc) (unit, c, c_len); \
154 PREFIX(fputc) (unit, c, c_len); }
162 extern int PREFIX(fput
) (char *, gfc_charlen_type
);
163 export_proto_np(PREFIX(fput
));
166 PREFIX(fput
) (char * c
, gfc_charlen_type c_len
)
168 return PREFIX(fputc
) (&six
, c
, c_len
);
172 #define FPUT_SUB(kind) \
173 extern void fput_i ## kind ## _sub \
174 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
175 export_proto(fput_i ## kind ## _sub); \
176 void fput_i ## kind ## _sub \
177 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
179 *st = PREFIX(fputc) (&six, c, c_len); \
181 PREFIX(fputc) (&six, c, c_len); }
189 /* SUBROUTINE FLUSH(UNIT)
190 INTEGER, INTENT(IN), OPTIONAL :: UNIT */
192 extern void flush_i4 (GFC_INTEGER_4
*);
193 export_proto(flush_i4
);
196 flush_i4 (GFC_INTEGER_4
*unit
)
200 /* flush all streams */
205 us
= find_unit (*unit
);
215 extern void flush_i8 (GFC_INTEGER_8
*);
216 export_proto(flush_i8
);
219 flush_i8 (GFC_INTEGER_8
*unit
)
223 /* flush all streams */
228 us
= find_unit (*unit
);
237 /* FSEEK intrinsic */
239 extern void fseek_sub (int *, GFC_IO_INT
*, int *, int *);
240 export_proto(fseek_sub
);
243 fseek_sub (int * unit
, GFC_IO_INT
* offset
, int * whence
, int * status
)
245 gfc_unit
* u
= find_unit (*unit
);
248 if (u
!= NULL
&& is_seekable(u
->s
))
250 result
= sseek(u
->s
, *offset
, *whence
);
256 *status
= (result
< 0 ? -1 : 0);
261 /* FTELL intrinsic */
266 gfc_unit
* u
= find_unit (unit
);
269 int pos
= fbuf_reset (u
);
271 sseek (u
->s
, pos
, SEEK_CUR
);
272 gfc_offset ret
= stell (u
->s
);
277 extern size_t PREFIX(ftell
) (int *);
278 export_proto_np(PREFIX(ftell
));
281 PREFIX(ftell
) (int * unit
)
283 return gf_ftell (*unit
);
286 #define FTELL_SUB(kind) \
287 extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
288 export_proto(ftell_i ## kind ## _sub); \
290 ftell_i ## kind ## _sub (int * unit, GFC_INTEGER_ ## kind * offset) \
292 *offset = gf_ftell (*unit); \
302 /* LOGICAL FUNCTION ISATTY(UNIT)
303 INTEGER, INTENT(IN) :: UNIT */
305 extern GFC_LOGICAL_4
isatty_l4 (int *);
306 export_proto(isatty_l4
);
309 isatty_l4 (int *unit
)
312 GFC_LOGICAL_4 ret
= 0;
314 u
= find_unit (*unit
);
317 ret
= (GFC_LOGICAL_4
) stream_isatty (u
->s
);
324 extern GFC_LOGICAL_8
isatty_l8 (int *);
325 export_proto(isatty_l8
);
328 isatty_l8 (int *unit
)
331 GFC_LOGICAL_8 ret
= 0;
333 u
= find_unit (*unit
);
336 ret
= (GFC_LOGICAL_8
) stream_isatty (u
->s
);
343 /* SUBROUTINE TTYNAM(UNIT,NAME)
344 INTEGER,SCALAR,INTENT(IN) :: UNIT
345 CHARACTER,SCALAR,INTENT(OUT) :: NAME */
347 extern void ttynam_sub (int *, char *, gfc_charlen_type
);
348 export_proto(ttynam_sub
);
351 ttynam_sub (int *unit
, char * name
, gfc_charlen_type name_len
)
357 memset (name
, ' ', name_len
);
358 u
= find_unit (*unit
);
361 n
= stream_ttyname (u
->s
);
365 while (*n
&& i
< 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
= stream_ttyname (u
->s
);
387 *name_len
= strlen (*name
);
388 *name
= strdup (*name
);