xref: /titanic_41/usr/src/lib/libast/common/sfio/sfset.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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(reg Sfio_t* f, reg int flags, reg int set)
31 #else
32 int sfset(f,flags,set)
33 reg Sfio_t*	f;
34 reg int		flags;
35 reg int		set;
36 #endif
37 {
38 	reg int	oflags;
39 
40 	SFMTXSTART(f,0);
41 
42 	if(flags == 0 && set == 0)
43 		SFMTXRETURN(f, (f->flags&SF_FLAGS));
44 
45 	if((oflags = (f->mode&SF_RDWR)) != (int)f->mode && _sfmode(f,oflags,0) < 0)
46 		SFMTXRETURN(f, 0);
47 
48 	if(flags == 0)
49 		SFMTXRETURN(f, (f->flags&SF_FLAGS));
50 
51 	SFLOCK(f,0);
52 
53 	/* preserve at least one rd/wr flag */
54 	oflags = f->flags;
55 	if(!(f->bits&SF_BOTH) || (flags&SF_RDWR) == SF_RDWR )
56 		flags &= ~SF_RDWR;
57 
58 	/* set the flag */
59 	if(set)
60 		f->flags |=  (flags&SF_SETS);
61 	else	f->flags &= ~(flags&SF_SETS);
62 
63 	/* must have at least one of read/write */
64 	if(!(f->flags&SF_RDWR))
65 		f->flags |= (oflags&SF_RDWR);
66 
67 	if(f->extent < 0)
68 		f->flags &= ~SF_APPENDWR;
69 
70 	/* turn to appropriate mode as necessary */
71 	if((flags &= SF_RDWR) )
72 	{	if(!set)
73 		{	if(flags == SF_READ)
74 				flags = SF_WRITE;
75 			else	flags = SF_READ;
76 		}
77 		if((flags == SF_WRITE && !(f->mode&SF_WRITE)) ||
78 		   (flags == SF_READ && !(f->mode&(SF_READ|SF_SYNCED))) )
79 			(void)_sfmode(f,flags,1);
80 	}
81 
82 	/* if not shared or unseekable, public means nothing */
83 	if(!(f->flags&SF_SHARE) || f->extent < 0)
84 		f->flags &= ~SF_PUBLIC;
85 
86 	SFOPEN(f,0);
87 	SFMTXRETURN(f, (oflags&SF_FLAGS));
88 }
89