17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate#include "streams_array.h"
307c478bdstevel@tonic-gate#include "streams_common.h"
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate/*
337c478bdstevel@tonic-gate * Single-byte character memory map-based streams implementation
347c478bdstevel@tonic-gate */
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gatestatic int
377c478bdstevel@tonic-gatestream_array_prime(stream_t *str)
387c478bdstevel@tonic-gate{
397c478bdstevel@tonic-gate	ASSERT((str->s_status & STREAM_SOURCE_MASK) == STREAM_ARRAY);
407c478bdstevel@tonic-gate
417c478bdstevel@tonic-gate	str->s_type.LA.s_cur_index = MIN(0, str->s_type.LA.s_array_size - 1);
427c478bdstevel@tonic-gate	if (str->s_type.LA.s_cur_index >= 0)
437c478bdstevel@tonic-gate		copy_line_rec(
447c478bdstevel@tonic-gate		    str->s_type.LA.s_array[str->s_type.LA.s_cur_index],
457c478bdstevel@tonic-gate		    &str->s_current);
467c478bdstevel@tonic-gate	else {
477c478bdstevel@tonic-gate		stream_set(str, STREAM_EOS_REACHED);
487c478bdstevel@tonic-gate		stream_unset(str, STREAM_PRIMED);
497c478bdstevel@tonic-gate		return (PRIME_FAILED_EMPTY_FILE);
507c478bdstevel@tonic-gate	}
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate	stream_set(str, STREAM_PRIMED);
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate	return (PRIME_SUCCEEDED);
557c478bdstevel@tonic-gate}
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gatestatic ssize_t
587c478bdstevel@tonic-gatestream_array_fetch(stream_t *str)
597c478bdstevel@tonic-gate{
607c478bdstevel@tonic-gate	ASSERT(str->s_status & STREAM_OPEN);
617c478bdstevel@tonic-gate	ASSERT(str->s_type.LA.s_cur_index < str->s_type.LA.s_array_size);
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate	if (++str->s_type.LA.s_cur_index == str->s_type.LA.s_array_size - 1)
647c478bdstevel@tonic-gate		stream_set(str, STREAM_EOS_REACHED);
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate	copy_line_rec(str->s_type.LA.s_array[str->s_type.LA.s_cur_index],
677c478bdstevel@tonic-gate	    &str->s_current);
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate	return (NEXT_LINE_COMPLETE);
707c478bdstevel@tonic-gate}
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate/*ARGSUSED*/
737c478bdstevel@tonic-gatestatic int
747c478bdstevel@tonic-gatestream_array_is_closable(stream_t *str)
757c478bdstevel@tonic-gate{
767c478bdstevel@tonic-gate	/*
777c478bdstevel@tonic-gate	 * Array streams are not closable.  That is, there is no open file
787c478bdstevel@tonic-gate	 * descriptor directly associated with an array stream.
797c478bdstevel@tonic-gate	 */
807c478bdstevel@tonic-gate	return (0);
817c478bdstevel@tonic-gate}
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gatestatic int
847c478bdstevel@tonic-gatestream_array_close(stream_t *str)
857c478bdstevel@tonic-gate{
867c478bdstevel@tonic-gate	stream_unset(str, STREAM_OPEN | STREAM_PRIMED);
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gate	return (1);
897c478bdstevel@tonic-gate}
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gatestatic int
927c478bdstevel@tonic-gatestream_array_free(stream_t *str)
937c478bdstevel@tonic-gate{
947c478bdstevel@tonic-gate	/*
957c478bdstevel@tonic-gate	 * It's now safe for us to close the various streams backing the array
967c478bdstevel@tonic-gate	 * stream's data.
977c478bdstevel@tonic-gate	 */
987c478bdstevel@tonic-gate	stream_unset(str, STREAM_PRIMED | STREAM_NOT_FREEABLE);
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gate	return (1);
1017c478bdstevel@tonic-gate}
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gatestatic int
1047c478bdstevel@tonic-gatestream_array_eos(stream_t *str)
1057c478bdstevel@tonic-gate{
1067c478bdstevel@tonic-gate	int retval = 0;
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate	if (str == NULL || str->s_status & STREAM_EOS_REACHED)
1097c478bdstevel@tonic-gate		return (1);
1107c478bdstevel@tonic-gate
1117c478bdstevel@tonic-gate	if (str->s_type.LA.s_cur_index + 1 >= str->s_type.LA.s_array_size) {
1127c478bdstevel@tonic-gate		retval = 1;
1137c478bdstevel@tonic-gate		stream_set(str, STREAM_EOS_REACHED);
1147c478bdstevel@tonic-gate	}
1157c478bdstevel@tonic-gate
1167c478bdstevel@tonic-gate	return (retval);
1177c478bdstevel@tonic-gate}
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate/*ARGSUSED*/
1207c478bdstevel@tonic-gatestatic void
1217c478bdstevel@tonic-gatestream_array_release_line(stream_t *str)
1227c478bdstevel@tonic-gate{
1237c478bdstevel@tonic-gate}
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gateconst stream_ops_t stream_array_ops = {
1267c478bdstevel@tonic-gate	stream_array_is_closable,
1277c478bdstevel@tonic-gate	stream_array_close,
1287c478bdstevel@tonic-gate	stream_array_eos,
1297c478bdstevel@tonic-gate	stream_array_fetch,
1307c478bdstevel@tonic-gate	NULL,
1317c478bdstevel@tonic-gate	stream_array_free,
1327c478bdstevel@tonic-gate	NULL,
1337c478bdstevel@tonic-gate	stream_array_prime,
1347c478bdstevel@tonic-gate	NULL,
1357c478bdstevel@tonic-gate	stream_array_release_line,
1367c478bdstevel@tonic-gate	NULL,
1377c478bdstevel@tonic-gate	NULL
1387c478bdstevel@tonic-gate};
139