intrinsics.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* Implementation of the FGET, FGETC, FPUT, FPUTC, FLUSH
  2. FTELL, TTYNAM and ISATTY intrinsics.
  3. Copyright (C) 2005-2015 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 <stdlib.h>
  24. #include <string.h>
  25. static const int five = 5;
  26. static const int six = 6;
  27. extern int PREFIX(fgetc) (const int *, char *, gfc_charlen_type);
  28. export_proto_np(PREFIX(fgetc));
  29. int
  30. PREFIX(fgetc) (const int * unit, char * c, gfc_charlen_type c_len)
  31. {
  32. int ret;
  33. gfc_unit * u = find_unit (*unit);
  34. if (u == NULL)
  35. return -1;
  36. fbuf_reset (u);
  37. if (u->mode == WRITING)
  38. {
  39. sflush (u->s);
  40. u->mode = READING;
  41. }
  42. memset (c, ' ', c_len);
  43. ret = sread (u->s, c, 1);
  44. unlock_unit (u);
  45. if (ret < 0)
  46. return ret;
  47. if (ret != 1)
  48. return -1;
  49. else
  50. return 0;
  51. }
  52. #define FGETC_SUB(kind) \
  53. extern void fgetc_i ## kind ## _sub \
  54. (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
  55. export_proto(fgetc_i ## kind ## _sub); \
  56. void fgetc_i ## kind ## _sub \
  57. (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
  58. { if (st != NULL) \
  59. *st = PREFIX(fgetc) (unit, c, c_len); \
  60. else \
  61. PREFIX(fgetc) (unit, c, c_len); }
  62. FGETC_SUB(1)
  63. FGETC_SUB(2)
  64. FGETC_SUB(4)
  65. FGETC_SUB(8)
  66. extern int PREFIX(fget) (char *, gfc_charlen_type);
  67. export_proto_np(PREFIX(fget));
  68. int
  69. PREFIX(fget) (char * c, gfc_charlen_type c_len)
  70. {
  71. return PREFIX(fgetc) (&five, c, c_len);
  72. }
  73. #define FGET_SUB(kind) \
  74. extern void fget_i ## kind ## _sub \
  75. (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
  76. export_proto(fget_i ## kind ## _sub); \
  77. void fget_i ## kind ## _sub \
  78. (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
  79. { if (st != NULL) \
  80. *st = PREFIX(fgetc) (&five, c, c_len); \
  81. else \
  82. PREFIX(fgetc) (&five, c, c_len); }
  83. FGET_SUB(1)
  84. FGET_SUB(2)
  85. FGET_SUB(4)
  86. FGET_SUB(8)
  87. extern int PREFIX(fputc) (const int *, char *, gfc_charlen_type);
  88. export_proto_np(PREFIX(fputc));
  89. int
  90. PREFIX(fputc) (const int * unit, char * c,
  91. gfc_charlen_type c_len __attribute__((unused)))
  92. {
  93. ssize_t s;
  94. gfc_unit * u = find_unit (*unit);
  95. if (u == NULL)
  96. return -1;
  97. fbuf_reset (u);
  98. if (u->mode == READING)
  99. {
  100. sflush (u->s);
  101. u->mode = WRITING;
  102. }
  103. s = swrite (u->s, c, 1);
  104. unlock_unit (u);
  105. if (s < 0)
  106. return -1;
  107. return 0;
  108. }
  109. #define FPUTC_SUB(kind) \
  110. extern void fputc_i ## kind ## _sub \
  111. (const int *, char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
  112. export_proto(fputc_i ## kind ## _sub); \
  113. void fputc_i ## kind ## _sub \
  114. (const int * unit, char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
  115. { if (st != NULL) \
  116. *st = PREFIX(fputc) (unit, c, c_len); \
  117. else \
  118. PREFIX(fputc) (unit, c, c_len); }
  119. FPUTC_SUB(1)
  120. FPUTC_SUB(2)
  121. FPUTC_SUB(4)
  122. FPUTC_SUB(8)
  123. extern int PREFIX(fput) (char *, gfc_charlen_type);
  124. export_proto_np(PREFIX(fput));
  125. int
  126. PREFIX(fput) (char * c, gfc_charlen_type c_len)
  127. {
  128. return PREFIX(fputc) (&six, c, c_len);
  129. }
  130. #define FPUT_SUB(kind) \
  131. extern void fput_i ## kind ## _sub \
  132. (char *, GFC_INTEGER_ ## kind *, gfc_charlen_type); \
  133. export_proto(fput_i ## kind ## _sub); \
  134. void fput_i ## kind ## _sub \
  135. (char * c, GFC_INTEGER_ ## kind * st, gfc_charlen_type c_len) \
  136. { if (st != NULL) \
  137. *st = PREFIX(fputc) (&six, c, c_len); \
  138. else \
  139. PREFIX(fputc) (&six, c, c_len); }
  140. FPUT_SUB(1)
  141. FPUT_SUB(2)
  142. FPUT_SUB(4)
  143. FPUT_SUB(8)
  144. /* SUBROUTINE FLUSH(UNIT)
  145. INTEGER, INTENT(IN), OPTIONAL :: UNIT */
  146. extern void flush_i4 (GFC_INTEGER_4 *);
  147. export_proto(flush_i4);
  148. void
  149. flush_i4 (GFC_INTEGER_4 *unit)
  150. {
  151. gfc_unit *us;
  152. /* flush all streams */
  153. if (unit == NULL)
  154. flush_all_units ();
  155. else
  156. {
  157. us = find_unit (*unit);
  158. if (us != NULL)
  159. {
  160. sflush (us->s);
  161. unlock_unit (us);
  162. }
  163. }
  164. }
  165. extern void flush_i8 (GFC_INTEGER_8 *);
  166. export_proto(flush_i8);
  167. void
  168. flush_i8 (GFC_INTEGER_8 *unit)
  169. {
  170. gfc_unit *us;
  171. /* flush all streams */
  172. if (unit == NULL)
  173. flush_all_units ();
  174. else
  175. {
  176. us = find_unit (*unit);
  177. if (us != NULL)
  178. {
  179. sflush (us->s);
  180. unlock_unit (us);
  181. }
  182. }
  183. }
  184. /* FSEEK intrinsic */
  185. extern void fseek_sub (int *, GFC_IO_INT *, int *, int *);
  186. export_proto(fseek_sub);
  187. void
  188. fseek_sub (int * unit, GFC_IO_INT * offset, int * whence, int * status)
  189. {
  190. gfc_unit * u = find_unit (*unit);
  191. ssize_t result = -1;
  192. if (u != NULL)
  193. {
  194. result = sseek(u->s, *offset, *whence);
  195. unlock_unit (u);
  196. }
  197. if (status)
  198. *status = (result < 0 ? -1 : 0);
  199. }
  200. /* FTELL intrinsic */
  201. static gfc_offset
  202. gf_ftell (int unit)
  203. {
  204. gfc_unit * u = find_unit (unit);
  205. if (u == NULL)
  206. return -1;
  207. int pos = fbuf_reset (u);
  208. if (pos != 0)
  209. sseek (u->s, pos, SEEK_CUR);
  210. gfc_offset ret = stell (u->s);
  211. unlock_unit (u);
  212. return ret;
  213. }
  214. /* Here is the ftell function with an incorrect return type; retained
  215. due to ABI compatibility. */
  216. extern size_t PREFIX(ftell) (int *);
  217. export_proto_np(PREFIX(ftell));
  218. size_t
  219. PREFIX(ftell) (int * unit)
  220. {
  221. return gf_ftell (*unit);
  222. }
  223. /* Here is the ftell function with the correct return type, ensuring
  224. that large files can be supported as long as the target supports
  225. large integers; as of 4.8 the FTELL intrinsic function will call
  226. this one instead of the old ftell above. */
  227. extern GFC_IO_INT PREFIX(ftell2) (int *);
  228. export_proto_np(PREFIX(ftell2));
  229. GFC_IO_INT
  230. PREFIX(ftell2) (int * unit)
  231. {
  232. return gf_ftell (*unit);
  233. }
  234. #define FTELL_SUB(kind) \
  235. extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
  236. export_proto(ftell_i ## kind ## _sub); \
  237. void \
  238. ftell_i ## kind ## _sub (int * unit, GFC_INTEGER_ ## kind * offset) \
  239. { \
  240. *offset = gf_ftell (*unit); \
  241. }
  242. FTELL_SUB(1)
  243. FTELL_SUB(2)
  244. FTELL_SUB(4)
  245. FTELL_SUB(8)
  246. /* LOGICAL FUNCTION ISATTY(UNIT)
  247. INTEGER, INTENT(IN) :: UNIT */
  248. extern GFC_LOGICAL_4 isatty_l4 (int *);
  249. export_proto(isatty_l4);
  250. GFC_LOGICAL_4
  251. isatty_l4 (int *unit)
  252. {
  253. gfc_unit *u;
  254. GFC_LOGICAL_4 ret = 0;
  255. u = find_unit (*unit);
  256. if (u != NULL)
  257. {
  258. ret = (GFC_LOGICAL_4) stream_isatty (u->s);
  259. unlock_unit (u);
  260. }
  261. return ret;
  262. }
  263. extern GFC_LOGICAL_8 isatty_l8 (int *);
  264. export_proto(isatty_l8);
  265. GFC_LOGICAL_8
  266. isatty_l8 (int *unit)
  267. {
  268. gfc_unit *u;
  269. GFC_LOGICAL_8 ret = 0;
  270. u = find_unit (*unit);
  271. if (u != NULL)
  272. {
  273. ret = (GFC_LOGICAL_8) stream_isatty (u->s);
  274. unlock_unit (u);
  275. }
  276. return ret;
  277. }
  278. /* SUBROUTINE TTYNAM(UNIT,NAME)
  279. INTEGER,SCALAR,INTENT(IN) :: UNIT
  280. CHARACTER,SCALAR,INTENT(OUT) :: NAME */
  281. extern void ttynam_sub (int *, char *, gfc_charlen_type);
  282. export_proto(ttynam_sub);
  283. void
  284. ttynam_sub (int *unit, char * name, gfc_charlen_type name_len)
  285. {
  286. gfc_unit *u;
  287. int nlen;
  288. int err = 1;
  289. u = find_unit (*unit);
  290. if (u != NULL)
  291. {
  292. err = stream_ttyname (u->s, name, name_len);
  293. if (err == 0)
  294. {
  295. nlen = strlen (name);
  296. memset (&name[nlen], ' ', name_len - nlen);
  297. }
  298. unlock_unit (u);
  299. }
  300. if (err != 0)
  301. memset (name, ' ', name_len);
  302. }
  303. extern void ttynam (char **, gfc_charlen_type *, int);
  304. export_proto(ttynam);
  305. void
  306. ttynam (char ** name, gfc_charlen_type * name_len, int unit)
  307. {
  308. gfc_unit *u;
  309. u = find_unit (unit);
  310. if (u != NULL)
  311. {
  312. *name = xmalloc (TTY_NAME_MAX);
  313. int err = stream_ttyname (u->s, *name, TTY_NAME_MAX);
  314. if (err == 0)
  315. {
  316. *name_len = strlen (*name);
  317. unlock_unit (u);
  318. return;
  319. }
  320. free (*name);
  321. unlock_unit (u);
  322. }
  323. *name_len = 0;
  324. *name = NULL;
  325. }