getttyent.c revision 22734
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1989, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 3. All advertising materials mentioning features or use of this software
141573Srgrimes *    must display the following acknowledgement:
151573Srgrimes *	This product includes software developed by the University of
161573Srgrimes *	California, Berkeley and its contributors.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes */
331573Srgrimes
341573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
351573Srgrimesstatic char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 6/4/93";
361573Srgrimes#endif /* LIBC_SCCS and not lint */
371573Srgrimes
381573Srgrimes#include <ttyent.h>
391573Srgrimes#include <stdio.h>
4019031Sjoerg#include <stdlib.h>
411573Srgrimes#include <ctype.h>
421573Srgrimes#include <string.h>
431573Srgrimes
441573Srgrimesstatic char zapchar;
451573Srgrimesstatic FILE *tf;
4619031Sjoergstatic size_t lbsize;
4719031Sjoergstatic char *line;
481573Srgrimes
4919031Sjoerg#define	MALLOCCHUNK	100
5019031Sjoerg
5119031Sjoergstatic char *skip __P((char *));
5219031Sjoergstatic char *value __P((char *));
5319031Sjoerg
541573Srgrimesstruct ttyent *
551573Srgrimesgetttynam(tty)
561573Srgrimes	const char *tty;
571573Srgrimes{
581573Srgrimes	register struct ttyent *t;
591573Srgrimes
601573Srgrimes	setttyent();
6117141Sjkh	while ( (t = getttyent()) )
621573Srgrimes		if (!strcmp(tty, t->ty_name))
631573Srgrimes			break;
641573Srgrimes	endttyent();
651573Srgrimes	return (t);
661573Srgrimes}
671573Srgrimes
681573Srgrimesstruct ttyent *
691573Srgrimesgetttyent()
701573Srgrimes{
711573Srgrimes	static struct ttyent tty;
7219031Sjoerg	register char *p;
731573Srgrimes	register int c;
7419031Sjoerg	size_t i;
751573Srgrimes
761573Srgrimes	if (!tf && !setttyent())
771573Srgrimes		return (NULL);
781573Srgrimes	for (;;) {
7919031Sjoerg		if (!fgets(p = line, lbsize, tf))
801573Srgrimes			return (NULL);
8119031Sjoerg		/* extend buffer if line was too big, and retry */
8219031Sjoerg		while (!index(p, '\n')) {
8319031Sjoerg			i = strlen(p);
8419031Sjoerg			lbsize += MALLOCCHUNK;
8519082Sjoerg			if ((p = realloc(line, lbsize)) == NULL) {
8619031Sjoerg				(void)endttyent();
8719031Sjoerg				return (NULL);
8819031Sjoerg			}
8919082Sjoerg			line = p;
9019031Sjoerg			if (!fgets(&line[i], lbsize - i, tf))
9119031Sjoerg				return (NULL);
921573Srgrimes		}
931573Srgrimes		while (isspace(*p))
941573Srgrimes			++p;
951573Srgrimes		if (*p && *p != '#')
961573Srgrimes			break;
971573Srgrimes	}
981573Srgrimes
991573Srgrimes	zapchar = 0;
1001573Srgrimes	tty.ty_name = p;
1011573Srgrimes	p = skip(p);
1021573Srgrimes	if (!*(tty.ty_getty = p))
1031573Srgrimes		tty.ty_getty = tty.ty_type = NULL;
1041573Srgrimes	else {
1051573Srgrimes		p = skip(p);
1061573Srgrimes		if (!*(tty.ty_type = p))
1071573Srgrimes			tty.ty_type = NULL;
1081573Srgrimes		else
1091573Srgrimes			p = skip(p);
1101573Srgrimes	}
1111573Srgrimes	tty.ty_status = 0;
1121573Srgrimes	tty.ty_window = NULL;
11321189Sdavidn	tty.ty_group  = _TTYS_NOGROUP;
1141573Srgrimes
1151573Srgrimes#define	scmp(e)	!strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
1161573Srgrimes#define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
1171573Srgrimes	for (; *p; p = skip(p)) {
1181573Srgrimes		if (scmp(_TTYS_OFF))
1191573Srgrimes			tty.ty_status &= ~TTY_ON;
1201573Srgrimes		else if (scmp(_TTYS_ON))
1211573Srgrimes			tty.ty_status |= TTY_ON;
1221573Srgrimes		else if (scmp(_TTYS_SECURE))
1231573Srgrimes			tty.ty_status |= TTY_SECURE;
12422734Sdavidn		else if (scmp(_TTYS_INSECURE))
12522734Sdavidn			tty.ty_status &= ~TTY_SECURE;
1261573Srgrimes		else if (vcmp(_TTYS_WINDOW))
1271573Srgrimes			tty.ty_window = value(p);
12821189Sdavidn		else if (vcmp(_TTYS_GROUP))
12921189Sdavidn			tty.ty_group = value(p);
1301573Srgrimes		else
1311573Srgrimes			break;
1321573Srgrimes	}
1331573Srgrimes
1341573Srgrimes	if (zapchar == '#' || *p == '#')
1351573Srgrimes		while ((c = *++p) == ' ' || c == '\t')
1361573Srgrimes			;
1371573Srgrimes	tty.ty_comment = p;
1381573Srgrimes	if (*p == 0)
1391573Srgrimes		tty.ty_comment = 0;
14017141Sjkh	if ( (p = index(p, '\n')) )
1411573Srgrimes		*p = '\0';
1421573Srgrimes	return (&tty);
1431573Srgrimes}
1441573Srgrimes
1451573Srgrimes#define	QUOTED	1
1461573Srgrimes
1471573Srgrimes/*
1481573Srgrimes * Skip over the current field, removing quotes, and return a pointer to
1491573Srgrimes * the next field.
1501573Srgrimes */
1511573Srgrimesstatic char *
1521573Srgrimesskip(p)
1531573Srgrimes	register char *p;
1541573Srgrimes{
1551573Srgrimes	register char *t;
1561573Srgrimes	register int c, q;
1571573Srgrimes
1581573Srgrimes	for (q = 0, t = p; (c = *p) != '\0'; p++) {
1591573Srgrimes		if (c == '"') {
1601573Srgrimes			q ^= QUOTED;	/* obscure, but nice */
1611573Srgrimes			continue;
1621573Srgrimes		}
1631573Srgrimes		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
1641573Srgrimes			p++;
1651573Srgrimes		*t++ = *p;
1661573Srgrimes		if (q == QUOTED)
1671573Srgrimes			continue;
1681573Srgrimes		if (c == '#') {
1691573Srgrimes			zapchar = c;
1701573Srgrimes			*p = 0;
1711573Srgrimes			break;
1721573Srgrimes		}
1731573Srgrimes		if (c == '\t' || c == ' ' || c == '\n') {
1741573Srgrimes			zapchar = c;
1751573Srgrimes			*p++ = 0;
1761573Srgrimes			while ((c = *p) == '\t' || c == ' ' || c == '\n')
1771573Srgrimes				p++;
1781573Srgrimes			break;
1791573Srgrimes		}
1801573Srgrimes	}
1811573Srgrimes	*--t = '\0';
1821573Srgrimes	return (p);
1831573Srgrimes}
1841573Srgrimes
1851573Srgrimesstatic char *
1861573Srgrimesvalue(p)
1871573Srgrimes	register char *p;
1881573Srgrimes{
1891573Srgrimes
1901573Srgrimes	return ((p = index(p, '=')) ? ++p : NULL);
1911573Srgrimes}
1921573Srgrimes
1931573Srgrimesint
1941573Srgrimessetttyent()
1951573Srgrimes{
1961573Srgrimes
19719031Sjoerg	if (line == NULL) {
19819031Sjoerg		if ((line = malloc(MALLOCCHUNK)) == NULL)
19919031Sjoerg			return (0);
20019031Sjoerg		lbsize = MALLOCCHUNK;
20119031Sjoerg	}
2021573Srgrimes	if (tf) {
2039272Shsu		rewind(tf);
2041573Srgrimes		return (1);
20517141Sjkh	} else if ( (tf = fopen(_PATH_TTYS, "r")) )
2061573Srgrimes		return (1);
2071573Srgrimes	return (0);
2081573Srgrimes}
2091573Srgrimes
2101573Srgrimesint
2111573Srgrimesendttyent()
2121573Srgrimes{
2131573Srgrimes	int rval;
2141573Srgrimes
21520383Sjoerg	/*
21620383Sjoerg         * NB: Don't free `line' because getttynam()
21720383Sjoerg	 * may still be referencing it
21820383Sjoerg	 */
2191573Srgrimes	if (tf) {
22019031Sjoerg		rval = (fclose(tf) != EOF);
2211573Srgrimes		tf = NULL;
2221573Srgrimes		return (rval);
2231573Srgrimes	}
2241573Srgrimes	return (1);
2251573Srgrimes}
226