1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #include "sfhdr.h"
23da2e3ebdSchin
24da2e3ebdSchin /* Write a buffer out to a file descriptor or
25da2e3ebdSchin ** extending a buffer for a SF_STRING stream.
26da2e3ebdSchin **
27da2e3ebdSchin ** Written by Kiem-Phong Vo
28da2e3ebdSchin */
29da2e3ebdSchin
30da2e3ebdSchin #if __STD_C
_sfflsbuf(Sfio_t * f,int c)317c2fbfb3SApril Chin int _sfflsbuf(Sfio_t* f, int c)
32da2e3ebdSchin #else
33da2e3ebdSchin int _sfflsbuf(f,c)
347c2fbfb3SApril Chin Sfio_t* f; /* write out the buffered content of this stream */
357c2fbfb3SApril Chin int c; /* if c>=0, c is also written out */
36da2e3ebdSchin #endif
37da2e3ebdSchin {
3834f9b3eeSRoland Mainz ssize_t n, w, written;
397c2fbfb3SApril Chin uchar* data;
40da2e3ebdSchin uchar outc;
417c2fbfb3SApril Chin int local, isall;
42da2e3ebdSchin int inpc = c;
4334f9b3eeSRoland Mainz SFMTXDECL(f); /* declare a local stream variable for multithreading */
44da2e3ebdSchin
457c2fbfb3SApril Chin SFMTXENTER(f,-1);
46da2e3ebdSchin
47da2e3ebdSchin GETLOCAL(f,local);
48da2e3ebdSchin
4934f9b3eeSRoland Mainz for(written = 0;; f->mode &= ~SF_LOCK)
50da2e3ebdSchin { /* check stream mode */
51da2e3ebdSchin if(SFMODE(f,local) != SF_WRITE && _sfmode(f,SF_WRITE,local) < 0)
52da2e3ebdSchin SFMTXRETURN(f, -1);
53da2e3ebdSchin SFLOCK(f,local);
54da2e3ebdSchin
55da2e3ebdSchin /* current data extent */
56da2e3ebdSchin n = f->next - (data = f->data);
57da2e3ebdSchin
58da2e3ebdSchin if(n == (f->endb-data) && (f->flags&SF_STRING))
59da2e3ebdSchin { /* extend string stream buffer */
60da2e3ebdSchin (void)SFWR(f,data,1,f->disc);
61da2e3ebdSchin
62da2e3ebdSchin /* !(f->flags&SF_STRING) is required because exception
63da2e3ebdSchin handlers may turn a string stream to a file stream */
64da2e3ebdSchin if(f->next < f->endb || !(f->flags&SF_STRING) )
65da2e3ebdSchin n = f->next - (data = f->data);
66da2e3ebdSchin else
67da2e3ebdSchin { SFOPEN(f,local);
68da2e3ebdSchin SFMTXRETURN(f, -1);
69da2e3ebdSchin }
70da2e3ebdSchin }
71da2e3ebdSchin
72da2e3ebdSchin if(c >= 0)
73da2e3ebdSchin { /* write into buffer */
74da2e3ebdSchin if(n < (f->endb - (data = f->data)))
75da2e3ebdSchin { *f->next++ = c;
76da2e3ebdSchin if(c == '\n' &&
77da2e3ebdSchin (f->flags&SF_LINE) && !(f->flags&SF_STRING))
78da2e3ebdSchin { c = -1;
79da2e3ebdSchin n += 1;
80da2e3ebdSchin }
81da2e3ebdSchin else break;
82da2e3ebdSchin }
83da2e3ebdSchin else if(n == 0)
84da2e3ebdSchin { /* unbuffered io */
85da2e3ebdSchin outc = (uchar)c;
86da2e3ebdSchin data = &outc;
87da2e3ebdSchin c = -1;
88da2e3ebdSchin n = 1;
89da2e3ebdSchin }
90da2e3ebdSchin }
91da2e3ebdSchin
92da2e3ebdSchin if(n == 0 || (f->flags&SF_STRING))
93da2e3ebdSchin break;
94da2e3ebdSchin
95da2e3ebdSchin isall = SFISALL(f,isall);
96da2e3ebdSchin if((w = SFWR(f,data,n,f->disc)) > 0)
97da2e3ebdSchin { if((n -= w) > 0) /* save unwritten data, then resume */
98da2e3ebdSchin memcpy((char*)f->data,(char*)data+w,n);
9934f9b3eeSRoland Mainz written += w;
100da2e3ebdSchin f->next = f->data+n;
101da2e3ebdSchin if(c < 0 && (!isall || n == 0))
102da2e3ebdSchin break;
103da2e3ebdSchin }
104da2e3ebdSchin else if(w == 0)
10534f9b3eeSRoland Mainz { if(written > 0) /* some buffer was cleared */
10634f9b3eeSRoland Mainz break; /* do normal exit below */
10734f9b3eeSRoland Mainz else /* nothing was done, returning failure */
108da2e3ebdSchin { SFOPEN(f,local);
109da2e3ebdSchin SFMTXRETURN(f, -1);
110da2e3ebdSchin }
11134f9b3eeSRoland Mainz }
11234f9b3eeSRoland Mainz else /* w < 0 means SF_EDISC or SF_ESTACK in sfwr() */
11334f9b3eeSRoland Mainz { if(c < 0) /* back to the calling write operation */
114da2e3ebdSchin break;
11534f9b3eeSRoland Mainz else continue; /* try again to write out c */
11634f9b3eeSRoland Mainz }
117da2e3ebdSchin }
118da2e3ebdSchin
119da2e3ebdSchin SFOPEN(f,local);
120da2e3ebdSchin
121da2e3ebdSchin if(inpc < 0)
122da2e3ebdSchin inpc = f->endb-f->next;
123da2e3ebdSchin
124da2e3ebdSchin SFMTXRETURN(f,inpc);
125da2e3ebdSchin }
126