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

« back to all changes in this revision

Viewing changes to tools/sfio/sfswap.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
/*      Swap two streams. If the second argument is NULL,
 
4
**      a new stream will be created. Always return the second argument
 
5
**      or the new stream. Note that this function will always work
 
6
**      unless streams are locked by SF_PUSH.
 
7
**
 
8
**      Written by Kiem-Phong Vo.
 
9
*/
 
10
 
 
11
#if __STD_C
 
12
Sfio_t* sfswap(reg Sfio_t* f1, reg Sfio_t* f2)
 
13
#else
 
14
Sfio_t* sfswap(f1,f2)
 
15
reg Sfio_t*     f1;
 
16
reg Sfio_t*     f2;
 
17
#endif
 
18
{
 
19
        Sfio_t  tmp;
 
20
        int     f1pool, f2pool, f1mode, f2mode, f1flags, f2flags;
 
21
 
 
22
        if(!f1 || (f1->mode&SF_AVAIL) || (SFFROZEN(f1) && (f1->mode&SF_PUSH)) )
 
23
                return NIL(Sfio_t*);
 
24
        if(f2 && SFFROZEN(f2) && (f2->mode&SF_PUSH) )
 
25
                return NIL(Sfio_t*);
 
26
        if(f1 == f2)
 
27
                return f2;
 
28
 
 
29
        f1mode = f1->mode;
 
30
        SFLOCK(f1,0);
 
31
        f1->mode |= SF_PUSH;            /* make sure there is no recursion on f1 */
 
32
        
 
33
        if(f2)
 
34
        {       f2mode = f2->mode;
 
35
                SFLOCK(f2,0);
 
36
                f2->mode |= SF_PUSH;    /* make sure there is no recursion on f2 */
 
37
        }
 
38
        else
 
39
        {       f2 = f1->file == 0 ? sfstdin :
 
40
                     f1->file == 1 ? sfstdout :
 
41
                     f1->file == 2 ? sfstderr : NIL(Sfio_t*);
 
42
                if((!f2 || !(f2->mode&SF_AVAIL)) )
 
43
                {       if(!(f2 = (Sfio_t*)malloc(sizeof(Sfio_t))) )
 
44
                        {       f1->mode = f1mode;
 
45
                                SFOPEN(f1,0);
 
46
                                return NIL(Sfio_t*);
 
47
                        }
 
48
 
 
49
                        SFCLEAR(f2,NIL(Vtmutex_t*));
 
50
                }
 
51
                f2->mode = SF_AVAIL|SF_LOCK;
 
52
                f2mode = SF_AVAIL;
 
53
        }
 
54
 
 
55
        if(!f1->pool)
 
56
                f1pool = -1;
 
57
        else for(f1pool = f1->pool->n_sf-1; f1pool >= 0; --f1pool)
 
58
                if(f1->pool->sf[f1pool] == f1)
 
59
                        break;
 
60
        if(!f2->pool)
 
61
                f2pool = -1;
 
62
        else for(f2pool = f2->pool->n_sf-1; f2pool >= 0; --f2pool)
 
63
                if(f2->pool->sf[f2pool] == f2)
 
64
                        break;
 
65
 
 
66
        f1flags = f1->flags;
 
67
        f2flags = f2->flags;
 
68
 
 
69
        /* swap image and pool entries */
 
70
        memcpy((Void_t*)(&tmp),(Void_t*)f1,sizeof(Sfio_t));
 
71
        memcpy((Void_t*)f1,(Void_t*)f2,sizeof(Sfio_t));
 
72
        memcpy((Void_t*)f2,(Void_t*)(&tmp),sizeof(Sfio_t));
 
73
        if(f2pool >= 0)
 
74
                f1->pool->sf[f2pool] = f1;
 
75
        if(f1pool >= 0)
 
76
                f2->pool->sf[f1pool] = f2;
 
77
 
 
78
        if(f2flags&SF_STATIC)
 
79
                f2->flags |= SF_STATIC;
 
80
        else    f2->flags &= ~SF_STATIC;
 
81
 
 
82
        if(f1flags&SF_STATIC)
 
83
                f1->flags |= SF_STATIC;
 
84
        else    f1->flags &= ~SF_STATIC;
 
85
 
 
86
        if(f2mode&SF_AVAIL)     /* swapping to a closed stream */
 
87
        {       if(!(f1->flags&SF_STATIC) )
 
88
                        free(f1);
 
89
        }
 
90
        else
 
91
        {       f1->mode = f2mode;
 
92
                SFOPEN(f1,0);
 
93
        }
 
94
 
 
95
        f2->mode = f1mode;
 
96
        SFOPEN(f2,0);
 
97
        return f2;
 
98
}