/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
*/

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: bgets.c 1.3 89/10/23 $@(#)libgen:bgets.c	1.1"

/*
	read no more that <count> characters into <buf> from stream <fp>,
	stoping at any character slisted in <stopstr>.
	stopstr == 0 uses previous value of stopstr.
	NOTE: This function will not work for multi-byte characters.
*/

#include <stdio.h>

#define CHARS	256

static unsigned char	stop[CHARS];

unsigned char *
bgets( buf, count, fp, stopstr )
unsigned char	*buf;
register
  int	count;
FILE	*fp;
unsigned char	*stopstr;
{
	register unsigned char	*cp;
	register int	c;

	if( stopstr ) {
		/* clear and set stopstr array */
		for( cp = stop;  cp < &stop[CHARS]; )
			*cp++ = 0;
		for( cp = stopstr;  *cp; )
			stop[ *cp++ ] = 1;
	}
	for( cp = buf;  ; ) {
		if( --count < 0 ) {
			*cp = '\0';
			break;
		}
		if( (c = getc(fp)) == EOF ) {
			*cp = '\0';
			if( cp == buf ) {
				cp = (unsigned char *) EOF;
			}
			break;
		}
		*cp++ = c;
		if( stop[ c ] ) {
			*cp = '\0';
			break;
		}
	}

	return  cp;
}
