xref: /titanic_41/usr/src/cmd/ypcmd/mknetid/getname.c (revision 66f9d5cb3cc0652e2d9d1366fb950efbe4ca2f24)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 #pragma 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 int
89 getname(name, namelen, ignore, sep, linep, com)
90 	char *name;
91 	int namelen;
92 	char *ignore;
93 	char *sep;
94 	char **linep;
95 	char *com;
96 {
97 	register char c;
98 	register char *lp;
99 	register char *np;
100 
101 	lp = *linep;
102 	do {
103 		c = *lp++;
104 	} while (isignore(c) && !iseol(c));
105 	if (iseol(c)) {
106 		*linep = lp - 1;
107 		return (-1);
108 	}
109 	np = name;
110 	while (! issep(c) && ! iseol(c) && np - name < namelen) {
111 		*np++ = c;
112 		c = *lp++;
113 	}
114 	lp--;
115 	if (np - name < namelen) {
116 		*np = 0;
117 		if (iseol(c)) {
118 			*lp = 0;
119 		} else {
120 			lp++; 	/* advance over separator */
121 		}
122 	} else {
123 		*linep = lp;
124 		return (0);
125 	}
126 	*linep = lp;
127 	return (1);
128 }
129