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