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

« back to all changes in this revision

Viewing changes to tools/sfio/sfstack.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
 
 
4
/*      Push/pop streams
 
5
**
 
6
**      Written by Kiem-Phong Vo.
 
7
*/
 
8
 
 
9
#define STKMTXLOCK(f1,f2) \
 
10
        { if(f1) SFMTXLOCK(f1); \
 
11
          if(f2) SFMTXLOCK(f2); \
 
12
        }
 
13
#define STKMTXRETURN(f1,f2,rv) \
 
14
        { if(f1) SFMTXUNLOCK(f1); \
 
15
          if(f2) SFMTXUNLOCK(f2); \
 
16
          return(rv); \
 
17
        }
 
18
 
 
19
#if __STD_C
 
20
Sfio_t* sfstack(Sfio_t* f1, Sfio_t* f2)
 
21
#else
 
22
Sfio_t* sfstack(f1,f2)
 
23
Sfio_t* f1;     /* base of stack        */
 
24
Sfio_t* f2;     /* top of stack */
 
25
#endif
 
26
{
 
27
        reg int         n;
 
28
        reg Sfio_t*     rf;
 
29
        reg Sfrsrv_t*   rsrv;
 
30
        reg Vtmutex_t*  mtx;
 
31
 
 
32
        STKMTXLOCK(f1,f2);
 
33
 
 
34
        if(f1 && (f1->mode&SF_RDWR) != f1->mode && _sfmode(f1,0,0) < 0)
 
35
                STKMTXRETURN(f1,f2, NIL(Sfio_t*));
 
36
        if(f2 && (f2->mode&SF_RDWR) != f2->mode && _sfmode(f2,0,0) < 0)
 
37
                STKMTXRETURN(f1,f2, NIL(Sfio_t*));
 
38
        if(!f1)
 
39
                STKMTXRETURN(f1,f2, f2);
 
40
 
 
41
        /* give access to other internal functions */
 
42
        _Sfstack = sfstack;
 
43
 
 
44
        if(f2 == SF_POPSTACK)
 
45
        {       if(!(f2 = f1->push))
 
46
                        STKMTXRETURN(f1,f2, NIL(Sfio_t*));
 
47
                f2->mode &= ~SF_PUSH;
 
48
        }
 
49
        else
 
50
        {       if(f2->push)
 
51
                        STKMTXRETURN(f1,f2, NIL(Sfio_t*));
 
52
                if(f1->pool && f1->pool != &_Sfpool && f1->pool != f2->pool &&
 
53
                   f1 == f1->pool->sf[0])
 
54
                {       /* get something else to pool front since f1 will be locked */
 
55
                        for(n = 1; n < f1->pool->n_sf; ++n)
 
56
                        {       if(SFFROZEN(f1->pool->sf[n]) )
 
57
                                        continue;
 
58
                                (*_Sfpmove)(f1->pool->sf[n],0);
 
59
                                break;
 
60
                        }
 
61
                }
 
62
        }
 
63
 
 
64
        if(f2->pool && f2->pool != &_Sfpool && f2 != f2->pool->sf[0])
 
65
                (*_Sfpmove)(f2,0);
 
66
 
 
67
        /* swap streams */
 
68
        sfswap(f1,f2);
 
69
 
 
70
        /* but the reserved buffer and mutex must remain the same */
 
71
        rsrv = f1->rsrv; f1->rsrv = f2->rsrv; f2->rsrv = rsrv;
 
72
        mtx = f1->mutex; f1->mutex = f2->mutex; f2->mutex = mtx;
 
73
 
 
74
        SFLOCK(f1,0);
 
75
        SFLOCK(f2,0);
 
76
 
 
77
        if(f2->push != f2)
 
78
        {       /* freeze the pushed stream */
 
79
                f2->mode |= SF_PUSH;
 
80
                f1->push = f2;
 
81
                rf = f1;
 
82
        }
 
83
        else
 
84
        {       /* unfreeze the just exposed stream */
 
85
                f1->mode &= ~SF_PUSH;
 
86
                f2->push = NIL(Sfio_t*);
 
87
                rf = f2;
 
88
        }
 
89
 
 
90
        SFOPEN(f1,0);
 
91
        SFOPEN(f2,0);
 
92
 
 
93
        STKMTXRETURN(f1,f2, rf);
 
94
}