Fix aarch64_evpc_tbl guard (PR 85910)
[official-gcc.git] / libgfortran / io / close.c
blobcec7a0848625274540ff974c49bae2a93a4efc45
1 /* Copyright (C) 2002-2018 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "io.h"
26 #include "unix.h"
27 #include "async.h"
28 #include <limits.h>
30 typedef enum
31 { CLOSE_DELETE, CLOSE_KEEP, CLOSE_UNSPECIFIED }
32 close_status;
34 static const st_option status_opt[] = {
35 {"keep", CLOSE_KEEP},
36 {"delete", CLOSE_DELETE},
37 {NULL, 0}
41 extern void st_close (st_parameter_close *);
42 export_proto(st_close);
44 void
45 st_close (st_parameter_close *clp)
47 close_status status;
48 gfc_unit *u;
49 #if !HAVE_UNLINK_OPEN_FILE
50 char *path;
52 path = NULL;
53 #endif
55 library_start (&clp->common);
57 status = !(clp->common.flags & IOPARM_CLOSE_HAS_STATUS) ? CLOSE_UNSPECIFIED :
58 find_option (&clp->common, clp->status, clp->status_len,
59 status_opt, "Bad STATUS parameter in CLOSE statement");
61 u = find_unit (clp->common.unit);
63 if (ASYNC_IO && u && u->au)
64 if (async_wait (&(clp->common), u->au))
66 library_end ();
67 return;
70 if ((clp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
72 library_end ();
73 return;
76 if (u != NULL)
78 if (close_share (u) < 0)
79 generate_error (&clp->common, LIBERROR_OS, "Problem in CLOSE");
80 if (u->flags.status == STATUS_SCRATCH)
82 if (status == CLOSE_KEEP)
83 generate_error (&clp->common, LIBERROR_BAD_OPTION,
84 "Can't KEEP a scratch file on CLOSE");
85 #if !HAVE_UNLINK_OPEN_FILE
86 path = strdup (u->filename);
87 #endif
89 else
91 if (status == CLOSE_DELETE)
93 if (u->flags.readonly)
94 generate_warning (&clp->common, "STATUS set to DELETE on CLOSE"
95 " but file protected by READONLY specifier");
96 else
98 #if HAVE_UNLINK_OPEN_FILE
99 remove (u->filename);
100 #else
101 path = strdup (u->filename);
102 #endif
107 close_unit (u);
109 #if !HAVE_UNLINK_OPEN_FILE
110 if (path != NULL)
112 remove (path);
113 free (path);
115 #endif
118 /* CLOSE on unconnected unit is legal and a no-op: F95 std., 9.3.5. */
119 library_end ();