xref: /onnv-gate/usr/src/cmd/sort/common/streams_stdio.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "streams_stdio.h"
30*0Sstevel@tonic-gate #include "streams_common.h"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #define	SHELF_OCCUPIED	1
33*0Sstevel@tonic-gate #define	SHELF_VACANT	0
34*0Sstevel@tonic-gate static int shelf = SHELF_VACANT;
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate /*
37*0Sstevel@tonic-gate  * Single-byte character file i/o-based streams implementation
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  *   The routines in this file contain the implementation of the i/o streams
40*0Sstevel@tonic-gate  *   interface for those situations where the input is via stdio.
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * The "shelf"
43*0Sstevel@tonic-gate  *   In the case where the input buffer contains insufficient room to hold the
44*0Sstevel@tonic-gate  *   entire line, the fractional line is shelved, and will be grafted to on the
45*0Sstevel@tonic-gate  *   subsequent read.
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate int
stream_stdio_open_for_write(stream_t * str)48*0Sstevel@tonic-gate stream_stdio_open_for_write(stream_t *str)
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate 	stream_simple_file_t	*SF = &(str->s_type.SF);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	ASSERT(!(str->s_status & STREAM_OPEN));
53*0Sstevel@tonic-gate 	ASSERT(!(str->s_status & STREAM_OUTPUT));
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	if (str->s_status & STREAM_NOTFILE)
56*0Sstevel@tonic-gate 		SF->s_fd = fileno(stdout);
57*0Sstevel@tonic-gate 	else
58*0Sstevel@tonic-gate 		if ((SF->s_fd = open(str->s_filename, O_CREAT | O_TRUNC |
59*0Sstevel@tonic-gate 		    O_WRONLY, OUTPUT_MODE)) < 0) {
60*0Sstevel@tonic-gate 			if (errno == EMFILE || errno == ENFILE)
61*0Sstevel@tonic-gate 				return (-1);
62*0Sstevel@tonic-gate 			else
63*0Sstevel@tonic-gate 				die(EMSG_OPEN, str->s_filename);
64*0Sstevel@tonic-gate 		}
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	stream_set(str, STREAM_OPEN | STREAM_OUTPUT);
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	return (1);
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * In the case of an instantaneous stream, we allocate a small buffer (64k) here
73*0Sstevel@tonic-gate  * for the stream; otherwise, the s_buffer and s_buffer_size members should have
74*0Sstevel@tonic-gate  * been set by stream_set_size() prior to calling stream_prime().
75*0Sstevel@tonic-gate  *
76*0Sstevel@tonic-gate  * Repriming (priming an already primed stream) is done when we are reentering a
77*0Sstevel@tonic-gate  * file after having sorted a previous portion of the file.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate static int
stream_stdio_prime(stream_t * str)80*0Sstevel@tonic-gate stream_stdio_prime(stream_t *str)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	stream_buffered_file_t *BF = &(str->s_type.BF);
83*0Sstevel@tonic-gate 	char *current_position;
84*0Sstevel@tonic-gate 	char *end_of_buffer;
85*0Sstevel@tonic-gate 	char *next_nl;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	ASSERT(!(str->s_status & STREAM_OUTPUT));
88*0Sstevel@tonic-gate 	ASSERT(str->s_status & (STREAM_SINGLE | STREAM_WIDE));
89*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if (str->s_status & STREAM_INSTANT && (str->s_buffer == NULL)) {
92*0Sstevel@tonic-gate 		str->s_buffer = xzmap(0, STDIO_VBUF_SIZE, PROT_READ |
93*0Sstevel@tonic-gate 		    PROT_WRITE, MAP_PRIVATE, 0);
94*0Sstevel@tonic-gate 		if (str->s_buffer == MAP_FAILED)
95*0Sstevel@tonic-gate 			die(EMSG_MMAP);
96*0Sstevel@tonic-gate 		str->s_buffer_size = STDIO_VBUF_SIZE;
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	ASSERT(str->s_buffer != NULL);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	if (stream_is_primed(str)) {
102*0Sstevel@tonic-gate 		/*
103*0Sstevel@tonic-gate 		 * l_data_length is only set to -1 in the case of coincidental
104*0Sstevel@tonic-gate 		 * exhaustion of the input butter.  This is thus the only case
105*0Sstevel@tonic-gate 		 * which involves no copying on a re-prime.
106*0Sstevel@tonic-gate 		 */
107*0Sstevel@tonic-gate 		int shelf_state = shelf;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 		ASSERT(str->s_current.l_data_length >= -1);
110*0Sstevel@tonic-gate 		(void) memcpy(str->s_buffer, str->s_current.l_data.sp,
111*0Sstevel@tonic-gate 		    str->s_current.l_data_length + 1);
112*0Sstevel@tonic-gate 		str->s_current.l_data.sp = str->s_buffer;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 		/*
115*0Sstevel@tonic-gate 		 * If our current line is incomplete, we need to get the rest of
116*0Sstevel@tonic-gate 		 * the line--if we can't, then we've exhausted memory.
117*0Sstevel@tonic-gate 		 */
118*0Sstevel@tonic-gate 		if ((str->s_current.l_data_length == -1 ||
119*0Sstevel@tonic-gate 		    shelf_state == SHELF_OCCUPIED ||
120*0Sstevel@tonic-gate 		    *(str->s_current.l_data.sp +
121*0Sstevel@tonic-gate 		    str->s_current.l_data_length) != '\n') &&
122*0Sstevel@tonic-gate 		    SOP_FETCH(str) == NEXT_LINE_INCOMPLETE &&
123*0Sstevel@tonic-gate 		    shelf_state == SHELF_OCCUPIED)
124*0Sstevel@tonic-gate 			die(EMSG_MEMORY);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 		str->s_current.l_collate.sp = NULL;
127*0Sstevel@tonic-gate 		str->s_current.l_collate_length = 0;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 		return (PRIME_SUCCEEDED);
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	stream_set(str, STREAM_PRIMED);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	current_position = (char *)str->s_buffer;
135*0Sstevel@tonic-gate 	end_of_buffer = (char *)str->s_buffer + str->s_buffer_size;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	trip_eof(BF->s_fp);
138*0Sstevel@tonic-gate 	if (!feof(BF->s_fp))
139*0Sstevel@tonic-gate 		(void) fgets(current_position, end_of_buffer - current_position,
140*0Sstevel@tonic-gate 		    BF->s_fp);
141*0Sstevel@tonic-gate 	else {
142*0Sstevel@tonic-gate 		stream_set(str, STREAM_EOS_REACHED);
143*0Sstevel@tonic-gate 		stream_unset(str, STREAM_PRIMED);
144*0Sstevel@tonic-gate 		return (PRIME_FAILED_EMPTY_FILE);
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	str->s_current.l_data.sp = current_position;
148*0Sstevel@tonic-gate 	/*
149*0Sstevel@tonic-gate 	 * Because one might run sort on a binary file, strlen() is no longer
150*0Sstevel@tonic-gate 	 * trustworthy--we must explicitly search for a newline.
151*0Sstevel@tonic-gate 	 */
152*0Sstevel@tonic-gate 	if ((next_nl = memchr(current_position, '\n',
153*0Sstevel@tonic-gate 	    end_of_buffer - current_position)) == NULL) {
154*0Sstevel@tonic-gate 		warn(WMSG_NEWLINE_ADDED, str->s_filename);
155*0Sstevel@tonic-gate 		str->s_current.l_data_length = MIN(strlen(current_position),
156*0Sstevel@tonic-gate 		    end_of_buffer - current_position);
157*0Sstevel@tonic-gate 	} else {
158*0Sstevel@tonic-gate 		str->s_current.l_data_length = next_nl - current_position;
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	str->s_current.l_collate.sp = NULL;
162*0Sstevel@tonic-gate 	str->s_current.l_collate_length = 0;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	__S(stats_incr_fetches());
165*0Sstevel@tonic-gate 	return (PRIME_SUCCEEDED);
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate  * stream_stdio_fetch() guarantees the return of a complete line, or a flag
170*0Sstevel@tonic-gate  * indicating that the complete line could not be read.
171*0Sstevel@tonic-gate  */
172*0Sstevel@tonic-gate static ssize_t
stream_stdio_fetch(stream_t * str)173*0Sstevel@tonic-gate stream_stdio_fetch(stream_t *str)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	ssize_t	dist_to_buf_end;
176*0Sstevel@tonic-gate 	int ret_val;
177*0Sstevel@tonic-gate 	char *graft_pt, *next_nl;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
180*0Sstevel@tonic-gate 	ASSERT(str->s_status & (STREAM_SINGLE | STREAM_WIDE));
181*0Sstevel@tonic-gate 	ASSERT((str->s_status & STREAM_EOS_REACHED) == 0);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	graft_pt = str->s_current.l_data.sp + str->s_current.l_data_length + 1;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	if (shelf == SHELF_VACANT) {
186*0Sstevel@tonic-gate 		/*
187*0Sstevel@tonic-gate 		 * The graft point is the start of the current line.
188*0Sstevel@tonic-gate 		 */
189*0Sstevel@tonic-gate 		str->s_current.l_data.sp = graft_pt;
190*0Sstevel@tonic-gate 	} else if (str->s_current.l_data_length > -1) {
191*0Sstevel@tonic-gate 		/*
192*0Sstevel@tonic-gate 		 * Correct for terminating NUL on shelved line.  This NUL is
193*0Sstevel@tonic-gate 		 * only present if we didn't have the coincidental case
194*0Sstevel@tonic-gate 		 * mentioned in the comment below.
195*0Sstevel@tonic-gate 		 */
196*0Sstevel@tonic-gate 		graft_pt--;
197*0Sstevel@tonic-gate 	}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	dist_to_buf_end = str->s_buffer_size - (graft_pt -
200*0Sstevel@tonic-gate 	    (char *)str->s_buffer);
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	if (dist_to_buf_end <= 1) {
203*0Sstevel@tonic-gate 		/*
204*0Sstevel@tonic-gate 		 * fgets()'s behaviour in the case of a one-character buffer is
205*0Sstevel@tonic-gate 		 * somewhat unhelpful:  it fills the buffer with '\0' and
206*0Sstevel@tonic-gate 		 * returns successfully (even if EOF has been reached for the
207*0Sstevel@tonic-gate 		 * file in question).  Since we may be in the middle of a
208*0Sstevel@tonic-gate 		 * grafting operation, we leave early, maintaining the shelf in
209*0Sstevel@tonic-gate 		 * its current state.
210*0Sstevel@tonic-gate 		 */
211*0Sstevel@tonic-gate 		str->s_current.l_data_length = -1;
212*0Sstevel@tonic-gate 		return (NEXT_LINE_INCOMPLETE);
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	if (fgets(graft_pt, dist_to_buf_end, str->s_type.BF.s_fp) == NULL) {
216*0Sstevel@tonic-gate 		if (feof(str->s_type.BF.s_fp))
217*0Sstevel@tonic-gate 			stream_set(str, STREAM_EOS_REACHED);
218*0Sstevel@tonic-gate 		else
219*0Sstevel@tonic-gate 			die(EMSG_READ, str->s_filename);
220*0Sstevel@tonic-gate 	}
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	trip_eof(str->s_type.BF.s_fp);
223*0Sstevel@tonic-gate 	/*
224*0Sstevel@tonic-gate 	 * Because one might run sort on a binary file, strlen() is no longer
225*0Sstevel@tonic-gate 	 * trustworthy--we must explicitly search for a newline.
226*0Sstevel@tonic-gate 	 */
227*0Sstevel@tonic-gate 	if ((next_nl = memchr(str->s_current.l_data.sp, '\n',
228*0Sstevel@tonic-gate 	    dist_to_buf_end)) == NULL) {
229*0Sstevel@tonic-gate 		str->s_current.l_data_length = strlen(str->s_current.l_data.sp);
230*0Sstevel@tonic-gate 	} else {
231*0Sstevel@tonic-gate 		str->s_current.l_data_length = next_nl -
232*0Sstevel@tonic-gate 		    str->s_current.l_data.sp;
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	str->s_current.l_collate_length = 0;
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	if (*(str->s_current.l_data.sp + str->s_current.l_data_length) !=
238*0Sstevel@tonic-gate 	    '\n') {
239*0Sstevel@tonic-gate 		if (!feof(str->s_type.BF.s_fp)) {
240*0Sstevel@tonic-gate 			/*
241*0Sstevel@tonic-gate 			 * We were only able to read part of the line; note that
242*0Sstevel@tonic-gate 			 * we have something on the shelf for our next fetch.
243*0Sstevel@tonic-gate 			 * If the shelf was previously occupied, and we still
244*0Sstevel@tonic-gate 			 * can't get the entire line, then we need more
245*0Sstevel@tonic-gate 			 * resources.
246*0Sstevel@tonic-gate 			 */
247*0Sstevel@tonic-gate 			if (shelf == SHELF_OCCUPIED)
248*0Sstevel@tonic-gate 				die(EMSG_MEMORY);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 			shelf = SHELF_OCCUPIED;
251*0Sstevel@tonic-gate 			ret_val = NEXT_LINE_INCOMPLETE;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 			__S(stats_incr_shelves());
254*0Sstevel@tonic-gate 		} else {
255*0Sstevel@tonic-gate 			stream_set(str, STREAM_EOS_REACHED);
256*0Sstevel@tonic-gate 			warn(WMSG_NEWLINE_ADDED, str->s_filename);
257*0Sstevel@tonic-gate 		}
258*0Sstevel@tonic-gate 	} else {
259*0Sstevel@tonic-gate 		shelf = SHELF_VACANT;
260*0Sstevel@tonic-gate 		ret_val = NEXT_LINE_COMPLETE;
261*0Sstevel@tonic-gate 		__S(stats_incr_fetches());
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	return (ret_val);
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate /*
268*0Sstevel@tonic-gate  * stdio_fetch_overwrite() is used when we are performing an operation where we
269*0Sstevel@tonic-gate  * need the buffer contents only over a single period.  (merge and check are
270*0Sstevel@tonic-gate  * operations of this kind.)  In this case, we read the current line at the head
271*0Sstevel@tonic-gate  * of the stream's defined buffer.  If we cannot read the entire line, we have
272*0Sstevel@tonic-gate  * not allocated sufficient memory.
273*0Sstevel@tonic-gate  */
274*0Sstevel@tonic-gate ssize_t
stream_stdio_fetch_overwrite(stream_t * str)275*0Sstevel@tonic-gate stream_stdio_fetch_overwrite(stream_t *str)
276*0Sstevel@tonic-gate {
277*0Sstevel@tonic-gate 	ssize_t	dist_to_buf_end;
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
280*0Sstevel@tonic-gate 	ASSERT(str->s_status & (STREAM_SINGLE | STREAM_WIDE));
281*0Sstevel@tonic-gate 	ASSERT((str->s_status & STREAM_EOS_REACHED) == 0);
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	str->s_current.l_data.sp = str->s_buffer;
284*0Sstevel@tonic-gate 	dist_to_buf_end = str->s_buffer_size;
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	if (fgets(str->s_current.l_data.sp, dist_to_buf_end,
287*0Sstevel@tonic-gate 	    str->s_type.BF.s_fp) == NULL) {
288*0Sstevel@tonic-gate 		if (feof(str->s_type.BF.s_fp))
289*0Sstevel@tonic-gate 			stream_set(str, STREAM_EOS_REACHED);
290*0Sstevel@tonic-gate 		else
291*0Sstevel@tonic-gate 			die(EMSG_READ, str->s_filename);
292*0Sstevel@tonic-gate 	}
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	trip_eof(str->s_type.BF.s_fp);
295*0Sstevel@tonic-gate 	str->s_current.l_data_length = strlen(str->s_current.l_data.sp) - 1;
296*0Sstevel@tonic-gate 	str->s_current.l_collate_length = 0;
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	if (str->s_current.l_data_length == -1 ||
299*0Sstevel@tonic-gate 	    *(str->s_current.l_data.sp + str->s_current.l_data_length) !=
300*0Sstevel@tonic-gate 	    '\n') {
301*0Sstevel@tonic-gate 		if (!feof(str->s_type.BF.s_fp)) {
302*0Sstevel@tonic-gate 			/*
303*0Sstevel@tonic-gate 			 * In the overwrite case, failure to read the entire
304*0Sstevel@tonic-gate 			 * line means our buffer size was insufficient (as we
305*0Sstevel@tonic-gate 			 * are using all of it).  Exit, requesting more
306*0Sstevel@tonic-gate 			 * resources.
307*0Sstevel@tonic-gate 			 */
308*0Sstevel@tonic-gate 			die(EMSG_MEMORY);
309*0Sstevel@tonic-gate 		} else {
310*0Sstevel@tonic-gate 			stream_set(str, STREAM_EOS_REACHED);
311*0Sstevel@tonic-gate 			warn(WMSG_NEWLINE_ADDED, str->s_filename);
312*0Sstevel@tonic-gate 		}
313*0Sstevel@tonic-gate 	}
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	__S(stats_incr_fetches());
316*0Sstevel@tonic-gate 	return (NEXT_LINE_COMPLETE);
317*0Sstevel@tonic-gate }
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate int
stream_stdio_is_closable(stream_t * str)320*0Sstevel@tonic-gate stream_stdio_is_closable(stream_t *str)
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	if (str->s_status & STREAM_OPEN && !(str->s_status & STREAM_NOTFILE))
323*0Sstevel@tonic-gate 		return (1);
324*0Sstevel@tonic-gate 	return (0);
325*0Sstevel@tonic-gate }
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate int
stream_stdio_close(stream_t * str)328*0Sstevel@tonic-gate stream_stdio_close(stream_t *str)
329*0Sstevel@tonic-gate {
330*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	if (!(str->s_status & STREAM_OUTPUT)) {
333*0Sstevel@tonic-gate 		if (!(str->s_status & STREAM_NOTFILE))
334*0Sstevel@tonic-gate 			(void) fclose(str->s_type.BF.s_fp);
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 		if (str->s_type.BF.s_vbuf != NULL) {
337*0Sstevel@tonic-gate 			free(str->s_type.BF.s_vbuf);
338*0Sstevel@tonic-gate 			str->s_type.BF.s_vbuf = NULL;
339*0Sstevel@tonic-gate 		}
340*0Sstevel@tonic-gate 	} else {
341*0Sstevel@tonic-gate 		if (cxwrite(str->s_type.SF.s_fd, NULL, 0) == 0)
342*0Sstevel@tonic-gate 			(void) close(str->s_type.SF.s_fd);
343*0Sstevel@tonic-gate 		else
344*0Sstevel@tonic-gate 			die(EMSG_WRITE, str->s_filename);
345*0Sstevel@tonic-gate 	}
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	stream_unset(str, STREAM_OPEN | STREAM_PRIMED | STREAM_OUTPUT);
348*0Sstevel@tonic-gate 	return (1);
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate static void
stream_stdio_send_eol(stream_t * str)352*0Sstevel@tonic-gate stream_stdio_send_eol(stream_t *str)
353*0Sstevel@tonic-gate {
354*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
355*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OUTPUT);
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	if (cxwrite(str->s_type.SF.s_fd, "\n", 1) < 0)
358*0Sstevel@tonic-gate 		die(EMSG_WRITE, str->s_filename);
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate void
stream_stdio_flush(stream_t * str)362*0Sstevel@tonic-gate stream_stdio_flush(stream_t *str)
363*0Sstevel@tonic-gate {
364*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
365*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OUTPUT);
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	if (cxwrite(str->s_type.SF.s_fd, NULL, 0) < 0)
368*0Sstevel@tonic-gate 		die(EMSG_WRITE, str->s_filename);
369*0Sstevel@tonic-gate }
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate static void
stream_stdio_put_line(stream_t * str,line_rec_t * line)372*0Sstevel@tonic-gate stream_stdio_put_line(stream_t *str, line_rec_t *line)
373*0Sstevel@tonic-gate {
374*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
375*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OUTPUT);
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	if (line->l_data_length >= 0) {
378*0Sstevel@tonic-gate 		if (cxwrite(str->s_type.SF.s_fd, line->l_data.sp,
379*0Sstevel@tonic-gate 		    line->l_data_length) < 0)
380*0Sstevel@tonic-gate 			die(EMSG_WRITE, str->s_filename);
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 		stream_stdio_send_eol(str);
383*0Sstevel@tonic-gate 		__S(stats_incr_puts());
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 	safe_free(line->l_raw_collate.sp);
386*0Sstevel@tonic-gate 	line->l_raw_collate.sp = NULL;
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate void
stream_stdio_put_line_unique(stream_t * str,line_rec_t * line)390*0Sstevel@tonic-gate stream_stdio_put_line_unique(stream_t *str, line_rec_t *line)
391*0Sstevel@tonic-gate {
392*0Sstevel@tonic-gate 	static line_rec_t pvs;
393*0Sstevel@tonic-gate 	static size_t collate_buf_len;
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
396*0Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OUTPUT);
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	if (pvs.l_collate.sp != NULL &&
399*0Sstevel@tonic-gate 	    collated(&pvs, line, 0, COLL_UNIQUE) == 0) {
400*0Sstevel@tonic-gate 		__S(stats_incr_not_unique());
401*0Sstevel@tonic-gate 		return;
402*0Sstevel@tonic-gate 	}
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	__S(stats_incr_put_unique());
405*0Sstevel@tonic-gate 	stream_stdio_put_line(str, line);
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	if (line->l_collate_length + 1 > collate_buf_len) {
408*0Sstevel@tonic-gate 		pvs.l_collate.sp = safe_realloc(pvs.l_collate.sp,
409*0Sstevel@tonic-gate 		    line->l_collate_length + 1);
410*0Sstevel@tonic-gate 		collate_buf_len = line->l_collate_length + 1;
411*0Sstevel@tonic-gate 	}
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	(void) memcpy(pvs.l_collate.sp, line->l_collate.sp,
414*0Sstevel@tonic-gate 	    line->l_collate_length);
415*0Sstevel@tonic-gate 	*(pvs.l_collate.sp + line->l_collate_length) = '\0';
416*0Sstevel@tonic-gate 	pvs.l_collate_length = line->l_collate_length;
417*0Sstevel@tonic-gate }
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate int
stream_stdio_unlink(stream_t * str)420*0Sstevel@tonic-gate stream_stdio_unlink(stream_t *str)
421*0Sstevel@tonic-gate {
422*0Sstevel@tonic-gate 	if (!(str->s_status & STREAM_NOTFILE))
423*0Sstevel@tonic-gate 		return (unlink(str->s_filename));
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 	return (0);
426*0Sstevel@tonic-gate }
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate int
stream_stdio_free(stream_t * str)429*0Sstevel@tonic-gate stream_stdio_free(stream_t *str)
430*0Sstevel@tonic-gate {
431*0Sstevel@tonic-gate 	/*
432*0Sstevel@tonic-gate 	 * Unmap the memory we allocated for input, if it's valid to do so.
433*0Sstevel@tonic-gate 	 */
434*0Sstevel@tonic-gate 	if (!(str->s_status & STREAM_OPEN) ||
435*0Sstevel@tonic-gate 	    (str->s_consumer != NULL &&
436*0Sstevel@tonic-gate 	    str->s_consumer->s_status & STREAM_NOT_FREEABLE))
437*0Sstevel@tonic-gate 		return (0);
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	if (str->s_buffer != NULL) {
440*0Sstevel@tonic-gate 		if (munmap(str->s_buffer, str->s_buffer_size) < 0)
441*0Sstevel@tonic-gate 			die(EMSG_MUNMAP, "/dev/zero");
442*0Sstevel@tonic-gate 		else {
443*0Sstevel@tonic-gate 			str->s_buffer = NULL;
444*0Sstevel@tonic-gate 			str->s_buffer_size = 0;
445*0Sstevel@tonic-gate 		}
446*0Sstevel@tonic-gate 	}
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	stream_unset(str, STREAM_PRIMED | STREAM_INSTANT);
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	return (1);
451*0Sstevel@tonic-gate }
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate static int
stream_stdio_eos(stream_t * str)454*0Sstevel@tonic-gate stream_stdio_eos(stream_t *str)
455*0Sstevel@tonic-gate {
456*0Sstevel@tonic-gate 	int retval = 0;
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	ASSERT(!(str->s_status & STREAM_OUTPUT));
459*0Sstevel@tonic-gate 	ASSERT(str->s_status & (STREAM_SINGLE | STREAM_WIDE));
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	if (str == NULL || str->s_status & STREAM_EOS_REACHED)
462*0Sstevel@tonic-gate 		return (1);
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	trip_eof(str->s_type.BF.s_fp);
465*0Sstevel@tonic-gate 	if (feof(str->s_type.BF.s_fp) &&
466*0Sstevel@tonic-gate 	    shelf == SHELF_VACANT &&
467*0Sstevel@tonic-gate 	    str->s_current.l_collate_length != -1) {
468*0Sstevel@tonic-gate 		retval = 1;
469*0Sstevel@tonic-gate 		stream_set(str, STREAM_EOS_REACHED);
470*0Sstevel@tonic-gate 	}
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 	return (retval);
473*0Sstevel@tonic-gate }
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate /*ARGSUSED*/
476*0Sstevel@tonic-gate static void
stream_stdio_release_line(stream_t * str)477*0Sstevel@tonic-gate stream_stdio_release_line(stream_t *str)
478*0Sstevel@tonic-gate {
479*0Sstevel@tonic-gate }
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate const stream_ops_t stream_stdio_ops = {
482*0Sstevel@tonic-gate 	stream_stdio_is_closable,
483*0Sstevel@tonic-gate 	stream_stdio_close,
484*0Sstevel@tonic-gate 	stream_stdio_eos,
485*0Sstevel@tonic-gate 	stream_stdio_fetch,
486*0Sstevel@tonic-gate 	stream_stdio_flush,
487*0Sstevel@tonic-gate 	stream_stdio_free,
488*0Sstevel@tonic-gate 	stream_stdio_open_for_write,
489*0Sstevel@tonic-gate 	stream_stdio_prime,
490*0Sstevel@tonic-gate 	stream_stdio_put_line,
491*0Sstevel@tonic-gate 	stream_stdio_release_line,
492*0Sstevel@tonic-gate 	stream_stdio_send_eol,
493*0Sstevel@tonic-gate 	stream_stdio_unlink
494*0Sstevel@tonic-gate };
495