sl@0
|
1 |
/* WBUF.C
|
sl@0
|
2 |
*
|
sl@0
|
3 |
* Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
4 |
* All rights reserved.
|
sl@0
|
5 |
*/
|
sl@0
|
6 |
|
sl@0
|
7 |
/* No user fns here. Pesch 15apr92. */
|
sl@0
|
8 |
|
sl@0
|
9 |
/*
|
sl@0
|
10 |
* Copyright (c) 1990 The Regents of the University of California.
|
sl@0
|
11 |
* All rights reserved.
|
sl@0
|
12 |
*
|
sl@0
|
13 |
* Redistribution and use in source and binary forms are permitted
|
sl@0
|
14 |
* provided that the above copyright notice and this paragraph are
|
sl@0
|
15 |
* duplicated in all such forms and that any documentation,
|
sl@0
|
16 |
* advertising materials, and other materials related to such
|
sl@0
|
17 |
* distribution and use acknowledge that the software was developed
|
sl@0
|
18 |
* by the University of California, Berkeley. The name of the
|
sl@0
|
19 |
* University may not be used to endorse or promote products derived
|
sl@0
|
20 |
* from this software without specific prior written permission.
|
sl@0
|
21 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
sl@0
|
22 |
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
sl@0
|
23 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
sl@0
|
24 |
*/
|
sl@0
|
25 |
|
sl@0
|
26 |
#include <stdio.h>
|
sl@0
|
27 |
#include "LOCAL.H"
|
sl@0
|
28 |
#include "FVWRITE.H"
|
sl@0
|
29 |
|
sl@0
|
30 |
/*
|
sl@0
|
31 |
* Write the given character into the (probably full) buffer for
|
sl@0
|
32 |
* the given file. Flush the buffer out if it is or becomes full,
|
sl@0
|
33 |
* or if c=='\n' and the file is line buffered.
|
sl@0
|
34 |
*/
|
sl@0
|
35 |
|
sl@0
|
36 |
int
|
sl@0
|
37 |
__swbuf (register int c,register FILE *fp)
|
sl@0
|
38 |
{
|
sl@0
|
39 |
register int n;
|
sl@0
|
40 |
|
sl@0
|
41 |
/* Ensure stdio has been initialized. */
|
sl@0
|
42 |
|
sl@0
|
43 |
CHECK_INIT (fp);
|
sl@0
|
44 |
|
sl@0
|
45 |
/*
|
sl@0
|
46 |
* In case we cannot write, or longjmp takes us out early,
|
sl@0
|
47 |
* make sure _w is 0 (if fully- or un-buffered) or -_bf._size
|
sl@0
|
48 |
* (if line buffered) so that we will get called again.
|
sl@0
|
49 |
* If we did not do this, a sufficient number of putc()
|
sl@0
|
50 |
* calls might wrap _w from negative to positive.
|
sl@0
|
51 |
*/
|
sl@0
|
52 |
|
sl@0
|
53 |
fp->_w = fp->_lbfsize;
|
sl@0
|
54 |
if (cantwrite (fp))
|
sl@0
|
55 |
return EOF;
|
sl@0
|
56 |
c = (unsigned char) c;
|
sl@0
|
57 |
|
sl@0
|
58 |
/*
|
sl@0
|
59 |
* If it is completely full, flush it out. Then, in any case,
|
sl@0
|
60 |
* stuff c into the buffer. If this causes the buffer to fill
|
sl@0
|
61 |
* completely, or if c is '\n' and the file is line buffered,
|
sl@0
|
62 |
* flush it (perhaps a second time). The second flush will always
|
sl@0
|
63 |
* happen on unbuffered streams, where _bf._size==1; fflush()
|
sl@0
|
64 |
* guarantees that putc() will always call wbuf() by setting _w
|
sl@0
|
65 |
* to 0, so we need not do anything else.
|
sl@0
|
66 |
*/
|
sl@0
|
67 |
|
sl@0
|
68 |
n = fp->_p - fp->_bf._base;
|
sl@0
|
69 |
if (n >= fp->_bf._size)
|
sl@0
|
70 |
{
|
sl@0
|
71 |
if (fflush (fp))
|
sl@0
|
72 |
return EOF;
|
sl@0
|
73 |
n = 0;
|
sl@0
|
74 |
}
|
sl@0
|
75 |
fp->_w--;
|
sl@0
|
76 |
*fp->_p++ = (unsigned char)c;
|
sl@0
|
77 |
if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n'))
|
sl@0
|
78 |
if (fflush (fp))
|
sl@0
|
79 |
return EOF;
|
sl@0
|
80 |
return c;
|
sl@0
|
81 |
}
|