close.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught
  3. This file is part of the GNU Fortran 95 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 "unix.h"
  21. #include <limits.h>
  22. typedef enum
  23. { CLOSE_DELETE, CLOSE_KEEP, CLOSE_UNSPECIFIED }
  24. close_status;
  25. static const st_option status_opt[] = {
  26. {"keep", CLOSE_KEEP},
  27. {"delete", CLOSE_DELETE},
  28. {NULL, 0}
  29. };
  30. extern void st_close (st_parameter_close *);
  31. export_proto(st_close);
  32. void
  33. st_close (st_parameter_close *clp)
  34. {
  35. close_status status;
  36. gfc_unit *u;
  37. #if !HAVE_UNLINK_OPEN_FILE
  38. char * path;
  39. path = NULL;
  40. #endif
  41. library_start (&clp->common);
  42. status = !(clp->common.flags & IOPARM_CLOSE_HAS_STATUS) ? CLOSE_UNSPECIFIED :
  43. find_option (&clp->common, clp->status, clp->status_len,
  44. status_opt, "Bad STATUS parameter in CLOSE statement");
  45. if ((clp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
  46. {
  47. library_end ();
  48. return;
  49. }
  50. u = find_unit (clp->common.unit);
  51. if (u != NULL)
  52. {
  53. if (u->flags.status == STATUS_SCRATCH)
  54. {
  55. if (status == CLOSE_KEEP)
  56. generate_error (&clp->common, LIBERROR_BAD_OPTION,
  57. "Can't KEEP a scratch file on CLOSE");
  58. #if !HAVE_UNLINK_OPEN_FILE
  59. path = strdup (u->filename);
  60. #endif
  61. }
  62. else
  63. {
  64. if (status == CLOSE_DELETE)
  65. {
  66. #if HAVE_UNLINK_OPEN_FILE
  67. delete_file (u);
  68. #else
  69. path = strdup (u->filename);
  70. #endif
  71. }
  72. }
  73. close_unit (u);
  74. #if !HAVE_UNLINK_OPEN_FILE
  75. if (path != NULL)
  76. {
  77. unlink (path);
  78. free (path);
  79. }
  80. #endif
  81. }
  82. /* CLOSE on unconnected unit is legal and a no-op: F95 std., 9.3.5. */
  83. library_end ();
  84. }