1 /* Copyright (C) 2002-2018 Free Software Foundation, Inc.
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
8 the Free Software Foundation; either version 3, or (at your option)
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 
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/>.  */
24 
25 #include "io.h"
26 #include "unix.h"
27 #include <limits.h>
28 #if !HAVE_UNLINK_OPEN_FILE
29 #include <string.h>
30 #endif
31 
32 typedef enum
33 { CLOSE_DELETE, CLOSE_KEEP, CLOSE_UNSPECIFIED }
34 close_status;
35 
36 static const st_option status_opt[] = {
37   {"keep", CLOSE_KEEP},
38   {"delete", CLOSE_DELETE},
39   {NULL, 0}
40 };
41 
42 
43 extern void st_close (st_parameter_close *);
44 export_proto(st_close);
45 
46 void
st_close(st_parameter_close * clp)47 st_close (st_parameter_close *clp)
48 {
49   close_status status;
50   gfc_unit *u;
51 #if !HAVE_UNLINK_OPEN_FILE
52   char *path;
53 
54   path = NULL;
55 #endif
56 
57   library_start (&clp->common);
58 
59   status = !(clp->common.flags & IOPARM_CLOSE_HAS_STATUS) ? CLOSE_UNSPECIFIED :
60     find_option (&clp->common, clp->status, clp->status_len,
61 		 status_opt, "Bad STATUS parameter in CLOSE statement");
62 
63   if ((clp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
64   {
65     library_end ();
66     return;
67   }
68 
69   u = find_unit (clp->common.unit);
70   if (u != NULL)
71     {
72       if (close_share (u) < 0)
73 	generate_error (&clp->common, LIBERROR_OS, "Problem in CLOSE");
74       if (u->flags.status == STATUS_SCRATCH)
75 	{
76 	  if (status == CLOSE_KEEP)
77 	    generate_error (&clp->common, LIBERROR_BAD_OPTION,
78 			    "Can't KEEP a scratch file on CLOSE");
79 #if !HAVE_UNLINK_OPEN_FILE
80 	  path = strdup (u->filename);
81 #endif
82 	}
83       else
84 	{
85 	  if (status == CLOSE_DELETE)
86 	    {
87 	      if (u->flags.readonly)
88 		generate_warning (&clp->common, "STATUS set to DELETE on CLOSE"
89 				  " but file protected by READONLY specifier");
90 	      else
91 		{
92 #if HAVE_UNLINK_OPEN_FILE
93 
94 		  if (remove (u->filename))
95 		    generate_error (&clp->common, LIBERROR_OS,
96 				    "File cannot be deleted");
97 #else
98 		  path = strdup (u->filename);
99 #endif
100 		}
101 	    }
102 	}
103 
104       close_unit (u);
105 
106 #if !HAVE_UNLINK_OPEN_FILE
107       if (path != NULL)
108 	{
109 	  if (remove (path))
110 	    generate_error (&clp->common, LIBERROR_OS,
111 			    "File cannot be deleted");
112 	  free (path);
113 	}
114 #endif
115     }
116 
117   /* CLOSE on unconnected unit is legal and a no-op: F95 std., 9.3.5. */
118   library_end ();
119 }
120