getttyent.c revision 200150
1207151Smarius/*
2207151Smarius * Copyright (c) 1989, 1993
3207151Smarius *	The Regents of the University of California.  All rights reserved.
4207151Smarius *
5207151Smarius * Redistribution and use in source and binary forms, with or without
6207151Smarius * modification, are permitted provided that the following conditions
7207151Smarius * are met:
8207151Smarius * 1. Redistributions of source code must retain the above copyright
9207151Smarius *    notice, this list of conditions and the following disclaimer.
10207151Smarius * 2. Redistributions in binary form must reproduce the above copyright
11207151Smarius *    notice, this list of conditions and the following disclaimer in the
12207151Smarius *    documentation and/or other materials provided with the distribution.
13207151Smarius * 4. Neither the name of the University nor the names of its contributors
14207151Smarius *    may be used to endorse or promote products derived from this software
15207151Smarius *    without specific prior written permission.
16207151Smarius *
17207151Smarius * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18207151Smarius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19207151Smarius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20207151Smarius * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21207151Smarius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22207151Smarius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23207151Smarius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24207151Smarius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25207151Smarius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26207151Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27207151Smarius * SUCH DAMAGE.
28207151Smarius */
29207151Smarius
30207151Smarius#if defined(LIBC_SCCS) && !defined(lint)
31207151Smariusstatic char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 6/4/93";
32207151Smarius#endif /* LIBC_SCCS and not lint */
33207151Smarius#include <sys/cdefs.h>
34207151Smarius__FBSDID("$FreeBSD: head/lib/libc/gen/getttyent.c 200150 2009-12-05 19:31:38Z ed $");
35207151Smarius
36207151Smarius#include <ttyent.h>
37207151Smarius#include <stdio.h>
38207151Smarius#include <stdlib.h>
39207151Smarius#include <ctype.h>
40207151Smarius#include <string.h>
41207151Smarius#include <dirent.h>
42207151Smarius#include <paths.h>
43207151Smarius
44207151Smariusstatic char zapchar;
45207151Smariusstatic FILE *tf;
46207151Smariusstatic int maxpts = -1;
47207151Smariusstatic int curpts = 0;
48207151Smariusstatic size_t lbsize;
49207151Smariusstatic char *line;
50207151Smarius
51207151Smarius#define PTS "pts/"
52207151Smarius#define	MALLOCCHUNK	100
53207151Smarius
54207151Smariusstatic char *skip(char *);
55207151Smariusstatic char *value(char *);
56207151Smarius
57207151Smariusstruct ttyent *
58207151Smariusgetttynam(const char *tty)
59207151Smarius{
60207151Smarius	struct ttyent *t;
61207151Smarius
62207151Smarius	if (strncmp(tty, "/dev/", 5) == 0)
63207151Smarius		tty += 5;
64207151Smarius	setttyent();
65207151Smarius	while ( (t = getttyent()) )
66207151Smarius		if (!strcmp(tty, t->ty_name))
67207151Smarius			break;
68207151Smarius	endttyent();
69207151Smarius	return (t);
70207151Smarius}
71207151Smarius
72207151Smariusstruct ttyent *
73207151Smariusgetttyent(void)
74207151Smarius{
75207151Smarius	static struct ttyent tty;
76207151Smarius	static char devpts_name[] = "pts/4294967295";
77207151Smarius	char *p;
78207151Smarius	int c;
79207151Smarius	size_t i;
80207151Smarius
81207151Smarius	if (!tf && !setttyent())
82207151Smarius		return (NULL);
83207151Smarius	for (;;) {
84207151Smarius		if (!fgets(p = line, lbsize, tf)) {
85207151Smarius			if (curpts <= maxpts) {
86207151Smarius				sprintf(devpts_name, "pts/%d", curpts++);
87207151Smarius				tty.ty_name = devpts_name;
88207151Smarius				tty.ty_getty = tty.ty_type = NULL;
89207151Smarius				tty.ty_status = TTY_NETWORK;
90207151Smarius				tty.ty_window = NULL;
91207151Smarius				tty.ty_comment = NULL;
92207151Smarius				tty.ty_group  = _TTYS_NOGROUP;
93207151Smarius				return (&tty);
94207151Smarius			}
95207151Smarius			return (NULL);
96207151Smarius		}
97207151Smarius		/* extend buffer if line was too big, and retry */
98207151Smarius		while (!index(p, '\n') && !feof(tf)) {
99207151Smarius			i = strlen(p);
100207151Smarius			lbsize += MALLOCCHUNK;
101207151Smarius			if ((p = realloc(line, lbsize)) == NULL) {
102207151Smarius				(void)endttyent();
103207151Smarius				return (NULL);
104207151Smarius			}
105207151Smarius			line = p;
106207151Smarius			if (!fgets(&line[i], lbsize - i, tf))
107207151Smarius				return (NULL);
108207151Smarius		}
109207151Smarius		while (isspace((unsigned char)*p))
110207151Smarius			++p;
111207151Smarius		if (*p && *p != '#')
112207151Smarius			break;
113207151Smarius	}
114207151Smarius
115207151Smarius#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
116207151Smarius#define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
117207151Smarius
118207151Smarius	zapchar = 0;
119207151Smarius	tty.ty_name = p;
120207151Smarius	tty.ty_status = 0;
121207151Smarius	tty.ty_window = NULL;
122207151Smarius	tty.ty_group  = _TTYS_NOGROUP;
123207151Smarius
124207151Smarius	p = skip(p);
125207151Smarius	if (!*(tty.ty_getty = p))
126207151Smarius		tty.ty_getty = tty.ty_type = NULL;
127207151Smarius	else {
128207151Smarius		p = skip(p);
129207151Smarius		if (!*(tty.ty_type = p))
130207151Smarius			tty.ty_type = NULL;
131207151Smarius		else {
132207151Smarius			/* compatibility kludge: handle network/dialup specially */
133207151Smarius			if (scmp(_TTYS_DIALUP))
134207151Smarius				tty.ty_status |= TTY_DIALUP;
135207151Smarius			else if (scmp(_TTYS_NETWORK))
136207151Smarius				tty.ty_status |= TTY_NETWORK;
137207151Smarius			p = skip(p);
138207151Smarius		}
139207151Smarius	}
140207151Smarius
141207151Smarius	for (; *p; p = skip(p)) {
142207151Smarius		if (scmp(_TTYS_OFF))
143207151Smarius			tty.ty_status &= ~TTY_ON;
144207151Smarius		else if (scmp(_TTYS_ON))
145207151Smarius			tty.ty_status |= TTY_ON;
146207151Smarius		else if (scmp(_TTYS_SECURE))
147207151Smarius			tty.ty_status |= TTY_SECURE;
148207151Smarius		else if (scmp(_TTYS_INSECURE))
149207151Smarius			tty.ty_status &= ~TTY_SECURE;
150207151Smarius		else if (scmp(_TTYS_DIALUP))
151207151Smarius			tty.ty_status |= TTY_DIALUP;
152207151Smarius		else if (scmp(_TTYS_NETWORK))
153207151Smarius			tty.ty_status |= TTY_NETWORK;
154207151Smarius		else if (vcmp(_TTYS_WINDOW))
155207151Smarius			tty.ty_window = value(p);
156207151Smarius		else if (vcmp(_TTYS_GROUP))
157207151Smarius			tty.ty_group = value(p);
158207151Smarius		else
159207151Smarius			break;
160207151Smarius	}
161207151Smarius
162207151Smarius	if (zapchar == '#' || *p == '#')
163207151Smarius		while ((c = *++p) == ' ' || c == '\t')
164207151Smarius			;
165207151Smarius	tty.ty_comment = p;
166207151Smarius	if (*p == 0)
167207151Smarius		tty.ty_comment = 0;
168207151Smarius	if ( (p = index(p, '\n')) )
169207151Smarius		*p = '\0';
170207151Smarius	return (&tty);
171207151Smarius}
172207151Smarius
173207151Smarius#define	QUOTED	1
174207151Smarius
175207151Smarius/*
176207151Smarius * Skip over the current field, removing quotes, and return a pointer to
177207151Smarius * the next field.
178207151Smarius */
179207151Smariusstatic char *
180207151Smariusskip(char *p)
181207151Smarius{
182207151Smarius	char *t;
183207151Smarius	int c, q;
184207151Smarius
185207151Smarius	for (q = 0, t = p; (c = *p) != '\0'; p++) {
186207151Smarius		if (c == '"') {
187			q ^= QUOTED;	/* obscure, but nice */
188			continue;
189		}
190		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
191			p++;
192		*t++ = *p;
193		if (q == QUOTED)
194			continue;
195		if (c == '#') {
196			zapchar = c;
197			*p = 0;
198			break;
199		}
200		if (c == '\t' || c == ' ' || c == '\n') {
201			zapchar = c;
202			*p++ = 0;
203			while ((c = *p) == '\t' || c == ' ' || c == '\n')
204				p++;
205			break;
206		}
207	}
208	*--t = '\0';
209	return (p);
210}
211
212static char *
213value(char *p)
214{
215
216	return ((p = index(p, '=')) ? ++p : NULL);
217}
218
219int
220setttyent(void)
221{
222	DIR *devpts_dir;
223
224	if (line == NULL) {
225		if ((line = malloc(MALLOCCHUNK)) == NULL)
226			return (0);
227		lbsize = MALLOCCHUNK;
228	}
229	devpts_dir = opendir(_PATH_DEV PTS);
230	if (devpts_dir) {
231		struct dirent *dp;
232
233		maxpts = -1;
234		while ((dp = readdir(devpts_dir))) {
235			if (strcmp(dp->d_name, ".") != 0 &&
236			    strcmp(dp->d_name, "..") != 0) {
237				if (atoi(dp->d_name) > maxpts) {
238					maxpts = atoi(dp->d_name);
239					curpts = 0;
240				}
241			}
242		}
243		closedir(devpts_dir);
244	}
245	if (tf) {
246		rewind(tf);
247		return (1);
248	} else if ( (tf = fopen(_PATH_TTYS, "r")) )
249		return (1);
250	return (0);
251}
252
253int
254endttyent(void)
255{
256	int rval;
257
258	maxpts = -1;
259	/*
260         * NB: Don't free `line' because getttynam()
261	 * may still be referencing it
262	 */
263	if (tf) {
264		rval = (fclose(tf) != EOF);
265		tf = NULL;
266		return (rval);
267	}
268	return (1);
269}
270
271static int
272isttystat(const char *tty, int flag)
273{
274	struct ttyent *t;
275
276	return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
277}
278
279
280int
281isdialuptty(const char *tty)
282{
283
284	return isttystat(tty, TTY_DIALUP);
285}
286
287int
288isnettty(const char *tty)
289{
290
291	return isttystat(tty, TTY_NETWORK);
292}
293