xref: /titanic_50/usr/src/lib/libast/common/sfio/sfclrlock.c (revision 9584cebb1c69707f4c67306b661c2ed47d8676f1)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 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 /*	Function to clear a locked stream.
25 **	This is useful for programs that longjmp from the mid of an sfio function.
26 **	There is no guarantee on data integrity in such a case.
27 **
28 **	Written by Kiem-Phong Vo
29 */
30 #if __STD_C
31 int sfclrlock(Sfio_t* f)
32 #else
33 int sfclrlock(f)
34 Sfio_t	*f;
35 #endif
36 {
37 	int	rv;
38 	SFMTXDECL(f);
39 
40 	/* already closed */
41 	if(f && (f->mode&SF_AVAIL))
42 		return 0;
43 
44 	SFMTXENTER(f,0);
45 
46 	/* clear error bits */
47 	f->flags &= ~(SF_ERROR|SF_EOF);
48 
49 	/* clear peek locks */
50 	if(f->mode&SF_PKRD)
51 	{	f->here -= f->endb-f->next;
52 		f->endb = f->next;
53 	}
54 
55 	SFCLRBITS(f);
56 
57 	/* throw away all lock bits except for stacking state SF_PUSH */
58 	f->mode &= (SF_RDWR|SF_INIT|SF_POOL|SF_PUSH|SF_SYNCED|SF_STDIO);
59 
60 	rv = (f->mode&SF_PUSH) ? 0 : (f->flags&SF_FLAGS);
61 
62 	SFMTXRETURN(f, rv);
63 }
64