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 /* Set some control flags or file descript for the stream
25da2e3ebdSchin **
26da2e3ebdSchin ** Written by Kiem-Phong Vo.
27da2e3ebdSchin */
28da2e3ebdSchin
29da2e3ebdSchin #if __STD_C
sfset(Sfio_t * f,int flags,int set)307c2fbfb3SApril Chin int sfset(Sfio_t* f, int flags, int set)
31da2e3ebdSchin #else
32da2e3ebdSchin int sfset(f,flags,set)
337c2fbfb3SApril Chin Sfio_t* f;
347c2fbfb3SApril Chin int flags;
357c2fbfb3SApril Chin int set;
36da2e3ebdSchin #endif
37da2e3ebdSchin {
38da2e3ebdSchin reg int oflags;
397c2fbfb3SApril Chin SFMTXDECL(f);
40da2e3ebdSchin
417c2fbfb3SApril Chin SFMTXENTER(f,0);
42da2e3ebdSchin
43da2e3ebdSchin if(flags == 0 && set == 0)
44da2e3ebdSchin SFMTXRETURN(f, (f->flags&SF_FLAGS));
45da2e3ebdSchin
46da2e3ebdSchin if((oflags = (f->mode&SF_RDWR)) != (int)f->mode && _sfmode(f,oflags,0) < 0)
47da2e3ebdSchin SFMTXRETURN(f, 0);
48da2e3ebdSchin
49da2e3ebdSchin if(flags == 0)
50da2e3ebdSchin SFMTXRETURN(f, (f->flags&SF_FLAGS));
51da2e3ebdSchin
52da2e3ebdSchin SFLOCK(f,0);
53da2e3ebdSchin
54da2e3ebdSchin /* preserve at least one rd/wr flag */
55da2e3ebdSchin oflags = f->flags;
56da2e3ebdSchin if(!(f->bits&SF_BOTH) || (flags&SF_RDWR) == SF_RDWR )
57da2e3ebdSchin flags &= ~SF_RDWR;
58da2e3ebdSchin
59da2e3ebdSchin /* set the flag */
60da2e3ebdSchin if(set)
61da2e3ebdSchin f->flags |= (flags&SF_SETS);
62da2e3ebdSchin else f->flags &= ~(flags&SF_SETS);
63da2e3ebdSchin
64da2e3ebdSchin /* must have at least one of read/write */
65da2e3ebdSchin if(!(f->flags&SF_RDWR))
66da2e3ebdSchin f->flags |= (oflags&SF_RDWR);
67da2e3ebdSchin
68da2e3ebdSchin if(f->extent < 0)
69da2e3ebdSchin f->flags &= ~SF_APPENDWR;
70da2e3ebdSchin
71da2e3ebdSchin /* turn to appropriate mode as necessary */
72da2e3ebdSchin if((flags &= SF_RDWR) )
73da2e3ebdSchin { if(!set)
74da2e3ebdSchin { if(flags == SF_READ)
75da2e3ebdSchin flags = SF_WRITE;
76da2e3ebdSchin else flags = SF_READ;
77da2e3ebdSchin }
78da2e3ebdSchin if((flags == SF_WRITE && !(f->mode&SF_WRITE)) ||
79da2e3ebdSchin (flags == SF_READ && !(f->mode&(SF_READ|SF_SYNCED))) )
80da2e3ebdSchin (void)_sfmode(f,flags,1);
81da2e3ebdSchin }
82da2e3ebdSchin
83da2e3ebdSchin /* if not shared or unseekable, public means nothing */
84da2e3ebdSchin if(!(f->flags&SF_SHARE) || f->extent < 0)
85da2e3ebdSchin f->flags &= ~SF_PUBLIC;
86da2e3ebdSchin
87da2e3ebdSchin SFOPEN(f,0);
88da2e3ebdSchin SFMTXRETURN(f, (oflags&SF_FLAGS));
89da2e3ebdSchin }
90