getttyent.c revision 203068
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 * 4. Neither the name of the University nor the names of its contributors
141573Srgrimes *    may be used to endorse or promote products derived from this software
151573Srgrimes *    without specific prior written permission.
161573Srgrimes *
171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271573Srgrimes * SUCH DAMAGE.
281573Srgrimes */
291573Srgrimes
301573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
311573Srgrimesstatic char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 6/4/93";
321573Srgrimes#endif /* LIBC_SCCS and not lint */
3390045Sobrien#include <sys/cdefs.h>
3490045Sobrien__FBSDID("$FreeBSD: head/lib/libc/gen/getttyent.c 203068 2010-01-27 11:54:42Z ed $");
351573Srgrimes
361573Srgrimes#include <ttyent.h>
371573Srgrimes#include <stdio.h>
3819031Sjoerg#include <stdlib.h>
391573Srgrimes#include <ctype.h>
401573Srgrimes#include <string.h>
411573Srgrimes
421573Srgrimesstatic char zapchar;
431573Srgrimesstatic FILE *tf;
4419031Sjoergstatic size_t lbsize;
4519031Sjoergstatic char *line;
461573Srgrimes
4719031Sjoerg#define	MALLOCCHUNK	100
4819031Sjoerg
4990045Sobrienstatic char *skip(char *);
5090045Sobrienstatic char *value(char *);
5119031Sjoerg
521573Srgrimesstruct ttyent *
53200150Sedgetttynam(const char *tty)
541573Srgrimes{
5590045Sobrien	struct ttyent *t;
561573Srgrimes
5724899Sdavidn	if (strncmp(tty, "/dev/", 5) == 0)
5824893Sdavidn		tty += 5;
591573Srgrimes	setttyent();
6017141Sjkh	while ( (t = getttyent()) )
611573Srgrimes		if (!strcmp(tty, t->ty_name))
621573Srgrimes			break;
631573Srgrimes	endttyent();
641573Srgrimes	return (t);
651573Srgrimes}
661573Srgrimes
671573Srgrimesstruct ttyent *
68200150Sedgetttyent(void)
691573Srgrimes{
701573Srgrimes	static struct ttyent tty;
7190045Sobrien	char *p;
7290045Sobrien	int c;
7319031Sjoerg	size_t i;
741573Srgrimes
751573Srgrimes	if (!tf && !setttyent())
761573Srgrimes		return (NULL);
771573Srgrimes	for (;;) {
78203068Sed		if (!fgets(p = line, lbsize, tf))
791573Srgrimes			return (NULL);
8019031Sjoerg		/* extend buffer if line was too big, and retry */
81175345Sdas		while (!index(p, '\n') && !feof(tf)) {
8219031Sjoerg			i = strlen(p);
8319031Sjoerg			lbsize += MALLOCCHUNK;
8419082Sjoerg			if ((p = realloc(line, lbsize)) == NULL) {
8519031Sjoerg				(void)endttyent();
8619031Sjoerg				return (NULL);
8719031Sjoerg			}
8819082Sjoerg			line = p;
8919031Sjoerg			if (!fgets(&line[i], lbsize - i, tf))
9019031Sjoerg				return (NULL);
911573Srgrimes		}
9252856Sache		while (isspace((unsigned char)*p))
931573Srgrimes			++p;
941573Srgrimes		if (*p && *p != '#')
951573Srgrimes			break;
961573Srgrimes	}
971573Srgrimes
9852856Sache#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
9924893Sdavidn#define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
10024893Sdavidn
1011573Srgrimes	zapchar = 0;
1021573Srgrimes	tty.ty_name = p;
103148401Smdodd	tty.ty_status = 0;
104148401Smdodd	tty.ty_window = NULL;
105148401Smdodd	tty.ty_group  = _TTYS_NOGROUP;
106148401Smdodd
1071573Srgrimes	p = skip(p);
1081573Srgrimes	if (!*(tty.ty_getty = p))
1091573Srgrimes		tty.ty_getty = tty.ty_type = NULL;
1101573Srgrimes	else {
1111573Srgrimes		p = skip(p);
1121573Srgrimes		if (!*(tty.ty_type = p))
1131573Srgrimes			tty.ty_type = NULL;
11424893Sdavidn		else {
11524893Sdavidn			/* compatibility kludge: handle network/dialup specially */
11624893Sdavidn			if (scmp(_TTYS_DIALUP))
11724893Sdavidn				tty.ty_status |= TTY_DIALUP;
11824893Sdavidn			else if (scmp(_TTYS_NETWORK))
11924893Sdavidn				tty.ty_status |= TTY_NETWORK;
1201573Srgrimes			p = skip(p);
12124893Sdavidn		}
1221573Srgrimes	}
1231573Srgrimes
1241573Srgrimes	for (; *p; p = skip(p)) {
1251573Srgrimes		if (scmp(_TTYS_OFF))
1261573Srgrimes			tty.ty_status &= ~TTY_ON;
1271573Srgrimes		else if (scmp(_TTYS_ON))
1281573Srgrimes			tty.ty_status |= TTY_ON;
1291573Srgrimes		else if (scmp(_TTYS_SECURE))
1301573Srgrimes			tty.ty_status |= TTY_SECURE;
13122734Sdavidn		else if (scmp(_TTYS_INSECURE))
13222734Sdavidn			tty.ty_status &= ~TTY_SECURE;
13324893Sdavidn		else if (scmp(_TTYS_DIALUP))
13424893Sdavidn			tty.ty_status |= TTY_DIALUP;
13524893Sdavidn		else if (scmp(_TTYS_NETWORK))
13624893Sdavidn			tty.ty_status |= TTY_NETWORK;
1371573Srgrimes		else if (vcmp(_TTYS_WINDOW))
1381573Srgrimes			tty.ty_window = value(p);
13921189Sdavidn		else if (vcmp(_TTYS_GROUP))
14021189Sdavidn			tty.ty_group = value(p);
1411573Srgrimes		else
1421573Srgrimes			break;
1431573Srgrimes	}
1441573Srgrimes
1451573Srgrimes	if (zapchar == '#' || *p == '#')
1461573Srgrimes		while ((c = *++p) == ' ' || c == '\t')
1471573Srgrimes			;
1481573Srgrimes	tty.ty_comment = p;
1491573Srgrimes	if (*p == 0)
1501573Srgrimes		tty.ty_comment = 0;
15117141Sjkh	if ( (p = index(p, '\n')) )
1521573Srgrimes		*p = '\0';
1531573Srgrimes	return (&tty);
1541573Srgrimes}
1551573Srgrimes
1561573Srgrimes#define	QUOTED	1
1571573Srgrimes
1581573Srgrimes/*
1591573Srgrimes * Skip over the current field, removing quotes, and return a pointer to
1601573Srgrimes * the next field.
1611573Srgrimes */
1621573Srgrimesstatic char *
163200150Sedskip(char *p)
1641573Srgrimes{
16590045Sobrien	char *t;
16690045Sobrien	int c, q;
1671573Srgrimes
1681573Srgrimes	for (q = 0, t = p; (c = *p) != '\0'; p++) {
1691573Srgrimes		if (c == '"') {
1701573Srgrimes			q ^= QUOTED;	/* obscure, but nice */
1711573Srgrimes			continue;
1721573Srgrimes		}
1731573Srgrimes		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
1741573Srgrimes			p++;
1751573Srgrimes		*t++ = *p;
1761573Srgrimes		if (q == QUOTED)
1771573Srgrimes			continue;
1781573Srgrimes		if (c == '#') {
1791573Srgrimes			zapchar = c;
1801573Srgrimes			*p = 0;
1811573Srgrimes			break;
1821573Srgrimes		}
1831573Srgrimes		if (c == '\t' || c == ' ' || c == '\n') {
1841573Srgrimes			zapchar = c;
1851573Srgrimes			*p++ = 0;
1861573Srgrimes			while ((c = *p) == '\t' || c == ' ' || c == '\n')
1871573Srgrimes				p++;
1881573Srgrimes			break;
1891573Srgrimes		}
1901573Srgrimes	}
1911573Srgrimes	*--t = '\0';
1921573Srgrimes	return (p);
1931573Srgrimes}
1941573Srgrimes
1951573Srgrimesstatic char *
196200150Sedvalue(char *p)
1971573Srgrimes{
1981573Srgrimes
1991573Srgrimes	return ((p = index(p, '=')) ? ++p : NULL);
2001573Srgrimes}
2011573Srgrimes
2021573Srgrimesint
203200150Sedsetttyent(void)
2041573Srgrimes{
2051573Srgrimes
20619031Sjoerg	if (line == NULL) {
20719031Sjoerg		if ((line = malloc(MALLOCCHUNK)) == NULL)
20819031Sjoerg			return (0);
20919031Sjoerg		lbsize = MALLOCCHUNK;
21019031Sjoerg	}
2111573Srgrimes	if (tf) {
2129272Shsu		rewind(tf);
2131573Srgrimes		return (1);
21417141Sjkh	} else if ( (tf = fopen(_PATH_TTYS, "r")) )
2151573Srgrimes		return (1);
2161573Srgrimes	return (0);
2171573Srgrimes}
2181573Srgrimes
2191573Srgrimesint
220200150Sedendttyent(void)
2211573Srgrimes{
2221573Srgrimes	int rval;
2231573Srgrimes
22420383Sjoerg	/*
22520383Sjoerg         * NB: Don't free `line' because getttynam()
22620383Sjoerg	 * may still be referencing it
22720383Sjoerg	 */
2281573Srgrimes	if (tf) {
22919031Sjoerg		rval = (fclose(tf) != EOF);
2301573Srgrimes		tf = NULL;
2311573Srgrimes		return (rval);
2321573Srgrimes	}
2331573Srgrimes	return (1);
2341573Srgrimes}
23524893Sdavidn
23624893Sdavidnstatic int
237200150Sedisttystat(const char *tty, int flag)
23824893Sdavidn{
23990045Sobrien	struct ttyent *t;
24024893Sdavidn
24124893Sdavidn	return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
24224893Sdavidn}
24324893Sdavidn
24424893Sdavidn
24524893Sdavidnint
246200150Sedisdialuptty(const char *tty)
24724893Sdavidn{
24824893Sdavidn
24924893Sdavidn	return isttystat(tty, TTY_DIALUP);
25024893Sdavidn}
25124893Sdavidn
252200150Sedint
253200150Sedisnettty(const char *tty)
25424893Sdavidn{
25524893Sdavidn
25624893Sdavidn	return isttystat(tty, TTY_NETWORK);
25724893Sdavidn}
258