xref: /titanic_50/usr/src/lib/libast/common/sfio/sfflsbuf.c (revision 3db3491215579980a91e230cf21b20608fbb8259)
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 /*	Write a buffer out to a file descriptor or
25 **	extending a buffer for a SF_STRING stream.
26 **
27 **	Written by Kiem-Phong Vo
28 */
29 
30 #if __STD_C
31 int _sfflsbuf(Sfio_t* f, int c)
32 #else
33 int _sfflsbuf(f,c)
34 Sfio_t*	f;	/* write out the buffered content of this stream */
35 int	c;	/* if c>=0, c is also written out */
36 #endif
37 {
38 	ssize_t		n, w;
39 	uchar*		data;
40 	uchar		outc;
41 	int		local, isall;
42 	int		inpc = c;
43 	SFMTXDECL(f);
44 
45 	SFMTXENTER(f,-1);
46 
47 	GETLOCAL(f,local);
48 
49 	for(;; f->mode &= ~SF_LOCK)
50 	{	/* check stream mode */
51 		if(SFMODE(f,local) != SF_WRITE && _sfmode(f,SF_WRITE,local) < 0)
52 			SFMTXRETURN(f, -1);
53 		SFLOCK(f,local);
54 
55 		/* current data extent */
56 		n = f->next - (data = f->data);
57 
58 		if(n == (f->endb-data) && (f->flags&SF_STRING))
59 		{	/* extend string stream buffer */
60 			(void)SFWR(f,data,1,f->disc);
61 
62 			/* !(f->flags&SF_STRING) is required because exception
63 			   handlers may turn a string stream to a file stream */
64 			if(f->next < f->endb || !(f->flags&SF_STRING) )
65 				n = f->next - (data = f->data);
66 			else
67 			{	SFOPEN(f,local);
68 				SFMTXRETURN(f, -1);
69 			}
70 		}
71 
72 		if(c >= 0)
73 		{	/* write into buffer */
74 			if(n < (f->endb - (data = f->data)))
75 			{	*f->next++ = c;
76 				if(c == '\n' &&
77 				   (f->flags&SF_LINE) && !(f->flags&SF_STRING))
78 				{	c = -1;
79 					n += 1;
80 				}
81 				else	break;
82 			}
83 			else if(n == 0)
84 			{	/* unbuffered io */
85 				outc = (uchar)c;
86 				data = &outc;
87 				c = -1;
88 				n = 1;
89 			}
90 		}
91 
92 		if(n == 0 || (f->flags&SF_STRING))
93 			break;
94 
95 		isall = SFISALL(f,isall);
96 		if((w = SFWR(f,data,n,f->disc)) > 0)
97 		{	if((n -= w) > 0) /* save unwritten data, then resume */
98 				memcpy((char*)f->data,(char*)data+w,n);
99 			f->next = f->data+n;
100 			if(c < 0 && (!isall || n == 0))
101 				break;
102 		}
103 		else if(w == 0)
104 		{	SFOPEN(f,local);
105 			SFMTXRETURN(f, -1);
106 		}
107 		else if(c < 0)
108 			break;
109 	}
110 
111 	SFOPEN(f,local);
112 
113 	if(inpc < 0)
114 		inpc = f->endb-f->next;
115 
116 	SFMTXRETURN(f,inpc);
117 }
118