2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: sfsync.c /main/3 1995/11/01 18:37:23 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS PROPRIETARY SOURCE CODE LICENSED BY *
31 * Copyright (c) 1995 AT&T Corp. *
32 * All Rights Reserved *
34 * This software is licensed by AT&T Corp. *
35 * under the terms and conditions of the license in *
36 * http://www.research.att.com/orgs/ssr/book/reuse *
38 * This software was created by the *
39 * Software Engineering Research Department *
40 * AT&T Bell Laboratories *
42 * For further information contact *
43 * gsf@research.att.com *
45 ***************************************************************/
48 /* Synchronize data in buffers with the file system.
49 ** If f is nil, all streams are sync-ed
51 ** Written by Kiem-Phong Vo (06/27/90)
56 reg Sfpool_t *p, *next;
59 reg int nsync, count, loop;
62 for(loop = 0; loop < MAXLOOP; ++loop)
63 { rv = nsync = count = 0;
64 for(p = &_Sfpool; p; p = next)
65 { /* find the next legitimate pool */
66 for(next = p->next; next; next = next->next)
70 /* walk the streams for _Sfpool only */
71 for(n = 0; n < ((p == &_Sfpool) ? p->n_sf : 1); ++n)
75 if(f->flags&SF_STRING )
79 if((f->mode&SF_READ) && (f->mode&SF_SYNCED) )
81 if((f->mode&SF_READ) && !(f->flags&SF_MMAP) &&
84 if((f->mode&SF_WRITE) && !(f->flags&SF_HOLE) &&
105 sfsync(reg Sfio_t* f)
108 reg Sfio_t* f; /* stream to be synchronized */
111 reg int local, rv, mode;
118 if(f->disc == _Sfudisc) /* throw away ungetc */
119 (void)sfclose((*_Sfstack)(f,NIL(Sfio_t*)));
123 if((f->mode&SF_RDWR) != SFMODE(f,local) && _sfmode(f,0,local) < 0)
128 for(; f; f = f->push)
131 /* pretend that this stream is not on a stack */
132 mode = f->mode&SF_PUSH;
135 /* these streams do not need synchronization */
136 if((f->flags&SF_STRING) || (f->mode&SF_SYNCED))
139 if((f->mode&SF_WRITE) && (f->next > f->data || (f->flags&SF_HOLE)) )
140 { /* sync the buffer, make sure pool don't move */
141 reg int pool = f->mode&SF_POOL;
143 if(f->next > f->data && SFFLSBUF(f,-1) < 0)
145 if(!SFISNULL(f) && (f->flags&SF_HOLE) )
146 { /* realize a previously created hole of 0's */
147 if(lseek(f->file,-1L,1) >= 0)
148 (void)write(f->file,"",1);
149 f->flags &= ~SF_HOLE;
154 if((f->mode&SF_READ) && f->extent >= 0 &&
155 ((f->flags&SF_MMAP) || f->next < f->endb) )
156 { /* make sure the file pointer is at the right place */
157 f->here -= (f->endb-f->next);
158 f->endr = f->endw = f->data;
159 f->mode = SF_READ|SF_SYNCED|SF_LOCK;
160 (void)SFSK(f,f->here,0,f->disc);
162 if((f->flags&SF_SHARE) && !(f->flags&SF_PUBLIC) &&
163 !(f->flags&SF_MMAP) )
164 { f->endb = f->next = f->data;
165 f->mode &= ~SF_SYNCED;
173 if(!local && !(f->flags&SF_ERROR) && (f->mode&~SF_RDWR) == 0 &&
174 (f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
175 (void)(*f->disc->exceptf)(f,SF_SYNC,f->disc);
179 if(!local && f && (f->mode&SF_POOL) && f->pool && f != f->pool->sf[0])
180 SFSYNC(f->pool->sf[0]);