xref: /csrg-svn/lib/libc/vax/stdio.old/gets.s (revision 34435)
124423Smckusick/*
224423Smckusick * Copyright (c) 1985 Regents of the University of California.
3*34435Sbostic * All rights reserved.
4*34435Sbostic *
5*34435Sbostic * Redistribution and use in source and binary forms are permitted
6*34435Sbostic * provided that this notice is preserved and that due credit is given
7*34435Sbostic * to the University of California at Berkeley. The name of the University
8*34435Sbostic * may not be used to endorse or promote products derived from this
9*34435Sbostic * software without specific written prior permission. This software
10*34435Sbostic * is provided ``as is'' without express or implied warranty.
1124423Smckusick */
1224423Smckusick
1326697Sdonn#ifdef LIBC_SCCS
14*34435Sbostic_sccsid:.asciz	"@(#)gets.s	5.3 (Berkeley) 05/23/88"
15*34435Sbostic#endif /* LIBC_SCCS */
1624423Smckusick
1724423Smckusick/*
1824423Smckusick * char *gets(s);
1924423Smckusick * char *s;
2024423Smckusick *
2124423Smckusick * argument: a target string
2224423Smckusick * side effects: reads bytes up to and including a newline from the
2324423Smckusick *	standard input into the target string and replaces the newline
2424423Smckusick *	with a null to null-terminate the string.
2524423Smckusick * result: the target string if successful, 0 otherwise.
2624423Smckusick */
2724423Smckusick
2824423Smckusick#include "DEFS.h"
2924423Smckusick
3024423Smckusick#define		NL	0xa
3124423Smckusick
3224423SmckusickENTRY(gets, R11|R10)
3324423Smckusick
3424423Smckusick#define		S	r11
3524423Smckusick	movl	4(ap),S
3624423Smckusick#define		IPTR	r10
3724423Smckusick#define		_CNT
3824423Smckusick#define		_PTR	4
3924423Smckusick#define		_BASE	8
4024423Smckusick#define		_BUFSIZ	12
4124423Smckusick#define		_FLAG	16
4224423Smckusick	movab	__iob,IPTR
4324423Smckusick
4424423Smckusick#define		OLD_S	4(ap)
4524423Smckusick
4624423Smckusick	/*
4724423Smckusick	 * If no characters, call _filbuf() to get some.
4824423Smckusick	 */
4924423Smckusick	tstl	_CNT(IPTR)
5024423Smckusick	jgtr	Lscan
5124423Smckusick
5224423SmckusickLloop:
5324423Smckusick	pushl	IPTR
5424423Smckusick	calls	$1,__filbuf
5524423Smckusick	tstl	r0			/* What did _filbuf() return? */
5624423Smckusick	jlss	Leof
5724423Smckusick	cmpb	r0,$NL
5824423Smckusick	jneq	1f
5924423Smckusick	clrb	(S)
6024423Smckusick	jbr	Lret
6124423Smckusick1:
6224423Smckusick	movb	r0,(S)+			/* Save the returned character */
6324423Smckusick	tstl	_BASE(IPTR)		/* Is input buffered? */
6424423Smckusick	jeql	Lloop
6524423Smckusick
6624423Smckusick	/*
6724423Smckusick	 * Look for a newline in the buffer.
6824423Smckusick	 */
6924423SmckusickLscan:
7024423Smckusick	locc	$NL,_CNT(IPTR),*_PTR(IPTR)
7124423Smckusick	jeql	Lagain
7224423Smckusick
7324423Smckusick	/*
7424423Smckusick	 * Success -- copy the data and return.
7524423Smckusick	 */
7624423Smckusick	subl3	r0,_CNT(IPTR),r2
7724423Smckusick	subl2	r2,_CNT(IPTR)
7824423Smckusick	movc3	r2,*_PTR(IPTR),(S)	/* Copy the data */
7924423Smckusick	clrb	(r3)
8024423Smckusick	movl	r1,_PTR(IPTR)
8124423Smckusick	decl	_CNT(IPTR)		/* Skip the newline */
8224423Smckusick	incl	_PTR(IPTR)
8324423Smckusick
8424423Smckusick	/*
8524423Smckusick	 * Normal return.
8624423Smckusick	 */
8724423SmckusickLret:
8824423Smckusick	movl	OLD_S,r0
8924423Smckusick	ret
9024423Smckusick
9124423Smckusick	/*
9224423Smckusick	 * If we run out of characters, copy the buffer and loop.
9324423Smckusick	 */
9424423SmckusickLagain:
9524423Smckusick	movc3	_CNT(IPTR),*_PTR(IPTR),(S)	/* Copy the data */
9624423Smckusick	movl	r3,S
9724423Smckusick	movl	_BASE(IPTR),_PTR(IPTR)		/* Reset stdio */
9824423Smckusick	clrl	_CNT(IPTR)
9924423Smckusick	jbr	Lloop
10024423Smckusick
10124423Smckusick	/*
10224423Smckusick	 * End of file?  Check to see if we copied any data.
10324423Smckusick	 */
10424423SmckusickLeof:
10524423Smckusick	cmpl	S,OLD_S
10624423Smckusick	jeql	Lerror
10724423Smckusick	clrb	(S)
10824423Smckusick	jbr	Lret
10924423Smckusick
11024423Smckusick	/*
11124423Smckusick	 * Error/eof return -- null pointer.
11224423Smckusick	 */
11324423SmckusickLerror:
11424423Smckusick	clrl	r0
11524423Smckusick	ret
116