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