1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  * $Id: getline.c,v 1.3 2001/06/09 17:09:24 darrenr Exp $
7*0Sstevel@tonic-gate  */
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate #include <stdio.h>
10*0Sstevel@tonic-gate #if !defined(__SVR4) && !defined(__GNUC__)
11*0Sstevel@tonic-gate #include <strings.h>
12*0Sstevel@tonic-gate #endif
13*0Sstevel@tonic-gate #include <string.h>
14*0Sstevel@tonic-gate #include "ipf.h"
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate /*
18*0Sstevel@tonic-gate  * Similar to fgets(3) but can handle '\\' and NL is converted to NUL.
19*0Sstevel@tonic-gate  * Returns NULL if error occured, EOF encounterd or input line is too long.
20*0Sstevel@tonic-gate  */
21*0Sstevel@tonic-gate char *getline(str, size, file, linenum)
22*0Sstevel@tonic-gate register char	*str;
23*0Sstevel@tonic-gate size_t	size;
24*0Sstevel@tonic-gate FILE	*file;
25*0Sstevel@tonic-gate int	*linenum;
26*0Sstevel@tonic-gate {
27*0Sstevel@tonic-gate 	char *p;
28*0Sstevel@tonic-gate 	int s, len;
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 	do {
31*0Sstevel@tonic-gate 		for (p = str, s = size;; p += (len - 1), s -= (len - 1)) {
32*0Sstevel@tonic-gate 			/*
33*0Sstevel@tonic-gate 			 * if an error occured, EOF was encounterd, or there
34*0Sstevel@tonic-gate 			 * was no room to put NUL, return NULL.
35*0Sstevel@tonic-gate 			 */
36*0Sstevel@tonic-gate 			if (fgets(p, s, file) == NULL)
37*0Sstevel@tonic-gate 				return (NULL);
38*0Sstevel@tonic-gate 			len = strlen(p);
39*0Sstevel@tonic-gate 			if (p[len - 1] != '\n') {
40*0Sstevel@tonic-gate 				p[len] = '\0';
41*0Sstevel@tonic-gate 				break;
42*0Sstevel@tonic-gate 			}
43*0Sstevel@tonic-gate 			(*linenum)++;
44*0Sstevel@tonic-gate 			p[len - 1] = '\0';
45*0Sstevel@tonic-gate 			if (len < 2 || p[len - 2] != '\\')
46*0Sstevel@tonic-gate 				break;
47*0Sstevel@tonic-gate 			else
48*0Sstevel@tonic-gate 				/*
49*0Sstevel@tonic-gate 				 * Convert '\\' to a space so words don't
50*0Sstevel@tonic-gate 				 * run together
51*0Sstevel@tonic-gate 				 */
52*0Sstevel@tonic-gate 				p[len - 2] = ' ';
53*0Sstevel@tonic-gate 		}
54*0Sstevel@tonic-gate 	} while (*str == '\0');
55*0Sstevel@tonic-gate 	return (str);
56*0Sstevel@tonic-gate }
57