summaryrefslogtreecommitdiffstats
path: root/private/crt32/stdio/setvbuf.c
blob: 665cee028092a006464218a9346c1a55c69bb5c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***
*setvbuf.c - set buffer size for a stream
*
*	Copyright (c) 1985-1993, Microsoft Corporation. All rights reserved.
*
*Purpose:
*	defines setvbuf() - set the buffering mode and size for a stream.
*
*Revision History:
*	09-19-83  RN	initial version
*	06-26-85  TC	modified to allow user defined buffers of various sizes
*	06-24-86  DFW	kludged to fix incompatability with Xenix values of
*			_IOFBF, _IOLBF
*	02-09-87  JCR	added "buffer=&(_iob2[fileno(stream)]._charbuf);"
*			to handle _IONBF case
*	02-25-87  JCR	added support for default buffer and IBMC20-condition
*			code
*	04-13-87  JCR	changed type of szie from int to size_t (unsigned int)
*			and changed a related comparison
*	06-29-87  JCR	Took out the _OLD_IOFBF/_OLD_IOLBF kludge for MSC.
*			Should be taken out for IBM too...
*	09-28-87  JCR	Corrected _iob2 indexing (now uses _iob_index() macro).
*	11-02-87  JCR	Multi-thread support
*	12-11-87  JCR	Added "_LOAD_DS" to declaration
*	05-27-88  PHG	Merged DLL and normal versions
*	06-06-88  JCR	Optimized _iob2 references
*	06-14-88  JCR	Use near pointer to reference _iob[] entries
*	08-09-88  JCR	Buffer size can't be greater than INT_MAX
*	08-25-88  GJF	Don't use FP_OFF() macro for the 386
*	08-18-89  GJF	Clean up, now specific to OS/2 2.0 (i.e., 386 flat
*			model). Also fixed copyright and indents.
*	02-15-90  GJF	_iob[], _iob2[] merge. Also, cleanup, a little tuning
*			and fixed copyright.
*	03-19-90  GJF	Made calling type _CALLTYPE1, added #include
*			<cruntime.h> and removed #include <register.h>.
*	05-29-90  SBM   Use _flush, not [_]fflush[_lk]
*	07-23-90  SBM	Replaced <assertm.h> by <assert.h>
*	10-03-90  GJF	New-style function declarator.
*	04-27-93  CFW	Change _IONBF size to 2 bytes to hold wide char.
*
*******************************************************************************/

#include <cruntime.h>
#include <stdio.h>
#include <file2.h>
#include <assert.h>
#include <malloc.h>
#include <internal.h>
#include <os2dll.h>
#include <limits.h>

/***
*int setvbuf(stream, buffer, type, size) - set buffering for a file
*
*Purpose:
*	Controls buffering and buffer size for the specified stream.  The
*	array pointed to by buf is used as a buffer, unless NULL, in which
*	case we'll allocate a buffer automatically. type specifies the type
*	of buffering: _IONBF = no buffer, _IOFBF = buffered, _IOLBF = same
*	as _IOFBF.
*
*Entry:
*	FILE *stream - stream to control buffer on
*	char *buffer - pointer to buffer to use (NULL means auto allocate)
*	int type     - type of buffering (_IONBF, _IOFBF or _IOLBF)
*	size_t size  - size of buffer
*
*Exit:
*	return 0 if successful
*	returns non-zero if fails
*
*Exceptions:
*
*******************************************************************************/

int _CRTAPI1 setvbuf (
	FILE *str,
	char *buffer,
	int type,
	size_t size
	)
{
	REG1 FILE *stream;
#ifdef MTHREAD
	int index;
#endif
	int retval=0;	/* assume good return */

	assert(str != NULL);

	/*
	 * (1) Make sure type is one of the three legal values.
	 * (2) If we are buffering, make sure size is greater than 0.
	 */
	if ( (type != _IONBF) && ((size == 0) || (size > INT_MAX) ||
	((type != _IOFBF) && (type != _IOLBF))) )
		return(-1);

	/* force size to be even */
	size &= (size_t)~1;

	/*
	 * Init stream pointers
	 */
	stream = str;

#ifdef MTHREAD
	index = _iob_index(stream);
#endif
	/*
	 * Lock the file
	 */
	_lock_str(index);

	/*
	 * Flush the current buffer and free it, if it is ours.
	 */
	_flush(stream);
	_freebuf(stream);

	/*
	 * Clear a bunch of bits in stream->_flag (all bits related to
	 * buffering and those which used to be in stream2->_flag2). Most
	 * of these should never be set when setvbuf() is called, but it
	 * doesn't cost anything to be safe.
	 */
	stream->_flag &= ~(_IOMYBUF | _IOYOURBUF | _IONBF | _IOFEOF |
	_IOFLRTN | _IOCTRLZ);

	/*
	 * CASE 1: No Buffering.
	 */
	if (type & _IONBF) {
		stream->_flag |= _IONBF;
		buffer = (char *)&(stream->_charbuf);
		size = 2;
	}

	/*
	 * NOTE: Cases 2 and 3 (below) cover type == _IOFBF or type == _IOLBF
	 * Line buffering is treated as the same as full buffering, so the
	 * _IOLBF bit in stream->_flag is never set. Finally, since _IOFBF is
	 * defined to be 0, full buffering is simply assumed whenever _IONBF
	 * is not set.
	 */

	/*
	 * CASE 2: Default Buffering -- Allocate a buffer for the user.
	 */
	else if ( buffer == NULL ) {
		if ( (buffer = malloc(size)) == NULL ) {
			/*
			 * force library pre-termination procedure (placed here
			 * because the code path should almost never be hit)
			 */
			_cflush++;

			retval = -1;
			goto done;
		}
		stream->_flag |= _IOMYBUF;
	}

	/*
	 * CASE 3: User Buffering -- Use the buffer supplied by the user.
	 */
	else {
		stream->_flag |= _IOYOURBUF;
	}

	/*
	 * Common return for all cases.
	 */
	stream->_bufsiz = size;
	stream->_ptr = stream->_base = buffer;
	stream->_cnt = 0;
done:
	_unlock_str(index);
	return(retval);
}