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. */
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
)
50 gfc_unit
* u
= find_unit (*unit
);
56 memset (c
, ' ', c_len
);
57 ret
= sread (u
->s
, c
, &s
);
70 #define FGETC_SUB(kind) \
71 extern void fgetc_i ## kind ## _sub \
72 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
73 export_proto(fgetc_i ## kind ## _sub); \
74 void fgetc_i ## kind ## _sub \
75 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
77 *st = PREFIX(fgetc) (unit, c, c_len); \
79 PREFIX(fgetc) (unit, c, c_len); }
87 extern int PREFIX(fget
) (char *, gfc_charlen_type
);
88 export_proto_np(PREFIX(fget
));
91 PREFIX(fget
) (char * c
, gfc_charlen_type c_len
)
93 return PREFIX(fgetc
) (&five
, c
, c_len
);
97 #define FGET_SUB(kind) \
98 extern void fget_i ## kind ## _sub \
99 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
100 export_proto(fget_i ## kind ## _sub); \
101 void fget_i ## kind ## _sub \
102 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
104 *st = PREFIX(fgetc) (&five, c, c_len); \
106 PREFIX(fgetc) (&five, c, c_len); }
115 extern int PREFIX(fputc
) (const int *, char *, gfc_charlen_type
);
116 export_proto_np(PREFIX(fputc
));
119 PREFIX(fputc
) (const int * unit
, char * c
,
120 gfc_charlen_type c_len
__attribute__((unused
)))
124 gfc_unit
* u
= find_unit (*unit
);
130 ret
= swrite (u
->s
, c
, &s
);
136 #define FPUTC_SUB(kind) \
137 extern void fputc_i ## kind ## _sub \
138 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
139 export_proto(fputc_i ## kind ## _sub); \
140 void fputc_i ## kind ## _sub \
141 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
143 *st = PREFIX(fputc) (unit, c, c_len); \
145 PREFIX(fputc) (unit, c, c_len); }
153 extern int PREFIX(fput
) (char *, gfc_charlen_type
);
154 export_proto_np(PREFIX(fput
));
157 PREFIX(fput
) (char * c
, gfc_charlen_type c_len
)
159 return PREFIX(fputc
) (&six
, c
, c_len
);
163 #define FPUT_SUB(kind) \
164 extern void fput_i ## kind ## _sub \
165 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
166 export_proto(fput_i ## kind ## _sub); \
167 void fput_i ## kind ## _sub \
168 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
170 *st = PREFIX(fputc) (&six, c, c_len); \
172 PREFIX(fputc) (&six, c, c_len); }
180 /* SUBROUTINE FLUSH(UNIT)
181 INTEGER, INTENT(IN), OPTIONAL :: UNIT */
183 extern void flush_i4 (GFC_INTEGER_4
*);
184 export_proto(flush_i4
);
187 flush_i4 (GFC_INTEGER_4
*unit
)
191 /* flush all streams */
196 us
= find_unit (*unit
);
206 extern void flush_i8 (GFC_INTEGER_8
*);
207 export_proto(flush_i8
);
210 flush_i8 (GFC_INTEGER_8
*unit
)
214 /* flush all streams */
219 us
= find_unit (*unit
);
228 /* FSEEK intrinsic */
230 extern void fseek_sub (int *, GFC_IO_INT
*, int *, int *);
231 export_proto(fseek_sub
);
234 fseek_sub (int * unit
, GFC_IO_INT
* offset
, int * whence
, int * status
)
236 gfc_unit
* u
= find_unit (*unit
);
237 try result
= FAILURE
;
239 if (u
!= NULL
&& is_seekable(u
->s
))
242 result
= sseek(u
->s
, *offset
); /* SEEK_SET */
243 else if (*whence
== 1)
244 result
= sseek(u
->s
, file_position(u
->s
) + *offset
); /* SEEK_CUR */
245 else if (*whence
== 2)
246 result
= sseek(u
->s
, file_length(u
->s
) + *offset
); /* SEEK_END */
252 *status
= (result
== FAILURE
? -1 : 0);
257 /* FTELL intrinsic */
259 extern size_t PREFIX(ftell
) (int *);
260 export_proto_np(PREFIX(ftell
));
263 PREFIX(ftell
) (int * unit
)
265 gfc_unit
* u
= find_unit (*unit
);
268 return ((size_t) -1);
269 ret
= (size_t) stream_offset (u
->s
);
274 #define FTELL_SUB(kind) \
275 extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
276 export_proto(ftell_i ## kind ## _sub); \
278 ftell_i ## kind ## _sub (int * unit, GFC_INTEGER_ ## kind * offset) \
280 gfc_unit * u = find_unit (*unit); \
285 *offset = stream_offset (u->s); \
297 /* LOGICAL FUNCTION ISATTY(UNIT)
298 INTEGER, INTENT(IN) :: UNIT */
300 extern GFC_LOGICAL_4
isatty_l4 (int *);
301 export_proto(isatty_l4
);
304 isatty_l4 (int *unit
)
307 GFC_LOGICAL_4 ret
= 0;
309 u
= find_unit (*unit
);
312 ret
= (GFC_LOGICAL_4
) stream_isatty (u
->s
);
319 extern GFC_LOGICAL_8
isatty_l8 (int *);
320 export_proto(isatty_l8
);
323 isatty_l8 (int *unit
)
326 GFC_LOGICAL_8 ret
= 0;
328 u
= find_unit (*unit
);
331 ret
= (GFC_LOGICAL_8
) stream_isatty (u
->s
);
338 /* SUBROUTINE TTYNAM(UNIT,NAME)
339 INTEGER,SCALAR,INTENT(IN) :: UNIT
340 CHARACTER,SCALAR,INTENT(OUT) :: NAME */
342 extern void ttynam_sub (int *, char *, gfc_charlen_type
);
343 export_proto(ttynam_sub
);
346 ttynam_sub (int *unit
, char * name
, gfc_charlen_type name_len
)
352 memset (name
, ' ', name_len
);
353 u
= find_unit (*unit
);
356 n
= stream_ttyname (u
->s
);
360 while (*n
&& i
< name_len
)
368 extern void ttynam (char **, gfc_charlen_type
*, int);
369 export_proto(ttynam
);
372 ttynam (char ** name
, gfc_charlen_type
* name_len
, int unit
)
376 u
= find_unit (unit
);
379 *name
= stream_ttyname (u
->s
);
382 *name_len
= strlen (*name
);
383 *name
= strdup (*name
);