]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/io/close.c
Update copyright years.
[thirdparty/gcc.git] / libgfortran / io / close.c
CommitLineData
99dee823 1/* Copyright (C) 2002-2021 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
cdc34b50 34{ CLOSE_INVALID = - 1, CLOSE_DELETE, CLOSE_KEEP, CLOSE_UNSPECIFIED }
6de9cd9a
DN
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
cdc34b50
TK
64 if (status == CLOSE_INVALID)
65 {
66 library_end ();
67 return;
68 }
69
2b4c9065
NK
70 u = find_unit (clp->common.unit);
71
72 if (ASYNC_IO && u && u->au)
73 if (async_wait (&(clp->common), u->au))
74 {
75 library_end ();
76 return;
77 }
78
5e805e44 79 if ((clp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
e0fd73d4
FXC
80 {
81 library_end ();
6de9cd9a 82 return;
e0fd73d4 83 }
6de9cd9a 84
6de9cd9a
DN
85 if (u != NULL)
86 {
0ef33d44
FR
87 if (close_share (u) < 0)
88 generate_error (&clp->common, LIBERROR_OS, "Problem in CLOSE");
6de9cd9a
DN
89 if (u->flags.status == STATUS_SCRATCH)
90 {
91 if (status == CLOSE_KEEP)
d74b97cc 92 generate_error (&clp->common, LIBERROR_BAD_OPTION,
6de9cd9a 93 "Can't KEEP a scratch file on CLOSE");
10c682a0 94#if !HAVE_UNLINK_OPEN_FILE
0e05c303 95 path = strdup (u->filename);
10c682a0 96#endif
6de9cd9a
DN
97 }
98 else
99 {
100 if (status == CLOSE_DELETE)
0ef33d44
FR
101 {
102 if (u->flags.readonly)
103 generate_warning (&clp->common, "STATUS set to DELETE on CLOSE"
104 " but file protected by READONLY specifier");
105 else
106 {
10c682a0 107#if HAVE_UNLINK_OPEN_FILE
0e34715e
JD
108
109 if (remove (u->filename))
110 generate_error (&clp->common, LIBERROR_OS,
9faf6e70 111 "File cannot be deleted");
10c682a0 112#else
0ef33d44 113 path = strdup (u->filename);
10c682a0 114#endif
0ef33d44
FR
115 }
116 }
6de9cd9a
DN
117 }
118
119 close_unit (u);
10c682a0
FXC
120
121#if !HAVE_UNLINK_OPEN_FILE
122 if (path != NULL)
4269f19c 123 {
2ee43ae6 124 if (remove (path))
0e34715e 125 generate_error (&clp->common, LIBERROR_OS,
9faf6e70 126 "File cannot be deleted");
4269f19c
JB
127 free (path);
128 }
10c682a0 129#endif
6de9cd9a 130 }
b8d403b4
JD
131
132 /* CLOSE on unconnected unit is legal and a no-op: F95 std., 9.3.5. */
6de9cd9a
DN
133 library_end ();
134}