PR c++/29318
[official-gcc.git] / libgfortran / intrinsics / fget.c
blob5c87ae6c3c11e0db40309b331a8385690bb12851
1 /* Implementation of the FGET, FGETC, FPUT and FPUTC intrinsics.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3 Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
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
19 executable.)
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. */
31 #include "config.h"
32 #include "libgfortran.h"
34 #include <string.h>
36 #include "../io/io.h"
38 static const int five = 5;
39 static const int six = 6;
41 extern int PREFIX(fgetc) (const int *, char *, gfc_charlen_type);
42 export_proto_np(PREFIX(fgetc));
44 int
45 PREFIX(fgetc) (const int * unit, char * c, gfc_charlen_type c_len)
47 int ret;
48 size_t s;
49 gfc_unit * u = find_unit (*unit);
51 if (u == NULL)
52 return -1;
54 s = 1;
55 memset (c, ' ', c_len);
56 ret = sread (u->s, c, &s);
57 unlock_unit (u);
59 if (ret != 0)
60 return ret;
62 if (s != 1)
63 return -1;
64 else
65 return 0;
69 #define FGETC_SUB(kind) \
70 extern void fgetc_i ## kind ## _sub \
71 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
72 export_proto(fgetc_i ## kind ## _sub); \
73 void fgetc_i ## kind ## _sub \
74 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
75 { if (st != NULL) \
76 *st = PREFIX(fgetc) (unit, c, c_len); \
77 else \
78 PREFIX(fgetc) (unit, c, c_len); }
80 FGETC_SUB(1)
81 FGETC_SUB(2)
82 FGETC_SUB(4)
83 FGETC_SUB(8)
86 extern int PREFIX(fget) (char *, gfc_charlen_type);
87 export_proto_np(PREFIX(fget));
89 int
90 PREFIX(fget) (char * c, gfc_charlen_type c_len)
92 return PREFIX(fgetc) (&five, c, c_len);
96 #define FGET_SUB(kind) \
97 extern void fget_i ## kind ## _sub \
98 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
99 export_proto(fget_i ## kind ## _sub); \
100 void fget_i ## kind ## _sub \
101 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
102 { if (st != NULL) \
103 *st = PREFIX(fgetc) (&five, c, c_len); \
104 else \
105 PREFIX(fgetc) (&five, c, c_len); }
107 FGET_SUB(1)
108 FGET_SUB(2)
109 FGET_SUB(4)
110 FGET_SUB(8)
114 extern int PREFIX(fputc) (const int *, char *, gfc_charlen_type);
115 export_proto_np(PREFIX(fputc));
118 PREFIX(fputc) (const int * unit, char * c,
119 gfc_charlen_type c_len __attribute__((unused)))
121 size_t s;
122 int ret;
123 gfc_unit * u = find_unit (*unit);
125 if (u == NULL)
126 return -1;
128 s = 1;
129 ret = swrite (u->s, c, &s);
130 unlock_unit (u);
131 return ret;
135 #define FPUTC_SUB(kind) \
136 extern void fputc_i ## kind ## _sub \
137 (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
138 export_proto(fputc_i ## kind ## _sub); \
139 void fputc_i ## kind ## _sub \
140 (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
141 { if (st != NULL) \
142 *st = PREFIX(fputc) (unit, c, c_len); \
143 else \
144 PREFIX(fputc) (unit, c, c_len); }
146 FPUTC_SUB(1)
147 FPUTC_SUB(2)
148 FPUTC_SUB(4)
149 FPUTC_SUB(8)
152 extern int PREFIX(fput) (char *, gfc_charlen_type);
153 export_proto_np(PREFIX(fput));
156 PREFIX(fput) (char * c, gfc_charlen_type c_len)
158 return PREFIX(fputc) (&six, c, c_len);
162 #define FPUT_SUB(kind) \
163 extern void fput_i ## kind ## _sub \
164 (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
165 export_proto(fput_i ## kind ## _sub); \
166 void fput_i ## kind ## _sub \
167 (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
168 { if (st != NULL) \
169 *st = PREFIX(fputc) (&six, c, c_len); \
170 else \
171 PREFIX(fputc) (&six, c, c_len); }
173 FPUT_SUB(1)
174 FPUT_SUB(2)
175 FPUT_SUB(4)
176 FPUT_SUB(8)