]>
Commit | Line | Data |
---|---|---|
f1717362 | 1 | /* Copyright (C) 2002-2016 Free Software Foundation, Inc. |
4ee9c684 | 2 | Contributed by Andy Vaught |
3 | ||
4 | This file is part of the GNU Fortran 95 runtime library (libgfortran). | |
5 | ||
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 | |
6bc9506f | 8 | the Free Software Foundation; either version 3, or (at your option) |
4ee9c684 | 9 | any later version. |
10 | ||
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. | |
15 | ||
6bc9506f | 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. | |
19 | ||
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/>. */ | |
4ee9c684 | 24 | |
4ee9c684 | 25 | #include "io.h" |
f65f6629 | 26 | #include "unix.h" |
1dc95e51 | 27 | #include <limits.h> |
4ee9c684 | 28 | |
29 | typedef enum | |
30 | { CLOSE_DELETE, CLOSE_KEEP, CLOSE_UNSPECIFIED } | |
31 | close_status; | |
32 | ||
fb35179a | 33 | static const st_option status_opt[] = { |
4ee9c684 | 34 | {"keep", CLOSE_KEEP}, |
35 | {"delete", CLOSE_DELETE}, | |
dddf4412 | 36 | {NULL, 0} |
4ee9c684 | 37 | }; |
38 | ||
39 | ||
60c514ba | 40 | extern void st_close (st_parameter_close *); |
7b6cb5bd | 41 | export_proto(st_close); |
42 | ||
4ee9c684 | 43 | void |
60c514ba | 44 | st_close (st_parameter_close *clp) |
4ee9c684 | 45 | { |
46 | close_status status; | |
f02dd226 | 47 | gfc_unit *u; |
1dc95e51 | 48 | #if !HAVE_UNLINK_OPEN_FILE |
49 | char * path; | |
50 | ||
51 | path = NULL; | |
52 | #endif | |
4ee9c684 | 53 | |
60c514ba | 54 | library_start (&clp->common); |
4ee9c684 | 55 | |
60c514ba | 56 | status = !(clp->common.flags & IOPARM_CLOSE_HAS_STATUS) ? CLOSE_UNSPECIFIED : |
57 | find_option (&clp->common, clp->status, clp->status_len, | |
58 | status_opt, "Bad STATUS parameter in CLOSE statement"); | |
4ee9c684 | 59 | |
60c514ba | 60 | if ((clp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
9d54665b | 61 | { |
62 | library_end (); | |
4ee9c684 | 63 | return; |
9d54665b | 64 | } |
4ee9c684 | 65 | |
60c514ba | 66 | u = find_unit (clp->common.unit); |
4ee9c684 | 67 | if (u != NULL) |
68 | { | |
69 | if (u->flags.status == STATUS_SCRATCH) | |
70 | { | |
71 | if (status == CLOSE_KEEP) | |
18f0b7df | 72 | generate_error (&clp->common, LIBERROR_BAD_OPTION, |
4ee9c684 | 73 | "Can't KEEP a scratch file on CLOSE"); |
1dc95e51 | 74 | #if !HAVE_UNLINK_OPEN_FILE |
8d832ee4 | 75 | path = strdup (u->filename); |
1dc95e51 | 76 | #endif |
4ee9c684 | 77 | } |
78 | else | |
79 | { | |
80 | if (status == CLOSE_DELETE) | |
1dc95e51 | 81 | { |
82 | #if HAVE_UNLINK_OPEN_FILE | |
eb3e44d2 | 83 | remove (u->filename); |
1dc95e51 | 84 | #else |
8d832ee4 | 85 | path = strdup (u->filename); |
1dc95e51 | 86 | #endif |
87 | } | |
4ee9c684 | 88 | } |
89 | ||
90 | close_unit (u); | |
1dc95e51 | 91 | |
92 | #if !HAVE_UNLINK_OPEN_FILE | |
93 | if (path != NULL) | |
fc3d374a | 94 | { |
eb3e44d2 | 95 | remove (path); |
fc3d374a | 96 | free (path); |
97 | } | |
1dc95e51 | 98 | #endif |
4ee9c684 | 99 | } |
39d37bf0 | 100 | |
101 | /* CLOSE on unconnected unit is legal and a no-op: F95 std., 9.3.5. */ | |
4ee9c684 | 102 | library_end (); |
103 | } |