getttyent.c revision 175345
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 6/4/93";
32#endif /* LIBC_SCCS and not lint */
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/lib/libc/gen/getttyent.c 175345 2008-01-15 06:50:50Z das $");
35
36#include <ttyent.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <ctype.h>
40#include <string.h>
41#include <dirent.h>
42#include <paths.h>
43
44static char zapchar;
45static FILE *tf;
46static int maxpts = 0;
47static int curpts = 0;
48static int pts_valid = 0;
49static size_t lbsize;
50static char *line;
51
52#define PTS "pts/"
53#define	MALLOCCHUNK	100
54
55static char *skip(char *);
56static char *value(char *);
57
58struct ttyent *
59getttynam(tty)
60	const char *tty;
61{
62	struct ttyent *t;
63
64	if (strncmp(tty, "/dev/", 5) == 0)
65		tty += 5;
66	setttyent();
67	while ( (t = getttyent()) )
68		if (!strcmp(tty, t->ty_name))
69			break;
70	endttyent();
71	return (t);
72}
73
74struct ttyent *
75getttyent()
76{
77	static struct ttyent tty;
78	static char devpts_name[] = "pts/4294967295";
79	char *p;
80	int c;
81	size_t i;
82
83	if (!tf && !setttyent())
84		return (NULL);
85	for (;;) {
86		if (!fgets(p = line, lbsize, tf)) {
87			if (pts_valid == 1 && curpts <= maxpts) {
88				sprintf(devpts_name, "pts/%d", curpts++);
89				tty.ty_name = devpts_name;
90				tty.ty_getty = tty.ty_type = NULL;
91				tty.ty_status = TTY_NETWORK;
92				tty.ty_window = NULL;
93				tty.ty_comment = NULL;
94				tty.ty_group  = _TTYS_NOGROUP;
95				return (&tty);
96			}
97			return (NULL);
98		}
99		/* extend buffer if line was too big, and retry */
100		while (!index(p, '\n') && !feof(tf)) {
101			i = strlen(p);
102			lbsize += MALLOCCHUNK;
103			if ((p = realloc(line, lbsize)) == NULL) {
104				(void)endttyent();
105				return (NULL);
106			}
107			line = p;
108			if (!fgets(&line[i], lbsize - i, tf))
109				return (NULL);
110		}
111		while (isspace((unsigned char)*p))
112			++p;
113		if (*p && *p != '#')
114			break;
115	}
116
117#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
118#define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
119
120	zapchar = 0;
121	tty.ty_name = p;
122	tty.ty_status = 0;
123	tty.ty_window = NULL;
124	tty.ty_group  = _TTYS_NOGROUP;
125
126	p = skip(p);
127	if (!*(tty.ty_getty = p))
128		tty.ty_getty = tty.ty_type = NULL;
129	else {
130		p = skip(p);
131		if (!*(tty.ty_type = p))
132			tty.ty_type = NULL;
133		else {
134			/* compatibility kludge: handle network/dialup specially */
135			if (scmp(_TTYS_DIALUP))
136				tty.ty_status |= TTY_DIALUP;
137			else if (scmp(_TTYS_NETWORK))
138				tty.ty_status |= TTY_NETWORK;
139			p = skip(p);
140		}
141	}
142
143	for (; *p; p = skip(p)) {
144		if (scmp(_TTYS_OFF))
145			tty.ty_status &= ~TTY_ON;
146		else if (scmp(_TTYS_ON))
147			tty.ty_status |= TTY_ON;
148		else if (scmp(_TTYS_SECURE))
149			tty.ty_status |= TTY_SECURE;
150		else if (scmp(_TTYS_INSECURE))
151			tty.ty_status &= ~TTY_SECURE;
152		else if (scmp(_TTYS_DIALUP))
153			tty.ty_status |= TTY_DIALUP;
154		else if (scmp(_TTYS_NETWORK))
155			tty.ty_status |= TTY_NETWORK;
156		else if (vcmp(_TTYS_WINDOW))
157			tty.ty_window = value(p);
158		else if (vcmp(_TTYS_GROUP))
159			tty.ty_group = value(p);
160		else
161			break;
162	}
163
164	if (zapchar == '#' || *p == '#')
165		while ((c = *++p) == ' ' || c == '\t')
166			;
167	tty.ty_comment = p;
168	if (*p == 0)
169		tty.ty_comment = 0;
170	if ( (p = index(p, '\n')) )
171		*p = '\0';
172	return (&tty);
173}
174
175#define	QUOTED	1
176
177/*
178 * Skip over the current field, removing quotes, and return a pointer to
179 * the next field.
180 */
181static char *
182skip(p)
183	char *p;
184{
185	char *t;
186	int c, q;
187
188	for (q = 0, t = p; (c = *p) != '\0'; p++) {
189		if (c == '"') {
190			q ^= QUOTED;	/* obscure, but nice */
191			continue;
192		}
193		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
194			p++;
195		*t++ = *p;
196		if (q == QUOTED)
197			continue;
198		if (c == '#') {
199			zapchar = c;
200			*p = 0;
201			break;
202		}
203		if (c == '\t' || c == ' ' || c == '\n') {
204			zapchar = c;
205			*p++ = 0;
206			while ((c = *p) == '\t' || c == ' ' || c == '\n')
207				p++;
208			break;
209		}
210	}
211	*--t = '\0';
212	return (p);
213}
214
215static char *
216value(p)
217	char *p;
218{
219
220	return ((p = index(p, '=')) ? ++p : NULL);
221}
222
223int
224setttyent()
225{
226	DIR *devpts_dir;
227
228	if (line == NULL) {
229		if ((line = malloc(MALLOCCHUNK)) == NULL)
230			return (0);
231		lbsize = MALLOCCHUNK;
232	}
233	devpts_dir = opendir(_PATH_DEV PTS);
234	if (devpts_dir) {
235		struct dirent *dp;
236
237		while ((dp = readdir(devpts_dir))) {
238			if (strcmp(dp->d_name, ".") != 0 &&
239			    strcmp(dp->d_name, "..") != 0) {
240				if (atoi(dp->d_name) > maxpts) {
241					maxpts = atoi(dp->d_name);
242					pts_valid = 1;
243					curpts = 0;
244				}
245			}
246		}
247		closedir(devpts_dir);
248	}
249	if (tf) {
250		rewind(tf);
251		return (1);
252	} else if ( (tf = fopen(_PATH_TTYS, "r")) )
253		return (1);
254	return (0);
255}
256
257int
258endttyent()
259{
260	int rval;
261
262	pts_valid = 0;
263	/*
264         * NB: Don't free `line' because getttynam()
265	 * may still be referencing it
266	 */
267	if (tf) {
268		rval = (fclose(tf) != EOF);
269		tf = NULL;
270		return (rval);
271	}
272	return (1);
273}
274
275static int
276isttystat(tty, flag)
277	const char *tty;
278	int flag;
279{
280	struct ttyent *t;
281
282	return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
283}
284
285
286int
287isdialuptty(tty)
288	const char *tty;
289{
290
291	return isttystat(tty, TTY_DIALUP);
292}
293
294int isnettty(tty)
295	const char *tty;
296{
297
298	return isttystat(tty, TTY_NETWORK);
299}
300