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: releng/10.2/lib/libc/gen/getttyent.c 267236 2014-06-08 17:50:07Z nwhitehorn $");
351573Srgrimes
361573Srgrimes#include <ttyent.h>
371573Srgrimes#include <stdio.h>
3819031Sjoerg#include <stdlib.h>
391573Srgrimes#include <ctype.h>
401573Srgrimes#include <string.h>
411573Srgrimes
42267236Snwhitehorn#include <sys/types.h>
43267236Snwhitehorn#include <sys/sysctl.h>
44267236Snwhitehorn
451573Srgrimesstatic char zapchar;
461573Srgrimesstatic FILE *tf;
4719031Sjoergstatic size_t lbsize;
4819031Sjoergstatic char *line;
491573Srgrimes
5019031Sjoerg#define	MALLOCCHUNK	100
5119031Sjoerg
5290045Sobrienstatic char *skip(char *);
5390045Sobrienstatic char *value(char *);
5419031Sjoerg
551573Srgrimesstruct ttyent *
56200150Sedgetttynam(const char *tty)
571573Srgrimes{
5890045Sobrien	struct ttyent *t;
591573Srgrimes
6024899Sdavidn	if (strncmp(tty, "/dev/", 5) == 0)
6124893Sdavidn		tty += 5;
621573Srgrimes	setttyent();
6317141Sjkh	while ( (t = getttyent()) )
641573Srgrimes		if (!strcmp(tty, t->ty_name))
651573Srgrimes			break;
661573Srgrimes	endttyent();
671573Srgrimes	return (t);
681573Srgrimes}
691573Srgrimes
70267236Snwhitehornstatic int
71267236Snwhitehornauto_tty_status(const char *ty_name)
72267236Snwhitehorn{
73267236Snwhitehorn	size_t len;
74267236Snwhitehorn	char *buf, *cons, *nextcons;
75267236Snwhitehorn
76267236Snwhitehorn	/* Check if this is an enabled kernel console line */
77267236Snwhitehorn	buf = NULL;
78267236Snwhitehorn	if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
79267236Snwhitehorn		return (0); /* Errors mean don't enable */
80267236Snwhitehorn	buf = malloc(len);
81267236Snwhitehorn	if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
82267236Snwhitehorn		goto done;
83267236Snwhitehorn
84267236Snwhitehorn	if ((cons = strchr(buf, '/')) == NULL)
85267236Snwhitehorn		goto done;
86267236Snwhitehorn	*cons = '\0';
87267236Snwhitehorn	nextcons = buf;
88267236Snwhitehorn	while ((cons = strsep(&nextcons, ",")) != NULL && strlen(cons) != 0) {
89267236Snwhitehorn		if (strcmp(cons, ty_name) == 0) {
90267236Snwhitehorn			free(buf);
91267236Snwhitehorn			return (TTY_ON);
92267236Snwhitehorn		}
93267236Snwhitehorn	}
94267236Snwhitehorn
95267236Snwhitehorndone:
96267236Snwhitehorn	free(buf);
97267236Snwhitehorn	return (0);
98267236Snwhitehorn}
99267236Snwhitehorn
1001573Srgrimesstruct ttyent *
101200150Sedgetttyent(void)
1021573Srgrimes{
1031573Srgrimes	static struct ttyent tty;
10490045Sobrien	char *p;
10590045Sobrien	int c;
10619031Sjoerg	size_t i;
1071573Srgrimes
1081573Srgrimes	if (!tf && !setttyent())
1091573Srgrimes		return (NULL);
1101573Srgrimes	for (;;) {
111203068Sed		if (!fgets(p = line, lbsize, tf))
1121573Srgrimes			return (NULL);
11319031Sjoerg		/* extend buffer if line was too big, and retry */
114229403Sed		while (!strchr(p, '\n') && !feof(tf)) {
11519031Sjoerg			i = strlen(p);
11619031Sjoerg			lbsize += MALLOCCHUNK;
11719082Sjoerg			if ((p = realloc(line, lbsize)) == NULL) {
11819031Sjoerg				(void)endttyent();
11919031Sjoerg				return (NULL);
12019031Sjoerg			}
12119082Sjoerg			line = p;
12219031Sjoerg			if (!fgets(&line[i], lbsize - i, tf))
12319031Sjoerg				return (NULL);
1241573Srgrimes		}
12552856Sache		while (isspace((unsigned char)*p))
1261573Srgrimes			++p;
1271573Srgrimes		if (*p && *p != '#')
1281573Srgrimes			break;
1291573Srgrimes	}
1301573Srgrimes
13152856Sache#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
13224893Sdavidn#define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
13324893Sdavidn
1341573Srgrimes	zapchar = 0;
1351573Srgrimes	tty.ty_name = p;
136148401Smdodd	tty.ty_status = 0;
137148401Smdodd	tty.ty_window = NULL;
138148401Smdodd	tty.ty_group  = _TTYS_NOGROUP;
139148401Smdodd
1401573Srgrimes	p = skip(p);
1411573Srgrimes	if (!*(tty.ty_getty = p))
1421573Srgrimes		tty.ty_getty = tty.ty_type = NULL;
1431573Srgrimes	else {
1441573Srgrimes		p = skip(p);
1451573Srgrimes		if (!*(tty.ty_type = p))
1461573Srgrimes			tty.ty_type = NULL;
14724893Sdavidn		else {
14824893Sdavidn			/* compatibility kludge: handle network/dialup specially */
14924893Sdavidn			if (scmp(_TTYS_DIALUP))
15024893Sdavidn				tty.ty_status |= TTY_DIALUP;
15124893Sdavidn			else if (scmp(_TTYS_NETWORK))
15224893Sdavidn				tty.ty_status |= TTY_NETWORK;
1531573Srgrimes			p = skip(p);
15424893Sdavidn		}
1551573Srgrimes	}
1561573Srgrimes
1571573Srgrimes	for (; *p; p = skip(p)) {
1581573Srgrimes		if (scmp(_TTYS_OFF))
1591573Srgrimes			tty.ty_status &= ~TTY_ON;
1601573Srgrimes		else if (scmp(_TTYS_ON))
1611573Srgrimes			tty.ty_status |= TTY_ON;
162267236Snwhitehorn		else if (scmp(_TTYS_ONIFCONSOLE))
163267236Snwhitehorn			tty.ty_status |= auto_tty_status(tty.ty_name);
1641573Srgrimes		else if (scmp(_TTYS_SECURE))
1651573Srgrimes			tty.ty_status |= TTY_SECURE;
16622734Sdavidn		else if (scmp(_TTYS_INSECURE))
16722734Sdavidn			tty.ty_status &= ~TTY_SECURE;
16824893Sdavidn		else if (scmp(_TTYS_DIALUP))
16924893Sdavidn			tty.ty_status |= TTY_DIALUP;
17024893Sdavidn		else if (scmp(_TTYS_NETWORK))
17124893Sdavidn			tty.ty_status |= TTY_NETWORK;
1721573Srgrimes		else if (vcmp(_TTYS_WINDOW))
1731573Srgrimes			tty.ty_window = value(p);
17421189Sdavidn		else if (vcmp(_TTYS_GROUP))
17521189Sdavidn			tty.ty_group = value(p);
1761573Srgrimes		else
1771573Srgrimes			break;
1781573Srgrimes	}
1791573Srgrimes
1801573Srgrimes	if (zapchar == '#' || *p == '#')
1811573Srgrimes		while ((c = *++p) == ' ' || c == '\t')
1821573Srgrimes			;
1831573Srgrimes	tty.ty_comment = p;
1841573Srgrimes	if (*p == 0)
1851573Srgrimes		tty.ty_comment = 0;
186229403Sed	if ((p = strchr(p, '\n')))
1871573Srgrimes		*p = '\0';
1881573Srgrimes	return (&tty);
1891573Srgrimes}
1901573Srgrimes
1911573Srgrimes#define	QUOTED	1
1921573Srgrimes
1931573Srgrimes/*
1941573Srgrimes * Skip over the current field, removing quotes, and return a pointer to
1951573Srgrimes * the next field.
1961573Srgrimes */
1971573Srgrimesstatic char *
198200150Sedskip(char *p)
1991573Srgrimes{
20090045Sobrien	char *t;
20190045Sobrien	int c, q;
2021573Srgrimes
2031573Srgrimes	for (q = 0, t = p; (c = *p) != '\0'; p++) {
2041573Srgrimes		if (c == '"') {
2051573Srgrimes			q ^= QUOTED;	/* obscure, but nice */
2061573Srgrimes			continue;
2071573Srgrimes		}
2081573Srgrimes		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
2091573Srgrimes			p++;
2101573Srgrimes		*t++ = *p;
2111573Srgrimes		if (q == QUOTED)
2121573Srgrimes			continue;
2131573Srgrimes		if (c == '#') {
2141573Srgrimes			zapchar = c;
2151573Srgrimes			*p = 0;
2161573Srgrimes			break;
2171573Srgrimes		}
2181573Srgrimes		if (c == '\t' || c == ' ' || c == '\n') {
2191573Srgrimes			zapchar = c;
2201573Srgrimes			*p++ = 0;
2211573Srgrimes			while ((c = *p) == '\t' || c == ' ' || c == '\n')
2221573Srgrimes				p++;
2231573Srgrimes			break;
2241573Srgrimes		}
2251573Srgrimes	}
2261573Srgrimes	*--t = '\0';
2271573Srgrimes	return (p);
2281573Srgrimes}
2291573Srgrimes
2301573Srgrimesstatic char *
231200150Sedvalue(char *p)
2321573Srgrimes{
2331573Srgrimes
234229403Sed	return ((p = strchr(p, '=')) ? ++p : NULL);
2351573Srgrimes}
2361573Srgrimes
2371573Srgrimesint
238200150Sedsetttyent(void)
2391573Srgrimes{
2401573Srgrimes
24119031Sjoerg	if (line == NULL) {
24219031Sjoerg		if ((line = malloc(MALLOCCHUNK)) == NULL)
24319031Sjoerg			return (0);
24419031Sjoerg		lbsize = MALLOCCHUNK;
24519031Sjoerg	}
2461573Srgrimes	if (tf) {
2479272Shsu		rewind(tf);
2481573Srgrimes		return (1);
249244092Sjilles	} else if ( (tf = fopen(_PATH_TTYS, "re")) )
2501573Srgrimes		return (1);
2511573Srgrimes	return (0);
2521573Srgrimes}
2531573Srgrimes
2541573Srgrimesint
255200150Sedendttyent(void)
2561573Srgrimes{
2571573Srgrimes	int rval;
2581573Srgrimes
25920383Sjoerg	/*
26020383Sjoerg         * NB: Don't free `line' because getttynam()
26120383Sjoerg	 * may still be referencing it
26220383Sjoerg	 */
2631573Srgrimes	if (tf) {
26419031Sjoerg		rval = (fclose(tf) != EOF);
2651573Srgrimes		tf = NULL;
2661573Srgrimes		return (rval);
2671573Srgrimes	}
2681573Srgrimes	return (1);
2691573Srgrimes}
27024893Sdavidn
27124893Sdavidnstatic int
272200150Sedisttystat(const char *tty, int flag)
27324893Sdavidn{
27490045Sobrien	struct ttyent *t;
27524893Sdavidn
27624893Sdavidn	return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
27724893Sdavidn}
27824893Sdavidn
27924893Sdavidn
28024893Sdavidnint
281200150Sedisdialuptty(const char *tty)
28224893Sdavidn{
28324893Sdavidn
28424893Sdavidn	return isttystat(tty, TTY_DIALUP);
28524893Sdavidn}
28624893Sdavidn
287200150Sedint
288200150Sedisnettty(const char *tty)
28924893Sdavidn{
29024893Sdavidn
29124893Sdavidn	return isttystat(tty, TTY_NETWORK);
29224893Sdavidn}
293