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