aboutsummaryrefslogtreecommitdiff
path: root/sys/libc/fgets.c
blob: 45c228c67f5c74d748edf6023c82113bb188aafa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/

#define	import_spp
#define	import_libc
#define	import_stdio
#include <iraf.h>


/*  FGETS -- Return a newline delimited string in the user buffer.  If the
**  buffer fills before newline is seen the string will not be newline
**  delimited.
*/
char *
fgets (
  char	*buf,			/* user supplied output buffer	*/
  int	maxch,			/* max chars out (incl EOS)	*/
  FILE	*fp			/* input file			*/
)
{
	register int	ch = 0, lastch = 0, n = maxch - 1;
	register char	*op = buf;

	while (--n >= 0 && (ch = getc (fp)) >= 0) {
	    lastch = ch;
	    if (ch == '\r')		/* handle DOS-style CR-NL	*/
		continue;
	    *op++ = ch;
	    if (ch == '\n')
		break;
	}

	if (ch == EOF && op == buf)
	    return ((char *) NULL);
	else {
#ifdef ADD_NEWLINE
	    if (lastch != '\n')		/* handle missing NL at EOF	*/
	        *op++ = '\n';
#endif
	    *op = EOS;
	    return (buf);
	}
}