open.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught
  3. F2003 I/O support contributed by Jerry DeLisle
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. 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. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <stdlib.h>
  29. static const st_option access_opt[] = {
  30. {"sequential", ACCESS_SEQUENTIAL},
  31. {"direct", ACCESS_DIRECT},
  32. {"append", ACCESS_APPEND},
  33. {"stream", ACCESS_STREAM},
  34. {NULL, 0}
  35. };
  36. static const st_option action_opt[] =
  37. {
  38. { "read", ACTION_READ},
  39. { "write", ACTION_WRITE},
  40. { "readwrite", ACTION_READWRITE},
  41. { NULL, 0}
  42. };
  43. static const st_option blank_opt[] =
  44. {
  45. { "null", BLANK_NULL},
  46. { "zero", BLANK_ZERO},
  47. { NULL, 0}
  48. };
  49. static const st_option delim_opt[] =
  50. {
  51. { "none", DELIM_NONE},
  52. { "apostrophe", DELIM_APOSTROPHE},
  53. { "quote", DELIM_QUOTE},
  54. { NULL, 0}
  55. };
  56. static const st_option form_opt[] =
  57. {
  58. { "formatted", FORM_FORMATTED},
  59. { "unformatted", FORM_UNFORMATTED},
  60. { NULL, 0}
  61. };
  62. static const st_option position_opt[] =
  63. {
  64. { "asis", POSITION_ASIS},
  65. { "rewind", POSITION_REWIND},
  66. { "append", POSITION_APPEND},
  67. { NULL, 0}
  68. };
  69. static const st_option status_opt[] =
  70. {
  71. { "unknown", STATUS_UNKNOWN},
  72. { "old", STATUS_OLD},
  73. { "new", STATUS_NEW},
  74. { "replace", STATUS_REPLACE},
  75. { "scratch", STATUS_SCRATCH},
  76. { NULL, 0}
  77. };
  78. static const st_option pad_opt[] =
  79. {
  80. { "yes", PAD_YES},
  81. { "no", PAD_NO},
  82. { NULL, 0}
  83. };
  84. static const st_option decimal_opt[] =
  85. {
  86. { "point", DECIMAL_POINT},
  87. { "comma", DECIMAL_COMMA},
  88. { NULL, 0}
  89. };
  90. static const st_option encoding_opt[] =
  91. {
  92. { "utf-8", ENCODING_UTF8},
  93. { "default", ENCODING_DEFAULT},
  94. { NULL, 0}
  95. };
  96. static const st_option round_opt[] =
  97. {
  98. { "up", ROUND_UP},
  99. { "down", ROUND_DOWN},
  100. { "zero", ROUND_ZERO},
  101. { "nearest", ROUND_NEAREST},
  102. { "compatible", ROUND_COMPATIBLE},
  103. { "processor_defined", ROUND_PROCDEFINED},
  104. { NULL, 0}
  105. };
  106. static const st_option sign_opt[] =
  107. {
  108. { "plus", SIGN_PLUS},
  109. { "suppress", SIGN_SUPPRESS},
  110. { "processor_defined", SIGN_PROCDEFINED},
  111. { NULL, 0}
  112. };
  113. static const st_option convert_opt[] =
  114. {
  115. { "native", GFC_CONVERT_NATIVE},
  116. { "swap", GFC_CONVERT_SWAP},
  117. { "big_endian", GFC_CONVERT_BIG},
  118. { "little_endian", GFC_CONVERT_LITTLE},
  119. { NULL, 0}
  120. };
  121. static const st_option async_opt[] =
  122. {
  123. { "yes", ASYNC_YES},
  124. { "no", ASYNC_NO},
  125. { NULL, 0}
  126. };
  127. /* Given a unit, test to see if the file is positioned at the terminal
  128. point, and if so, change state from NO_ENDFILE flag to AT_ENDFILE.
  129. This prevents us from changing the state from AFTER_ENDFILE to
  130. AT_ENDFILE. */
  131. static void
  132. test_endfile (gfc_unit * u)
  133. {
  134. if (u->endfile == NO_ENDFILE)
  135. {
  136. gfc_offset sz = ssize (u->s);
  137. if (sz == 0 || sz == stell (u->s))
  138. u->endfile = AT_ENDFILE;
  139. }
  140. }
  141. /* Change the modes of a file, those that are allowed * to be
  142. changed. */
  143. static void
  144. edit_modes (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
  145. {
  146. /* Complain about attempts to change the unchangeable. */
  147. if (flags->status != STATUS_UNSPECIFIED && flags->status != STATUS_OLD &&
  148. u->flags.status != flags->status)
  149. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  150. "Cannot change STATUS parameter in OPEN statement");
  151. if (flags->access != ACCESS_UNSPECIFIED && u->flags.access != flags->access)
  152. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  153. "Cannot change ACCESS parameter in OPEN statement");
  154. if (flags->form != FORM_UNSPECIFIED && u->flags.form != flags->form)
  155. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  156. "Cannot change FORM parameter in OPEN statement");
  157. if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN)
  158. && opp->recl_in != u->recl)
  159. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  160. "Cannot change RECL parameter in OPEN statement");
  161. if (flags->action != ACTION_UNSPECIFIED && u->flags.action != flags->action)
  162. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  163. "Cannot change ACTION parameter in OPEN statement");
  164. /* Status must be OLD if present. */
  165. if (flags->status != STATUS_UNSPECIFIED && flags->status != STATUS_OLD &&
  166. flags->status != STATUS_UNKNOWN)
  167. {
  168. if (flags->status == STATUS_SCRATCH)
  169. notify_std (&opp->common, GFC_STD_GNU,
  170. "OPEN statement must have a STATUS of OLD or UNKNOWN");
  171. else
  172. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  173. "OPEN statement must have a STATUS of OLD or UNKNOWN");
  174. }
  175. if (u->flags.form == FORM_UNFORMATTED)
  176. {
  177. if (flags->delim != DELIM_UNSPECIFIED)
  178. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  179. "DELIM parameter conflicts with UNFORMATTED form in "
  180. "OPEN statement");
  181. if (flags->blank != BLANK_UNSPECIFIED)
  182. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  183. "BLANK parameter conflicts with UNFORMATTED form in "
  184. "OPEN statement");
  185. if (flags->pad != PAD_UNSPECIFIED)
  186. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  187. "PAD parameter conflicts with UNFORMATTED form in "
  188. "OPEN statement");
  189. if (flags->decimal != DECIMAL_UNSPECIFIED)
  190. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  191. "DECIMAL parameter conflicts with UNFORMATTED form in "
  192. "OPEN statement");
  193. if (flags->encoding != ENCODING_UNSPECIFIED)
  194. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  195. "ENCODING parameter conflicts with UNFORMATTED form in "
  196. "OPEN statement");
  197. if (flags->round != ROUND_UNSPECIFIED)
  198. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  199. "ROUND parameter conflicts with UNFORMATTED form in "
  200. "OPEN statement");
  201. if (flags->sign != SIGN_UNSPECIFIED)
  202. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  203. "SIGN parameter conflicts with UNFORMATTED form in "
  204. "OPEN statement");
  205. }
  206. if ((opp->common.flags & IOPARM_LIBRETURN_MASK) == IOPARM_LIBRETURN_OK)
  207. {
  208. /* Change the changeable: */
  209. if (flags->blank != BLANK_UNSPECIFIED)
  210. u->flags.blank = flags->blank;
  211. if (flags->delim != DELIM_UNSPECIFIED)
  212. u->flags.delim = flags->delim;
  213. if (flags->pad != PAD_UNSPECIFIED)
  214. u->flags.pad = flags->pad;
  215. if (flags->decimal != DECIMAL_UNSPECIFIED)
  216. u->flags.decimal = flags->decimal;
  217. if (flags->encoding != ENCODING_UNSPECIFIED)
  218. u->flags.encoding = flags->encoding;
  219. if (flags->async != ASYNC_UNSPECIFIED)
  220. u->flags.async = flags->async;
  221. if (flags->round != ROUND_UNSPECIFIED)
  222. u->flags.round = flags->round;
  223. if (flags->sign != SIGN_UNSPECIFIED)
  224. u->flags.sign = flags->sign;
  225. /* Reposition the file if necessary. */
  226. switch (flags->position)
  227. {
  228. case POSITION_UNSPECIFIED:
  229. case POSITION_ASIS:
  230. break;
  231. case POSITION_REWIND:
  232. if (sseek (u->s, 0, SEEK_SET) != 0)
  233. goto seek_error;
  234. u->current_record = 0;
  235. u->last_record = 0;
  236. test_endfile (u);
  237. break;
  238. case POSITION_APPEND:
  239. if (sseek (u->s, 0, SEEK_END) < 0)
  240. goto seek_error;
  241. if (flags->access != ACCESS_STREAM)
  242. u->current_record = 0;
  243. u->endfile = AT_ENDFILE; /* We are at the end. */
  244. break;
  245. seek_error:
  246. generate_error (&opp->common, LIBERROR_OS, NULL);
  247. break;
  248. }
  249. }
  250. unlock_unit (u);
  251. }
  252. /* Open an unused unit. */
  253. gfc_unit *
  254. new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
  255. {
  256. gfc_unit *u2;
  257. stream *s;
  258. char tmpname[5 /* fort. */ + 10 /* digits of unit number */ + 1 /* 0 */];
  259. /* Change unspecifieds to defaults. Leave (flags->action ==
  260. ACTION_UNSPECIFIED) alone so open_external() can set it based on
  261. what type of open actually works. */
  262. if (flags->access == ACCESS_UNSPECIFIED)
  263. flags->access = ACCESS_SEQUENTIAL;
  264. if (flags->form == FORM_UNSPECIFIED)
  265. flags->form = (flags->access == ACCESS_SEQUENTIAL)
  266. ? FORM_FORMATTED : FORM_UNFORMATTED;
  267. if (flags->async == ASYNC_UNSPECIFIED)
  268. flags->async = ASYNC_NO;
  269. if (flags->status == STATUS_UNSPECIFIED)
  270. flags->status = STATUS_UNKNOWN;
  271. /* Checks. */
  272. if (flags->delim != DELIM_UNSPECIFIED
  273. && flags->form == FORM_UNFORMATTED)
  274. {
  275. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  276. "DELIM parameter conflicts with UNFORMATTED form in "
  277. "OPEN statement");
  278. goto fail;
  279. }
  280. if (flags->blank == BLANK_UNSPECIFIED)
  281. flags->blank = BLANK_NULL;
  282. else
  283. {
  284. if (flags->form == FORM_UNFORMATTED)
  285. {
  286. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  287. "BLANK parameter conflicts with UNFORMATTED form in "
  288. "OPEN statement");
  289. goto fail;
  290. }
  291. }
  292. if (flags->pad == PAD_UNSPECIFIED)
  293. flags->pad = PAD_YES;
  294. else
  295. {
  296. if (flags->form == FORM_UNFORMATTED)
  297. {
  298. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  299. "PAD parameter conflicts with UNFORMATTED form in "
  300. "OPEN statement");
  301. goto fail;
  302. }
  303. }
  304. if (flags->decimal == DECIMAL_UNSPECIFIED)
  305. flags->decimal = DECIMAL_POINT;
  306. else
  307. {
  308. if (flags->form == FORM_UNFORMATTED)
  309. {
  310. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  311. "DECIMAL parameter conflicts with UNFORMATTED form "
  312. "in OPEN statement");
  313. goto fail;
  314. }
  315. }
  316. if (flags->encoding == ENCODING_UNSPECIFIED)
  317. flags->encoding = ENCODING_DEFAULT;
  318. else
  319. {
  320. if (flags->form == FORM_UNFORMATTED)
  321. {
  322. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  323. "ENCODING parameter conflicts with UNFORMATTED form in "
  324. "OPEN statement");
  325. goto fail;
  326. }
  327. }
  328. /* NB: the value for ROUND when it's not specified by the user does not
  329. have to be PROCESSOR_DEFINED; the standard says that it is
  330. processor dependent, and requires that it is one of the
  331. possible value (see F2003, 9.4.5.13). */
  332. if (flags->round == ROUND_UNSPECIFIED)
  333. flags->round = ROUND_PROCDEFINED;
  334. else
  335. {
  336. if (flags->form == FORM_UNFORMATTED)
  337. {
  338. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  339. "ROUND parameter conflicts with UNFORMATTED form in "
  340. "OPEN statement");
  341. goto fail;
  342. }
  343. }
  344. if (flags->sign == SIGN_UNSPECIFIED)
  345. flags->sign = SIGN_PROCDEFINED;
  346. else
  347. {
  348. if (flags->form == FORM_UNFORMATTED)
  349. {
  350. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  351. "SIGN parameter conflicts with UNFORMATTED form in "
  352. "OPEN statement");
  353. goto fail;
  354. }
  355. }
  356. if (flags->position != POSITION_ASIS && flags->access == ACCESS_DIRECT)
  357. {
  358. generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
  359. "ACCESS parameter conflicts with SEQUENTIAL access in "
  360. "OPEN statement");
  361. goto fail;
  362. }
  363. else
  364. if (flags->position == POSITION_UNSPECIFIED)
  365. flags->position = POSITION_ASIS;
  366. if (flags->access == ACCESS_DIRECT
  367. && (opp->common.flags & IOPARM_OPEN_HAS_RECL_IN) == 0)
  368. {
  369. generate_error (&opp->common, LIBERROR_MISSING_OPTION,
  370. "Missing RECL parameter in OPEN statement");
  371. goto fail;
  372. }
  373. if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN) && opp->recl_in <= 0)
  374. {
  375. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  376. "RECL parameter is non-positive in OPEN statement");
  377. goto fail;
  378. }
  379. switch (flags->status)
  380. {
  381. case STATUS_SCRATCH:
  382. if ((opp->common.flags & IOPARM_OPEN_HAS_FILE) == 0)
  383. {
  384. opp->file = NULL;
  385. break;
  386. }
  387. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  388. "FILE parameter must not be present in OPEN statement");
  389. goto fail;
  390. case STATUS_OLD:
  391. case STATUS_NEW:
  392. case STATUS_REPLACE:
  393. case STATUS_UNKNOWN:
  394. if ((opp->common.flags & IOPARM_OPEN_HAS_FILE))
  395. break;
  396. opp->file = tmpname;
  397. opp->file_len = snprintf(opp->file, sizeof (tmpname), "fort.%d",
  398. (int) opp->common.unit);
  399. break;
  400. default:
  401. internal_error (&opp->common, "new_unit(): Bad status");
  402. }
  403. /* Make sure the file isn't already open someplace else.
  404. Do not error if opening file preconnected to stdin, stdout, stderr. */
  405. u2 = NULL;
  406. if ((opp->common.flags & IOPARM_OPEN_HAS_FILE) != 0)
  407. u2 = find_file (opp->file, opp->file_len);
  408. if (u2 != NULL
  409. && (options.stdin_unit < 0 || u2->unit_number != options.stdin_unit)
  410. && (options.stdout_unit < 0 || u2->unit_number != options.stdout_unit)
  411. && (options.stderr_unit < 0 || u2->unit_number != options.stderr_unit))
  412. {
  413. unlock_unit (u2);
  414. generate_error (&opp->common, LIBERROR_ALREADY_OPEN, NULL);
  415. goto cleanup;
  416. }
  417. if (u2 != NULL)
  418. unlock_unit (u2);
  419. /* Open file. */
  420. s = open_external (opp, flags);
  421. if (s == NULL)
  422. {
  423. char errbuf[256];
  424. char *path = fc_strdup (opp->file, opp->file_len);
  425. size_t msglen = opp->file_len + 22 + sizeof (errbuf);
  426. char *msg = xmalloc (msglen);
  427. snprintf (msg, msglen, "Cannot open file '%s': %s", path,
  428. gf_strerror (errno, errbuf, sizeof (errbuf)));
  429. generate_error (&opp->common, LIBERROR_OS, msg);
  430. free (msg);
  431. free (path);
  432. goto cleanup;
  433. }
  434. if (flags->status == STATUS_NEW || flags->status == STATUS_REPLACE)
  435. flags->status = STATUS_OLD;
  436. /* Create the unit structure. */
  437. if (u->unit_number != opp->common.unit)
  438. internal_error (&opp->common, "Unit number changed");
  439. u->s = s;
  440. u->flags = *flags;
  441. u->read_bad = 0;
  442. u->endfile = NO_ENDFILE;
  443. u->last_record = 0;
  444. u->current_record = 0;
  445. u->mode = READING;
  446. u->maxrec = 0;
  447. u->bytes_left = 0;
  448. u->saved_pos = 0;
  449. if (flags->position == POSITION_APPEND)
  450. {
  451. if (sseek (u->s, 0, SEEK_END) < 0)
  452. {
  453. generate_error (&opp->common, LIBERROR_OS, NULL);
  454. goto cleanup;
  455. }
  456. u->endfile = AT_ENDFILE;
  457. }
  458. /* Unspecified recl ends up with a processor dependent value. */
  459. if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN))
  460. {
  461. u->flags.has_recl = 1;
  462. u->recl = opp->recl_in;
  463. u->recl_subrecord = u->recl;
  464. u->bytes_left = u->recl;
  465. }
  466. else
  467. {
  468. u->flags.has_recl = 0;
  469. u->recl = max_offset;
  470. if (compile_options.max_subrecord_length)
  471. {
  472. u->recl_subrecord = compile_options.max_subrecord_length;
  473. }
  474. else
  475. {
  476. switch (compile_options.record_marker)
  477. {
  478. case 0:
  479. /* Fall through */
  480. case sizeof (GFC_INTEGER_4):
  481. u->recl_subrecord = GFC_MAX_SUBRECORD_LENGTH;
  482. break;
  483. case sizeof (GFC_INTEGER_8):
  484. u->recl_subrecord = max_offset - 16;
  485. break;
  486. default:
  487. runtime_error ("Illegal value for record marker");
  488. break;
  489. }
  490. }
  491. }
  492. /* If the file is direct access, calculate the maximum record number
  493. via a division now instead of letting the multiplication overflow
  494. later. */
  495. if (flags->access == ACCESS_DIRECT)
  496. u->maxrec = max_offset / u->recl;
  497. if (flags->access == ACCESS_STREAM)
  498. {
  499. u->maxrec = max_offset;
  500. u->recl = 1;
  501. u->bytes_left = 1;
  502. u->strm_pos = stell (u->s) + 1;
  503. }
  504. u->filename = fc_strdup (opp->file, opp->file_len);
  505. /* Curiously, the standard requires that the
  506. position specifier be ignored for new files so a newly connected
  507. file starts out at the initial point. We still need to figure
  508. out if the file is at the end or not. */
  509. test_endfile (u);
  510. if (flags->status == STATUS_SCRATCH && opp->file != NULL)
  511. free (opp->file);
  512. if (flags->form == FORM_FORMATTED)
  513. {
  514. if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN))
  515. fbuf_init (u, u->recl);
  516. else
  517. fbuf_init (u, 0);
  518. }
  519. else
  520. u->fbuf = NULL;
  521. return u;
  522. cleanup:
  523. /* Free memory associated with a temporary filename. */
  524. if (flags->status == STATUS_SCRATCH && opp->file != NULL)
  525. free (opp->file);
  526. fail:
  527. close_unit (u);
  528. return NULL;
  529. }
  530. /* Open a unit which is already open. This involves changing the
  531. modes or closing what is there now and opening the new file. */
  532. static void
  533. already_open (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
  534. {
  535. if ((opp->common.flags & IOPARM_OPEN_HAS_FILE) == 0)
  536. {
  537. edit_modes (opp, u, flags);
  538. return;
  539. }
  540. /* If the file is connected to something else, close it and open a
  541. new unit. */
  542. if (!compare_file_filename (u, opp->file, opp->file_len))
  543. {
  544. if (sclose (u->s) == -1)
  545. {
  546. unlock_unit (u);
  547. generate_error (&opp->common, LIBERROR_OS,
  548. "Error closing file in OPEN statement");
  549. return;
  550. }
  551. u->s = NULL;
  552. #if !HAVE_UNLINK_OPEN_FILE
  553. if (u->filename && u->flags.status == STATUS_SCRATCH)
  554. unlink (u->filename);
  555. #endif
  556. free (u->filename);
  557. u->filename = NULL;
  558. u = new_unit (opp, u, flags);
  559. if (u != NULL)
  560. unlock_unit (u);
  561. return;
  562. }
  563. edit_modes (opp, u, flags);
  564. }
  565. /* Open file. */
  566. extern void st_open (st_parameter_open *opp);
  567. export_proto(st_open);
  568. void
  569. st_open (st_parameter_open *opp)
  570. {
  571. unit_flags flags;
  572. gfc_unit *u = NULL;
  573. GFC_INTEGER_4 cf = opp->common.flags;
  574. unit_convert conv;
  575. library_start (&opp->common);
  576. /* Decode options. */
  577. flags.access = !(cf & IOPARM_OPEN_HAS_ACCESS) ? ACCESS_UNSPECIFIED :
  578. find_option (&opp->common, opp->access, opp->access_len,
  579. access_opt, "Bad ACCESS parameter in OPEN statement");
  580. flags.action = !(cf & IOPARM_OPEN_HAS_ACTION) ? ACTION_UNSPECIFIED :
  581. find_option (&opp->common, opp->action, opp->action_len,
  582. action_opt, "Bad ACTION parameter in OPEN statement");
  583. flags.blank = !(cf & IOPARM_OPEN_HAS_BLANK) ? BLANK_UNSPECIFIED :
  584. find_option (&opp->common, opp->blank, opp->blank_len,
  585. blank_opt, "Bad BLANK parameter in OPEN statement");
  586. flags.delim = !(cf & IOPARM_OPEN_HAS_DELIM) ? DELIM_UNSPECIFIED :
  587. find_option (&opp->common, opp->delim, opp->delim_len,
  588. delim_opt, "Bad DELIM parameter in OPEN statement");
  589. flags.pad = !(cf & IOPARM_OPEN_HAS_PAD) ? PAD_UNSPECIFIED :
  590. find_option (&opp->common, opp->pad, opp->pad_len,
  591. pad_opt, "Bad PAD parameter in OPEN statement");
  592. flags.decimal = !(cf & IOPARM_OPEN_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
  593. find_option (&opp->common, opp->decimal, opp->decimal_len,
  594. decimal_opt, "Bad DECIMAL parameter in OPEN statement");
  595. flags.encoding = !(cf & IOPARM_OPEN_HAS_ENCODING) ? ENCODING_UNSPECIFIED :
  596. find_option (&opp->common, opp->encoding, opp->encoding_len,
  597. encoding_opt, "Bad ENCODING parameter in OPEN statement");
  598. flags.async = !(cf & IOPARM_OPEN_HAS_ASYNCHRONOUS) ? ASYNC_UNSPECIFIED :
  599. find_option (&opp->common, opp->asynchronous, opp->asynchronous_len,
  600. async_opt, "Bad ASYNCHRONOUS parameter in OPEN statement");
  601. flags.round = !(cf & IOPARM_OPEN_HAS_ROUND) ? ROUND_UNSPECIFIED :
  602. find_option (&opp->common, opp->round, opp->round_len,
  603. round_opt, "Bad ROUND parameter in OPEN statement");
  604. flags.sign = !(cf & IOPARM_OPEN_HAS_SIGN) ? SIGN_UNSPECIFIED :
  605. find_option (&opp->common, opp->sign, opp->sign_len,
  606. sign_opt, "Bad SIGN parameter in OPEN statement");
  607. flags.form = !(cf & IOPARM_OPEN_HAS_FORM) ? FORM_UNSPECIFIED :
  608. find_option (&opp->common, opp->form, opp->form_len,
  609. form_opt, "Bad FORM parameter in OPEN statement");
  610. flags.position = !(cf & IOPARM_OPEN_HAS_POSITION) ? POSITION_UNSPECIFIED :
  611. find_option (&opp->common, opp->position, opp->position_len,
  612. position_opt, "Bad POSITION parameter in OPEN statement");
  613. flags.status = !(cf & IOPARM_OPEN_HAS_STATUS) ? STATUS_UNSPECIFIED :
  614. find_option (&opp->common, opp->status, opp->status_len,
  615. status_opt, "Bad STATUS parameter in OPEN statement");
  616. /* First, we check wether the convert flag has been set via environment
  617. variable. This overrides the convert tag in the open statement. */
  618. conv = get_unformatted_convert (opp->common.unit);
  619. if (conv == GFC_CONVERT_NONE)
  620. {
  621. /* Nothing has been set by environment variable, check the convert tag. */
  622. if (cf & IOPARM_OPEN_HAS_CONVERT)
  623. conv = find_option (&opp->common, opp->convert, opp->convert_len,
  624. convert_opt,
  625. "Bad CONVERT parameter in OPEN statement");
  626. else
  627. conv = compile_options.convert;
  628. }
  629. /* We use big_endian, which is 0 on little-endian machines
  630. and 1 on big-endian machines. */
  631. switch (conv)
  632. {
  633. case GFC_CONVERT_NATIVE:
  634. case GFC_CONVERT_SWAP:
  635. break;
  636. case GFC_CONVERT_BIG:
  637. conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
  638. break;
  639. case GFC_CONVERT_LITTLE:
  640. conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
  641. break;
  642. default:
  643. internal_error (&opp->common, "Illegal value for CONVERT");
  644. break;
  645. }
  646. flags.convert = conv;
  647. if (flags.position != POSITION_UNSPECIFIED
  648. && flags.access == ACCESS_DIRECT)
  649. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  650. "Cannot use POSITION with direct access files");
  651. if (flags.access == ACCESS_APPEND)
  652. {
  653. if (flags.position != POSITION_UNSPECIFIED
  654. && flags.position != POSITION_APPEND)
  655. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  656. "Conflicting ACCESS and POSITION flags in"
  657. " OPEN statement");
  658. notify_std (&opp->common, GFC_STD_GNU,
  659. "Extension: APPEND as a value for ACCESS in OPEN statement");
  660. flags.access = ACCESS_SEQUENTIAL;
  661. flags.position = POSITION_APPEND;
  662. }
  663. if (flags.position == POSITION_UNSPECIFIED)
  664. flags.position = POSITION_ASIS;
  665. if ((opp->common.flags & IOPARM_LIBRETURN_MASK) == IOPARM_LIBRETURN_OK)
  666. {
  667. if ((opp->common.flags & IOPARM_OPEN_HAS_NEWUNIT))
  668. opp->common.unit = get_unique_unit_number(opp);
  669. else if (opp->common.unit < 0)
  670. {
  671. u = find_unit (opp->common.unit);
  672. if (u == NULL) /* Negative unit and no NEWUNIT-created unit found. */
  673. {
  674. generate_error (&opp->common, LIBERROR_BAD_OPTION,
  675. "Bad unit number in OPEN statement");
  676. library_end ();
  677. return;
  678. }
  679. }
  680. if (u == NULL)
  681. u = find_or_create_unit (opp->common.unit);
  682. if (u->s == NULL)
  683. {
  684. u = new_unit (opp, u, &flags);
  685. if (u != NULL)
  686. unlock_unit (u);
  687. }
  688. else
  689. already_open (opp, u, &flags);
  690. }
  691. if ((opp->common.flags & IOPARM_OPEN_HAS_NEWUNIT)
  692. && (opp->common.flags & IOPARM_LIBRETURN_MASK) == IOPARM_LIBRETURN_OK)
  693. *opp->newunit = opp->common.unit;
  694. library_end ();
  695. }