xref: /titanic_41/usr/src/cmd/ypcmd/revnetgroup/util.c (revision cde2885fdf538266ee2a3b08dee2d5075ce8fa2b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1996-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SMI4.1 1.4 */
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include "util.h"
32 
33 
34 
35 
36 /*
37  * This is just like fgets, but recognizes that "\\n" signals a continuation
38  * of a line
39  */
40 char *
41 getline(line, maxlen, fp)
42 	char *line;
43 	int maxlen;
44 	FILE *fp;
45 {
46 	register char *p;
47 	register char *start;
48 	int c;
49 
50 	start = line;
51 
52 nextline:
53 	if (fgets(start, maxlen, fp) == NULL) {
54 		return (NULL);
55 	}
56 	for (p = start; *p; p++) {
57 		if (*p == '\n') {
58 			if (p > start && *(p-1) == '\\') {
59 				start = p - 1;
60 				maxlen++;
61 				goto nextline;
62 			} else {
63 				return (line);
64 			}
65 		}
66 		maxlen--;
67 	}
68 
69 	/*
70 	 * Input line is too long. Rest of the line needs to be discarded.
71 	 * Reinsert the last char into the stream. This is done so that
72 	 * in case the last char read is '\' and it is followed by a '\n'
73 	 * then the next line too can be discarded.
74 	 */
75 	if (p > start)
76 		(void) ungetc(*(p-1), fp);
77 
78 	/*
79 	 * Discard the rest of the line
80 	 */
81 	while ((c = getc(fp)) != EOF) {
82 		if (c == '\n')
83 			break;
84 		else if (c == '\\') {
85 			/*
86 			 * Ignore the next character except EOF
87 			 */
88 			if (getc(fp) == EOF)
89 				break;
90 		}
91 	}
92 
93 	maxlen = strlen(line) + 1;
94 
95 	/*
96 	 * Certain functions expects a newline in the buffer.
97 	 */
98 	if (maxlen >= 2)
99 		line[maxlen - 2] = '\n';
100 	(void) fprintf(stderr, "Following line too long - remaining chars "
101 			"ignored\n--- %s", line);
102 	return (line);
103 }
104 
105 
106 void
107 fatal(message)
108 	char *message;
109 {
110 	(void) fprintf(stderr, "fatal error: %s\n", message);
111 	exit(1);
112 }
113