]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/io/close.c
x86: Default CET run-time support to auto
[thirdparty/gcc.git] / libgfortran / io / close.c
CommitLineData
8d9254fc 1/* Copyright (C) 2002-2020 Free Software Foundation, Inc.
6de9cd9a
DN
2 Contributed by Andy Vaught
3
4This file is part of the GNU Fortran 95 runtime library (libgfortran).
5
6Libgfortran is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
748086b7 8the Free Software Foundation; either version 3, or (at your option)
6de9cd9a
DN
9any later version.
10
11Libgfortran is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
748086b7
JJ
16Under Section 7 of GPL version 3, you are granted additional
17permissions described in the GCC Runtime Library Exception, version
183.1, as published by the Free Software Foundation.
19
20You should have received a copy of the GNU General Public License and
21a copy of the GCC Runtime Library Exception along with this program;
22see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23<http://www.gnu.org/licenses/>. */
6de9cd9a 24
6de9cd9a 25#include "io.h"
92cbdb68 26#include "unix.h"
2b4c9065 27#include "async.h"
10c682a0 28#include <limits.h>
ff504cc2
GP
29#if !HAVE_UNLINK_OPEN_FILE
30#include <string.h>
31#endif
6de9cd9a
DN
32
33typedef enum
34{ CLOSE_DELETE, CLOSE_KEEP, CLOSE_UNSPECIFIED }
35close_status;
36
09003779 37static const st_option status_opt[] = {
6de9cd9a
DN
38 {"keep", CLOSE_KEEP},
39 {"delete", CLOSE_DELETE},
4b6903ec 40 {NULL, 0}
6de9cd9a
DN
41};
42
43
5e805e44 44extern void st_close (st_parameter_close *);
7d7b8bfe
RH
45export_proto(st_close);
46
6de9cd9a 47void
5e805e44 48st_close (st_parameter_close *clp)
6de9cd9a
DN
49{
50 close_status status;
909087e0 51 gfc_unit *u;
10c682a0 52#if !HAVE_UNLINK_OPEN_FILE
f29876bb 53 char *path;
10c682a0
FXC
54
55 path = NULL;
56#endif
6de9cd9a 57
5e805e44 58 library_start (&clp->common);
6de9cd9a 59
5e805e44
JJ
60 status = !(clp->common.flags & IOPARM_CLOSE_HAS_STATUS) ? CLOSE_UNSPECIFIED :
61 find_option (&clp->common, clp->status, clp->status_len,
62 status_opt, "Bad STATUS parameter in CLOSE statement");
6de9cd9a 63
2b4c9065
NK
64 u = find_unit (clp->common.unit);
65
66 if (ASYNC_IO && u && u->au)
67 if (async_wait (&(clp->common), u->au))
68 {
69 library_end ();
70 return;
71 }
72
5e805e44 73 if ((clp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
e0fd73d4
FXC
74 {
75 library_end ();
6de9cd9a 76 return;
e0fd73d4 77 }
6de9cd9a 78
6de9cd9a
DN
79 if (u != NULL)
80 {
0ef33d44
FR
81 if (close_share (u) < 0)
82 generate_error (&clp->common, LIBERROR_OS, "Problem in CLOSE");
6de9cd9a
DN
83 if (u->flags.status == STATUS_SCRATCH)
84 {
85 if (status == CLOSE_KEEP)
d74b97cc 86 generate_error (&clp->common, LIBERROR_BAD_OPTION,
6de9cd9a 87 "Can't KEEP a scratch file on CLOSE");
10c682a0 88#if !HAVE_UNLINK_OPEN_FILE
0e05c303 89 path = strdup (u->filename);
10c682a0 90#endif
6de9cd9a
DN
91 }
92 else
93 {
94 if (status == CLOSE_DELETE)
0ef33d44
FR
95 {
96 if (u->flags.readonly)
97 generate_warning (&clp->common, "STATUS set to DELETE on CLOSE"
98 " but file protected by READONLY specifier");
99 else
100 {
10c682a0 101#if HAVE_UNLINK_OPEN_FILE
0e34715e
JD
102
103 if (remove (u->filename))
104 generate_error (&clp->common, LIBERROR_OS,
9faf6e70 105 "File cannot be deleted");
10c682a0 106#else
0ef33d44 107 path = strdup (u->filename);
10c682a0 108#endif
0ef33d44
FR
109 }
110 }
6de9cd9a
DN
111 }
112
113 close_unit (u);
10c682a0
FXC
114
115#if !HAVE_UNLINK_OPEN_FILE
116 if (path != NULL)
4269f19c 117 {
2ee43ae6 118 if (remove (path))
0e34715e 119 generate_error (&clp->common, LIBERROR_OS,
9faf6e70 120 "File cannot be deleted");
4269f19c
JB
121 free (path);
122 }
10c682a0 123#endif
6de9cd9a 124 }
b8d403b4
JD
125
126 /* CLOSE on unconnected unit is legal and a no-op: F95 std., 9.3.5. */
6de9cd9a
DN
127 library_end ();
128}