xref: /onnv-gate/usr/src/cmd/ypcmd/revnetgroup/util.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM  * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*13093SRoger.Faulkner@Oracle.COM 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate #include "util.h"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * This is just like fgets, but recognizes that "\\n" signals a continuation
350Sstevel@tonic-gate  * of a line
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate char *
getaline(line,maxlen,fp)38*13093SRoger.Faulkner@Oracle.COM getaline(line, maxlen, fp)
390Sstevel@tonic-gate 	char *line;
400Sstevel@tonic-gate 	int maxlen;
410Sstevel@tonic-gate 	FILE *fp;
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	register char *p;
440Sstevel@tonic-gate 	register char *start;
450Sstevel@tonic-gate 	int c;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 	start = line;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate nextline:
500Sstevel@tonic-gate 	if (fgets(start, maxlen, fp) == NULL) {
510Sstevel@tonic-gate 		return (NULL);
520Sstevel@tonic-gate 	}
530Sstevel@tonic-gate 	for (p = start; *p; p++) {
540Sstevel@tonic-gate 		if (*p == '\n') {
550Sstevel@tonic-gate 			if (p > start && *(p-1) == '\\') {
560Sstevel@tonic-gate 				start = p - 1;
570Sstevel@tonic-gate 				maxlen++;
580Sstevel@tonic-gate 				goto nextline;
590Sstevel@tonic-gate 			} else {
600Sstevel@tonic-gate 				return (line);
610Sstevel@tonic-gate 			}
620Sstevel@tonic-gate 		}
630Sstevel@tonic-gate 		maxlen--;
640Sstevel@tonic-gate 	}
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * Input line is too long. Rest of the line needs to be discarded.
680Sstevel@tonic-gate 	 * Reinsert the last char into the stream. This is done so that
690Sstevel@tonic-gate 	 * in case the last char read is '\' and it is followed by a '\n'
700Sstevel@tonic-gate 	 * then the next line too can be discarded.
710Sstevel@tonic-gate 	 */
720Sstevel@tonic-gate 	if (p > start)
730Sstevel@tonic-gate 		(void) ungetc(*(p-1), fp);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/*
760Sstevel@tonic-gate 	 * Discard the rest of the line
770Sstevel@tonic-gate 	 */
780Sstevel@tonic-gate 	while ((c = getc(fp)) != EOF) {
790Sstevel@tonic-gate 		if (c == '\n')
800Sstevel@tonic-gate 			break;
810Sstevel@tonic-gate 		else if (c == '\\') {
820Sstevel@tonic-gate 			/*
830Sstevel@tonic-gate 			 * Ignore the next character except EOF
840Sstevel@tonic-gate 			 */
850Sstevel@tonic-gate 			if (getc(fp) == EOF)
860Sstevel@tonic-gate 				break;
870Sstevel@tonic-gate 		}
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	maxlen = strlen(line) + 1;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	/*
930Sstevel@tonic-gate 	 * Certain functions expects a newline in the buffer.
940Sstevel@tonic-gate 	 */
950Sstevel@tonic-gate 	if (maxlen >= 2)
960Sstevel@tonic-gate 		line[maxlen - 2] = '\n';
970Sstevel@tonic-gate 	(void) fprintf(stderr, "Following line too long - remaining chars "
980Sstevel@tonic-gate 			"ignored\n--- %s", line);
990Sstevel@tonic-gate 	return (line);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate void
fatal(message)1040Sstevel@tonic-gate fatal(message)
1050Sstevel@tonic-gate 	char *message;
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	(void) fprintf(stderr, "fatal error: %s\n", message);
1080Sstevel@tonic-gate 	exit(1);
1090Sstevel@tonic-gate }
110