xref: /titanic_52/usr/src/cmd/ypcmd/mknetid/getname.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1986,1987,1988,1989,1990 Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SMI4.1 1.3 */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <string.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #define	iseol(c)	(c == 0 || c == '\n' || strchr(com, c) != NULL)
34*7c478bd9Sstevel@tonic-gate #define	issep(c)	(strchr(sep, c) != NULL)
35*7c478bd9Sstevel@tonic-gate #define	isignore(c)	(strchr(ignore, c) != NULL)
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  * getline()
39*7c478bd9Sstevel@tonic-gate  * Read a line from a file.
40*7c478bd9Sstevel@tonic-gate  * What's returned is a cookie to be passed to getname
41*7c478bd9Sstevel@tonic-gate  */
42*7c478bd9Sstevel@tonic-gate char **
43*7c478bd9Sstevel@tonic-gate getline(line, maxlinelen, f, lcount, com)
44*7c478bd9Sstevel@tonic-gate 	char *line;
45*7c478bd9Sstevel@tonic-gate 	int maxlinelen;
46*7c478bd9Sstevel@tonic-gate 	FILE *f;
47*7c478bd9Sstevel@tonic-gate 	int *lcount;
48*7c478bd9Sstevel@tonic-gate 	char *com;
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	char *p;
51*7c478bd9Sstevel@tonic-gate 	static char *lp;
52*7c478bd9Sstevel@tonic-gate 	do {
53*7c478bd9Sstevel@tonic-gate 		if (! fgets(line, maxlinelen, f)) {
54*7c478bd9Sstevel@tonic-gate 			return (NULL);
55*7c478bd9Sstevel@tonic-gate 		}
56*7c478bd9Sstevel@tonic-gate 		(*lcount)++;
57*7c478bd9Sstevel@tonic-gate 	} while (iseol(line[0]));
58*7c478bd9Sstevel@tonic-gate 	p = line;
59*7c478bd9Sstevel@tonic-gate 	for (;;) {
60*7c478bd9Sstevel@tonic-gate 		while (*p) {
61*7c478bd9Sstevel@tonic-gate 			p++;
62*7c478bd9Sstevel@tonic-gate 		}
63*7c478bd9Sstevel@tonic-gate 		if (*--p == '\n' && *--p == '\\') {
64*7c478bd9Sstevel@tonic-gate 			if (! fgets(p, maxlinelen, f)) {
65*7c478bd9Sstevel@tonic-gate 				break;
66*7c478bd9Sstevel@tonic-gate 			}
67*7c478bd9Sstevel@tonic-gate 			(*lcount)++;
68*7c478bd9Sstevel@tonic-gate 		} else {
69*7c478bd9Sstevel@tonic-gate 			break;
70*7c478bd9Sstevel@tonic-gate 		}
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 	lp = line;
73*7c478bd9Sstevel@tonic-gate 	return (&lp);
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate /*
78*7c478bd9Sstevel@tonic-gate  * getname()
79*7c478bd9Sstevel@tonic-gate  * Get the next entry from the line.
80*7c478bd9Sstevel@tonic-gate  * You tell getname() which characters to ignore before storing them
81*7c478bd9Sstevel@tonic-gate  * into name, and which characters separate entries in a line.
82*7c478bd9Sstevel@tonic-gate  * The cookie is updated appropriately.
83*7c478bd9Sstevel@tonic-gate  * return:
84*7c478bd9Sstevel@tonic-gate  *	  1: one entry parsed
85*7c478bd9Sstevel@tonic-gate  *	  0: partial entry parsed, ran out of space in name
86*7c478bd9Sstevel@tonic-gate  *  -1: no more entries in line
87*7c478bd9Sstevel@tonic-gate  */
88*7c478bd9Sstevel@tonic-gate getname(name, namelen, ignore, sep, linep, com)
89*7c478bd9Sstevel@tonic-gate 	char *name;
90*7c478bd9Sstevel@tonic-gate 	int namelen;
91*7c478bd9Sstevel@tonic-gate 	char *ignore;
92*7c478bd9Sstevel@tonic-gate 	char *sep;
93*7c478bd9Sstevel@tonic-gate 	char **linep;
94*7c478bd9Sstevel@tonic-gate 	char *com;
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	register char c;
97*7c478bd9Sstevel@tonic-gate 	register char *lp;
98*7c478bd9Sstevel@tonic-gate 	register char *np;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	lp = *linep;
101*7c478bd9Sstevel@tonic-gate 	do {
102*7c478bd9Sstevel@tonic-gate 		c = *lp++;
103*7c478bd9Sstevel@tonic-gate 	} while (isignore(c) && !iseol(c));
104*7c478bd9Sstevel@tonic-gate 	if (iseol(c)) {
105*7c478bd9Sstevel@tonic-gate 		*linep = lp - 1;
106*7c478bd9Sstevel@tonic-gate 		return (-1);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 	np = name;
109*7c478bd9Sstevel@tonic-gate 	while (! issep(c) && ! iseol(c) && np - name < namelen) {
110*7c478bd9Sstevel@tonic-gate 		*np++ = c;
111*7c478bd9Sstevel@tonic-gate 		c = *lp++;
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 	lp--;
114*7c478bd9Sstevel@tonic-gate 	if (np - name < namelen) {
115*7c478bd9Sstevel@tonic-gate 		*np = 0;
116*7c478bd9Sstevel@tonic-gate 		if (iseol(c)) {
117*7c478bd9Sstevel@tonic-gate 			*lp = 0;
118*7c478bd9Sstevel@tonic-gate 		} else {
119*7c478bd9Sstevel@tonic-gate 			lp++; 	/* advance over separator */
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 	} else {
122*7c478bd9Sstevel@tonic-gate 		*linep = lp;
123*7c478bd9Sstevel@tonic-gate 		return (0);
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 	*linep = lp;
126*7c478bd9Sstevel@tonic-gate 	return (1);
127*7c478bd9Sstevel@tonic-gate }
128