14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #include "sfhdr.h"
234887Schin
244887Schin /* Set some control flags or file descript for the stream
254887Schin **
264887Schin ** Written by Kiem-Phong Vo.
274887Schin */
284887Schin
294887Schin #if __STD_C
sfset(Sfio_t * f,int flags,int set)308462SApril.Chin@Sun.COM int sfset(Sfio_t* f, int flags, int set)
314887Schin #else
324887Schin int sfset(f,flags,set)
338462SApril.Chin@Sun.COM Sfio_t* f;
348462SApril.Chin@Sun.COM int flags;
358462SApril.Chin@Sun.COM int set;
364887Schin #endif
374887Schin {
384887Schin reg int oflags;
398462SApril.Chin@Sun.COM SFMTXDECL(f);
404887Schin
418462SApril.Chin@Sun.COM SFMTXENTER(f,0);
424887Schin
434887Schin if(flags == 0 && set == 0)
444887Schin SFMTXRETURN(f, (f->flags&SF_FLAGS));
454887Schin
464887Schin if((oflags = (f->mode&SF_RDWR)) != (int)f->mode && _sfmode(f,oflags,0) < 0)
474887Schin SFMTXRETURN(f, 0);
484887Schin
494887Schin if(flags == 0)
504887Schin SFMTXRETURN(f, (f->flags&SF_FLAGS));
514887Schin
524887Schin SFLOCK(f,0);
534887Schin
544887Schin /* preserve at least one rd/wr flag */
554887Schin oflags = f->flags;
564887Schin if(!(f->bits&SF_BOTH) || (flags&SF_RDWR) == SF_RDWR )
574887Schin flags &= ~SF_RDWR;
584887Schin
594887Schin /* set the flag */
604887Schin if(set)
614887Schin f->flags |= (flags&SF_SETS);
624887Schin else f->flags &= ~(flags&SF_SETS);
634887Schin
644887Schin /* must have at least one of read/write */
654887Schin if(!(f->flags&SF_RDWR))
664887Schin f->flags |= (oflags&SF_RDWR);
674887Schin
684887Schin if(f->extent < 0)
694887Schin f->flags &= ~SF_APPENDWR;
704887Schin
714887Schin /* turn to appropriate mode as necessary */
724887Schin if((flags &= SF_RDWR) )
734887Schin { if(!set)
744887Schin { if(flags == SF_READ)
754887Schin flags = SF_WRITE;
764887Schin else flags = SF_READ;
774887Schin }
784887Schin if((flags == SF_WRITE && !(f->mode&SF_WRITE)) ||
794887Schin (flags == SF_READ && !(f->mode&(SF_READ|SF_SYNCED))) )
804887Schin (void)_sfmode(f,flags,1);
814887Schin }
824887Schin
834887Schin /* if not shared or unseekable, public means nothing */
844887Schin if(!(f->flags&SF_SHARE) || f->extent < 0)
854887Schin f->flags &= ~SF_PUBLIC;
864887Schin
874887Schin SFOPEN(f,0);
884887Schin SFMTXRETURN(f, (oflags&SF_FLAGS));
894887Schin }
90