xref: /titanic_50/usr/src/lib/libast/common/sfio/sfset.c (revision 1da7e59911a9f3f267c3ef294dc342767dee7952)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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
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;
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 && _sfmode(f,oflags,0) < 0)
47 		SFMTXRETURN(f, 0);
48 
49 	if(flags == 0)
50 		SFMTXRETURN(f, (f->flags&SF_FLAGS));
51 
52 	SFLOCK(f,0);
53 
54 	/* preserve at least one rd/wr flag */
55 	oflags = f->flags;
56 	if(!(f->bits&SF_BOTH) || (flags&SF_RDWR) == SF_RDWR )
57 		flags &= ~SF_RDWR;
58 
59 	/* set the flag */
60 	if(set)
61 		f->flags |=  (flags&SF_SETS);
62 	else	f->flags &= ~(flags&SF_SETS);
63 
64 	/* must have at least one of read/write */
65 	if(!(f->flags&SF_RDWR))
66 		f->flags |= (oflags&SF_RDWR);
67 
68 	if(f->extent < 0)
69 		f->flags &= ~SF_APPENDWR;
70 
71 	/* turn to appropriate mode as necessary */
72 	if((flags &= SF_RDWR) )
73 	{	if(!set)
74 		{	if(flags == SF_READ)
75 				flags = SF_WRITE;
76 			else	flags = SF_READ;
77 		}
78 		if((flags == SF_WRITE && !(f->mode&SF_WRITE)) ||
79 		   (flags == SF_READ && !(f->mode&(SF_READ|SF_SYNCED))) )
80 			(void)_sfmode(f,flags,1);
81 	}
82 
83 	/* if not shared or unseekable, public means nothing */
84 	if(!(f->flags&SF_SHARE) || f->extent < 0)
85 		f->flags &= ~SF_PUBLIC;
86 
87 	SFOPEN(f,0);
88 	SFMTXRETURN(f, (oflags&SF_FLAGS));
89 }
90