xref: /titanic_50/usr/src/lib/libast/common/sfio/sfclrlock.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
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 /*	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(reg Sfio_t* f)
32 #else
33 int sfclrlock(f)
34 reg Sfio_t	*f;
35 #endif
36 {
37 	int	rv;
38 
39 	/* already closed */
40 	if(f && (f->mode&SF_AVAIL))
41 		return 0;
42 
43 	SFMTXSTART(f,0);
44 
45 	/* clear error bits */
46 	f->flags &= ~(SF_ERROR|SF_EOF);
47 
48 	/* clear peek locks */
49 	if(f->mode&SF_PKRD)
50 	{	f->here -= f->endb-f->next;
51 		f->endb = f->next;
52 	}
53 
54 	SFCLRBITS(f);
55 
56 	/* throw away all lock bits except for stacking state SF_PUSH */
57 	f->mode &= (SF_RDWR|SF_INIT|SF_POOL|SF_PUSH|SF_SYNCED|SF_STDIO);
58 
59 	rv = (f->mode&SF_PUSH) ? 0 : (f->flags&SF_FLAGS);
60 
61 	SFMTXRETURN(f, rv);
62 }
63