file_pos.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught and Janne Blomqvist
  3. This file is part of the GNU Fortran runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "io.h"
  20. #include "fbuf.h"
  21. #include "unix.h"
  22. #include <string.h>
  23. /* file_pos.c-- Implement the file positioning statements, i.e. BACKSPACE,
  24. ENDFILE, and REWIND as well as the FLUSH statement. */
  25. /* formatted_backspace(fpp, u)-- Move the file back one line. The
  26. current position is after the newline that terminates the previous
  27. record, and we have to sift backwards to find the newline before
  28. that or the start of the file, whichever comes first. */
  29. #define READ_CHUNK 4096
  30. static void
  31. formatted_backspace (st_parameter_filepos *fpp, gfc_unit *u)
  32. {
  33. gfc_offset base;
  34. char p[READ_CHUNK];
  35. ssize_t n;
  36. base = stell (u->s) - 1;
  37. do
  38. {
  39. n = (base < READ_CHUNK) ? base : READ_CHUNK;
  40. base -= n;
  41. if (sseek (u->s, base, SEEK_SET) < 0)
  42. goto io_error;
  43. if (sread (u->s, p, n) != n)
  44. goto io_error;
  45. /* We have moved backwards from the current position, it should
  46. not be possible to get a short read. Because it is not
  47. clear what to do about such thing, we ignore the possibility. */
  48. /* There is no memrchr() in the C library, so we have to do it
  49. ourselves. */
  50. while (n > 0)
  51. {
  52. n--;
  53. if (p[n] == '\n')
  54. {
  55. base += n + 1;
  56. goto done;
  57. }
  58. }
  59. }
  60. while (base != 0);
  61. /* base is the new pointer. Seek to it exactly. */
  62. done:
  63. if (sseek (u->s, base, SEEK_SET) < 0)
  64. goto io_error;
  65. u->last_record--;
  66. u->endfile = NO_ENDFILE;
  67. return;
  68. io_error:
  69. generate_error (&fpp->common, LIBERROR_OS, NULL);
  70. }
  71. /* unformatted_backspace(fpp) -- Move the file backwards for an unformatted
  72. sequential file. We are guaranteed to be between records on entry and
  73. we have to shift to the previous record. Loop over subrecords. */
  74. static void
  75. unformatted_backspace (st_parameter_filepos *fpp, gfc_unit *u)
  76. {
  77. gfc_offset m, slen;
  78. GFC_INTEGER_4 m4;
  79. GFC_INTEGER_8 m8;
  80. ssize_t length;
  81. int continued;
  82. char p[sizeof (GFC_INTEGER_8)];
  83. if (compile_options.record_marker == 0)
  84. length = sizeof (GFC_INTEGER_4);
  85. else
  86. length = compile_options.record_marker;
  87. do
  88. {
  89. slen = - (gfc_offset) length;
  90. if (sseek (u->s, slen, SEEK_CUR) < 0)
  91. goto io_error;
  92. if (sread (u->s, p, length) != length)
  93. goto io_error;
  94. /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
  95. if (likely (u->flags.convert == GFC_CONVERT_NATIVE))
  96. {
  97. switch (length)
  98. {
  99. case sizeof(GFC_INTEGER_4):
  100. memcpy (&m4, p, sizeof (m4));
  101. m = m4;
  102. break;
  103. case sizeof(GFC_INTEGER_8):
  104. memcpy (&m8, p, sizeof (m8));
  105. m = m8;
  106. break;
  107. default:
  108. runtime_error ("Illegal value for record marker");
  109. break;
  110. }
  111. }
  112. else
  113. {
  114. uint32_t u32;
  115. uint64_t u64;
  116. switch (length)
  117. {
  118. case sizeof(GFC_INTEGER_4):
  119. memcpy (&u32, p, sizeof (u32));
  120. u32 = __builtin_bswap32 (u32);
  121. memcpy (&m4, &u32, sizeof (m4));
  122. m = m4;
  123. break;
  124. case sizeof(GFC_INTEGER_8):
  125. memcpy (&u64, p, sizeof (u64));
  126. u64 = __builtin_bswap64 (u64);
  127. memcpy (&m8, &u64, sizeof (m8));
  128. m = m8;
  129. break;
  130. default:
  131. runtime_error ("Illegal value for record marker");
  132. break;
  133. }
  134. }
  135. continued = m < 0;
  136. if (continued)
  137. m = -m;
  138. if (sseek (u->s, -m -2 * length, SEEK_CUR) < 0)
  139. goto io_error;
  140. } while (continued);
  141. u->last_record--;
  142. return;
  143. io_error:
  144. generate_error (&fpp->common, LIBERROR_OS, NULL);
  145. }
  146. extern void st_backspace (st_parameter_filepos *);
  147. export_proto(st_backspace);
  148. void
  149. st_backspace (st_parameter_filepos *fpp)
  150. {
  151. gfc_unit *u;
  152. library_start (&fpp->common);
  153. u = find_unit (fpp->common.unit);
  154. if (u == NULL)
  155. {
  156. generate_error (&fpp->common, LIBERROR_BAD_UNIT, NULL);
  157. goto done;
  158. }
  159. /* Direct access is prohibited, and so is unformatted stream access. */
  160. if (u->flags.access == ACCESS_DIRECT)
  161. {
  162. generate_error (&fpp->common, LIBERROR_OPTION_CONFLICT,
  163. "Cannot BACKSPACE a file opened for DIRECT access");
  164. goto done;
  165. }
  166. if (u->flags.access == ACCESS_STREAM && u->flags.form == FORM_UNFORMATTED)
  167. {
  168. generate_error (&fpp->common, LIBERROR_OPTION_CONFLICT,
  169. "Cannot BACKSPACE an unformatted stream file");
  170. goto done;
  171. }
  172. /* Make sure format buffer is flushed and reset. */
  173. if (u->flags.form == FORM_FORMATTED)
  174. {
  175. int pos = fbuf_reset (u);
  176. if (pos != 0)
  177. sseek (u->s, pos, SEEK_CUR);
  178. }
  179. /* Check for special cases involving the ENDFILE record first. */
  180. if (u->endfile == AFTER_ENDFILE)
  181. {
  182. u->endfile = AT_ENDFILE;
  183. u->flags.position = POSITION_APPEND;
  184. sflush (u->s);
  185. }
  186. else
  187. {
  188. if (stell (u->s) == 0)
  189. {
  190. u->flags.position = POSITION_REWIND;
  191. goto done; /* Common special case */
  192. }
  193. if (u->mode == WRITING)
  194. {
  195. /* If there are previously written bytes from a write with
  196. ADVANCE="no", add a record marker before performing the
  197. BACKSPACE. */
  198. if (u->previous_nonadvancing_write)
  199. finish_last_advance_record (u);
  200. u->previous_nonadvancing_write = 0;
  201. unit_truncate (u, stell (u->s), &fpp->common);
  202. u->mode = READING;
  203. }
  204. if (u->flags.form == FORM_FORMATTED)
  205. formatted_backspace (fpp, u);
  206. else
  207. unformatted_backspace (fpp, u);
  208. u->flags.position = POSITION_UNSPECIFIED;
  209. u->endfile = NO_ENDFILE;
  210. u->current_record = 0;
  211. u->bytes_left = 0;
  212. }
  213. done:
  214. if (u != NULL)
  215. unlock_unit (u);
  216. library_end ();
  217. }
  218. extern void st_endfile (st_parameter_filepos *);
  219. export_proto(st_endfile);
  220. void
  221. st_endfile (st_parameter_filepos *fpp)
  222. {
  223. gfc_unit *u;
  224. library_start (&fpp->common);
  225. u = find_unit (fpp->common.unit);
  226. if (u != NULL)
  227. {
  228. if (u->flags.access == ACCESS_DIRECT)
  229. {
  230. generate_error (&fpp->common, LIBERROR_OPTION_CONFLICT,
  231. "Cannot perform ENDFILE on a file opened "
  232. "for DIRECT access");
  233. goto done;
  234. }
  235. if (u->flags.access == ACCESS_SEQUENTIAL
  236. && u->endfile == AFTER_ENDFILE)
  237. {
  238. generate_error (&fpp->common, LIBERROR_OPTION_CONFLICT,
  239. "Cannot perform ENDFILE on a file already "
  240. "positioned after the EOF marker");
  241. goto done;
  242. }
  243. /* If there are previously written bytes from a write with ADVANCE="no",
  244. add a record marker before performing the ENDFILE. */
  245. if (u->previous_nonadvancing_write)
  246. finish_last_advance_record (u);
  247. u->previous_nonadvancing_write = 0;
  248. if (u->current_record)
  249. {
  250. st_parameter_dt dtp;
  251. dtp.common = fpp->common;
  252. memset (&dtp.u.p, 0, sizeof (dtp.u.p));
  253. dtp.u.p.current_unit = u;
  254. next_record (&dtp, 1);
  255. }
  256. unit_truncate (u, stell (u->s), &fpp->common);
  257. u->endfile = AFTER_ENDFILE;
  258. if (0 == stell (u->s))
  259. u->flags.position = POSITION_REWIND;
  260. }
  261. else
  262. {
  263. if (fpp->common.unit < 0)
  264. {
  265. generate_error (&fpp->common, LIBERROR_BAD_OPTION,
  266. "Bad unit number in statement");
  267. return;
  268. }
  269. u = find_or_create_unit (fpp->common.unit);
  270. if (u->s == NULL)
  271. {
  272. /* Open the unit with some default flags. */
  273. st_parameter_open opp;
  274. unit_flags u_flags;
  275. memset (&u_flags, '\0', sizeof (u_flags));
  276. u_flags.access = ACCESS_SEQUENTIAL;
  277. u_flags.action = ACTION_READWRITE;
  278. /* Is it unformatted? */
  279. if (!(fpp->common.flags & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
  280. | IOPARM_DT_IONML_SET)))
  281. u_flags.form = FORM_UNFORMATTED;
  282. else
  283. u_flags.form = FORM_UNSPECIFIED;
  284. u_flags.delim = DELIM_UNSPECIFIED;
  285. u_flags.blank = BLANK_UNSPECIFIED;
  286. u_flags.pad = PAD_UNSPECIFIED;
  287. u_flags.decimal = DECIMAL_UNSPECIFIED;
  288. u_flags.encoding = ENCODING_UNSPECIFIED;
  289. u_flags.async = ASYNC_UNSPECIFIED;
  290. u_flags.round = ROUND_UNSPECIFIED;
  291. u_flags.sign = SIGN_UNSPECIFIED;
  292. u_flags.status = STATUS_UNKNOWN;
  293. u_flags.convert = GFC_CONVERT_NATIVE;
  294. opp.common = fpp->common;
  295. opp.common.flags &= IOPARM_COMMON_MASK;
  296. u = new_unit (&opp, u, &u_flags);
  297. if (u == NULL)
  298. return;
  299. u->endfile = AFTER_ENDFILE;
  300. }
  301. }
  302. done:
  303. unlock_unit (u);
  304. library_end ();
  305. }
  306. extern void st_rewind (st_parameter_filepos *);
  307. export_proto(st_rewind);
  308. void
  309. st_rewind (st_parameter_filepos *fpp)
  310. {
  311. gfc_unit *u;
  312. library_start (&fpp->common);
  313. u = find_unit (fpp->common.unit);
  314. if (u != NULL)
  315. {
  316. if (u->flags.access == ACCESS_DIRECT)
  317. generate_error (&fpp->common, LIBERROR_BAD_OPTION,
  318. "Cannot REWIND a file opened for DIRECT access");
  319. else
  320. {
  321. /* If there are previously written bytes from a write with ADVANCE="no",
  322. add a record marker before performing the ENDFILE. */
  323. if (u->previous_nonadvancing_write)
  324. finish_last_advance_record (u);
  325. u->previous_nonadvancing_write = 0;
  326. fbuf_reset (u);
  327. u->last_record = 0;
  328. if (sseek (u->s, 0, SEEK_SET) < 0)
  329. {
  330. generate_error (&fpp->common, LIBERROR_OS, NULL);
  331. library_end ();
  332. return;
  333. }
  334. /* Set this for compatibilty with g77 for /dev/null. */
  335. if (ssize (u->s) == 0)
  336. u->endfile = AT_ENDFILE;
  337. else
  338. {
  339. /* We are rewinding so we are not at the end. */
  340. u->endfile = NO_ENDFILE;
  341. }
  342. u->current_record = 0;
  343. u->strm_pos = 1;
  344. u->read_bad = 0;
  345. }
  346. /* Update position for INQUIRE. */
  347. u->flags.position = POSITION_REWIND;
  348. unlock_unit (u);
  349. }
  350. library_end ();
  351. }
  352. extern void st_flush (st_parameter_filepos *);
  353. export_proto(st_flush);
  354. void
  355. st_flush (st_parameter_filepos *fpp)
  356. {
  357. gfc_unit *u;
  358. library_start (&fpp->common);
  359. u = find_unit (fpp->common.unit);
  360. if (u != NULL)
  361. {
  362. /* Make sure format buffer is flushed. */
  363. if (u->flags.form == FORM_FORMATTED)
  364. fbuf_flush (u, u->mode);
  365. sflush (u->s);
  366. unlock_unit (u);
  367. }
  368. else
  369. /* FLUSH on unconnected unit is illegal: F95 std., 9.3.5. */
  370. generate_error (&fpp->common, LIBERROR_BAD_OPTION,
  371. "Specified UNIT in FLUSH is not connected");
  372. library_end ();
  373. }