xref: /onnv-gate/usr/src/cmd/ipf/lib/common/getline.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
50Sstevel@tonic-gate  *
60Sstevel@tonic-gate  * $Id: getline.c,v 1.3 2001/06/09 17:09:24 darrenr Exp $
70Sstevel@tonic-gate  */
80Sstevel@tonic-gate 
90Sstevel@tonic-gate #include <stdio.h>
100Sstevel@tonic-gate #if !defined(__SVR4) && !defined(__GNUC__)
110Sstevel@tonic-gate #include <strings.h>
120Sstevel@tonic-gate #endif
130Sstevel@tonic-gate #include <string.h>
140Sstevel@tonic-gate #include "ipf.h"
150Sstevel@tonic-gate 
160Sstevel@tonic-gate 
170Sstevel@tonic-gate /*
180Sstevel@tonic-gate  * Similar to fgets(3) but can handle '\\' and NL is converted to NUL.
190Sstevel@tonic-gate  * Returns NULL if error occured, EOF encounterd or input line is too long.
200Sstevel@tonic-gate  */
21*13093SRoger.Faulkner@Oracle.COM char *
getaline(char * str,size_t size,FILE * file,int * linenum)22*13093SRoger.Faulkner@Oracle.COM getaline(char *str, size_t size, FILE *file, int *linenum)
230Sstevel@tonic-gate {
240Sstevel@tonic-gate 	char *p;
250Sstevel@tonic-gate 	int s, len;
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 	do {
28*13093SRoger.Faulkner@Oracle.COM 		for (p = str, s = size; ; p += (len - 1), s -= (len - 1)) {
290Sstevel@tonic-gate 			/*
300Sstevel@tonic-gate 			 * if an error occured, EOF was encounterd, or there
310Sstevel@tonic-gate 			 * was no room to put NUL, return NULL.
320Sstevel@tonic-gate 			 */
330Sstevel@tonic-gate 			if (fgets(p, s, file) == NULL)
340Sstevel@tonic-gate 				return (NULL);
350Sstevel@tonic-gate 			len = strlen(p);
360Sstevel@tonic-gate 			if (p[len - 1] != '\n') {
370Sstevel@tonic-gate 				p[len] = '\0';
380Sstevel@tonic-gate 				break;
390Sstevel@tonic-gate 			}
400Sstevel@tonic-gate 			(*linenum)++;
410Sstevel@tonic-gate 			p[len - 1] = '\0';
420Sstevel@tonic-gate 			if (len < 2 || p[len - 2] != '\\')
430Sstevel@tonic-gate 				break;
440Sstevel@tonic-gate 			else
450Sstevel@tonic-gate 				/*
460Sstevel@tonic-gate 				 * Convert '\\' to a space so words don't
470Sstevel@tonic-gate 				 * run together
480Sstevel@tonic-gate 				 */
490Sstevel@tonic-gate 				p[len - 2] = ' ';
500Sstevel@tonic-gate 		}
510Sstevel@tonic-gate 	} while (*str == '\0');
520Sstevel@tonic-gate 	return (str);
530Sstevel@tonic-gate }
54