/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "streams_stdio.h"
#include "streams_common.h"
#define SHELF_VACANT 0
/*
*
* The routines in this file contain the implementation of the i/o streams
* interface for those situations where the input is via stdio.
*
* The "shelf"
* In the case where the input buffer contains insufficient room to hold the
* entire line, the fractional line is shelved, and will be grafted to on the
* subsequent read.
*/
int
{
else
O_WRONLY, OUTPUT_MODE)) < 0) {
return (-1);
else
}
return (1);
}
/*
* In the case of an instantaneous stream, we allocate a small buffer (64k) here
* for the stream; otherwise, the s_buffer and s_buffer_size members should have
* been set by stream_set_size() prior to calling stream_prime().
*
* Repriming (priming an already primed stream) is done when we are reentering a
* file after having sorted a previous portion of the file.
*/
static int
{
char *current_position;
char *end_of_buffer;
char *next_nl;
PROT_WRITE, MAP_PRIVATE, 0);
}
if (stream_is_primed(str)) {
/*
* l_data_length is only set to -1 in the case of coincidental
* exhaustion of the input butter. This is thus the only case
* which involves no copying on a re-prime.
*/
/*
* If our current line is incomplete, we need to get the rest of
* the line--if we can't, then we've exhausted memory.
*/
shelf_state == SHELF_OCCUPIED ||
return (PRIME_SUCCEEDED);
}
else {
return (PRIME_FAILED_EMPTY_FILE);
}
/*
* Because one might run sort on a binary file, strlen() is no longer
* trustworthy--we must explicitly search for a newline.
*/
} else {
}
return (PRIME_SUCCEEDED);
}
/*
* stream_stdio_fetch() guarantees the return of a complete line, or a flag
* indicating that the complete line could not be read.
*/
static ssize_t
{
int ret_val;
if (shelf == SHELF_VACANT) {
/*
* The graft point is the start of the current line.
*/
/*
* Correct for terminating NUL on shelved line. This NUL is
* only present if we didn't have the coincidental case
* mentioned in the comment below.
*/
graft_pt--;
}
if (dist_to_buf_end <= 1) {
/*
* fgets()'s behaviour in the case of a one-character buffer is
* somewhat unhelpful: it fills the buffer with '\0' and
* returns successfully (even if EOF has been reached for the
* file in question). Since we may be in the middle of a
* grafting operation, we leave early, maintaining the shelf in
* its current state.
*/
return (NEXT_LINE_INCOMPLETE);
}
else
}
/*
* Because one might run sort on a binary file, strlen() is no longer
* trustworthy--we must explicitly search for a newline.
*/
dist_to_buf_end)) == NULL) {
} else {
}
'\n') {
/*
* We were only able to read part of the line; note that
* we have something on the shelf for our next fetch.
* If the shelf was previously occupied, and we still
* can't get the entire line, then we need more
* resources.
*/
if (shelf == SHELF_OCCUPIED)
} else {
}
} else {
}
return (ret_val);
}
/*
* stdio_fetch_overwrite() is used when we are performing an operation where we
* need the buffer contents only over a single period. (merge and check are
* operations of this kind.) In this case, we read the current line at the head
* of the stream's defined buffer. If we cannot read the entire line, we have
* not allocated sufficient memory.
*/
{
else
}
'\n') {
/*
* In the overwrite case, failure to read the entire
* line means our buffer size was insufficient (as we
* are using all of it). Exit, requesting more
* resources.
*/
} else {
}
}
return (NEXT_LINE_COMPLETE);
}
int
{
return (1);
return (0);
}
int
{
}
} else {
else
}
return (1);
}
static void
{
}
void
{
}
static void
{
if (line->l_data_length >= 0) {
line->l_data_length) < 0)
__S(stats_incr_puts());
}
}
void
{
return;
}
}
}
int
{
return (0);
}
int
{
/*
* Unmap the memory we allocated for input, if it's valid to do so.
*/
return (0);
else {
str->s_buffer_size = 0;
}
}
return (1);
}
static int
{
int retval = 0;
return (1);
shelf == SHELF_VACANT &&
retval = 1;
}
return (retval);
}
/*ARGSUSED*/
static void
{
}
};