1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#include "sfhdr.h"
/* Close a stream. A file stream is synced before closing.
**
** Written by Kiem-Phong Vo
*/
#if __STD_C
int sfclose(Sfio_t* f)
#else
int sfclose(f)
Sfio_t* f;
#endif
{
reg int local, ex, rv;
Void_t* data = NIL(Void_t*);
SFMTXDECL(f); /* declare a local stream variable for multithreading */
SFMTXENTER(f, -1);
GETLOCAL(f,local);
if(!(f->mode&SF_INIT) &&
SFMODE(f,local) != (f->mode&SF_RDWR) &&
SFMODE(f,local) != (f->mode&(SF_READ|SF_SYNCED)) &&
_sfmode(f,SF_SYNCED,local) < 0)
SFMTXRETURN(f,-1);
/* closing a stack of streams */
while(f->push)
{ reg Sfio_t* pop;
if(!(pop = (*_Sfstack)(f,NIL(Sfio_t*))) )
SFMTXRETURN(f,-1);
if(sfclose(pop) < 0)
{ (*_Sfstack)(f,pop);
SFMTXRETURN(f,-1);
}
}
rv = 0;
if(f->disc == _Sfudisc) /* closing the ungetc stream */
f->disc = NIL(Sfdisc_t*);
else if(f->file >= 0) /* sync file pointer */
{ f->bits |= SF_ENDING;
rv = sfsync(f);
}
SFLOCK(f,0);
/* raise discipline exceptions */
if(f->disc && (ex = SFRAISE(f,local ? SF_NEW : SF_CLOSING,NIL(Void_t*))) != 0)
SFMTXRETURN(f,ex);
if(!local && f->pool)
{ /* remove from pool */
if(f->pool == &_Sfpool)
{ reg int n;
POOLMTXLOCK(&_Sfpool);
for(n = 0; n < _Sfpool.n_sf; ++n)
{ if(_Sfpool.sf[n] != f)
continue;
/* found it */
_Sfpool.n_sf -= 1;
for(; n < _Sfpool.n_sf; ++n)
_Sfpool.sf[n] = _Sfpool.sf[n+1];
break;
}
POOLMTXUNLOCK(&_Sfpool);
}
else
{ f->mode &= ~SF_LOCK; /**/ASSERT(_Sfpmove);
if((*_Sfpmove)(f,-1) < 0)
{ SFOPEN(f,0);
SFMTXRETURN(f,-1);
}
f->mode |= SF_LOCK;
}
f->pool = NIL(Sfpool_t*);
}
if(f->data && (!local || (f->flags&SF_STRING) || (f->bits&SF_MMAP) ) )
{ /* free buffer */
#ifdef MAP_TYPE
if(f->bits&SF_MMAP)
SFMUNMAP(f,f->data,f->endb-f->data);
else
#endif
if(f->flags&SF_MALLOC)
data = (Void_t*)f->data;
f->data = NIL(uchar*);
f->size = -1;
}
/* zap the file descriptor */
if(_Sfnotify)
(*_Sfnotify)(f, SF_CLOSING, (void*)((long)f->file));
if(f->file >= 0 && !(f->flags&SF_STRING))
{ while(sysclosef(f->file) < 0 )
{ if(errno == EINTR)
errno = 0;
else
{ rv = -1;
break;
}
}
}
f->file = -1;
SFKILL(f);
f->flags &= SF_STATIC;
f->here = 0;
f->extent = -1;
f->endb = f->endr = f->endw = f->next = f->data;
/* zap any associated auxiliary buffer */
if(f->rsrv)
{ free(f->rsrv);
f->rsrv = NIL(Sfrsrv_t*);
}
/* delete any associated sfpopen-data */
if(f->proc && _sfpclose(f) < 0 )
rv = -1;
/* destroy the mutex */
if(f->mutex)
{ (void)vtmtxclrlock(f->mutex);
if(f != sfstdin && f != sfstdout && f != sfstderr)
{ (void)vtmtxclose(f->mutex);
f->mutex = NIL(Vtmutex_t*);
}
}
if(!local)
{ if(f->disc && (ex = SFRAISE(f,SF_FINAL,NIL(Void_t*))) != 0 )
{ rv = ex;
goto done;
}
if(!(f->flags&SF_STATIC) )
free(f);
else
{ f->disc = NIL(Sfdisc_t*);
f->stdio = NIL(Void_t*);
f->mode = SF_AVAIL;
}
}
done:
if(data)
free(data);
return rv;
}
|