1 /****************************************************************
2 Copyright 1990 - 1997 by AT&T, Lucent Technologies and Bellcore.
3 
4 Permission to use, copy, modify, and distribute this software
5 and its documentation for any purpose and without fee is hereby
6 granted, provided that the above copyright notice appear in all
7 copies and that both that the copyright notice and this
8 permission notice and warranty disclaimer appear in supporting
9 documentation, and that the names of AT&T, Bell Laboratories,
10 Lucent or Bellcore or any of their entities not be used in
11 advertising or publicity pertaining to distribution of the
12 software without specific, written prior permission.
13 
14 AT&T, Lucent and Bellcore disclaim all warranties with regard to
15 this software, including all implied warranties of
16 merchantability and fitness.  In no event shall AT&T, Lucent or
17 Bellcore be liable for any special, indirect or consequential
18 damages or any damages whatsoever resulting from loss of use,
19 data or profits, whether in an action of contract, negligence or
20 other tortious action, arising out of or in connection with the
21 use or performance of this software.
22 ****************************************************************/
23 
24 #include <f2c_config.h>
25 #include "f2c.h"
26 #include "fio.h"
27 
28 #undef abs
29 #undef min
30 #undef max
31 #include <stdlib.h>
32 #if defined(NON_UNIX_STDIO) || defined(_MSC_VER) || defined(__MINGW32__)
33 # include <stdio.h>
34 # define unlink remove
35 #else
36 # include <unistd.h>
37 #endif
38 
f_clos(cllist * a)39 integer f_clos(cllist *a)
40 {
41 	unit *b;
42 
43 	if(a->cunit >= MXUNIT) return(0);
44 	b= &f__units[a->cunit];
45 	if(b->ufd==NULL)
46 		goto done;
47 	if (b->uscrtch == 1)
48 		goto Delete;
49 	if (!a->csta)
50 		goto Keep;
51 	switch(*a->csta) {
52 		default:
53 	 	Keep:
54 		case 'k':
55 		case 'K':
56 			if(b->uwrt == 1)
57 				t_runc((alist *)a);
58 			if(b->ufnm) {
59 				fclose(b->ufd);
60 				free(b->ufnm);
61 				}
62 			break;
63 		case 'd':
64 		case 'D':
65 		Delete:
66 			fclose(b->ufd);
67 			if(b->ufnm) {
68 				unlink(b->ufnm); /*SYSDEP*/
69 				free(b->ufnm);
70 				}
71 		}
72 	b->ufd=NULL;
73  done:
74 	b->uend=0;
75 	b->ufnm=NULL;
76 	return(0);
77 }
78 
79 void
f_exit(void)80 f_exit(void)
81 {
82 	static int run = 0;
83 	int i;
84 	static cllist xx;
85 	/* Do not execute f_exit() twice */
86 	if (run)
87 		return;
88 	run = 1;
89 	if (!xx.cerr) {
90 		xx.cerr=1;
91 		xx.csta=NULL;
92 		for(i=0;i<MXUNIT;i++)
93 		{
94 			xx.cunit=i;
95 			(void) f_clos(&xx);
96 		}
97 	}
98 }
99 
flush_(void)100 int flush_(void)
101 {	int i;
102 	for(i=0;i<MXUNIT;i++)
103 		if(f__units[i].ufd != NULL && f__units[i].uwrt)
104 			fflush(f__units[i].ufd);
105 return 0;
106 }
107