xref: /titanic_50/usr/src/cmd/ypcmd/mknetid/getname.c (revision 554ff184129088135ad2643c1c9832174a17be88)
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) 1986,1987,1988,1989,1990 Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 
28 #ident	"%Z%%M%	%I%	%E% SMI"	/* SMI4.1 1.3 */
29 
30 #include <stdio.h>
31 #include <string.h>
32 
33 #define	iseol(c)	(c == 0 || c == '\n' || strchr(com, c) != NULL)
34 #define	issep(c)	(strchr(sep, c) != NULL)
35 #define	isignore(c)	(strchr(ignore, c) != NULL)
36 
37 /*
38  * getline()
39  * Read a line from a file.
40  * What's returned is a cookie to be passed to getname
41  */
42 char **
43 getline(line, maxlinelen, f, lcount, com)
44 	char *line;
45 	int maxlinelen;
46 	FILE *f;
47 	int *lcount;
48 	char *com;
49 {
50 	char *p;
51 	static char *lp;
52 	do {
53 		if (! fgets(line, maxlinelen, f)) {
54 			return (NULL);
55 		}
56 		(*lcount)++;
57 	} while (iseol(line[0]));
58 	p = line;
59 	for (;;) {
60 		while (*p) {
61 			p++;
62 		}
63 		if (*--p == '\n' && *--p == '\\') {
64 			if (! fgets(p, maxlinelen, f)) {
65 				break;
66 			}
67 			(*lcount)++;
68 		} else {
69 			break;
70 		}
71 	}
72 	lp = line;
73 	return (&lp);
74 }
75 
76 
77 /*
78  * getname()
79  * Get the next entry from the line.
80  * You tell getname() which characters to ignore before storing them
81  * into name, and which characters separate entries in a line.
82  * The cookie is updated appropriately.
83  * return:
84  *	  1: one entry parsed
85  *	  0: partial entry parsed, ran out of space in name
86  *  -1: no more entries in line
87  */
88 getname(name, namelen, ignore, sep, linep, com)
89 	char *name;
90 	int namelen;
91 	char *ignore;
92 	char *sep;
93 	char **linep;
94 	char *com;
95 {
96 	register char c;
97 	register char *lp;
98 	register char *np;
99 
100 	lp = *linep;
101 	do {
102 		c = *lp++;
103 	} while (isignore(c) && !iseol(c));
104 	if (iseol(c)) {
105 		*linep = lp - 1;
106 		return (-1);
107 	}
108 	np = name;
109 	while (! issep(c) && ! iseol(c) && np - name < namelen) {
110 		*np++ = c;
111 		c = *lp++;
112 	}
113 	lp--;
114 	if (np - name < namelen) {
115 		*np = 0;
116 		if (iseol(c)) {
117 			*lp = 0;
118 		} else {
119 			lp++; 	/* advance over separator */
120 		}
121 	} else {
122 		*linep = lp;
123 		return (0);
124 	}
125 	*linep = lp;
126 	return (1);
127 }
128