1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #include	"sfhdr.h"
23 
24 /*	Set some control flags or file descript for the stream
25 **
26 **	Written by Kiem-Phong Vo.
27 */
28 
29 #if __STD_C
sfset(Sfio_t * f,int flags,int set)30 int sfset(Sfio_t* f, int flags, int set)
31 #else
32 int sfset(f,flags,set)
33 Sfio_t*		f;
34 int		flags;
35 int		set;
36 #endif
37 {
38 	reg int	oflags, tflags, rv;
39 	SFMTXDECL(f);
40 
41 	SFMTXENTER(f,0);
42 
43 	if(flags == 0 && set == 0)
44 		SFMTXRETURN(f, (f->flags&SF_FLAGS));
45 
46 	if((oflags = (f->mode&SF_RDWR)) != (int)f->mode)
47 	{	/* avoid sfsetbuf() isatty() call if user sets (SF_LINE|SF_WCWIDTH) */
48 		if(set && (flags & (SF_LINE|SF_WCWIDTH)) && !(f->flags & (SF_LINE|SF_WCWIDTH)))
49 		{	tflags = (SF_LINE|SF_WCWIDTH);
50 			f->flags |= tflags;
51 		}
52 		else	tflags = 0;
53 		rv = _sfmode(f,oflags,0);
54 		if(tflags)
55 			f->flags &= ~tflags;
56 		if(rv < 0)
57 			SFMTXRETURN(f, 0);
58 	}
59 	if(flags == 0)
60 		SFMTXRETURN(f, (f->flags&SF_FLAGS));
61 
62 	SFLOCK(f,0);
63 
64 	/* preserve at least one rd/wr flag */
65 	oflags = f->flags;
66 	if(!(f->bits&SF_BOTH) || (flags&SF_RDWR) == SF_RDWR )
67 		flags &= ~SF_RDWR;
68 
69 	/* set the flag */
70 	if(set)
71 		f->flags |=  (flags&SF_SETS);
72 	else	f->flags &= ~(flags&SF_SETS);
73 
74 	/* must have at least one of read/write */
75 	if(!(f->flags&SF_RDWR))
76 		f->flags |= (oflags&SF_RDWR);
77 
78 	if(f->extent < 0)
79 		f->flags &= ~SF_APPENDWR;
80 
81 	/* turn to appropriate mode as necessary */
82 	if((flags &= SF_RDWR) )
83 	{	if(!set)
84 		{	if(flags == SF_READ)
85 				flags = SF_WRITE;
86 			else	flags = SF_READ;
87 		}
88 		if((flags == SF_WRITE && !(f->mode&SF_WRITE)) ||
89 		   (flags == SF_READ && !(f->mode&(SF_READ|SF_SYNCED))) )
90 			(void)_sfmode(f,flags,1);
91 	}
92 
93 	/* if not shared or unseekable, public means nothing */
94 	if(!(f->flags&SF_SHARE) || f->extent < 0)
95 		f->flags &= ~SF_PUBLIC;
96 
97 	SFOPEN(f,0);
98 	SFMTXRETURN(f, (oflags&SF_FLAGS));
99 }
100