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