~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to tools/sfio/sfsync.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include        "sfhdr.h"
 
2
 
 
3
/*      Synchronize data in buffers with the file system.
 
4
**      If f is nil, all streams are sync-ed
 
5
**
 
6
**      Written by Kiem-Phong Vo.
 
7
*/
 
8
 
 
9
#if __STD_C
 
10
static int _sfall(void)
 
11
#else
 
12
static int _sfall()
 
13
#endif
 
14
{
 
15
        reg Sfpool_t    *p, *next;
 
16
        reg Sfio_t*     f;
 
17
        reg int         n, rv;
 
18
        reg int         nsync, count, loop;
 
19
#define MAXLOOP 3
 
20
 
 
21
        for(loop = 0; loop < MAXLOOP; ++loop)
 
22
        {       rv = nsync = count = 0;
 
23
                for(p = &_Sfpool; p; p = next)
 
24
                {       /* find the next legitimate pool */
 
25
                        for(next = p->next; next; next = next->next)
 
26
                                if(next->n_sf > 0)
 
27
                                        break;
 
28
 
 
29
                        /* walk the streams for _Sfpool only */
 
30
                        for(n = 0; n < ((p == &_Sfpool) ? p->n_sf : 1); ++n)
 
31
                        {       count += 1;
 
32
                                f = p->sf[n];
 
33
 
 
34
                                if(f->flags&SF_STRING )
 
35
                                        goto did_sync;
 
36
                                if(SFFROZEN(f))
 
37
                                        continue;
 
38
                                if((f->mode&SF_READ) && (f->mode&SF_SYNCED) )
 
39
                                        goto did_sync;
 
40
                                if((f->mode&SF_READ) && !(f->bits&SF_MMAP) &&
 
41
                                   f->next == f->endb)
 
42
                                        goto did_sync;
 
43
                                if((f->mode&SF_WRITE) && !(f->bits&SF_HOLE) &&
 
44
                                   f->next == f->data)
 
45
                                        goto did_sync;
 
46
 
 
47
                                if(sfsync(f) < 0)
 
48
                                        rv = -1;
 
49
 
 
50
                        did_sync:
 
51
                                nsync += 1;
 
52
                        }
 
53
                }
 
54
 
 
55
                if(nsync == count)
 
56
                        break;
 
57
        }
 
58
        return rv;
 
59
}
 
60
 
 
61
#if __STD_C
 
62
int sfsync(reg Sfio_t* f)
 
63
#else
 
64
int sfsync(f)
 
65
reg Sfio_t*     f;      /* stream to be synchronized */
 
66
#endif
 
67
{
 
68
        int     local, rv, mode;
 
69
        Sfio_t* origf;
 
70
 
 
71
        if(!(origf = f) )
 
72
                return _sfall();
 
73
 
 
74
        SFMTXSTART(origf,-1);
 
75
 
 
76
        GETLOCAL(origf,local);
 
77
 
 
78
        if(origf->disc == _Sfudisc)     /* throw away ungetc */
 
79
                (void)sfclose((*_Sfstack)(origf,NIL(Sfio_t*)));
 
80
 
 
81
        rv = 0;
 
82
 
 
83
        if((origf->mode&SF_RDWR) != SFMODE(origf,local) && _sfmode(origf,0,local) < 0)
 
84
        {       rv = -1;
 
85
                goto done;
 
86
        }
 
87
 
 
88
        for(; f; f = f->push)
 
89
        {       
 
90
                if((f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
 
91
                        (void)(*f->disc->exceptf)(f,SF_SYNC,(Void_t*)((int)1),f->disc);
 
92
 
 
93
                SFLOCK(f,local);
 
94
 
 
95
                /* pretend that this stream is not on a stack */
 
96
                mode = f->mode&SF_PUSH;
 
97
                f->mode &= ~SF_PUSH;
 
98
 
 
99
                /* these streams do not need synchronization */
 
100
                if((f->flags&SF_STRING) || (f->mode&SF_SYNCED))
 
101
                        goto next;
 
102
 
 
103
                if((f->mode&SF_WRITE) && (f->next > f->data || (f->bits&SF_HOLE)) )
 
104
                {       /* sync the buffer, make sure pool don't move */
 
105
                        reg int pool = f->mode&SF_POOL;
 
106
                        f->mode &= ~SF_POOL;
 
107
                        if(f->next > f->data && (SFWRALL(f), SFFLSBUF(f,-1)) < 0)
 
108
                                rv = -1;
 
109
                        if(!SFISNULL(f) && (f->bits&SF_HOLE) )
 
110
                        {       /* realize a previously created hole of 0's */
 
111
                                if(SFSK(f,(Sfoff_t)(-1),SEEK_CUR,f->disc) >= 0)
 
112
                                        (void)SFWR(f,"",1,f->disc);
 
113
                                f->bits &= ~SF_HOLE;
 
114
                        }
 
115
                        f->mode |= pool;
 
116
                }
 
117
 
 
118
                if((f->mode&SF_READ) && f->extent >= 0 &&
 
119
                   ((f->bits&SF_MMAP) || f->next < f->endb) )
 
120
                {       /* make sure the file pointer is at the right place */
 
121
                        f->here -= (f->endb-f->next);
 
122
                        f->endr = f->endw = f->data;
 
123
                        f->mode = SF_READ|SF_SYNCED|SF_LOCK;
 
124
                        (void)SFSK(f,f->here,SEEK_SET,f->disc);
 
125
 
 
126
                        if((f->flags&SF_SHARE) && !(f->flags&SF_PUBLIC) &&
 
127
                           !(f->bits&SF_MMAP) )
 
128
                        {       f->endb = f->next = f->data;
 
129
                                f->mode &= ~SF_SYNCED;
 
130
                        }
 
131
                }
 
132
 
 
133
        next:
 
134
                f->mode |= mode;
 
135
                SFOPEN(f,local);
 
136
 
 
137
                if((f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
 
138
                        (void)(*f->disc->exceptf)(f,SF_SYNC,(Void_t*)((int)0),f->disc);
 
139
        }
 
140
 
 
141
done:
 
142
        if(!local && f && (f->mode&SF_POOL) && f->pool && f != f->pool->sf[0])
 
143
                SFSYNC(f->pool->sf[0]);
 
144
 
 
145
        SFMTXRETURN(origf, rv);
 
146
}