DataMuseum.dk

Presents historical artifacts from the history of:

Commodore CBM-900

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about Commodore CBM-900

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download

⟦885dedf80⟧ TextFile

    Length: 1197 (0x4ad)
    Types: TextFile
    Notes: UNIX file
    Names: »fgets.c«

Derivation

└─⟦f27320a65⟧ Bits:30001972 Commodore 900 hard disk image with partial source code
    └─⟦f4b8d8c84⟧ UNIX V7 Filesystem
        └─ ⟦this⟧ »unimenu/src/menu/fgets.c« 

TextFile

/*
 * @(#)fgets.c	1.1 15:34:37 4/6/85";
 *
 * replace some of the stdio stuff
 *	getc()
 *	ungetc()
 *	fgets()
 *
 * NOTE: these functions only approximate their stdio counterparts
 * they only work on one file at a time. This saves us an unbelievable
 * amount of code. They also use file descriptors NOT FILE *.
 */

#define	BUFSIZ	512
#define	EOF	-1
static	char	Inbuf[BUFSIZ] = {0};
static	int	Incnt = 0;
static	int	Insiz = 0;
static	char	*Inptr = 0;

clribuf()
{
	Incnt = Insiz = 0;
	Inptr = Inbuf;
}

getc(fd)
int	fd;
{
	if ( Incnt >= Insiz )
	{
		Incnt = 0;
		Inptr = Inbuf;
		Insiz = read(fd, Inbuf, BUFSIZ);
		if ( Insiz <= 0 )
			return ( EOF );
	}
	Incnt++;
	return ( (int) *Inptr++ );
}

ungetc(c)
char	c;
{
	if ( Incnt > 0 )
	{
		Incnt--;
		*(--Inptr) = c;
	}
	else
	{
		Inbuf[0] = c;
		Incnt = 0;
		Insiz = 1;
	}
}

char	*fgets(str, max, fd)
char	*str;
int	max;
int	fd;
{
char	*buf;
char	i;

	buf = str;
	while ( --max > 0 )			/* read max - 1 chars */
	{
		i = getc(fd);
		if (i == EOF )
			break;
		*buf++ = i;
		if ( i == '\n' )		/* fgets keeps the newline */
			break;
	}
	*buf = '\0';
	if ( buf == str )			/* nothing read returns NULL */
		return ((char *) 0);
	return ( str );
}