xref: /illumos-gate/usr/src/cmd/sort/streams_stdio.c (revision 101e15b5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include "streams_stdio.h"
287c478bd9Sstevel@tonic-gate #include "streams_common.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #define	SHELF_OCCUPIED	1
317c478bd9Sstevel@tonic-gate #define	SHELF_VACANT	0
327c478bd9Sstevel@tonic-gate static int shelf = SHELF_VACANT;
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * Single-byte character file i/o-based streams implementation
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *   The routines in this file contain the implementation of the i/o streams
387c478bd9Sstevel@tonic-gate  *   interface for those situations where the input is via stdio.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * The "shelf"
417c478bd9Sstevel@tonic-gate  *   In the case where the input buffer contains insufficient room to hold the
427c478bd9Sstevel@tonic-gate  *   entire line, the fractional line is shelved, and will be grafted to on the
437c478bd9Sstevel@tonic-gate  *   subsequent read.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate int
stream_stdio_open_for_write(stream_t * str)467c478bd9Sstevel@tonic-gate stream_stdio_open_for_write(stream_t *str)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	stream_simple_file_t	*SF = &(str->s_type.SF);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	ASSERT(!(str->s_status & STREAM_OPEN));
517c478bd9Sstevel@tonic-gate 	ASSERT(!(str->s_status & STREAM_OUTPUT));
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if (str->s_status & STREAM_NOTFILE)
547c478bd9Sstevel@tonic-gate 		SF->s_fd = fileno(stdout);
557c478bd9Sstevel@tonic-gate 	else
567c478bd9Sstevel@tonic-gate 		if ((SF->s_fd = open(str->s_filename, O_CREAT | O_TRUNC |
577c478bd9Sstevel@tonic-gate 		    O_WRONLY, OUTPUT_MODE)) < 0) {
587c478bd9Sstevel@tonic-gate 			if (errno == EMFILE || errno == ENFILE)
597c478bd9Sstevel@tonic-gate 				return (-1);
607c478bd9Sstevel@tonic-gate 			else
617c478bd9Sstevel@tonic-gate 				die(EMSG_OPEN, str->s_filename);
627c478bd9Sstevel@tonic-gate 		}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	stream_set(str, STREAM_OPEN | STREAM_OUTPUT);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	return (1);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * In the case of an instantaneous stream, we allocate a small buffer (64k) here
717c478bd9Sstevel@tonic-gate  * for the stream; otherwise, the s_buffer and s_buffer_size members should have
727c478bd9Sstevel@tonic-gate  * been set by stream_set_size() prior to calling stream_prime().
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * Repriming (priming an already primed stream) is done when we are reentering a
757c478bd9Sstevel@tonic-gate  * file after having sorted a previous portion of the file.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate static int
stream_stdio_prime(stream_t * str)787c478bd9Sstevel@tonic-gate stream_stdio_prime(stream_t *str)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	stream_buffered_file_t *BF = &(str->s_type.BF);
817c478bd9Sstevel@tonic-gate 	char *current_position;
827c478bd9Sstevel@tonic-gate 	char *end_of_buffer;
837c478bd9Sstevel@tonic-gate 	char *next_nl;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	ASSERT(!(str->s_status & STREAM_OUTPUT));
867c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & (STREAM_SINGLE | STREAM_WIDE));
877c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (str->s_status & STREAM_INSTANT && (str->s_buffer == NULL)) {
907c478bd9Sstevel@tonic-gate 		str->s_buffer = xzmap(0, STDIO_VBUF_SIZE, PROT_READ |
917c478bd9Sstevel@tonic-gate 		    PROT_WRITE, MAP_PRIVATE, 0);
927c478bd9Sstevel@tonic-gate 		if (str->s_buffer == MAP_FAILED)
937c478bd9Sstevel@tonic-gate 			die(EMSG_MMAP);
947c478bd9Sstevel@tonic-gate 		str->s_buffer_size = STDIO_VBUF_SIZE;
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	ASSERT(str->s_buffer != NULL);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (stream_is_primed(str)) {
1007c478bd9Sstevel@tonic-gate 		/*
1017c478bd9Sstevel@tonic-gate 		 * l_data_length is only set to -1 in the case of coincidental
1027c478bd9Sstevel@tonic-gate 		 * exhaustion of the input butter.  This is thus the only case
1037c478bd9Sstevel@tonic-gate 		 * which involves no copying on a re-prime.
1047c478bd9Sstevel@tonic-gate 		 */
1057c478bd9Sstevel@tonic-gate 		int shelf_state = shelf;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		ASSERT(str->s_current.l_data_length >= -1);
1087c478bd9Sstevel@tonic-gate 		(void) memcpy(str->s_buffer, str->s_current.l_data.sp,
1097c478bd9Sstevel@tonic-gate 		    str->s_current.l_data_length + 1);
1107c478bd9Sstevel@tonic-gate 		str->s_current.l_data.sp = str->s_buffer;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		/*
1137c478bd9Sstevel@tonic-gate 		 * If our current line is incomplete, we need to get the rest of
1147c478bd9Sstevel@tonic-gate 		 * the line--if we can't, then we've exhausted memory.
1157c478bd9Sstevel@tonic-gate 		 */
1167c478bd9Sstevel@tonic-gate 		if ((str->s_current.l_data_length == -1 ||
1177c478bd9Sstevel@tonic-gate 		    shelf_state == SHELF_OCCUPIED ||
1187c478bd9Sstevel@tonic-gate 		    *(str->s_current.l_data.sp +
1197c478bd9Sstevel@tonic-gate 		    str->s_current.l_data_length) != '\n') &&
1207c478bd9Sstevel@tonic-gate 		    SOP_FETCH(str) == NEXT_LINE_INCOMPLETE &&
1217c478bd9Sstevel@tonic-gate 		    shelf_state == SHELF_OCCUPIED)
1227c478bd9Sstevel@tonic-gate 			die(EMSG_MEMORY);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		str->s_current.l_collate.sp = NULL;
1257c478bd9Sstevel@tonic-gate 		str->s_current.l_collate_length = 0;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		return (PRIME_SUCCEEDED);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	stream_set(str, STREAM_PRIMED);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	current_position = (char *)str->s_buffer;
1337c478bd9Sstevel@tonic-gate 	end_of_buffer = (char *)str->s_buffer + str->s_buffer_size;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	trip_eof(BF->s_fp);
1367c478bd9Sstevel@tonic-gate 	if (!feof(BF->s_fp))
1377c478bd9Sstevel@tonic-gate 		(void) fgets(current_position, end_of_buffer - current_position,
1387c478bd9Sstevel@tonic-gate 		    BF->s_fp);
1397c478bd9Sstevel@tonic-gate 	else {
1407c478bd9Sstevel@tonic-gate 		stream_set(str, STREAM_EOS_REACHED);
1417c478bd9Sstevel@tonic-gate 		stream_unset(str, STREAM_PRIMED);
1427c478bd9Sstevel@tonic-gate 		return (PRIME_FAILED_EMPTY_FILE);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	str->s_current.l_data.sp = current_position;
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Because one might run sort on a binary file, strlen() is no longer
1487c478bd9Sstevel@tonic-gate 	 * trustworthy--we must explicitly search for a newline.
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	if ((next_nl = memchr(current_position, '\n',
1517c478bd9Sstevel@tonic-gate 	    end_of_buffer - current_position)) == NULL) {
1527c478bd9Sstevel@tonic-gate 		warn(WMSG_NEWLINE_ADDED, str->s_filename);
1537c478bd9Sstevel@tonic-gate 		str->s_current.l_data_length = MIN(strlen(current_position),
1547c478bd9Sstevel@tonic-gate 		    end_of_buffer - current_position);
1557c478bd9Sstevel@tonic-gate 	} else {
1567c478bd9Sstevel@tonic-gate 		str->s_current.l_data_length = next_nl - current_position;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	str->s_current.l_collate.sp = NULL;
1607c478bd9Sstevel@tonic-gate 	str->s_current.l_collate_length = 0;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	__S(stats_incr_fetches());
1637c478bd9Sstevel@tonic-gate 	return (PRIME_SUCCEEDED);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * stream_stdio_fetch() guarantees the return of a complete line, or a flag
1687c478bd9Sstevel@tonic-gate  * indicating that the complete line could not be read.
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate static ssize_t
stream_stdio_fetch(stream_t * str)1717c478bd9Sstevel@tonic-gate stream_stdio_fetch(stream_t *str)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	ssize_t	dist_to_buf_end;
1747c478bd9Sstevel@tonic-gate 	int ret_val;
1757c478bd9Sstevel@tonic-gate 	char *graft_pt, *next_nl;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
1787c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & (STREAM_SINGLE | STREAM_WIDE));
1797c478bd9Sstevel@tonic-gate 	ASSERT((str->s_status & STREAM_EOS_REACHED) == 0);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	graft_pt = str->s_current.l_data.sp + str->s_current.l_data_length + 1;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (shelf == SHELF_VACANT) {
1847c478bd9Sstevel@tonic-gate 		/*
1857c478bd9Sstevel@tonic-gate 		 * The graft point is the start of the current line.
1867c478bd9Sstevel@tonic-gate 		 */
1877c478bd9Sstevel@tonic-gate 		str->s_current.l_data.sp = graft_pt;
1887c478bd9Sstevel@tonic-gate 	} else if (str->s_current.l_data_length > -1) {
1897c478bd9Sstevel@tonic-gate 		/*
1907c478bd9Sstevel@tonic-gate 		 * Correct for terminating NUL on shelved line.  This NUL is
1917c478bd9Sstevel@tonic-gate 		 * only present if we didn't have the coincidental case
1927c478bd9Sstevel@tonic-gate 		 * mentioned in the comment below.
1937c478bd9Sstevel@tonic-gate 		 */
1947c478bd9Sstevel@tonic-gate 		graft_pt--;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	dist_to_buf_end = str->s_buffer_size - (graft_pt -
1987c478bd9Sstevel@tonic-gate 	    (char *)str->s_buffer);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (dist_to_buf_end <= 1) {
2017c478bd9Sstevel@tonic-gate 		/*
2027c478bd9Sstevel@tonic-gate 		 * fgets()'s behaviour in the case of a one-character buffer is
2037c478bd9Sstevel@tonic-gate 		 * somewhat unhelpful:  it fills the buffer with '\0' and
2047c478bd9Sstevel@tonic-gate 		 * returns successfully (even if EOF has been reached for the
2057c478bd9Sstevel@tonic-gate 		 * file in question).  Since we may be in the middle of a
2067c478bd9Sstevel@tonic-gate 		 * grafting operation, we leave early, maintaining the shelf in
2077c478bd9Sstevel@tonic-gate 		 * its current state.
2087c478bd9Sstevel@tonic-gate 		 */
2097c478bd9Sstevel@tonic-gate 		str->s_current.l_data_length = -1;
2107c478bd9Sstevel@tonic-gate 		return (NEXT_LINE_INCOMPLETE);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (fgets(graft_pt, dist_to_buf_end, str->s_type.BF.s_fp) == NULL) {
2147c478bd9Sstevel@tonic-gate 		if (feof(str->s_type.BF.s_fp))
2157c478bd9Sstevel@tonic-gate 			stream_set(str, STREAM_EOS_REACHED);
2167c478bd9Sstevel@tonic-gate 		else
2177c478bd9Sstevel@tonic-gate 			die(EMSG_READ, str->s_filename);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	trip_eof(str->s_type.BF.s_fp);
2217c478bd9Sstevel@tonic-gate 	/*
2227c478bd9Sstevel@tonic-gate 	 * Because one might run sort on a binary file, strlen() is no longer
2237c478bd9Sstevel@tonic-gate 	 * trustworthy--we must explicitly search for a newline.
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	if ((next_nl = memchr(str->s_current.l_data.sp, '\n',
2267c478bd9Sstevel@tonic-gate 	    dist_to_buf_end)) == NULL) {
2277c478bd9Sstevel@tonic-gate 		str->s_current.l_data_length = strlen(str->s_current.l_data.sp);
2287c478bd9Sstevel@tonic-gate 	} else {
2297c478bd9Sstevel@tonic-gate 		str->s_current.l_data_length = next_nl -
2307c478bd9Sstevel@tonic-gate 		    str->s_current.l_data.sp;
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	str->s_current.l_collate_length = 0;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (*(str->s_current.l_data.sp + str->s_current.l_data_length) !=
2367c478bd9Sstevel@tonic-gate 	    '\n') {
2377c478bd9Sstevel@tonic-gate 		if (!feof(str->s_type.BF.s_fp)) {
2387c478bd9Sstevel@tonic-gate 			/*
2397c478bd9Sstevel@tonic-gate 			 * We were only able to read part of the line; note that
2407c478bd9Sstevel@tonic-gate 			 * we have something on the shelf for our next fetch.
2417c478bd9Sstevel@tonic-gate 			 * If the shelf was previously occupied, and we still
2427c478bd9Sstevel@tonic-gate 			 * can't get the entire line, then we need more
2437c478bd9Sstevel@tonic-gate 			 * resources.
2447c478bd9Sstevel@tonic-gate 			 */
2457c478bd9Sstevel@tonic-gate 			if (shelf == SHELF_OCCUPIED)
2467c478bd9Sstevel@tonic-gate 				die(EMSG_MEMORY);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 			shelf = SHELF_OCCUPIED;
2497c478bd9Sstevel@tonic-gate 			ret_val = NEXT_LINE_INCOMPLETE;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 			__S(stats_incr_shelves());
2527c478bd9Sstevel@tonic-gate 		} else {
2537c478bd9Sstevel@tonic-gate 			stream_set(str, STREAM_EOS_REACHED);
2547c478bd9Sstevel@tonic-gate 			warn(WMSG_NEWLINE_ADDED, str->s_filename);
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 	} else {
2577c478bd9Sstevel@tonic-gate 		shelf = SHELF_VACANT;
2587c478bd9Sstevel@tonic-gate 		ret_val = NEXT_LINE_COMPLETE;
2597c478bd9Sstevel@tonic-gate 		__S(stats_incr_fetches());
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	return (ret_val);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * stdio_fetch_overwrite() is used when we are performing an operation where we
2677c478bd9Sstevel@tonic-gate  * need the buffer contents only over a single period.  (merge and check are
2687c478bd9Sstevel@tonic-gate  * operations of this kind.)  In this case, we read the current line at the head
2697c478bd9Sstevel@tonic-gate  * of the stream's defined buffer.  If we cannot read the entire line, we have
2707c478bd9Sstevel@tonic-gate  * not allocated sufficient memory.
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate ssize_t
stream_stdio_fetch_overwrite(stream_t * str)2737c478bd9Sstevel@tonic-gate stream_stdio_fetch_overwrite(stream_t *str)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	ssize_t	dist_to_buf_end;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
2787c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & (STREAM_SINGLE | STREAM_WIDE));
2797c478bd9Sstevel@tonic-gate 	ASSERT((str->s_status & STREAM_EOS_REACHED) == 0);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	str->s_current.l_data.sp = str->s_buffer;
2827c478bd9Sstevel@tonic-gate 	dist_to_buf_end = str->s_buffer_size;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (fgets(str->s_current.l_data.sp, dist_to_buf_end,
2857c478bd9Sstevel@tonic-gate 	    str->s_type.BF.s_fp) == NULL) {
2867c478bd9Sstevel@tonic-gate 		if (feof(str->s_type.BF.s_fp))
2877c478bd9Sstevel@tonic-gate 			stream_set(str, STREAM_EOS_REACHED);
2887c478bd9Sstevel@tonic-gate 		else
2897c478bd9Sstevel@tonic-gate 			die(EMSG_READ, str->s_filename);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	trip_eof(str->s_type.BF.s_fp);
2937c478bd9Sstevel@tonic-gate 	str->s_current.l_data_length = strlen(str->s_current.l_data.sp) - 1;
2947c478bd9Sstevel@tonic-gate 	str->s_current.l_collate_length = 0;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (str->s_current.l_data_length == -1 ||
2977c478bd9Sstevel@tonic-gate 	    *(str->s_current.l_data.sp + str->s_current.l_data_length) !=
2987c478bd9Sstevel@tonic-gate 	    '\n') {
2997c478bd9Sstevel@tonic-gate 		if (!feof(str->s_type.BF.s_fp)) {
3007c478bd9Sstevel@tonic-gate 			/*
3017c478bd9Sstevel@tonic-gate 			 * In the overwrite case, failure to read the entire
3027c478bd9Sstevel@tonic-gate 			 * line means our buffer size was insufficient (as we
3037c478bd9Sstevel@tonic-gate 			 * are using all of it).  Exit, requesting more
3047c478bd9Sstevel@tonic-gate 			 * resources.
3057c478bd9Sstevel@tonic-gate 			 */
3067c478bd9Sstevel@tonic-gate 			die(EMSG_MEMORY);
3077c478bd9Sstevel@tonic-gate 		} else {
3087c478bd9Sstevel@tonic-gate 			stream_set(str, STREAM_EOS_REACHED);
3097c478bd9Sstevel@tonic-gate 			warn(WMSG_NEWLINE_ADDED, str->s_filename);
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	__S(stats_incr_fetches());
3147c478bd9Sstevel@tonic-gate 	return (NEXT_LINE_COMPLETE);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate int
stream_stdio_is_closable(stream_t * str)3187c478bd9Sstevel@tonic-gate stream_stdio_is_closable(stream_t *str)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	if (str->s_status & STREAM_OPEN && !(str->s_status & STREAM_NOTFILE))
3217c478bd9Sstevel@tonic-gate 		return (1);
3227c478bd9Sstevel@tonic-gate 	return (0);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate int
stream_stdio_close(stream_t * str)3267c478bd9Sstevel@tonic-gate stream_stdio_close(stream_t *str)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (!(str->s_status & STREAM_OUTPUT)) {
3317c478bd9Sstevel@tonic-gate 		if (!(str->s_status & STREAM_NOTFILE))
3327c478bd9Sstevel@tonic-gate 			(void) fclose(str->s_type.BF.s_fp);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		if (str->s_type.BF.s_vbuf != NULL) {
3357c478bd9Sstevel@tonic-gate 			free(str->s_type.BF.s_vbuf);
3367c478bd9Sstevel@tonic-gate 			str->s_type.BF.s_vbuf = NULL;
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 	} else {
3397c478bd9Sstevel@tonic-gate 		if (cxwrite(str->s_type.SF.s_fd, NULL, 0) == 0)
3407c478bd9Sstevel@tonic-gate 			(void) close(str->s_type.SF.s_fd);
3417c478bd9Sstevel@tonic-gate 		else
3427c478bd9Sstevel@tonic-gate 			die(EMSG_WRITE, str->s_filename);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	stream_unset(str, STREAM_OPEN | STREAM_PRIMED | STREAM_OUTPUT);
3467c478bd9Sstevel@tonic-gate 	return (1);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate static void
stream_stdio_send_eol(stream_t * str)3507c478bd9Sstevel@tonic-gate stream_stdio_send_eol(stream_t *str)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
3537c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OUTPUT);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (cxwrite(str->s_type.SF.s_fd, "\n", 1) < 0)
3567c478bd9Sstevel@tonic-gate 		die(EMSG_WRITE, str->s_filename);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate void
stream_stdio_flush(stream_t * str)3607c478bd9Sstevel@tonic-gate stream_stdio_flush(stream_t *str)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
3637c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OUTPUT);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (cxwrite(str->s_type.SF.s_fd, NULL, 0) < 0)
3667c478bd9Sstevel@tonic-gate 		die(EMSG_WRITE, str->s_filename);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate static void
stream_stdio_put_line(stream_t * str,line_rec_t * line)3707c478bd9Sstevel@tonic-gate stream_stdio_put_line(stream_t *str, line_rec_t *line)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
3737c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OUTPUT);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (line->l_data_length >= 0) {
3767c478bd9Sstevel@tonic-gate 		if (cxwrite(str->s_type.SF.s_fd, line->l_data.sp,
3777c478bd9Sstevel@tonic-gate 		    line->l_data_length) < 0)
3787c478bd9Sstevel@tonic-gate 			die(EMSG_WRITE, str->s_filename);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		stream_stdio_send_eol(str);
3817c478bd9Sstevel@tonic-gate 		__S(stats_incr_puts());
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	safe_free(line->l_raw_collate.sp);
3847c478bd9Sstevel@tonic-gate 	line->l_raw_collate.sp = NULL;
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate void
stream_stdio_put_line_unique(stream_t * str,line_rec_t * line)3887c478bd9Sstevel@tonic-gate stream_stdio_put_line_unique(stream_t *str, line_rec_t *line)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	static line_rec_t pvs;
3917c478bd9Sstevel@tonic-gate 	static size_t collate_buf_len;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OPEN);
3947c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & STREAM_OUTPUT);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (pvs.l_collate.sp != NULL &&
3977c478bd9Sstevel@tonic-gate 	    collated(&pvs, line, 0, COLL_UNIQUE) == 0) {
3987c478bd9Sstevel@tonic-gate 		__S(stats_incr_not_unique());
3997c478bd9Sstevel@tonic-gate 		return;
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	__S(stats_incr_put_unique());
4037c478bd9Sstevel@tonic-gate 	stream_stdio_put_line(str, line);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (line->l_collate_length + 1 > collate_buf_len) {
4067c478bd9Sstevel@tonic-gate 		pvs.l_collate.sp = safe_realloc(pvs.l_collate.sp,
4077c478bd9Sstevel@tonic-gate 		    line->l_collate_length + 1);
4087c478bd9Sstevel@tonic-gate 		collate_buf_len = line->l_collate_length + 1;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	(void) memcpy(pvs.l_collate.sp, line->l_collate.sp,
4127c478bd9Sstevel@tonic-gate 	    line->l_collate_length);
4137c478bd9Sstevel@tonic-gate 	*(pvs.l_collate.sp + line->l_collate_length) = '\0';
4147c478bd9Sstevel@tonic-gate 	pvs.l_collate_length = line->l_collate_length;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate int
stream_stdio_unlink(stream_t * str)4187c478bd9Sstevel@tonic-gate stream_stdio_unlink(stream_t *str)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	if (!(str->s_status & STREAM_NOTFILE))
4217c478bd9Sstevel@tonic-gate 		return (unlink(str->s_filename));
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	return (0);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate int
stream_stdio_free(stream_t * str)4277c478bd9Sstevel@tonic-gate stream_stdio_free(stream_t *str)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * Unmap the memory we allocated for input, if it's valid to do so.
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 	if (!(str->s_status & STREAM_OPEN) ||
4337c478bd9Sstevel@tonic-gate 	    (str->s_consumer != NULL &&
4347c478bd9Sstevel@tonic-gate 	    str->s_consumer->s_status & STREAM_NOT_FREEABLE))
4357c478bd9Sstevel@tonic-gate 		return (0);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if (str->s_buffer != NULL) {
4387c478bd9Sstevel@tonic-gate 		if (munmap(str->s_buffer, str->s_buffer_size) < 0)
4397c478bd9Sstevel@tonic-gate 			die(EMSG_MUNMAP, "/dev/zero");
4407c478bd9Sstevel@tonic-gate 		else {
4417c478bd9Sstevel@tonic-gate 			str->s_buffer = NULL;
4427c478bd9Sstevel@tonic-gate 			str->s_buffer_size = 0;
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	stream_unset(str, STREAM_PRIMED | STREAM_INSTANT);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	return (1);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate static int
stream_stdio_eos(stream_t * str)4527c478bd9Sstevel@tonic-gate stream_stdio_eos(stream_t *str)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	int retval = 0;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	ASSERT(!(str->s_status & STREAM_OUTPUT));
4577c478bd9Sstevel@tonic-gate 	ASSERT(str->s_status & (STREAM_SINGLE | STREAM_WIDE));
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (str == NULL || str->s_status & STREAM_EOS_REACHED)
4607c478bd9Sstevel@tonic-gate 		return (1);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	trip_eof(str->s_type.BF.s_fp);
4637c478bd9Sstevel@tonic-gate 	if (feof(str->s_type.BF.s_fp) &&
4647c478bd9Sstevel@tonic-gate 	    shelf == SHELF_VACANT &&
4657c478bd9Sstevel@tonic-gate 	    str->s_current.l_collate_length != -1) {
4667c478bd9Sstevel@tonic-gate 		retval = 1;
4677c478bd9Sstevel@tonic-gate 		stream_set(str, STREAM_EOS_REACHED);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	return (retval);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4747c478bd9Sstevel@tonic-gate static void
stream_stdio_release_line(stream_t * str)4757c478bd9Sstevel@tonic-gate stream_stdio_release_line(stream_t *str)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate const stream_ops_t stream_stdio_ops = {
4807c478bd9Sstevel@tonic-gate 	stream_stdio_is_closable,
4817c478bd9Sstevel@tonic-gate 	stream_stdio_close,
4827c478bd9Sstevel@tonic-gate 	stream_stdio_eos,
4837c478bd9Sstevel@tonic-gate 	stream_stdio_fetch,
4847c478bd9Sstevel@tonic-gate 	stream_stdio_flush,
4857c478bd9Sstevel@tonic-gate 	stream_stdio_free,
4867c478bd9Sstevel@tonic-gate 	stream_stdio_open_for_write,
4877c478bd9Sstevel@tonic-gate 	stream_stdio_prime,
4887c478bd9Sstevel@tonic-gate 	stream_stdio_put_line,
4897c478bd9Sstevel@tonic-gate 	stream_stdio_release_line,
4907c478bd9Sstevel@tonic-gate 	stream_stdio_send_eol,
4917c478bd9Sstevel@tonic-gate 	stream_stdio_unlink
4927c478bd9Sstevel@tonic-gate };
493