intrinsics.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* Implementation of the FGET, FGETC, FPUT, FPUTC, FLUSH
  2. FTELL, TTYNAM and ISATTY intrinsics.
  3. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "io.h"
  21. #include "fbuf.h"
  22. #include "unix.h"
  23. #include <string.h>
  24. static const int five = 5;
  25. static const int six = 6;
  26. extern int PREFIX(fgetc) (const int *, char *, gfc_charlen_type);
  27. export_proto_np(PREFIX(fgetc));
  28. int
  29. PREFIX(fgetc) (const int *unit, char *c, gfc_charlen_type c_len)
  30. {
  31. int ret;
  32. gfc_unit *u = find_unit (*unit);
  33. if (u == NULL)
  34. return -1;
  35. fbuf_reset (u);
  36. if (u->mode == WRITING)
  37. {
  38. sflush (u->s);
  39. u->mode = READING;
  40. }
  41. memset (c, ' ', c_len);
  42. ret = sread (u->s, c, 1);
  43. unlock_unit (u);
  44. if (ret < 0)
  45. return ret;
  46. if (ret != 1)
  47. return -1;
  48. else
  49. return 0;
  50. }
  51. #define FGETC_SUB(kind) \
  52. extern void fgetc_i ## kind ## _sub \
  53. (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
  54. export_proto(fgetc_i ## kind ## _sub); \
  55. void fgetc_i ## kind ## _sub \
  56. (const int *unit, char *c, GFC_INTEGER_ ## kind *st, gfc_charlen_type c_len) \
  57. { if (st != NULL) \
  58. *st = PREFIX(fgetc) (unit, c, c_len); \
  59. else \
  60. PREFIX(fgetc) (unit, c, c_len); }
  61. FGETC_SUB(1)
  62. FGETC_SUB(2)
  63. FGETC_SUB(4)
  64. FGETC_SUB(8)
  65. extern int PREFIX(fget) (char *, gfc_charlen_type);
  66. export_proto_np(PREFIX(fget));
  67. int
  68. PREFIX(fget) (char *c, gfc_charlen_type c_len)
  69. {
  70. return PREFIX(fgetc) (&five, c, c_len);
  71. }
  72. #define FGET_SUB(kind) \
  73. extern void fget_i ## kind ## _sub \
  74. (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
  75. export_proto(fget_i ## kind ## _sub); \
  76. void fget_i ## kind ## _sub \
  77. (char *c, GFC_INTEGER_ ## kind *st, gfc_charlen_type c_len) \
  78. { if (st != NULL) \
  79. *st = PREFIX(fgetc) (&five, c, c_len); \
  80. else \
  81. PREFIX(fgetc) (&five, c, c_len); }
  82. FGET_SUB(1)
  83. FGET_SUB(2)
  84. FGET_SUB(4)
  85. FGET_SUB(8)
  86. extern int PREFIX(fputc) (const int *, char *, gfc_charlen_type);
  87. export_proto_np(PREFIX(fputc));
  88. int
  89. PREFIX(fputc) (const int *unit, char *c,
  90. gfc_charlen_type c_len __attribute__((unused)))
  91. {
  92. ssize_t s;
  93. gfc_unit *u = find_unit (*unit);
  94. if (u == NULL)
  95. return -1;
  96. fbuf_reset (u);
  97. if (u->mode == READING)
  98. {
  99. sflush (u->s);
  100. u->mode = WRITING;
  101. }
  102. s = swrite (u->s, c, 1);
  103. unlock_unit (u);
  104. if (s < 0)
  105. return -1;
  106. return 0;
  107. }
  108. #define FPUTC_SUB(kind) \
  109. extern void fputc_i ## kind ## _sub \
  110. (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
  111. export_proto(fputc_i ## kind ## _sub); \
  112. void fputc_i ## kind ## _sub \
  113. (const int *unit, char *c, GFC_INTEGER_ ## kind *st, gfc_charlen_type c_len) \
  114. { if (st != NULL) \
  115. *st = PREFIX(fputc) (unit, c, c_len); \
  116. else \
  117. PREFIX(fputc) (unit, c, c_len); }
  118. FPUTC_SUB(1)
  119. FPUTC_SUB(2)
  120. FPUTC_SUB(4)
  121. FPUTC_SUB(8)
  122. extern int PREFIX(fput) (char *, gfc_charlen_type);
  123. export_proto_np(PREFIX(fput));
  124. int
  125. PREFIX(fput) (char *c, gfc_charlen_type c_len)
  126. {
  127. return PREFIX(fputc) (&six, c, c_len);
  128. }
  129. #define FPUT_SUB(kind) \
  130. extern void fput_i ## kind ## _sub \
  131. (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
  132. export_proto(fput_i ## kind ## _sub); \
  133. void fput_i ## kind ## _sub \
  134. (char *c, GFC_INTEGER_ ## kind *st, gfc_charlen_type c_len) \
  135. { if (st != NULL) \
  136. *st = PREFIX(fputc) (&six, c, c_len); \
  137. else \
  138. PREFIX(fputc) (&six, c, c_len); }
  139. FPUT_SUB(1)
  140. FPUT_SUB(2)
  141. FPUT_SUB(4)
  142. FPUT_SUB(8)
  143. /* SUBROUTINE FLUSH(UNIT)
  144. INTEGER, INTENT(IN), OPTIONAL :: UNIT */
  145. extern void flush_i4 (GFC_INTEGER_4 *);
  146. export_proto(flush_i4);
  147. void
  148. flush_i4 (GFC_INTEGER_4 *unit)
  149. {
  150. gfc_unit *us;
  151. /* flush all streams */
  152. if (unit == NULL)
  153. flush_all_units ();
  154. else
  155. {
  156. us = find_unit (*unit);
  157. if (us != NULL)
  158. {
  159. sflush (us->s);
  160. unlock_unit (us);
  161. }
  162. }
  163. }
  164. extern void flush_i8 (GFC_INTEGER_8 *);
  165. export_proto(flush_i8);
  166. void
  167. flush_i8 (GFC_INTEGER_8 *unit)
  168. {
  169. gfc_unit *us;
  170. /* flush all streams */
  171. if (unit == NULL)
  172. flush_all_units ();
  173. else
  174. {
  175. us = find_unit (*unit);
  176. if (us != NULL)
  177. {
  178. sflush (us->s);
  179. unlock_unit (us);
  180. }
  181. }
  182. }
  183. /* FSEEK intrinsic */
  184. extern void fseek_sub (int *, GFC_IO_INT *, int *, int *);
  185. export_proto(fseek_sub);
  186. void
  187. fseek_sub (int *unit, GFC_IO_INT *offset, int *whence, int *status)
  188. {
  189. gfc_unit *u = find_unit (*unit);
  190. ssize_t result = -1;
  191. if (u != NULL)
  192. {
  193. result = sseek(u->s, *offset, *whence);
  194. unlock_unit (u);
  195. }
  196. if (status)
  197. *status = (result < 0 ? -1 : 0);
  198. }
  199. /* FTELL intrinsic */
  200. static gfc_offset
  201. gf_ftell (int unit)
  202. {
  203. gfc_unit *u = find_unit (unit);
  204. if (u == NULL)
  205. return -1;
  206. int pos = fbuf_reset (u);
  207. if (pos != 0)
  208. sseek (u->s, pos, SEEK_CUR);
  209. gfc_offset ret = stell (u->s);
  210. unlock_unit (u);
  211. return ret;
  212. }
  213. extern GFC_IO_INT PREFIX(ftell) (int *);
  214. export_proto_np(PREFIX(ftell));
  215. GFC_IO_INT
  216. PREFIX(ftell) (int *unit)
  217. {
  218. return gf_ftell (*unit);
  219. }
  220. #define FTELL_SUB(kind) \
  221. extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
  222. export_proto(ftell_i ## kind ## _sub); \
  223. void \
  224. ftell_i ## kind ## _sub (int *unit, GFC_INTEGER_ ## kind *offset) \
  225. { \
  226. *offset = gf_ftell (*unit); \
  227. }
  228. FTELL_SUB(1)
  229. FTELL_SUB(2)
  230. FTELL_SUB(4)
  231. FTELL_SUB(8)
  232. /* LOGICAL FUNCTION ISATTY(UNIT)
  233. INTEGER, INTENT(IN) :: UNIT */
  234. extern GFC_LOGICAL_4 isatty_l4 (int *);
  235. export_proto(isatty_l4);
  236. GFC_LOGICAL_4
  237. isatty_l4 (int *unit)
  238. {
  239. gfc_unit *u;
  240. GFC_LOGICAL_4 ret = 0;
  241. u = find_unit (*unit);
  242. if (u != NULL)
  243. {
  244. ret = (GFC_LOGICAL_4) stream_isatty (u->s);
  245. unlock_unit (u);
  246. }
  247. return ret;
  248. }
  249. extern GFC_LOGICAL_8 isatty_l8 (int *);
  250. export_proto(isatty_l8);
  251. GFC_LOGICAL_8
  252. isatty_l8 (int *unit)
  253. {
  254. gfc_unit *u;
  255. GFC_LOGICAL_8 ret = 0;
  256. u = find_unit (*unit);
  257. if (u != NULL)
  258. {
  259. ret = (GFC_LOGICAL_8) stream_isatty (u->s);
  260. unlock_unit (u);
  261. }
  262. return ret;
  263. }
  264. /* SUBROUTINE TTYNAM(UNIT,NAME)
  265. INTEGER,SCALAR,INTENT(IN) :: UNIT
  266. CHARACTER,SCALAR,INTENT(OUT) :: NAME */
  267. extern void ttynam_sub (int *, char *, gfc_charlen_type);
  268. export_proto(ttynam_sub);
  269. void
  270. ttynam_sub (int *unit, char *name, gfc_charlen_type name_len)
  271. {
  272. gfc_unit *u;
  273. int nlen;
  274. int err = 1;
  275. u = find_unit (*unit);
  276. if (u != NULL)
  277. {
  278. err = stream_ttyname (u->s, name, name_len);
  279. if (err == 0)
  280. {
  281. nlen = strlen (name);
  282. memset (&name[nlen], ' ', name_len - nlen);
  283. }
  284. unlock_unit (u);
  285. }
  286. if (err != 0)
  287. memset (name, ' ', name_len);
  288. }
  289. extern void ttynam (char **, gfc_charlen_type *, int);
  290. export_proto(ttynam);
  291. void
  292. ttynam (char **name, gfc_charlen_type *name_len, int unit)
  293. {
  294. gfc_unit *u;
  295. u = find_unit (unit);
  296. if (u != NULL)
  297. {
  298. *name = xmalloc (TTY_NAME_MAX);
  299. int err = stream_ttyname (u->s, *name, TTY_NAME_MAX);
  300. if (err == 0)
  301. {
  302. *name_len = strlen (*name);
  303. unlock_unit (u);
  304. return;
  305. }
  306. free (*name);
  307. unlock_unit (u);
  308. }
  309. *name_len = 0;
  310. *name = NULL;
  311. }