xref: /onnv-gate/usr/src/cmd/ypcmd/mknetid/getname.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  */
25702Sth160488 
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #define	iseol(c)	(c == 0 || c == '\n' || strchr(com, c) != NULL)
300Sstevel@tonic-gate #define	issep(c)	(strchr(sep, c) != NULL)
310Sstevel@tonic-gate #define	isignore(c)	(strchr(ignore, c) != NULL)
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
34*13093SRoger.Faulkner@Oracle.COM  * getaline()
350Sstevel@tonic-gate  * Read a line from a file.
360Sstevel@tonic-gate  * What's returned is a cookie to be passed to getname
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate char **
getaline(line,maxlinelen,f,lcount,com)39*13093SRoger.Faulkner@Oracle.COM getaline(line, maxlinelen, f, lcount, com)
400Sstevel@tonic-gate 	char *line;
410Sstevel@tonic-gate 	int maxlinelen;
420Sstevel@tonic-gate 	FILE *f;
430Sstevel@tonic-gate 	int *lcount;
440Sstevel@tonic-gate 	char *com;
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	char *p;
470Sstevel@tonic-gate 	static char *lp;
480Sstevel@tonic-gate 	do {
490Sstevel@tonic-gate 		if (! fgets(line, maxlinelen, f)) {
500Sstevel@tonic-gate 			return (NULL);
510Sstevel@tonic-gate 		}
520Sstevel@tonic-gate 		(*lcount)++;
530Sstevel@tonic-gate 	} while (iseol(line[0]));
540Sstevel@tonic-gate 	p = line;
550Sstevel@tonic-gate 	for (;;) {
560Sstevel@tonic-gate 		while (*p) {
570Sstevel@tonic-gate 			p++;
580Sstevel@tonic-gate 		}
590Sstevel@tonic-gate 		if (*--p == '\n' && *--p == '\\') {
600Sstevel@tonic-gate 			if (! fgets(p, maxlinelen, f)) {
610Sstevel@tonic-gate 				break;
620Sstevel@tonic-gate 			}
630Sstevel@tonic-gate 			(*lcount)++;
640Sstevel@tonic-gate 		} else {
650Sstevel@tonic-gate 			break;
660Sstevel@tonic-gate 		}
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 	lp = line;
690Sstevel@tonic-gate 	return (&lp);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * getname()
750Sstevel@tonic-gate  * Get the next entry from the line.
760Sstevel@tonic-gate  * You tell getname() which characters to ignore before storing them
770Sstevel@tonic-gate  * into name, and which characters separate entries in a line.
780Sstevel@tonic-gate  * The cookie is updated appropriately.
790Sstevel@tonic-gate  * return:
800Sstevel@tonic-gate  *	  1: one entry parsed
810Sstevel@tonic-gate  *	  0: partial entry parsed, ran out of space in name
820Sstevel@tonic-gate  *  -1: no more entries in line
830Sstevel@tonic-gate  */
84702Sth160488 int
getname(name,namelen,ignore,sep,linep,com)850Sstevel@tonic-gate getname(name, namelen, ignore, sep, linep, com)
860Sstevel@tonic-gate 	char *name;
870Sstevel@tonic-gate 	int namelen;
880Sstevel@tonic-gate 	char *ignore;
890Sstevel@tonic-gate 	char *sep;
900Sstevel@tonic-gate 	char **linep;
910Sstevel@tonic-gate 	char *com;
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	register char c;
940Sstevel@tonic-gate 	register char *lp;
950Sstevel@tonic-gate 	register char *np;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	lp = *linep;
980Sstevel@tonic-gate 	do {
990Sstevel@tonic-gate 		c = *lp++;
1000Sstevel@tonic-gate 	} while (isignore(c) && !iseol(c));
1010Sstevel@tonic-gate 	if (iseol(c)) {
1020Sstevel@tonic-gate 		*linep = lp - 1;
1030Sstevel@tonic-gate 		return (-1);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	np = name;
1060Sstevel@tonic-gate 	while (! issep(c) && ! iseol(c) && np - name < namelen) {
1070Sstevel@tonic-gate 		*np++ = c;
1080Sstevel@tonic-gate 		c = *lp++;
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 	lp--;
1110Sstevel@tonic-gate 	if (np - name < namelen) {
1120Sstevel@tonic-gate 		*np = 0;
1130Sstevel@tonic-gate 		if (iseol(c)) {
1140Sstevel@tonic-gate 			*lp = 0;
1150Sstevel@tonic-gate 		} else {
1160Sstevel@tonic-gate 			lp++; 	/* advance over separator */
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 	} else {
1190Sstevel@tonic-gate 		*linep = lp;
1200Sstevel@tonic-gate 		return (0);
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	*linep = lp;
1230Sstevel@tonic-gate 	return (1);
1240Sstevel@tonic-gate }
125